|
@@ -0,0 +1,154 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace refill\yunlingfs;
|
|
|
+
|
|
|
+require_once(BASE_HELPER_RAPI_PATH . '/yunlingfs/config.php');
|
|
|
+
|
|
|
+use refill;
|
|
|
+use Log;
|
|
|
+
|
|
|
+class RefillPhone extends refill\IRefillPhone
|
|
|
+{
|
|
|
+ public function __construct($cfgs)
|
|
|
+ {
|
|
|
+ parent::__construct($cfgs);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function req_params(int $phone, int $amount, string $order_sn)
|
|
|
+ {
|
|
|
+ $params['userid'] = config::USER_ID;
|
|
|
+ $params['productid'] = '';
|
|
|
+ $params['productType'] = '1';
|
|
|
+ $params['price'] = $amount;
|
|
|
+ $params['num'] = 1;
|
|
|
+ $params['mobile'] = $phone;
|
|
|
+ $params['spordertime'] = date("YmdHis");
|
|
|
+ $params['sporderid'] = $order_sn;
|
|
|
+ $params['back_url'] = config::NOTIFY_URL;
|
|
|
+ return $params;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function add($card_no, $card_type, $amount, $params)
|
|
|
+ {
|
|
|
+ $order_sn = $params['order_sn'];
|
|
|
+ $params = $this->req_params($card_no, $amount, $order_sn);
|
|
|
+ $params['operator'] = config::operator[$card_type];
|
|
|
+ $region = $this->getPhoneInfo($card_no);
|
|
|
+ if($region === false) {
|
|
|
+ return [false, '归属地获取失败', false];
|
|
|
+ }
|
|
|
+
|
|
|
+ $params['region'] = $region;
|
|
|
+ $sign = $this->sign($params);
|
|
|
+ $params['sign'] = $sign;
|
|
|
+
|
|
|
+ $resp = http_request(config::ORDER_URL, $params);
|
|
|
+
|
|
|
+ if (empty($resp)) {
|
|
|
+ return [false, '网络错误', true];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Log::record($resp, Log::DEBUG);
|
|
|
+ $resp = $this->xmlToArray($resp);
|
|
|
+ if (empty($resp)) {
|
|
|
+ return [false, '网络错误', true];
|
|
|
+ }
|
|
|
+ $resultno = intval($resp['resultno']);
|
|
|
+ if (in_array($resultno, [0,1,2])) {
|
|
|
+ return [true, $resp['orderid'], false];
|
|
|
+ } else {
|
|
|
+ return [false, $resp['message'], false];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function query($refill_info)
|
|
|
+ {
|
|
|
+ $params['sporderid'] = $refill_info['order_sn'];
|
|
|
+ $params['userid'] = config::USER_ID;
|
|
|
+
|
|
|
+ $resp = http_request(config::QUERY_URL, $params);
|
|
|
+
|
|
|
+ if (empty($resp)) {
|
|
|
+ return [false, '网络错误'];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Log::record($resp, Log::DEBUG);
|
|
|
+ $resp = $this->xmlToArray($resp);
|
|
|
+ if (empty($resp)) {
|
|
|
+ return [false, '网络错误'];
|
|
|
+ }
|
|
|
+ $resultno = intval($resp['resultno']);
|
|
|
+ if ($resultno === 1) {
|
|
|
+ $order_state = ORDER_STATE_SUCCESS;
|
|
|
+ $updata['official_sn'] = $resp['supnumber'];
|
|
|
+ Model('refill_order')->edit($refill_info['order_id'], $updata);
|
|
|
+ } elseif ($resultno === 9) {
|
|
|
+ $order_state = ORDER_STATE_CANCEL;
|
|
|
+ } elseif (in_array($resultno, [0,2])) {
|
|
|
+ $order_state = ORDER_STATE_SEND;
|
|
|
+ } else {
|
|
|
+ return [false, $resp['message']];
|
|
|
+ }
|
|
|
+ return [true, $order_state];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function sign($params)
|
|
|
+ {
|
|
|
+ $userid = config::USER_ID;
|
|
|
+ $key = config::KEY;
|
|
|
+ $content = "userid={$userid}&productid={$params['productid']}&price={$params['price']}&num={$params['num']}&mobile={$params['mobile']}&spordertime={$params['spordertime']}";
|
|
|
+ $content .= "&sporderid={$params['sporderid']}&key={$key}";
|
|
|
+ return md5($content);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function xmlToArray($xml)
|
|
|
+ {
|
|
|
+ //禁止引用外部xml实体
|
|
|
+ libxml_disable_entity_loader(true);
|
|
|
+ $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
|
|
|
+ $val = json_decode(json_encode($xmlstring), true);
|
|
|
+
|
|
|
+ return $val;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getPhoneInfo($phone)
|
|
|
+ {
|
|
|
+ $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$phone}";
|
|
|
+ $resp = http_request($url); //获取API返回 的数据
|
|
|
+ $resp = mb_convert_encoding($resp, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
|
|
|
+
|
|
|
+ $datas = explode('=', $resp);
|
|
|
+ if(count($datas) == 2)
|
|
|
+ {
|
|
|
+ $body = trim($datas[1]);
|
|
|
+ if(preg_match_all("/(\w+):'([^']+)/", $body, $m)) {
|
|
|
+ $res = array_combine($m[1], $m[2]);
|
|
|
+ return $this->formatProvince($res['province']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function formatProvince(string $province) : string
|
|
|
+ {
|
|
|
+ if(empty($province)) {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+
|
|
|
+ $checkArr = ["省","市","自治区","特别行政区"];
|
|
|
+
|
|
|
+ for($i = 0; $i < count($checkArr); $i++) {
|
|
|
+ if(strpos($province, $checkArr[$i]) === false) {
|
|
|
+ continue;
|
|
|
+ } else {
|
|
|
+ $province = mb_strcut($province, 0, strrpos($province, $checkArr[$i]));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $province;
|
|
|
+ }
|
|
|
+}
|