stanley-king 1 year ago
parent
commit
49e0b36cb0

+ 10 - 0
helper/refill/api/mh/mifeng_fast/ReadMe.MD

@@ -0,0 +1,10 @@
+
+
+诚邀贵司入驻我司新平台,这是我司新平台接口文档地址:http://openapi.mf178.cn/docs/api
+【商户中心】http://merchant.task.mf178.cn/
+AppKey:659270031
+AppSecret:79fa5202cc1b176d
+商户后台
+https://merchant.task.mf178.cn/
+账户:bjmh001
+密码:bjmh001@888

+ 51 - 0
helper/refill/api/mh/mifeng_fast/RefillCallBack.php

@@ -0,0 +1,51 @@
+<?php
+namespace refill\mifeng_fast;
+
+require_once(BASE_HELPER_RAPI_PATH . '/mifeng_fast/config.php');
+use refill;
+use Log;
+
+class RefillCallBack implements refill\IRefillCallBack
+{
+    public function verify($params): bool
+    {
+        $sign = config::sign($params);
+        if ($params['sign'] == $sign) {
+            return true;
+        } else {
+            Log::record('xianyu_fast\RefillCallBack::verify fail',Log::DEBUG);
+            return false;
+        }
+    }
+
+    //[$order_id, $success, $can_try, $need_handle, $official_sn]
+    public function notify($params): array
+    {
+        $order_sn = $params['third_id'];
+        $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'];
+        $state = intval($params['state']);
+
+        if ($state === 3) {
+            $official_sn = $resp['voucher'] ?? '';
+            $ch_trade_no = $resp['order_id'] ?? '';
+            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 ($state === 4) {
+            $ch_trade_no = $resp['order_id'] ?? '';
+            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, ''];
+        }
+    }
+}

+ 193 - 0
helper/refill/api/mh/mifeng_fast/RefillPhone.php

@@ -0,0 +1,193 @@
+<?php
+
+namespace refill\mifeng_fast;
+
+require_once(BASE_HELPER_RAPI_PATH . '/mifeng_fast/config.php');
+
+use mtopcard;
+use refill;
+use Log;
+
+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,int $regin_no): array
+    {
+        $operator_getter = function ($card_type)
+        {
+            if ($card_type == mtopcard\ChinaMobileCard) {
+                return "移动";
+            } elseif ($card_type == mtopcard\ChinaUnicomCard) {
+                return "联通";
+            } elseif ($card_type == mtopcard\ChinaTelecomCard) {
+                return "电信";
+            } else {
+                return false;
+            }
+        };
+
+        $operator_id = $operator_getter($card_type);
+        $region = mtopcard\scard_region($regin_no);
+
+        if ($operator_id === false) {
+            return [];
+        }
+
+        $params = [
+            'app_key' => config::AppKey,
+            'timestamp' => time(),
+            'product_id' => '10001',
+            'third_id' => $order_sn,
+            'call_back_url' => config::NOTIFY_URL,
+            'datas' => [
+                'target' => "$phone",
+                'amount' => $amount,
+                'operator_id' => $operator_id
+            ]
+        ];
+
+        if (!empty($region)) {
+            $params['datas']['prov_code'] = $region;
+        }
+        $params['sign'] = config::sign($params);
+
+        return $params;
+    }
+
+    //[$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, $params['regin_no'] ?? -1);
+        if (empty($params)) {
+            return [false, '提单参数不符合', false];
+        }
+
+        $resp = http_post_data(config::ORDER_URL, json_encode($params), config::ExtHeaders);
+        if (empty($resp)) {
+            return [false, '网络错误', true];
+        }
+        else
+        {
+            Log::record($resp, Log::DEBUG);
+            $resp = json_decode($resp, true);
+
+            if (empty($resp)) {
+                return [false, '网络错误', true];
+            }
+
+            $code = $resp['code'];
+            if ($code === 0) {
+                return [true, $resp['data']['order_id'], false];
+            } elseif (in_array($code, [10010, 10015])) {
+                $net_errno = "HTTP-$code";
+                $errmsg = config::ERRMSG[$code] ?? '';
+                return [false, $errmsg, true];
+            } else {
+                $errmsg = config::ERRMSG[$code] ?? '';
+                return [false, $errmsg, false];
+            }
+        }
+    }
+
+    private function query_params($refill_info)
+    {
+        $params = [
+            'app_key' => config::AppKey,
+            'timestamp' => time(),
+            'third_id' => $refill_info['order_sn'],
+            'time' => $refill_info['commit_time']
+        ];
+
+        $params['sign'] = config::sign($params);
+
+        return $params;
+    }
+
+    public function query($refill_info): array
+    {
+        $params = $this->query_params($refill_info);
+        $resp = http_post_data(config::QUERY_URL, json_encode($params), config::ExtHeaders);
+
+        if (empty($resp)) {
+            return [false, '系统错误', ''];
+        }
+        else
+        {
+            Log::record($resp, Log::DEBUG);
+            $resp = json_decode($resp, true);
+
+            if (empty($resp)) {
+                return [false, '系统错误', ''];
+            }
+
+            $official_sn = '';
+            $code = intval($resp['code']);
+            if ($code === 0)
+            {
+                $order_id = $refill_info['order_id'];
+
+                $data = $resp['data'];
+                $state = $data['state'];
+
+                if ($state === 3) {
+                    $official_sn = $data['voucher']   ?? '';
+                    $ch_trade_no = $data['order_id'] ?? '';
+                    Model('refill_order')->edit($order_id, ['ch_trade_no' => $ch_trade_no,'official_sn' => $official_sn]);
+
+                    $order_state = ORDER_STATE_SUCCESS;
+                }
+                elseif(in_array($state, [4,6])) {
+                    $ch_trade_no = $data['order_id'] ?? '';
+                    Model('refill_order')->edit($order_id, ['ch_trade_no' => $ch_trade_no]);
+
+                    $order_state = ORDER_STATE_CANCEL;
+                }
+                else {
+                    $order_state = ORDER_STATE_SEND;
+                }
+            }
+            else {
+                return [false, '其他,或网络错误', ''];
+            }
+
+            return [true, $order_state, $official_sn];
+        }
+    }
+
+    private function balance_params()
+    {
+        $params = [
+            'app_key' => config::AppKey,
+            'timestamp' => time(),
+        ];
+        $params['sign'] = config::sign($params);
+
+        return $params;
+    }
+
+    public function balance(): array
+    {
+        $params = $this->balance_params();
+        $resp = http_post_data(config::BALANCE_URL, json_encode($params), config::ExtHeaders);
+
+        if (empty($resp)) {
+            return [false, '系统错误'];
+        }
+        else
+        {
+            Log::record($resp, Log::DEBUG);
+            $resp = json_decode($resp, true);
+            if (empty($resp)) {
+                return [false, '系统错误'];
+            } elseif ($resp['code'] === 0) {
+                return [true, ncPriceFormat($resp['data']['amount'])];
+            } else {
+                return [false, '其它,失败'];
+            }
+        }
+    }
+}

