Browse Source

add official_sn

stanley-king 4 years ago
parent
commit
d9190a61ab
3 changed files with 82 additions and 73 deletions
  1. 2 3
      data/config/prod/refill.ini.php
  2. 6 0
      helper/refill/IRefill.php
  3. 74 70
      helper/refill/RefillFactory.php

+ 2 - 3
data/config/prod/refill.ini.php

@@ -29,7 +29,7 @@ $lx_oil = ['name' => 'lx', 'store_id' => 14, 'card_type' => ['sinopec'],
     'amount' => [1000 => ['goods_id' => 6311, 'price' => 958] //,
 //        2000 => ['goods_id' => 6322, 'price' => 1916]
     ],
-    'period' => ['start' => '14:30', 'end' => '17:55'],'refill_type' => 'inner_store'];
+    'period' => ['start' => '14:30', 'end' => '17:55'],'official_sn' => true,'refill_type' => 'inner_store'];
 
 //'zzx' => $zzx_oil,
 //$oil_providers = ['suhc' => $suhc_oil, 'suhctm' => $suhctm_oil, 'lx' => $lx_oil];
@@ -57,8 +57,7 @@ $bjb_phone = ['name' => 'bjb', 'store_id' => 12, 'card_type' => ['chinaunicom',
         30 => ['goods_id' => 6301, 'price' => 28.65],
         50 => ['goods_id' => 6302, 'price' => 47.75],
         100 => ['goods_id' => 6303, 'price' => 95.5],
-        200 => ['goods_id' => 6304, 'price' => 191]],
-    'refill_type' => 'api'];
+        200 => ['goods_id' => 6304, 'price' => 191]],'official_sn' => true,'refill_type' => 'api'];
 
 
 $phone_providers = ['bjb' => $bjb_phone];

+ 6 - 0
helper/refill/IRefill.php

@@ -9,6 +9,7 @@ abstract class IRefill
     protected $mName;
     protected $mSotreID;
     protected $mRefillType;
+    protected $mOfficialSN;
     protected $mCardType = [];
     protected $mAmounts = [];
     protected $mPeriod;
@@ -24,6 +25,7 @@ abstract class IRefill
         }
 
         $this->mPeriod = $cfgs['period'];
+        $this->mOfficialSN = $cfgs['official_sn'] ?? false;
     }
 
     abstract public function add($card_no,$card_type,$amount,$params);
@@ -37,6 +39,10 @@ abstract class IRefill
         return $this->mSotreID;
     }
 
+    public function official_sn() {
+        return $this->mOfficialSN === true;
+    }
+
     public function refill_type() {
         return $this->mRefillType;
     }

+ 74 - 70
helper/refill/RefillFactory.php

