123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace refill\guochuang;
- require_once(BASE_HELPER_RAPI_PATH . '/guochuang/config.php');
- use refill;
- use Log;
- class RefillPhone extends refill\IRefillPhone
- {
- public function __construct($cfgs)
- {
- parent::__construct($cfgs);
- }
- public function goods($quality,int $amount,int $card_type,$regin_no,$other)
- {
- [$goods_id, $price] = parent::goods($quality,$amount,$card_type,$regin_no,$other);
- if($goods_id <= 0) return [0,0];
- $key = "{$card_type}-{$amount}-{$regin_no}";
- $price = config::Price[$key];
- if(empty($price)) {
- Log::record("channel cannot find price where name={$this->mName}, goods_id = {$goods_id} card_type={$card_type} amount={$amount} regin_no={$regin_no}",Log::ERR);
- return [0,0];
- } else {
- return [$goods_id,ncPriceFormat($price)];
- }
- }
- private function req_params(int $phone, int $amount, int $card_type, string $order_sn)
- {
- $params['phone'] = $phone;
- $params['phoneType'] = config::operator[$card_type];
- $params['money'] = $amount;
- $params['outerId'] = $order_sn;
- $params['callBackUrl'] = config::NOTIFY_URL;
- $params['speed'] = 0;
- return $params;
- }
- //统一请求
- private function url_request($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;
- $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($get_params) ? http_build_query($get_params) : $get_params);
- return http_request($url, $params, 'POST', false, [], $net_errno);
- }
- public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
- {
- refill\util::send_quick($params['order_sn']);
- return [true , '',false];
- }
- public function query($refill_info)
- {
- //上游单号,可以为空,但需参与签名
- $params['id'] = '';
- $params['outerId'] = $refill_info['order_sn'];
- $resp = $this->url_request($params,'order.status.query');
- 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')
- {
- $status = $resp['status'];
- if ($status === 'SUCCESS') {
- $order_state = ORDER_STATE_SUCCESS;
- $save['official_sn'] = strtolower($resp['evidence']) == 'null' ? '' : $resp['evidence'];
- Model('refill_order')->edit($refill_info['order_id'], $save);
- } 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];
- }
- return [true, $order_state];
- }
- else
- {
- return [false, "code={$code}"];
- }
- }
- }
- public function balance()
- {
- $resp = $this->url_request([],'user.balance.query');
- 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;
- }
- }
|