Browse Source

“添加lifang通道”

zb727 1 year ago
parent
commit
911dbaa237

+ 52 - 0
helper/refill/api/mh/lifang_normal/RefillCallBack.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace refill\lifang_normal;
+
+require_once(BASE_HELPER_RAPI_PATH . '/lifang_normal/config.php');
+
+use refill;
+use refill\lifang_normal\config;
+
+class RefillCallBack implements refill\IRefillCallBack
+{
+    public function verify($params): bool
+    {
+        $input = $params;
+        unset($input['sign']);
+        $sign = config::sign($input,config::notify_keys);
+        if ($params['sign'] == $sign) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    //[$order_id, $success, $can_try, $need_handle, $official_sn]
+    public function notify($params): array
+    {
+        $order_sn = $params['outOrderId'];
+        $ch_trade_no = $params['orderId'];
+        $order_info = Model('vr_order')->getOrderInfo(['order_sn' => $order_sn]);
+
+        if (empty($order_info)) {
+            return [false, false, false, false, ''];
+        }
+
+        $order_id = $order_info['order_id'];
+        $official_sn = $params['chargeOrderInfo'] ?? '';
+
+        //订单状态,0 充值中, 1 充值成功, 9 充值失败, 6 存疑(人工核实)
+        $status = $params['status'];
+        if ($status == 1) {
+            Model('refill_order')->edit($order_id, ['ch_trade_no' => $ch_trade_no, 'official_sn' => $official_sn]);
+            return [$order_id, true, false, true, $official_sn];
+        }
+        elseif ($status == 9) {
+            Model('refill_order')->edit($order_id, ['ch_trade_no' => $ch_trade_no]);
+            return [$order_id, false, true, true, ''];
+        }
+        else {
+            return [$order_id, false, false, false, ''];
+        }
+    }
+}

+ 135 - 0
helper/refill/api/mh/lifang_normal/RefillPhone.php

@@ -0,0 +1,135 @@
+<?php
+
+namespace refill\lifang_normal;
+
+require_once(BASE_HELPER_RAPI_PATH . '/lifang_normal/config.php');
+
+use mtopcard;
+use refill;
+use Log;
+use refill\lifang_normal\config;
+
+class RefillPhone extends refill\IRefillPhone
+{
+    public function __construct($cfgs)
+    {
+        parent::__construct($cfgs);
+    }
+
+    private function add_params(int $phone, int $amount, string $order_sn, int $card_type): array
+    {
+        $input = [
+            'amount'        => $amount,
+            'outOrderId'    => $order_sn,
+            'phoneNumber'   => $phone,
+            'requestDate'   => config::time_stamp(),
+        ];
+
+        $input =  config::gen_params($input,config::add_keys);
+        $input['callBackUrl'] = config::NOTIFY_URL;
+
+        return  $input;
+    }
+
+    //[$state, $errmsg, $neterr]
+    public function add($card_no, $card_type, $amount, $params, &$net_errno = 0): array
+    {
+        $params = $this->add_params($card_no, $amount, $params['order_sn'], $card_type);
+        if(empty($params)) {
+            return [false, '提单参数不符合', false];
+        }
+
+        $resp = http_request(config::ORDER_URL, $params, 'POST', false, [], $net_errno);
+        if (empty($resp)) {
+            return [false, '系统错误', true];
+        }
+        else
+        {
+            /// 订单状态,0 充值中 1 充值成功 9 充值失败 6 存疑(人工核实)
+            Log::record($resp, Log::DEBUG);
+            $resp = json_decode($resp, true);
+            if (empty($resp)) {
+                return [false, '系统错误', true];
+            } elseif ($resp['code'] === 200) {
+                return [true, $resp['orderId'], false];
+            } else {
+                return [false, $resp['msg'], false];
+            }
+        }
+    }
+
+    public function query($refill_info): array
+    {
+        $input = [
+            'outOrderId'  => $refill_info['order_sn'],
+            'requestDate' => config::time_stamp()
+        ];
+
+        $params = config::gen_params($input,config::query_keys);
+
+        $resp = http_request(config::QUERY_URL, $params , 'POST');
+        if (empty($resp)) {
+            return [false, '系统错误', ''];
+        }
+        else
+        {
+            Log::record($resp, Log::DEBUG);
+            $resp = json_decode($resp, true);
+            if (empty($resp)) {
+                return [false, '系统错误', ''];
+            }
+
+            $code = intval($resp['code']);
+            if ($code === 200)
+            {
+                $val = $resp['data'];
+                $status = intval($val['status']);
+                $official_sn = '';
+
+                //订单状态,0 充值中 1 充值成功 9 充值失败 6 存疑(人工核实)
+                if ($status == 1) {
+                    Model('refill_order')->edit($refill_info['order_id'], ['ch_trade_no' => $val['orderId'],'official_sn' => $official_sn]);
+                    $order_state = ORDER_STATE_SUCCESS;
+                }
+                elseif ($status == 9) {
+                    Model('refill_order')->edit($refill_info['order_id'], ['ch_trade_no' => $val['orderId']]);
+                    $order_state = ORDER_STATE_CANCEL;
+                }
+                else {
+                    $order_state = ORDER_STATE_SEND;
+                }
+
+                return [true, $order_state, $official_sn];
+            }
+            else
+            {
+                return [false, $resp['msg']];
+            }
+        }
+    }
+
+    public function balance(): array
+    {
+        $input = [
+            'requestDate' => config::time_stamp()
+        ];
+        $params = config::gen_params($input,config::balance_keys);
+
+        $resp = http_request(config::BALANCE_URL, $params , 'POST');
+        if (empty($resp)) {
+            return [false, '系统错误'];
+        }
+        else
+        {
+            Log::record($resp, Log::DEBUG);
+            $resp = json_decode($resp, true);
+            if (empty($resp)) {
+                return [false, '系统错误'];
+            } elseif ($resp['code'] === 200) {
+                return [true, ncPriceFormat($resp['data']['leftBalance'])];
+            } else {
+                return [false, $resp['msg']];
+            }
+        }
+    }
+}

+ 49 - 0
helper/refill/api/mh/lifang_normal/config.php

@@ -0,0 +1,49 @@
+<?php
+
+namespace refill\lifang_normal;
+
+class config
+{
+    public const accessToken = 'beaf87f8-7c6b-cb8a-98b8-53c207ca9aef';
+    public const appSecret = '24b67f6f-2696-ede0-ced6-0b975b05d8e1';
+    public const ORDER_URL = 'http://api.lifang001.com/api/mobile/charge';
+    public const QUERY_URL = 'http://api.lifang001.com/api/mobile/query';
+    public const BALANCE_URL = 'http://api.lifang001.com/api/finance/balance';
+    public const NOTIFY_URL = BASE_SITE_URL . "/racc/callback/mh/lifang_normal.php";
+
+    public const add_keys = ['accessToken', 'amount', 'outOrderId', 'phoneNumber', 'requestDate'];
+    public const query_keys = ['accessToken', 'outOrderId', 'requestDate'];
+    public const balance_keys = ['accessToken', 'requestDate'];
+    public const notify_keys = ['accessToken', 'orderId', 'outOrderId', 'requestDate', 'status'];
+
+    public static function sign($params,$keys): string
+    {
+        $str = '';
+        foreach ($keys as $key)
+        {
+            if($key == 'accessToken') {
+                $str .= $key.config::accessToken;
+            }
+            else {
+                $val = $params[$key] ?? '';
+                $str .= $key.$val;
+            }
+        }
+
+        $str .= self::appSecret;
+
+        return strtoupper(md5($str));
+    }
+
+    public static function gen_params($params,$keys)
+    {
+        $params['sign'] = config::sign($params,$keys);
+        $params['accessToken'] = config::accessToken;
+        return $params;
+    }
+
+    public static function time_stamp ()
+    {
+        return date('YmdHis', time());
+    }
+}

+ 4 - 0
racc/callback/mh/lifang_normal.php

@@ -0,0 +1,4 @@
+<?php
+
+refill\util::push_notify('lifang_normal',$_POST);
+echo ('success');

+ 18 - 1
test/mh/TestRefill.php

@@ -40,7 +40,6 @@ class TestRefill extends TestCase
         $provider->verify($content);
         $provider->notify($content);
     }
-
     public function testSuhu()
     {
         $provider = $this->getProvider('suhu_normal');
@@ -92,6 +91,24 @@ class TestRefill extends TestCase
         $provider->notify($content);
     }
 
+    public function testLifang()
+    {
+        $provider = $this->getProvider('lifang_normal');
+        $order_sn = $this->make_sn();
+        $resp = $provider->add(15652921127, 5, 30, ['order_sn' => $order_sn, 'regin_no' => 1]);
+        $resp = $provider->query(['order_sn' => $order_sn]);
+        $resp = $provider->balance();
+    }
+
+    public function testLifangCB()
+    {
+        $_POST='{"orderId":"S23112800025098","outOrderId":"60201701159853851187","requestDate":"20231128164156","sign":"E172D40D040D26C8226E237ACED8C861","accessToken":"beaf87f8-7c6b-cb8a-98b8-53c207ca9aef","status":"1"}';
+        $content = json_decode($_POST,true);
+        $provider = $this->getProvider('lifang_normal','RefillCallBack');
+        $provider->verify($content);
+        $provider->notify($content);
+    }
+
     public function testJson()
     {
         $val = json_encode(['account' => '13911129867']);