Selaa lähdekoodia

add black card

stanley-king 4 vuotta sitten
vanhempi
commit
5a2b4414e0

+ 1 - 0
helper/refill/RefillFactory.php

@@ -11,6 +11,7 @@ require_once(BASE_HELPER_PATH . '/refill/IRefillOil.php');
 require_once(BASE_HELPER_PATH . '/refill/IRefillPhone.php');
 require_once(BASE_HELPER_PATH . '/refill/IRefillCallBack.php');
 require_once(BASE_HELPER_PATH . '/refill/CalcMerchantPrice.php');
+require_once(BASE_HELPER_PATH . '/refill/util.php');
 
 
 require_once(BASE_HELPER_PATH . '/refill/suhc/RefillOil.php');

+ 20 - 0
helper/refill/gftd/RefillCallBack.php

@@ -84,6 +84,7 @@ class RefillCallBack implements refill\IRefillCallBack
             return [$order_id, true, false,true];
         }
         elseif ($status === 109) {
+            $this->onError($params['message'],$order_id);
             return [$order_id, false, false,true];
         }
         elseif ($status === 120) {
@@ -93,4 +94,23 @@ class RefillCallBack implements refill\IRefillCallBack
             return [$order_id, false, false,false];
         }
     }
+
+    public function onError($msg,$order_id)
+    {
+        if(in_array($msg,config::BlackMsgs))
+        {
+            $refill = Model('refill_order');
+            $order = $refill->getOrderInfo(['order_id' => $order_id]);
+
+            if(empty($order)) return false;
+
+            $card_no = $order['card_no'];
+            if(!empty($card_no)) {
+                refill\util::set_black($card_no);
+                return true;
+            }
+        }
+
+        return false;
+    }
 }

+ 6 - 49
helper/refill/gftd/RefillOil.php

@@ -29,55 +29,12 @@ class RefillOil extends refill\IRefillOil
         $params['fuelCardUserName'] = "";
         $params['fuelCardUserID'] = "";
 
-        $refill_info = $this->refill_info($card_no,$card_type);
-        $params['telephone'] = $refill_info['bind_phone'];
-        $params['phoneNumber'] = $refill_info['bind_phone'];
+        $card_info = refill\util::read_card($card_no,$card_type);
 
-        return [$params, $refill_info];
-    }
-
-    private function refill_info($card_no, $card_type)
-    {
-        $data = rcache($card_no, 'cardrefill-');
-        if(empty($data))
-        {
-            $mod_topcard = Model('topcard');
-            $ret = $mod_topcard->get_card($card_no);
-            if(empty($ret)) {
-                $bind_phone = $this->make_mobile();
-                $mod_topcard->add($card_no,$card_type,time(),$bind_phone);
-                $data['bind_phone'] = $bind_phone;
-                $data['refill_time'] = time();
-                $data['times'] = 0;
-
-                wcache($card_no,$data,'cardrefill-');
-            }
-            else {
-                $val = $ret[0];
+        $params['telephone'] = $card_info['bind_phone'];
+        $params['phoneNumber'] = $card_info['bind_phone'];
 
-                $data['bind_phone'] = $val['bind_phone'];
-                $data['refill_time'] = time();
-                $data['times'] = 0;
-            }
-        }
-
-        return $data;
-    }
-
-    private function inc_card($card_no, $refill_info)
-    {
-        $refill_info['times'] += 1;
-        $refill_info['refill_time'] = time();
-        wcache($card_no,$refill_info,'cardrefill-');
-    }
-
-    private function make_mobile()
-    {
-        $prefix = ["139","138","137","136","135","134","159","158","157","150","151","152",
-            "188","187","182","183","184","178","130","131","132","156","155","186","185",
-            "176","133","153","189","180","181","177"];
-        $no = "1" . mt_rand(3000000000, 3999999999);
-        return $no;
+        return [$params, $card_info];
     }
 
     private function product_id(int $amount,int $cart_type)
