stanley-king 3 лет назад
Родитель
Сommit
6e600b7d8b
3 измененных файлов с 53 добавлено и 45 удалено
  1. 4 1
      data/logic/buy_virtual.logic.php
  2. 47 44
      helper/refill/RefillBase.php
  3. 2 0
      test/TestAccountDivert.php

+ 4 - 1
data/logic/buy_virtual.logic.php

@@ -343,7 +343,8 @@ class buy_virtualLogic
         $data_pd['amount'] = $order_amount;
         $data_pd['order_sn'] = $order_info['order_sn'];
 
-        if ($available_pd_amount >= $order_amount) {
+        if ($available_pd_amount >= $order_amount)
+        {
             //预存款立即支付,订单支付完成
             $model_pd->changePd('order_pay',$data_pd,$fRefill);
 
@@ -386,6 +387,8 @@ class buy_virtualLogic
                 throw new Exception('订单更新失败');
             }
         }
+
+        return true;
     }
 
     /**

+ 47 - 44
helper/refill/RefillBase.php

@@ -302,7 +302,7 @@ class RefillBase
 
             $mod_refill = Model('refill_order');
             $channel_amount = $price * $order->quantity();
-            [$order_success,$order_id,$order_sn] = $this->create_refill_order($order,$goods_id,$minfo,$calc,$channel_name,$channel_amount,$mch_amount);
+            [$order_success,$order_id,$order_sn] = $this->create_order($order,$goods_id,$minfo,$calc,$channel_name,$channel_amount,$mch_amount);
             $last_orderid = $order_id;
             if(!$order_success) continue;
 
@@ -393,32 +393,12 @@ class RefillBase
         }
     }
 
-    private function create_refill_order(order $order,$goods_id,$minfo,$calc,$ch_name,$ch_amount,$mch_amount)
+    private function create_order(order $order, $goods_id, $minfo, $calc, $ch_name, $ch_amount, $mch_amount)
     {
-        try
+        $refill_creater = function (order $order,$last_orderid,$order_id,$order_sn,$mod_refill) use($ch_name, $ch_amount,$mch_amount)
         {
-            $fTrans = false;
-            $logic_buy_virtual = Logic('buy_virtual');
-            $mod_refill = Model('refill_order');
-            if($fTrans) {
-                $trans = new trans_wapper($mod_refill,__METHOD__);
-            } else {
-                $start = microtime(true);
-            }
-
-            $input['goods_id'] = $goods_id;
-            $input['quantity'] = $order->quantity();
-            $input['buyer_phone'] = $minfo->mobile();
-            $input['buyer_name'] = $minfo->truename();
-            $input['buyer_msg'] = $_POST['buyer_msg'] ?? '';
-            $input['order_from'] = 1;
-            $input['pd_pay'] = true;
-            $result = $logic_buy_virtual->buyStep3($input, $order->buyer_id(), [$calc, 'calc_vorder_amount'], true, false);
-            if ($result['state'] === true)
+            try
             {
-                $order_sn = $result['data']['order_sn'];
-                $order_id = $result['data']['order_id'];
-                $last_orderid = $order->last_order_id();
                 if($last_orderid > 0) {
                     $mod_refill->edit($last_orderid, ['inner_status' => 1]);
                 }
@@ -438,46 +418,69 @@ class RefillBase
 
                 $order->commit_times_inc();
                 $orderext = $order->refill_params($order_id,$order_sn,$refill_amount, $ch_name, $ch_amount,$mch_amount);
-                $mod_refill->add_refill($orderext);
+                $fInsert = $mod_refill->add_refill($orderext);
 
                 if($order->is_third()) {
                     $ext = $order->third_extparams($order_id,$order_sn);
                     $thrid_refill->addExt($ext);
                 }
 
-                if(!$this->pay_completed($order_sn))
-                {
-                    $logic_vr_order = Logic("vr_order");
-                    $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
-                    $logic_vr_order->changeOrderStateCancel($order_info, '', "预存款不足以支付该订单",true,true);
-                    if($fTrans) {
-                        $trans->commit();
-                    }
-                    return [false,$order_id,$order_sn];
+                if(!$fInsert) {
+                    return false;
+                } else {
+                    return true;
                 }
-                elseif($fTrans) {
-                    $trans->commit();
+            }
+            catch (Exception $ex)
+            {
+                Log::record($ex->getMessage(),Log::ERR);
+                return false;
+            }
+        };
+
+        try
+        {
+            $logic_buy_virtual = Logic('buy_virtual');
+            $mod_refill = Model('refill_order');
+
+            $input['goods_id'] = $goods_id;
+            $input['quantity'] = $order->quantity();
+            $input['buyer_phone'] = $minfo->mobile();
+            $input['buyer_name'] = $minfo->truename();
+            $input['buyer_msg'] = $_POST['buyer_msg'] ?? '';
+            $input['order_from'] = 1;
+            $input['pd_pay'] = true;
+
+            $start = microtime(true);
+            $result = $logic_buy_virtual->buyStep3($input, $order->buyer_id(), [$calc, 'calc_vorder_amount'], true, true);
+            if ($result['state'] === true)
+            {
+                $order_sn = $result['data']['order_sn'];
+                $order_id = $result['data']['order_id'];
+                $last_orderid = $order->last_order_id();
+                $fSuccess = $refill_creater($order,$last_orderid,$order_id,$order_sn,$mod_refill);
+                if(!$fSuccess) {
+                    Log::record("refill_creater fail:order_sn={$order_sn}",Log::ERR);
                 }
-                else {
-                    Log::record(sprintf(__METHOD__ . " request time=%.6f", microtime(true) - $start), Log::DEBUG);
+                if (!$this->pay_completed($order_sn)) {
+                    $logic_vr_order = Logic("vr_order");
+                    $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
+                    $logic_vr_order->changeOrderStateCancel($order_info, '', "预存款不足以支付该订单", true, true);
+                    return [false, $order_id, $order_sn];
                 }
 
+                Log::record(sprintf(__METHOD__ . " request time=%.6f", microtime(true) - $start), Log::DEBUG);
                 return [true,$order_id,$order_sn];
             }
             else
             {
-                if ($fTrans) $trans->commit();
-                Log::record("{$result['msg']}", Log::ERR);
-
+                Log::record(__METHOD__ . " fail:buyStep3 err msg={$result['msg']}", Log::ERR);
                 return [false,0,''];
             }
         }
         catch (Exception $ex)
         {
             Log::record($ex->getMessage(), Log::ERR);
-            if($fTrans) {
-                $trans->rollback();
-            }
             return [false,0,''];
         }
     }

+ 2 - 0
test/TestAccountDivert.php

@@ -59,11 +59,13 @@ class TestAccountDivert extends TestCase
         $helper->init_channels();
     }
 
+    //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAccountDivert::testInitChannels)( .*)?$/" --test-suffix TestAccountDivert.php /var/www/html/test
     public function testInitChannelsAccount()
     {
         $helper = new refill\divert_account();
         $helper->init_channels_account();
     }
+    //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAccountDivert::testInitChannelsAccount)( .*)?$/" --test-suffix TestAccountDivert.php /var/www/html/test
 
     public function testStoreMember()
     {