+ 52 - 0
helper/refill/api/mh/mifeng_fast/config.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace refill\mifeng_fast;
+class config
+{
+    const AppKey = "659270031";
+    const AppSecret = '79fa5202cc1b176d';
+
+    const CH_HOST = 'http://merchant.task.mf178.cn';
+    const ORDER_URL   = config::CH_HOST . '/api/merchant/upload_order';
+    const QUERY_URL   = config::CH_HOST . '/api/merchant/order_info';
+    const BALANCE_URL = config::CH_HOST . '/api/merchant/account';
+
+    const NOTIFY_URL  = BASE_SITE_URL . "/racc/callback/mh/mifeng_fast.php";
+
+    public const ERRCODES = [600,602,603,606,622,623,624,615,637,751];
+
+    const ExtHeaders = ['Content-Type: application/json;charset=UTF-8'];
+
+    public const ERRMSG = [
+        10001 => '参数错误',
+        10002 => '签名错误',
+        10003 => '时间戳超期',
+        10004 => '商户不存在',
+        10005 => '商户被禁用',
+        10006 => '商户请求IP错误',
+        10010 => '第三方订单号已存在',
+        10011 => '账户余额不足',
+        10012 => '渠道不可用或无资源',
+        10013 => '充值故障,请稍后再试',
+        10014 => '充值号码有问题(号码格式错误、空号或已拉黑)',
+        10015 => '订单不存在',
+        10016 => '请求太频繁,请稍后重试'
+    ];
+
+    public static function sign($params)
+    {
+        ksort($params);
+
+        $buff = '';
+        foreach ($params as $k => $v)
+        {
+            if ($k != "sign" && $k != "datas") {
+                $buff .= $k . $v;
+            }
+        }
+        $buff .= config::AppKey;
+        $sign = md5($buff);
+
+        return $sign;
+    }
+}

+ 3 - 3
helper/refill/api/mh/zhixin_fast/RefillCallBack.php

@@ -32,14 +32,14 @@ class RefillCallBack implements refill\IRefillCallBack
         $recharge_status = intval($params['recharge_status']);
 
         if ($recharge_status === 2) {
-            $official_sn = $resp['data']['elecardID']   ?? '';
-            $ch_trade_no = $resp['data']['up_order_no'] ?? '';
+            $official_sn = $params['elecardID'] ?? '';
+            $ch_trade_no = $params['up_order_no'] ?? '';
             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 ($recharge_status === 6) {
-            $ch_trade_no = $resp['data']['up_order_no'] ?? '';
+            $ch_trade_no = $params['up_order_no'] ?? '';
             Model('refill_order')->edit($order_id, ['ch_trade_no' => $ch_trade_no]);
 
             return [$order_id, false, true, true, ''];

+ 3 - 3
helper/refill/api/mh/zhixin_fast/RefillPhone.php

@@ -81,10 +81,10 @@ class RefillPhone extends refill\IRefillPhone
                 return [false, config::ERRMSG[$nRtn], false];
             } elseif (in_array($nRtn, [625])) {
                 $net_errno = "HTTP-$nRtn";
-                return [false, $net_errno, true];
+                return [false, "重复订单号", true];
             } else {
                 $net_errno = "HTTP-998";
-                return [false, $net_errno, true];
+                return [false, "其他异常", true];
             }
         }
     }
@@ -178,7 +178,7 @@ class RefillPhone extends refill\IRefillPhone
             if (empty($resp)) {
                 return [false, '系统错误'];
             } elseif (intval($resp['code']) === 2) {
-                return [true, ncPriceFormat($resp['data']['balance'] + $resp['data']['credit'])];
+                return [true, ncPriceFormat($resp['data']['balance'])];
             } else {
                 return [false, '其它,失败'];
             }

+ 10 - 0
test/mh/TestRefill.php

@@ -138,6 +138,16 @@ class TestRefill extends TestCase
         $resp = $provider->balance();
     }
 
+    public function testMifengFast()
+    {
+        $provider = $this->getProvider('mifeng_fast');
+        $order_sn = $this->make_sn();
+        $resp = $provider->add(13911129867, 4, 1, ['order_sn' => $order_sn, 'regin_no' => 1]);//30为测试产品
+        $resp = $provider->query(['order_sn' => $order_sn]);
+        $resp = $provider->balance();
+    }
+
+
     public function testSohanFastCB()
     {
         $post = [