@@ -82,13 +82,11 @@ class RefillFactory
     private function combine_goods($configs)
     {
         $result = [];
-        foreach ($configs as $cfg)
-        {
+        foreach ($configs as $cfg) {
             $card_types = $cfg['card_type'] ?? [];
             $amounts = $cfg['amount'] ?? [];
 
-            foreach ($card_types as $type)
-            {
+            foreach ($card_types as $type) {
                 if (array_key_exists($type, $result)) {
                     $item = $result[$type];
                 } else {
@@ -112,48 +110,40 @@ class RefillFactory
         $oil_configs = $config['oil_providers'];
 
         $names = [];
-        foreach ($oil_configs as $cfg)
-        {
+        foreach ($oil_configs as $cfg) {
             $name = $cfg['name'];
             $names[] = $name;
 
-            try
-            {
+            try {
                 $class = "refill\\{$name}\\RefillOil";
-                if(class_exists($class,false)) {
+                if (class_exists($class, false)) {
                     $provider = new $class($cfg);
                     $this->mOilProvider[] = $provider;
                 } else {
                     $error = "Base Error: class {$class} isn't exists!";
                     throw new Exception($error);
                 }
-            }
-            catch (Exception $ex)
-            {
-                Log::record($ex->getMessage(),Log::ERR);
+            } catch (Exception $ex) {
+                Log::record($ex->getMessage(), Log::ERR);
             }
         }
 
         $pho_configs = $config['phone_providers'];
-        foreach ($pho_configs as $cfg)
-        {
+        foreach ($pho_configs as $cfg) {
             $name = $cfg['name'];
             $names[] = $name;
 
-            try
-            {
+            try {
                 $class = "refill\\{$name}\\RefillPhone";
-                if(class_exists($class,false)) {
+                if (class_exists($class, false)) {
                     $provider = new $class($cfg);
                     $this->mPhoneProvider[] = $provider;
                 } else {
                     $error = "Base Error: class {$class} isn't exists!";
                     throw new Exception($error);
                 }
-            }
-            catch (Exception $ex)
-            {
-                Log::record($ex->getMessage(),Log::ERR);
+            } catch (Exception $ex) {
+                Log::record($ex->getMessage(), Log::ERR);
             }
         }
 
@@ -187,6 +177,24 @@ class RefillFactory
         return $providers;
     }
 
+    public function provider(string $chname, int $card_type)
+    {
+        if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
+            $providers = $this->mOilProvider;
+        } elseif ($card_type == mtopcard\ChinaMobileCard || $card_type == mtopcard\ChinaUnicomCard || $card_type == mtopcard\ChinaTelecomCard) {
+            $providers = $this->mPhoneProvider;
+        } else {
+            return null;
+        }
+
+        foreach ($providers as $provider) {
+            if ($provider->name() == $chname) {
+                return $provider;
+            }
+        }
+        return null;
+    }
+
     private function find_phone(int $amount, int $card_type): array
     {
         $providers = [];
@@ -205,31 +213,26 @@ class RefillFactory
 
     public function notify($chname, $input)
     {
-        try
-        {
+        try {
             $class_name = "refill\\{$chname}\\RefillCallBack";
-            if(class_exists($class_name,false)) {
+            if (class_exists($class_name, false)) {
                 $caller = new $class_name();
             } else {
                 $error = "Base Error: class {$class_name} isn't exists!";
                 throw new Exception($error);
             }
-        }
-        catch (Exception $ex)
-        {
-            Log::record($ex->getMessage(),Log::ERR);
+        } catch (Exception $ex) {
+            Log::record($ex->getMessage(), Log::ERR);
             return false;
         }
 
-        if ($caller->verify($input))
-        {
-            [$order_id, $success, $can_try,$need_handle] = $caller->notify($input);
-            if(!$need_handle) {
+        if ($caller->verify($input)) {
+            [$order_id, $success, $can_try, $need_handle] = $caller->notify($input);
+            if (!$need_handle) {
                 return true;
             }
 
-            if ($order_id !== false)
-            {
+            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']);
@@ -244,28 +247,23 @@ class RefillFactory
                 $logic_vr_order = Logic("vr_order");
                 if ($success) {
                     $logic_vr_order->changeOrderStateSuccess($order_id);
-                }
-                elseif ($can_try)
-                {
+                } elseif ($can_try) {
                     $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,正在重试");
                     if ($this->retry($refill_info, $order_info) !== false) {
                         $mod_refill->edit($order_id, ['inner_status' => 1, 'notify_time' => time(), 'notify_state' => 1]);
                         return true;
                     }
-                }
-                else {
+                } else {
                     $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,不可重试.");
                 }
 
                 $mod_refill->edit($order_id, ['notify_time' => time(), 'notify_state' => 1]);
                 QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id]);
-            }
-            else {
+            } else {
                 Log::record("系统无此订单ID:{$order_id}", Log::ERR);
 
             }
-        }
-        else {
+        } else {
             Log::record("{$chname} 签名失败.");
         }
         return true;
@@ -273,8 +271,7 @@ class RefillFactory
 
     private function retry(array $refill_info, array $order_info)
     {
-        $checker = function ($refill, $order)
-        {
+        $checker = function ($refill, $order) {
             $state = intval($order['order_state']);
             if ($state !== ORDER_STATE_SEND) {
                 return false;
@@ -286,7 +283,7 @@ class RefillFactory
 //            }
 
             $period = time() - $refill['order_time'];
-            if($period >= 10 * 60) {
+            if ($period >= 10 * 60) {
                 return false;
             }
 
@@ -323,7 +320,7 @@ class RefillFactory
         if (empty($providers)) {
             return [202, "找不到合适的充值通道"];
         }
-        if(empty($notify_url)) {
+        if (empty($notify_url)) {
             $notify_url = "";
         }
 
@@ -332,7 +329,7 @@ class RefillFactory
         $mch_amount = $calc->calc_vgoods_price([]);
         $available = $minfo->available_predeposit();
         if ($mch_amount > $available) {
-            Log::record("下单时机构余额不足,可用余额为:{$available}",Log::DEBUG);
+            Log::record("下单时机构余额不足,可用余额为:{$available}", Log::DEBUG);
             return [203, "余额不足"];
         }
 
@@ -349,12 +346,11 @@ class RefillFactory
         usort($providers, $ascending);
 
         $refill_state = false;
-        foreach ($providers as $provider)
-        {
+        foreach ($providers as $provider) {
             $channel_name = $provider->name();
             [$goods_id, $price] = $provider->goods($amount);
 
-            if($price > $mch_amount) continue;
+            if ($price > $mch_amount) continue;
 
             $input['goods_id'] = $goods_id;
             $input['quantity'] = 1;
@@ -370,12 +366,11 @@ class RefillFactory
             $result = $logic_buy_virtual->buyStep3($input, $buyer_id, [$calc, 'calc_vorder_amount'], true);
 
             $mod_refill = Model('refill_order');
-            if ($result['state'] === true)
-            {
+            if ($result['state'] === true) {
                 $order_sn = $result['data']['order_sn'];
                 $order_id = $result['data']['order_id'];
 
-                if(empty($mch_order)) {
+                if (empty($mch_order)) {
                     $mch_order = $order_sn;
                 }
                 //虚拟订单表信息扩展
@@ -393,10 +388,9 @@ class RefillFactory
 
             $params = ['order_sn' => $order_sn, 'idcard' => $idcard, 'card_name' => $card_name];
             [$state, $err] = $provider->add($card_no, $card_type, $amount, $params);
-            if ($state)
-            {
+            if ($state) {
                 $trade_no = $err;
-                if($provider->refill_type() == 'api') {
+                if ($provider->refill_type() == 'api') {
                     $logic_vr_order = Logic("vr_order");
                     $logic_vr_order->changeOrderStateSend($order_id);
                 }
@@ -411,7 +405,7 @@ class RefillFactory
                 $logic_vr_order = Logic("vr_order");
                 $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
                 $logic_vr_order->changeOrderStateCancel($order_info, '', "调用{$channel_name}接口失败");
-                $mod_refill->edit($order_id, ['commit_time' => time(),'inner_status' => 1]);
+                $mod_refill->edit($order_id, ['commit_time' => time(), 'inner_status' => 1]);
             }
         }
 
@@ -429,7 +423,7 @@ class RefillFactory
             return (substr($haystack, 0, $length) === $needle);
         };
 
-        return $checker($url,"http://") || $checker($url,"https://");
+        return $checker($url, "http://") || $checker($url, "https://");
     }
 
     public function notify_merchant($order_id)
@@ -473,11 +467,10 @@ class RefillFactory
         $params['sign'] = $sign;
 
         //如果http请求内部,又发出回调自己的请求,在处理进程非动态扩容的情况下,容易造成阻塞.
-        if($this->is_url($notify_url)) {
+        if ($this->is_url($notify_url)) {
             $resp = http_request($notify_url, $params, 'POST');
-        }
-        else {
-            $resp = RBridgeFactory::instance()->notify($notify_url,$params);
+        } else {
+            $resp = RBridgeFactory::instance()->notify($notify_url, $params);
         }
 
         if ($resp == "SUCCESS") {
@@ -507,8 +500,7 @@ class RefillFactory
         $card_type = intval($refill_info['card_type']);
 
         $pthis = $this;
-        $finder = function ($chname, $card_type) use($pthis)
-        {
+        $finder = function ($chname, $card_type) use ($pthis) {
             if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
                 $providers = $pthis->mOilProvider;
             } else {
@@ -524,21 +516,31 @@ class RefillFactory
             return NULL;
         };
 
-        $provider = $finder($chname,$card_type);
+        $provider = $finder($chname, $card_type);
         [$state, $err] = $provider->query($refill_info);
 
-        if($state === true) {
+        if ($state === true) {
             $mod_refill->edit($order_id, ['notify_time' => time(), 'notify_state' => 1]);
             QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id]);
             return true;
-        }
-        else {
+        } else {
             return false;
         }
     }
 
     private function body($state, $refill_info, $mch_info)
     {
+        $official_sn = '';
+
+        $name = $refill_info['channel_name'];
+        if(empty(!$name))
+        {
+            $provider = $this->provider($name,intval($refill_info['card_type']));
+            if(!empty($provider) && $provider->official_sn()) {
+                $official_sn = $refill_info['ch_trade_no'] ?? '';
+            }
+        }
+
         $params = [
             "mchid" => $refill_info['mchid'],
             "order_sn" => $refill_info['mch_order'],
@@ -547,8 +549,10 @@ class RefillFactory
             "trade_no" => $refill_info['order_sn'],
             "idcard" => $refill_info['idcard'] ?? "",
             "card_name" => $refill_info['card_name'] ?? "",
+            'official_sn' => $official_sn,
             "state" => $state];
 
+        Log::record("chname = {$name} official_sn = {$official_sn}",Log::DEBUG);
         $secure_key = $mch_info['secure_key'];
         $sign = $this->sign($params, $secure_key);