@@ -147,7 +104,7 @@ class RefillOil extends refill\IRefillOil
 
     public function add($card_no,$card_type,$amount,$input)
     {
-        [$params,$refill_info] = $this->req_params($card_no,$card_type,$amount,$input);
+        [$params,$card_info] = $this->req_params($card_no,$card_type,$amount,$input);
 
         $sign = $this->sign($params);
         $params['signature'] = $sign;
@@ -164,7 +121,7 @@ class RefillOil extends refill\IRefillOil
 
             if($resp['code'] == 0)
             {
-                $this->inc_card($card_no,$refill_info);
+                refill\util::inc_card($card_no,$card_info);
                 $data = $resp['data'];
                 return [true,$data['orderNumber']];
             }

+ 1 - 0
helper/refill/gftd/config.php

@@ -14,4 +14,5 @@ class config
 
     const NOTIFY_URL = "https://www.xyzshops.cn/mobile/refill_gftd.php";
     const ExtHeaders = ['Content-Type: application/json;charset=UTF-8;','Accept:application/json;charset=UTF-8;'];
+    const BlackMsgs = ["只能给绑定正确手机号的油卡充值或只能给主卡充值","只能给主卡且卡状态正常的加油卡充值","加油卡卡号错误或不支持"];
 }

+ 20 - 0
helper/refill/gftdsinop/RefillCallBack.php

@@ -84,6 +84,7 @@ class RefillCallBack implements refill\IRefillCallBack
             return [$order_id, true, false,true];
         }
         elseif ($status === 109) {
+            $this->onError($params['message'],$order_id);
             return [$order_id, false, false,true];
         }
         elseif ($status === 120) {
@@ -93,4 +94,23 @@ class RefillCallBack implements refill\IRefillCallBack
             return [$order_id, false, false,false];
         }
     }
+
+    public function onError($msg,$order_id)
+    {
+        if(in_array($msg,config::BlackMsgs))
+        {
+            $refill = Model('refill_order');
+            $order = $refill->getOrderInfo(['order_id' => $order_id]);
+
+            if(empty($order)) return false;
+
+            $card_no = $order['card_no'];
+            if(!empty($card_no)) {
+                refill\util::set_black($card_no);
+                return true;
+            }
+        }
+
+        return false;
+    }
 }

+ 6 - 46
helper/refill/gftdsinop/RefillOil.php

@@ -29,52 +29,12 @@ class RefillOil extends refill\IRefillOil
         $params['fuelCardUserName'] = "";
         $params['fuelCardUserID'] = "";
 
-        $refill_info = $this->refill_info($card_no,$card_type);
-        $params['telephone'] = $refill_info['bind_phone'];
-        $params['phoneNumber'] = $refill_info['bind_phone'];
+        $card_info = refill\util::read_card($card_no,$card_type);
 
-        return [$params, $refill_info];
-    }
-
-    private function refill_info($card_no, $card_type)
-    {
-        $data = rcache($card_no, 'cardrefill-');
-        if(empty($data))
-        {
-            $mod_topcard = Model('topcard');
-            $ret = $mod_topcard->get_card($card_no);
-            if(empty($ret)) {
-                $bind_phone = $this->make_mobile();
-                $mod_topcard->add($card_no,$card_type,time(),$bind_phone);
-                $data['bind_phone'] = $bind_phone;
-                $data['refill_time'] = time();
-                $data['times'] = 0;
-
-                wcache($card_no,$data,'cardrefill-');
-            }
-            else {
-                $val = $ret[0];
+        $params['telephone'] = $card_info['bind_phone'];
+        $params['phoneNumber'] = $card_info['bind_phone'];
 
-                $data['bind_phone'] = $val['bind_phone'];
-                $data['refill_time'] = time();
-                $data['times'] = 0;
-            }
-        }
-
-        return $data;
-    }
-
-    private function inc_card($card_no, $refill_info)
-    {
-        $refill_info['times'] += 1;
-        $refill_info['refill_time'] = time();
-        wcache($card_no,$refill_info,'cardrefill-');
-    }
-
-    private function make_mobile()
-    {
-        $no = "1" . mt_rand(3000000000, 3999999999);
-        return $no;
+        return [$params, $card_info];
     }
 
     private function product_id(int $amount,int $cart_type)
