|
@@ -0,0 +1,198 @@
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+namespace refill\guochuang_nation_trd;
|
|
|
|
+
|
|
|
|
+require_once(BASE_HELPER_RAPI_PATH . '/guochuang_nation_trd/config.php');
|
|
|
|
+
|
|
|
|
+use refill;
|
|
|
|
+use Log;
|
|
|
|
+
|
|
|
|
+class RefillPhone extends refill\IRefillPhone
|
|
|
|
+{
|
|
|
|
+ public function __construct($cfgs)
|
|
|
|
+ {
|
|
|
|
+ parent::__construct($cfgs);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function none_support($card_no)
|
|
|
|
+ {
|
|
|
|
+ return strpos($card_no, '148') === 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function add_params(int $phone, int $amount, int $card_type, string $order_sn,$regin_no)
|
|
|
|
+ {
|
|
|
|
+ $params['phone'] = $phone;
|
|
|
|
+ $params['phoneType'] = config::operator[$card_type];
|
|
|
|
+ $params['money'] = $amount;
|
|
|
|
+ $params['outerId'] = $order_sn;
|
|
|
|
+ $params['callBackUrl'] = config::NOTIFY_URL;
|
|
|
|
+ $params['speed'] = 0;
|
|
|
|
+ $params['provId'] = config::ProvinceMap[$regin_no];
|
|
|
|
+
|
|
|
|
+ if(empty($params['provId'])) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ return $params;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function form_uri($params, $service)
|
|
|
|
+ {
|
|
|
|
+ $get_params['service'] = $service;
|
|
|
|
+ $get_params['userId'] = config::UserId;
|
|
|
|
+ $get_params['ts'] = $this->getMillisecond();
|
|
|
|
+
|
|
|
|
+ $sign = $this->sign($params,$get_params);
|
|
|
|
+ $get_params['sign'] = $sign;
|
|
|
|
+
|
|
|
|
+ $url = config::API_URL;
|
|
|
|
+ $uri = $url . (strpos($url, '?') ? '&' : '?') . (is_array($get_params) ? http_build_query($get_params) : $get_params);
|
|
|
|
+
|
|
|
|
+ return $uri;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function add($card_no, $card_type, $amount, $input, &$net_errno = 0)
|
|
|
|
+ {
|
|
|
|
+ $order_sn = $input['order_sn'];
|
|
|
|
+ $regin_no = $input['regin_no'] ?? -1;
|
|
|
|
+ if($regin_no <= 0) {
|
|
|
|
+ return [false, '省份获取错误', false];
|
|
|
|
+ }
|
|
|
|
+ if($this->none_support($card_no)) {
|
|
|
|
+ return [false, '不支持物联网卡号', false];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $params = $this->add_params($card_no, $amount, $card_type, $order_sn,$regin_no);
|
|
|
|
+ if($params === false) {
|
|
|
|
+ return [false, '省份信息错误', false];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $uri = $this->form_uri($params,'order.phone.charge');
|
|
|
|
+ $resp = http_request($uri, $params, 'POST', false, [], $net_errno);
|
|
|
|
+
|
|
|
|
+ if (empty($resp)) {
|
|
|
|
+ return [false, '系统错误', true];
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ Log::record($resp, Log::DEBUG);
|
|
|
|
+
|
|
|
|
+ $resp = json_decode($resp ,true);
|
|
|
|
+ $code = $resp['code'];
|
|
|
|
+ if (empty($resp)) {
|
|
|
|
+ return [false, '系统错误', true];
|
|
|
|
+ } elseif ($code === 'SUCCESS') {
|
|
|
|
+ return [true, $resp['id'], false];
|
|
|
|
+ } elseif (in_array($code, config::ORDER_ERR_CODE)) {
|
|
|
|
+ return [false, $code, false];
|
|
|
|
+ } elseif (in_array($code, ['SYSTEM_ERROR', 'ORDER_ID_EXIST'])) {
|
|
|
|
+ $net_errno = "HTTP-{$code}";
|
|
|
|
+ return [false, $code, true];
|
|
|
|
+ } else {
|
|
|
|
+ $err = 998;
|
|
|
|
+ $net_errno = "HTTP-{$err}";
|
|
|
|
+ return [false, $code, true];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function query($refill_info)
|
|
|
|
+ {
|
|
|
|
+ //上游单号,可以为空,但需参与签名
|
|
|
|
+ $params['id'] = '';
|
|
|
|
+ $params['outerId'] = $refill_info['order_sn'];
|
|
|
|
+
|
|
|
|
+ $uri = $this->form_uri($params,'order.status.query');
|
|
|
|
+ $resp = http_request($uri, $params, 'POST');
|
|
|
|
+
|
|
|
|
+ if (empty($resp)) {
|
|
|
|
+ return [false, '网络错误', ''];
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ Log::record($resp, Log::DEBUG);
|
|
|
|
+ $resp = json_decode($resp, true);
|
|
|
|
+ $code = $resp['code'];
|
|
|
|
+ if (empty($resp))
|
|
|
|
+ {
|
|
|
|
+ return [false, '网络错误', ''];
|
|
|
|
+ }
|
|
|
|
+ elseif ($code === 'SUCCESS')
|
|
|
|
+ {
|
|
|
|
+ $offical_sn = '';
|
|
|
|
+ $status = $resp['status'];
|
|
|
|
+
|
|
|
|
+ if ($status === 'SUCCESS') {
|
|
|
|
+ $offical_sn = strtolower($resp['evidence']) == 'null' ? '' : $resp['evidence'];
|
|
|
|
+ $save['official_sn'] = $offical_sn;
|
|
|
|
+ Model('refill_order')->edit($refill_info['order_id'], $save);
|
|
|
|
+ $order_state = ORDER_STATE_SUCCESS;
|
|
|
|
+ } elseif ($status === 'FAIL') {
|
|
|
|
+ $order_state = ORDER_STATE_CANCEL;
|
|
|
|
+ } elseif ($status === 'PROCESSING') {
|
|
|
|
+ $order_state = ORDER_STATE_SEND;
|
|
|
|
+ } elseif ($status === 'ORDER_NOT_EXIST' && (time() - $refill_info['commit_time'] >= 600)) {
|
|
|
|
+ $order_state = ORDER_STATE_NOEXIST;
|
|
|
|
+ } else {
|
|
|
|
+ return [false, $code, $offical_sn];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return [true, $order_state, $offical_sn];
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ return [false, "code={$code}", ''];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function balance()
|
|
|
|
+ {
|
|
|
|
+ $uri = $this->form_uri([],'user.balance.query');
|
|
|
|
+ $resp = http_request($uri, [], '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'] === 'SUCCESS') {
|
|
|
|
+ return [true, $resp['balance']];
|
|
|
|
+ } else {
|
|
|
|
+ return [false, $resp['code']];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function sign($params,$get_params)
|
|
|
|
+ {
|
|
|
|
+ $userId = config::UserId;
|
|
|
|
+ $api_key = config::ApiKey;
|
|
|
|
+ $content = "service={$get_params['service']}&userId={$userId}&ts={$get_params['ts']}&";
|
|
|
|
+ if(!empty($params))
|
|
|
|
+ {
|
|
|
|
+ foreach ($params as $key => $value){
|
|
|
|
+ $content .= "{$key}={$value}&";
|
|
|
|
+ }
|
|
|
|
+ $content = rtrim($content, '&');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $content .= "&key={$api_key}";
|
|
|
|
+ return strtoupper(md5($content));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取毫秒级别的时间戳
|
|
|
|
+ */
|
|
|
|
+ private function getMillisecond()
|
|
|
|
+ {
|
|
|
|
+ $cur = microtime (true);
|
|
|
|
+ $cur = intval($cur * 1000);
|
|
|
|
+ return $cur;
|
|
|
|
+ }
|
|
|
|
+}
|