123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace refill\feimingyu_fs;
- require_once(BASE_HELPER_RAPI_PATH . '/feimingyu_fs/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, $regin_no)
- {
- $params['appId'] = config::APP_ID;
- $params['timestamp'] = $this->get_millisecond();
- $params['applyNo'] = $order_sn;
- $params['rechargeNo'] = $phone;
- $params['agentProductId'] = config::PRODUCT[$card_type][$regin_no][$amount];
- $params['denomination'] = $amount;
- $params['callbackUrl'] = config::NOTIFY_URL;
- return $params;
- }
- public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
- {
- $regin_no = $params['regin_no'] ?? -1;
- if($regin_no <= 0) {
- return [false, '省份获取错误', false];
- }
- $order_sn = $params['order_sn'];
- $params = $this->req_params($card_no, $amount, $card_type, $order_sn, $regin_no);
- if(empty($params['agentProductId'])) {
- return [false, '商品编号错误', false];
- }
- $sign = config::sign($params);
- $params['sign'] = $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);
- if (empty($resp)) {
- return [false, '网络错误', true];
- } elseif ($resp['code'] === '00000') {
- return [true, $resp['data']['orderNo'], false];
- } elseif ($resp['code'] === 'A0503') {
- $net_errno = "HTTP-{$resp['code']}";
- return [false, $resp['msg'], true];
- } else {
- return [false, $resp['msg'], false];
- }
- }
- }
- public function query($refill_info)
- {
- $params['appId'] = config::APP_ID;
- $params['timestamp'] = $this->get_millisecond();
- $params['applyNo'] = $refill_info['order_sn'];
- $params['sign'] = config::sign($params);
- $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, '网络错误', ''];
- }
- elseif ($resp['code'] === '00000')
- {
- $offical_sn = '';
- $status = $resp['data']['orderStatus'];
- if ($status === 'SUCCESS') {
- $offical_sn = $resp['data']['ext1'];
- $updata['official_sn'] = $offical_sn;
- Model('refill_order')->edit($refill_info['order_id'], $updata);
- $order_state = ORDER_STATE_SUCCESS;
- } elseif ($status === 'FAILED') {
- $order_state = ORDER_STATE_CANCEL;
- } elseif ($status === 'PROCESSING' || $status === 'INIT') {
- $order_state = ORDER_STATE_SEND;
- } else {
- return [false, $resp['msg'], $offical_sn];
- }
- return [true, $order_state, $offical_sn];
- }
- else
- {
- return [false, $resp['msg'], ''];
- }
- }
- }
- public function balance()
- {
- $params['appId'] = config::APP_ID;
- $params['timestamp'] = $this->get_millisecond();
- $params['sign'] = config::sign($params);
- $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['code'] === '00000') {
- return [true, $resp['data']['balance']];
- } else {
- return [false, $resp['msg']];
- }
- }
- }
- /**
- * 获取毫秒级别的时间戳
- */
- private function get_millisecond()
- {
- list($msec, $sec) = explode(' ', microtime());
- $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
- return date('YmdHis').substr($msectime, -3);
- }
- }
|