@@ -144,7 +104,7 @@ class RefillOil extends refill\IRefillOil
 
     public function add($card_no,$card_type,$amount,$input)
     {
-        [$params,$refill_info] = $this->req_params($card_no,$card_type,$amount,$input);
+        [$params,$card_info] = $this->req_params($card_no,$card_type,$amount,$input);
 
         $sign = $this->sign($params);
         $params['signature'] = $sign;
@@ -161,7 +121,7 @@ class RefillOil extends refill\IRefillOil
 
             if($resp['code'] == 0)
             {
-                $this->inc_card($card_no,$refill_info);
+                refill\util::inc_card($card_no,$card_info);
                 $data = $resp['data'];
                 return [true,$data['orderNumber']];
             }

+ 1 - 0
helper/refill/gftdsinop/config.php

@@ -14,4 +14,5 @@ class config
 
     const NOTIFY_URL = "https://www.xyzshops.cn/mobile/refill_gftd.php";
     const ExtHeaders = ['Content-Type: application/json;charset=UTF-8;','Accept:application/json;charset=UTF-8;'];
+    const BlackMsgs = ["只能给绑定正确手机号的油卡充值或只能给主卡充值","只能给主卡且卡状态正常的加油卡充值","加油卡卡号错误或不支持"];
 }

+ 148 - 0
helper/refill/util.php

@@ -0,0 +1,148 @@
+<?php
+
+namespace refill;
+
+require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
+
+use mtopcard;
+use Log;
+
+class util
+{
+    static function make_mobile()
+    {
+        static $prefix = ["139","138","137","136","135","134","159","158","157","150","151","152",
+            "188","187","182","183","184","178","130","131","132","156","155","186","185",
+            "176","133","153","189","180","181","177"];
+
+        $pos = mt_rand(0,count($prefix));
+        $no = "{$prefix[$pos]}" . mt_rand(10000000, 99999999);
+
+        return $no;
+    }
+
+    static function read_card($card_no,$card_type = 0)
+    {
+        if(empty($card_no)) return false;
+
+        $data = rcache($card_no, 'cardrefill-');
+        if(empty($data))
+        {
+            $mod_topcard = Model('topcard');
+            $ret = $mod_topcard->get_card($card_no);
+            if(empty($ret))
+            {
+                if($card_type === 0) {
+                    $card_type = mtopcard\card_type($card_no);
+                }
+                $bind_phone = util::make_mobile();
+                $mod_topcard->add($card_no,$card_type,time(),$bind_phone);
+
+                $data['bind_phone'] = $bind_phone;
+                $data['refill_time'] = time();
+                $data['times'] = 0;
+                $data['black_card'] = 0;
+
+                wcache($card_no,$data,'cardrefill-');
+            }
+            else {
+                $val = $ret[0];
+
+                $data['bind_phone'] = $val['bind_phone'];
+                $data['black_card'] = $val['black_card'];
+                $data['refill_time'] = time();
+                $data['times'] = 0;
+            }
+        }
+
+        //之前没加black_card处理,这个字段不存在.
+        if(!array_key_exists('black_card',$data)) {
+            $data['black_card'] = 0;
+        }
+
+        return $data;
+    }
+
+    static function inc_card($card_no, $card_info)
+    {
+        $card_info['times'] += 1;
+        $card_info['refill_time'] = time();
+        wcache($card_no,$card_info,'cardrefill-');
+    }
+
+    static function del_card($card_no)
+    {
+        dcache($card_no,'cardrefill-');
+    }
+
+    static function set_black($card_no)
+    {
+        if(empty($card_no)) return false;
+
+        $card_info = util::read_card($card_no);
+        if(!empty($card_info)) {
+            $card_info['black_card'] = 1;
+            $mod_topcard = Model('topcard');
+            $mod_topcard->where(['card_no' => $card_no])->update(['black_card' => 1]);
+            wcache($card_no,$card_info,'cardrefill-');
+
+            return true;
+        }
+        else {
+            return false;
+        }
+    }
+
+    private static function black_order($order_sn,$msg)
+    {
+        static $errMsgs = ["只能给绑定正确手机号的油卡充值或只能给主卡充值","只能给主卡且卡状态正常的加油卡充值","加油卡卡号错误或不支持"];
+
+        if(empty($msg)) return false;
+
+        if(in_array($msg,$errMsgs))
+        {
+
+            $refill = Model('refill_order');
+            $order = $refill->getOrderInfo(['order_sn' => $order_sn]);
+
+            if(empty($order)) return false;
+
+            $card_no = $order['card_no'];
+            return util::set_black($card_no);
+        }
+    }
+
+    static function black_from_log($file_name)
+    {
+        $fn = fopen($file_name,"r");
+        if(empty($fn)) {
+            Log::record("Open File {$file_name} error.",Log::ERR);
+            return false;
+        }
+        else {
+            Log::record("{$file_name} start woring",Log::DEBUG);
+        }
+
+        $errs = [];
+        while(! feof($fn))  {
+            $line = trim(fgets($fn));
+            $ret = preg_match('/[\w\W]+"channelOrderNumber":"(?P<order_sn>[^"]+)"[\w\W]+"message":"(?P<message>[\x{4e00}-\x{9fa5}]+)"[\w\W]+"status":109/u',$line,$matches);
+            if($ret) {
+                $order_sn = $matches['order_sn'];
+                $message = $matches['message'];
+
+                self::black_order($order_sn,$message);
+
+                $errs[$message] = empty($errs[$message]) ? 1 : $errs[$message] + 1;
+            }
+        }
+
+        foreach ($errs as $msg => $count) {
+            Log::record("msg:{$msg} count:{$count}",Log::DEBUG);
+        }
+
+        fclose($fn);
+
+        return true;
+    }
+}

