浏览代码

refactor add function from refill controller to RefillFactory

stanley-king 4 年之前
父节点
当前提交
dfa6d38885
共有 4 个文件被更改,包括 133 次插入88 次删除
  1. 1 0
      data/config/prod/refill.ini.php
  2. 125 12
      helper/refill/RefillFactory.php
  3. 0 1
      helper/refill/beixt/RefillCallBack.php
  4. 7 75
      mobile/control/refill.php

+ 1 - 0
data/config/prod/refill.ini.php

@@ -17,4 +17,5 @@ $beixt_phone = ['name' => 'beixt','store_id' => 8,'card_type' => ['chinamobile',
         200 => ['goods_id' => 6297,'price' => 195]],
     'refill_type' => 'api'];
 $phone_providers = ['beixt' => $beixt_phone];
+
 $config['phone_providers'] = $phone_providers;

+ 125 - 12
helper/refill/RefillFactory.php

@@ -22,10 +22,12 @@ require_once(BASE_HELPER_PATH . '/refill/beixts/RefillCallBack.php');
 use Log;
 use mtopcard;
 use QueueClient;
+use member_info;
 
 class RefillFactory
 {
     static private $stInstance = null;
+
     static public function instance()
     {
         if (self::$stInstance == null) {
@@ -36,6 +38,7 @@ class RefillFactory
 
     private $mOilProvider = [];
     private $mPhoneProvider = [];
+    private $mProviderNames = [];
 
     private function __construct()
     {
@@ -85,8 +88,10 @@ class RefillFactory
         global $config;
         $pro_configs = $config['oil_providers'];
 
+        $names = [];
         foreach ($pro_configs as $cfg) {
             $name = $cfg['name'];
+            $names[] = $name;
             if($name == 'suhc') {
                 $provider = new suhc\RefillOil($cfg);
                 $this->mOilProvider[] = $provider;
@@ -100,6 +105,8 @@ class RefillFactory
         $pro_configs = $config['phone_providers'];
         foreach ($pro_configs as $cfg) {
             $name = $cfg['name'];
+            $names[] = $name;
+
             if($name == 'beixt') {
                 $provider = new beixt\RefillPhone($cfg);
                 $this->mPhoneProvider[] = $provider;
@@ -109,6 +116,8 @@ class RefillFactory
 
             }
         }
+
+        $this->mProviderNames = array_unique($names);
     }
 
     public function find_providers(int $amount,int $card_type) : array
@@ -162,33 +171,53 @@ class RefillFactory
 
     public function notify($chname,$input)
     {
-        if($chname == "suhc") {
-            $caller = new suhc\RefillCallBack();
-        }elseif ($chname == 'beixt'){
-            $caller = new beixt\RefillCallBack();
-        }elseif ($chname == 'beixts'){
-            $caller = new beixts\RefillCallBack();
-        }
-        else {
+        if(!in_array($chname,$this->mProviderNames)) {
             return false;
         }
 
+        $class_name = "{$chname}\\RefillCallBack";
+        $caller = new $class_name();
+
         if($caller->verify($input))
         {
-            [$order_id,$success] = $caller->notify($input);
+            [$order_id,$success,$can_try] = $caller->notify($input);
             if($order_id !== false)
             {
+                $mod_order = Model('vr_order');
+                $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
+                $order_state = intval($order_info['order_state']);
+
+                if($order_state != ORDER_STATE_SEND) {
+                    return false;
+                }
+
+                $mod_refill = Model('refill_order');
+                $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id]);
+
                 $logic_vr_order = Logic("vr_order");
                 if($success) {
                     $logic_vr_order->changeOrderStateSuccess($order_id);
-                } else {
-                    $logic_vr_order->changeOrderStateCancel($order_id);
                 }
+                elseif ($can_try)
+                {
+                    $logic_vr_order->changeOrderStateCancel($order_id);
+                    if($this->retry($refill_info) === false) {
 
-                $mod_refill = Model('refill_order');
+                    }
+                    else {
+
+                    }
+                }
+                else {
+                        $logic_vr_order->changeOrderStateCancel($order_id);
+                }
                 $mod_refill->edit($order_id,['notify_time' => time(),'notify_state' => 1]);
                 QueueClient::push("NotifyMerchantComplete",['order_id' => $order_id]);
             }
+            else {
+                Log::record("系统无此订单ID:{$order_id}",Log::ERR);
+
+            }
         }
         else {
             Log::record("{$chname} 签名失败.");
@@ -196,6 +225,90 @@ class RefillFactory
         return true;
     }
 
+    private function retry(array $refill_info)
+    {
+        return false;
+    }
+
+    public function add($mchid,$buyer_id,$amount,$card_no,$mch_order,$notify_url,$try_times = 0)
+    {
+        $card_type = mtopcard\card_type($card_no);
+        $providers = $this->find_providers($amount,$card_type);
+
+        if(empty($providers)) {
+            return [202,"找不到合适的充值通道"];
+        }
+
+        $minfo = new member_info($buyer_id);
+
+        $calc = new CalcMerchantPrice($mchid,$amount,$card_type);
+        $mch_amount = $calc->calc_vgoods_price([]);
+        if($mch_amount > $minfo->available_predeposit()) {
+            return [203,"余额不足"];
+        }
+
+        $refill_state = false;
+        foreach ($providers as $provider)
+        {
+            $channel_name = $provider->name();
+            [$goods_id, $price] = $provider->goods($amount);
+
+            $input['goods_id'] = $goods_id;
+            $input['quantity'] = 1;
+            $input['price'] = $price;
+
+            $input['buyer_phone'] = $minfo->mobile();
+            $input['buyer_name']  = $minfo->truename();
+            $input['buyer_msg']   = $_POST['buyer_msg'] ?? '';
+            $input['order_from'] = 1;
+            $input['pd_pay'] = true;
+
+            $logic_buy_virtual = Logic('buy_virtual');
+            $result = $logic_buy_virtual->buyStep3($input, $buyer_id,[$calc,'calc_vorder_amount'],true);
+
+            $mod_refill = Model('refill_order');
+            if($result['state'] === true)
+            {
+                $order_sn = $result['data']['order_sn'];
+                $order_id = $result['data']['order_id'];
+                //虚拟订单表信息扩展
+                $orderext = ['order_id' => $order_id,'order_sn' => $order_sn,'mchid' => $mchid,
+                    'refill_amount' => $amount,'mch_order' => $mch_order,
+                    'notify_url' => $notify_url,'channel_name' => $channel_name,
+                    'mch_amount' => $mch_amount,'channel_amount' => $price,'order_time' => time(),
+                    'card_type' => $card_type,'card_no' => $card_no];
+                $mod_refill->add_refill($orderext);
+            }
+            else {
+                continue;
+            }
+
+            $params = ['order_sn' => $order_sn];
+            [$state,$err] = $provider->add($card_no,$card_type,$amount,$params);
+            if($state) {
+                $trade_no = $err;
+                $logic_vr_order = Logic("vr_order");
+                $logic_vr_order->changeOrderStateSend($order_id);
+                $data = ['commit_time' => time(),'ch_trade_no' => $trade_no];
+                $mod_refill->edit($order_id,$data);
+                $refill_state = true;
+                break;
+            }
+            else {
+                Log::record("channel:{$channel_name} err:{$err}");
+                $logic_vr_order = Logic("vr_order");
+                $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
+                $logic_vr_order->changeOrderStateCancel($order_info,'',"调用{$channel_name}接口失败");
+            }
+        }
+
+        if($refill_state) {
+            return [true,$order_sn];
+        } else {
+            return [204,"充值失败."];
+        }
+    }
+
     public function notify_merchant($order_id)
     {
         if($order_id <= 0) {

+ 0 - 1
helper/refill/beixt/RefillCallBack.php

@@ -7,7 +7,6 @@ use refill;
 
 class RefillCallBack implements refill\IRefillCallBack
 {
-
     public function verify($params) : bool
     {
         $sign = $this->sign($params);

+ 7 - 75
mobile/control/refill.php

@@ -70,83 +70,15 @@ class refillControl extends merchantControl
         $amount = intval($_GET['amount']);
         $card_no = $_GET['cardno'];
         $notify_url = $_GET['notifyurl'];
-        $mch_order = $_GET['order_sn']; //对方的order
+        $mch_order = $_GET['order_sn']; //对方的order编号
 
-        $card_type = mtopcard\card_type($card_no);
-        $providers = refill\RefillFactory::instance()->find_providers($amount,$card_type);
-
-        if(empty($providers)) {
-            return self::outerr(202,"找不到合适的充值通道");
-        }
-
-        $buyer_id = $this->adminid();
-        $minfo = new member_info($buyer_id);
-
-        $calc = new refill\CalcMerchantPrice($this->mchid(),$amount,$card_type);
-        $mch_amount = $calc->calc_vgoods_price([]);
-        if($mch_amount > $minfo->available_predeposit()) {
-            return self::outerr(203,"余额不足");
-        }
-
-        $refill_state = false;
-        foreach ($providers as $provider)
-        {
-            $channel_name = $provider->name();
-            [$goods_id, $price] = $provider->goods($amount);
-
-            $input['goods_id'] = $goods_id;
-            $input['quantity'] = 1;
-            $input['price'] = $price;
-
-            $input['buyer_phone'] = $minfo->mobile();
-            $input['buyer_name']  = $minfo->truename();
-            $input['buyer_msg']   = $_POST['buyer_msg'] ?? '';
-            $input['order_from'] = 1;
-            $input['pd_pay'] = true;
-
-            $logic_buy_virtual = Logic('buy_virtual');
-            $result = $logic_buy_virtual->buyStep3($input, $buyer_id,[$calc,'calc_vorder_amount'],true);
-
-            $mod_refill = Model('refill_order');
-            if($result['state'] === true)
-            {
-                $order_sn = $result['data']['order_sn'];
-                $order_id = $result['data']['order_id'];
-                //虚拟订单表信息扩展
-                $orderext = ['order_id' => $order_id,'order_sn' => $order_sn,'mchid' => $this->mchid(),
-                    'refill_amount' => $amount,'mch_order' => $mch_order,
-                    'notify_url' => $notify_url,'channel_name' => $channel_name,
-                    'mch_amount' => $mch_amount,'channel_amount' => $price,'order_time' => time(),
-                    'card_type' => $card_type,'card_no' => $card_no];
-                $mod_refill->add_refill($orderext);
-            }
-            else {
-                continue;
-            }
-
-            $params = ['order_sn' => $order_sn];
-            [$state,$err] = $provider->add($card_no,$card_type,$amount,$params);
-            if($state) {
-                $trade_no = $err;
-                $logic_vr_order = Logic("vr_order");
-                $logic_vr_order->changeOrderStateSend($order_id);
-                $data = ['commit_time' => time(),'ch_trade_no' => $trade_no];
-                $mod_refill->edit($order_id,$data);
-                $refill_state = true;
-                break;
-            }
-            else {
-                Log::record("channel:{$channel_name} err:{$err}");
-                $logic_vr_order = Logic("vr_order");
-                $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
-                $logic_vr_order->changeOrderStateCancel($order_info,'',"调用{$channel_name}接口失败");
-            }
+        [$state,$err] = refill\RefillFactory::instance()->add($this->mchid(),$this->adminid(),$amount,$card_no,$mch_order,$notify_url);
+        if($state == true) {
+            return self::outsuccess(['state' => true]);
         }
-
-        if($refill_state) {
-            return self::outsuccess(['state' => true,'trade_no' => $order_sn]);
-        } else {
-            return self::outerr(204,"充值失败.");
+        else {
+            $code = $state;
+            return self::outerr($code,$err);
         }
     }