|
@@ -0,0 +1,167 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace refill\legou;
|
|
|
+
|
|
|
+require_once(BASE_HELPER_RAPI_PATH . '/legou/config.php');
|
|
|
+
|
|
|
+use mtopcard\cards_helper;
|
|
|
+use refill;
|
|
|
+use Log;
|
|
|
+
|
|
|
+class RefillPhone extends refill\IRefillPhone
|
|
|
+{
|
|
|
+ public function __construct($cfgs)
|
|
|
+ {
|
|
|
+ parent::__construct($cfgs);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function req_params(int $phone,$card_type, int $amount, string $order_sn)
|
|
|
+ {
|
|
|
+ $params['username'] = config::USERNAME;
|
|
|
+ $params['order Number'] = $order_sn;
|
|
|
+ $params['cardNumber'] = $phone;
|
|
|
+ $params['cardExt'] = $amount;
|
|
|
+ $params['productNo'] = '400001';
|
|
|
+ $params['timestamp'] = $this->getMillisecond();
|
|
|
+ $params['notifyUrl'] = config::NOTIFY_URL;
|
|
|
+
|
|
|
+ return $params;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function add($card_no, $card_type, $amount, $input,&$net_errno = 0)
|
|
|
+ {
|
|
|
+ $params = $this->req_params($card_no,$card_type, $amount, $input['order_sn']);
|
|
|
+
|
|
|
+ [$success,$card] = cards_helper::reserve($card_type,$amount,$input['buyer_id'],$card_no,$card_type,$input['order_id'],config::StoreIDS);
|
|
|
+ if(!$success) {
|
|
|
+ return [false, '没有可用卡密', false];
|
|
|
+ }
|
|
|
+
|
|
|
+ $rcard_no = $card->card_no();
|
|
|
+ $rcard_key = $card->card_key();
|
|
|
+ $params['rechargeNum'] = $rcard_no;
|
|
|
+ $params['rechargePwd'] = $rcard_key;
|
|
|
+ $sign = $this->sign($params);
|
|
|
+ $params['sign'] = $sign;
|
|
|
+
|
|
|
+ $params['rechargeNum'] = $this->encryptWithOpenssl($rcard_no);
|
|
|
+ $params['rechargePwd'] = $this->encryptWithOpenssl($rcard_key);
|
|
|
+
|
|
|
+ $resp = http_request(config::ORDER_URL, $params , 'POST' , false , config::ExtHeaders , $net_errno);
|
|
|
+
|
|
|
+ $order_id = $params['order_id'];
|
|
|
+ if (empty($resp)) {
|
|
|
+ cards_helper::reuse($order_id);
|
|
|
+ return [false, '网络错误', true];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Log::record($resp, Log::DEBUG);
|
|
|
+ $resp = json_decode($resp ,true);
|
|
|
+
|
|
|
+ if (empty($resp)) {
|
|
|
+ cards_helper::reuse($order_id);
|
|
|
+ return [false, '网络错误', true];
|
|
|
+ }
|
|
|
+
|
|
|
+ $status = intval($resp['code']['status']);
|
|
|
+ if ($status == 100000) {
|
|
|
+ return [true, $resp['obj'], false];
|
|
|
+ } else {
|
|
|
+ cards_helper::reuse($order_id);
|
|
|
+ Log::record("refill {$card_no} err: {$resp['code']['desc']}", Log::DEBUG);
|
|
|
+ return [false, $resp['code']['desc'], false];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function query($refill_info)
|
|
|
+ {
|
|
|
+ $params['orderNumber'] = $refill_info['order_sn'];
|
|
|
+ $params['username'] = config::USERNAME;
|
|
|
+ $content = $params['username'] . $params['orderNumber'] . config::KEY;
|
|
|
+ $params['sign'] = 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, '网络错误'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $oper_status = intval($resp['code']['status']);
|
|
|
+ if ($oper_status == 100000)
|
|
|
+ {
|
|
|
+ $obj = $resp['obj'];
|
|
|
+ $code = $obj['resultCode'];
|
|
|
+
|
|
|
+ $order_id = $refill_info['order_id'];
|
|
|
+
|
|
|
+ if ($code == 2) { //充值成功
|
|
|
+ $order_state = ORDER_STATE_SUCCESS;
|
|
|
+ $updata['official_sn'] = strtolower($obj['rechargeVoucher']) == 'null' ? '' : $obj['rechargeVoucher'];
|
|
|
+ Model('refill_order')->edit($refill_info['order_id'], $updata);
|
|
|
+
|
|
|
+ cards_helper::assign($order_id, '');
|
|
|
+ } elseif ($code == 3) { //充值失败
|
|
|
+ $order_state = ORDER_STATE_CANCEL;
|
|
|
+ $this->onError($order_id, $obj['rechargeDesc']);
|
|
|
+ } elseif($code == 4) { //订单存疑,需要人工确认
|
|
|
+ return [false, $resp['code']['desc']];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $order_state = ORDER_STATE_SEND;
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, $order_state];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return [false, $resp['code']['desc']];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function onError($order_id, $desc)
|
|
|
+ {
|
|
|
+ $desc = json_decode($desc, true);
|
|
|
+ $status = $desc['rechargeStatus'];
|
|
|
+ $desc = $desc['rechargeDesc'];
|
|
|
+
|
|
|
+ if (in_array($status, [600400, 600401, 600402, 600403, 600404, 600405])) {
|
|
|
+ cards_helper::freeze($order_id,$desc);
|
|
|
+ } else {
|
|
|
+ cards_helper::reuse($order_id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function sign($params)
|
|
|
+ {
|
|
|
+ $key = config::KEY;
|
|
|
+ $content = $params['username'] . $params['orderNumber'] . $params['cardNumber'] . $params['cardExt'] . $params['rechargeNum'] . $params['rechargePwd'] . $params['productNo'];
|
|
|
+ $content .= $params['timestamp'] . $key;
|
|
|
+ return md5($content);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取毫秒级别的时间戳
|
|
|
+ */
|
|
|
+ private function getMillisecond()
|
|
|
+ {
|
|
|
+ $cur = microtime (true);
|
|
|
+ $cur = intval($cur * 1000);
|
|
|
+ return $cur;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function encryptWithOpenssl($data = '')
|
|
|
+ {
|
|
|
+ $key = substr(config::KEY , 0 ,16);
|
|
|
+ $iv = substr(config::KEY , -16);
|
|
|
+ return base64_encode(openssl_encrypt($data, "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv));
|
|
|
+ }
|
|
|
+}
|