|
@@ -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, '其它,失败'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|