123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- namespace refill\zhongst_doubi;
- require_once(BASE_HELPER_RAPI_PATH . '/zhongst_doubi/config.php');
- use refill;
- use Log;
- class RefillPhone extends refill\IRefillThird
- {
- public function __construct($cfgs)
- {
- parent::__construct($cfgs);
- }
- private function req_params($phone, int $amount, string $order_sn, $product_code)
- {
- $params['szAgentId'] = config::USER_ID;
- $params['szOrderId'] = $order_sn;
- $params['szPhoneNum'] = $phone;
- $params['nMoney'] = $amount;
- $params['nSortType'] = '1031';
- $params['szProductId'] = $product_code;
- $params['nProductClass'] = 1;
- $params['nProductType'] = 1;
- $params['szTimeStamp'] = date("Y-m-d H:i:s");
- $params['szNotifyUrl'] = config::NOTIFY_URL;
- return $params;
- }
- private function getProductAmount($sys_pcode)
- {
- $thrid_refill = Model('thrid_refill');
- $product = $thrid_refill->getProduct(['system_code' => $sys_pcode]);
- if (empty($product)) {
- return false;
- } else {
- return $product['refill_amount'];
- }
- }
- private function getProductCode($goods_id, $sys_pcode)
- {
- $thrid_refill = Model('thrid_refill');
- $product = $thrid_refill->getProviderProduct($this->mStoreID,$goods_id,$sys_pcode);
- if (empty($product)) {
- return false;
- } else {
- return $product['channel_code'];
- }
- }
- public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
- {
- $order_sn = $params['order_sn'];
- $goods_id = intval($params['goods_id']);
- $product_code = $this->getProductCode($goods_id, $params['product_code']);
- Model('thrid_refill')->edit_third($params['order_id'], ['chcode' => $product_code]);
- $amount = $this->getProductAmount($params['product_code']);
- if(empty($amount)) {
- return [false, '产品未开启', false];
- }
- $params = $this->req_params($card_no, $amount, $order_sn, $product_code);
- if(empty($params['szProductId'])) {
- return [false, '商品编号错误', false];
- }
- $sign = $this->sign($params);
- $params['szVerifyString'] = $sign;
- $resp = http_request(config::ORDER_URL, $params, 'POST', false, config::ExtHeaders, $net_errno);
- if (empty($resp)) {
- return [false, '网络错误', true];
- }
- else
- {
- Log::record($resp, Log::DEBUG);
- $resp = json_decode($resp, true);
- $nRtn = $resp['nRtn'];
- if (empty($resp)) {
- return [false, '网络错误', true];
- } elseif ($nRtn === 0) {
- return [true, '', false];
- } elseif (in_array($nRtn, config::ERR_NOS, true)) {
- return [false, $resp['szRtnCode'], false];
- } elseif (in_array($nRtn, [2050, 999], true)) {
- $net_errno = "HTTP-{$nRtn}";
- return [false, $resp['szRtnCode'], true];
- } else {
- $err = 998;
- $net_errno = "HTTP-{$err}";
- return [false, $resp['szRtnCode'], true];
- }
- }
- }
- public function query($refill_info)
- {
- $params['szAgentId'] = config::USER_ID;
- $params['szOrderId'] = $refill_info['order_sn'];
- $key = config::KEY;
- $content = "szAgentId={$params['szAgentId']}&szOrderId={$params['szOrderId']}&szKey={$key}";
- $params['szVerifyString'] = md5($content);
- $resp = http_request(config::QUERY_URL, $params, 'POST', false, config::ExtHeaders);
- if (empty($resp)) {
- return [false, '网络错误'];
- }
- else
- {
- Log::record($resp, Log::DEBUG);
- $resp = json_decode($resp, true);
- if (empty($resp)) {
- return [false, '网络错误'];
- }
- $status = $resp['nRtn'];
- if ($status === 5012) {
- $updata['official_sn'] = $resp['szRtnMsg'];
- Model('refill_order')->edit($refill_info['order_id'], $updata);
- $order_state = ORDER_STATE_SUCCESS;
- } elseif ($status === 5013) {
- $order_state = ORDER_STATE_CANCEL;
- } elseif (in_array($status, [5011,5019],true)) {
- $order_state = ORDER_STATE_SEND;
- } elseif ($status === 5005 && (time() - $refill_info['commit_time'] >= 300)) {
- $order_state = ORDER_STATE_NOEXIST;
- } else {
- return [false, $resp['szRtnMsg']];
- }
- return [true, $order_state];
- }
- }
- public function balance()
- {
- $params['szAgentId'] = config::USER_ID;
- $key = config::KEY;
- $content = "szAgentId={$params['szAgentId']}&szKey={$key}";
- $params['szVerifyString'] = md5($content);
- $resp = http_request(config::BALANCE_URL, $params, 'POST', false, 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['nRtn'] === 0) {
- return [true, $resp['fBalance']];
- } else {
- return [false, $resp['szRtnCode']];
- }
- }
- }
- private function sign($params)
- {
- $userid = config::USER_ID;
- $key = config::KEY;
- $content = "szAgentId={$userid}&szOrderId={$params['szOrderId']}&szPhoneNum={$params['szPhoneNum']}&nMoney={$params['nMoney']}&nSortType={$params['nSortType']}";
- $content .= "&nProductClass={$params['nProductClass']}&nProductType={$params['nProductType']}&szTimeStamp={$params['szTimeStamp']}&szKey={$key}";
- return md5($content);
- }
- }
|