+ 13 - 3
mobile/control/refill.php

@@ -1,7 +1,5 @@
 <?php
 
-use refill\CalcMerchantPrice;
-
 require_once(BASE_ROOT_PATH . '/mobile/control/merchant.php');
 require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
 require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
@@ -74,7 +72,11 @@ class refillControl extends merchantControl
         $card_name = $_GET['card_name'] ?? '';
 
         if(!$this->check_mchorder($this->mchid(),$mch_order)) {
-            return self::outerr(201,"客户订单号重复或者为空.");
+            return self::outerr(205,"客户订单号重复或者为空.");
+        }
+
+        if(!$this->can_refill($card_no)) {
+            return self::outerr(206,"平台不支持该卡充值.");
         }
 
         [$state, $err] = refill\RefillFactory::instance()->add($this->mchid(), $this->adminid(), $amount, $card_no, $mch_order, $idcard, $card_name, $notify_url);
@@ -87,6 +89,14 @@ class refillControl extends merchantControl
         }
     }
 
+    private function can_refill($cardno)
+    {
+        $card_info = refill\util::read_card($cardno);
+        if(empty($card_info)) return false;
+
+        return intval($card_info['black_card']) === 0;
+    }
+
     private function check_mchorder($mchid,$mch_order)
     {
         if(empty($mch_order)) {

+ 27 - 0
test/TestRefill.php

@@ -738,6 +738,33 @@ class TestRefill extends TestCase
     {
         $ret = $this->check_mchorder(1,'13281475');
     }
+
+    public function testMakemobile()
+    {
+        $mobile = refill\util::make_mobile();
+        $ret = refill\util::set_black('1000111100020445281');
+    }
+
+    public function testLoadBlack()
+    {
+        $path = BASE_DATA_PATH . "/log";
+        $names = ['20210119-mobile.log',
+            '20210120-mobile.log','20210121-mobile.log','20210122-mobile.log','20210123-mobile.log',
+            '20210124-mobile.log','20210125-mobile.log'];
+
+        foreach ($names as $name)
+        {
+            $file_name = $path . "/" . $name;
+            refill\util::black_from_log($file_name);
+        }
+    }
+
+    public function testErrre()
+    {
+        $line = '[13 2021-01-24 16:07:03] DEBUG: type = application/json;charset=utf-8, content={"channelOrderNumber":"610664819621371793","orderNumber":"GYFL1611475621525437","message":"充值失败","signature":"6c28c15b9ce2b2243742ecebbd929ac5","status":109}';
+        $ret = preg_match('/[\w\W]+"channelOrderNumber":"(?P<order_sn>[^"]+)"[\w\W]+"message":"(?P<message>[\x{4e00}-\x{9fa5}]+)"[\w\W]+"status":109/u',$line,$matches);
+    }
+
 }