123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace refill\yanhao;
- require_once(BASE_HELPER_RAPI_PATH . '/yanhao/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];
- $store_id = $this->mStoreID;
- $pcode = $other['product_code'];
- $thrid_refill = Model('thrid_refill');
- $product = $thrid_refill->getProviderProduct($store_id, $goods_id, $pcode);
- if (empty($product)) {
- Log::record("cannot find provider's produce where name={$this->mName}, goods_id = {$goods_id} pcode={$pcode}", Log::ERR);
- return [0, 0];
- } else {
- return [$goods_id, ncPriceFormat($product['channel_amount'])];
- }
- }
- private function getProductCode($goods_id, $sys_pcode)
- {
- $thrid_refill = Model('thrid_refill');
- $store_id = $this->mStoreID;
- $product = $thrid_refill->getProviderProduct($store_id, $goods_id, $sys_pcode);
- if (empty($product)) {
- return false;
- } else {
- return $product['channel_code'];
- }
- }
- private function req_params(int $phone, string $order_sn)
- {
- $params['UserId'] = config::USER_ID;
- $params['TimesTamp'] = $this->getMillisecond();
- $params['Count'] = 1;
- $params['UserOrderId'] = $order_sn;
- $params['Account'] = $phone;
- $params['ClientIP'] = config::API_IP;
- $params['NotifyUrl'] = config::NOTIFY_URL;
- return $params;
- }
- public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
- {
- $params = $this->req_params($card_no, $params['order_sn']);
- $goods_id = intval($params['goods_id']);
- $channel_code = $this->getProductCode($goods_id, $params['product_code']);
- if(empty($channel_code)) {
- return [false, '产品有误', false];
- }
- $params['ProductNumber'] = $channel_code;
- $sign = $this->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'] === 0) {
- return [true, $resp['OutOrderID'], false];
- } elseif ($resp['Code'] === -170 || $resp['Code'] === -180){
- $net_errno = "HTTP-{$resp['Code']}";
- return [false, $net_errno, true];
- } else {
- return [false, $resp['Msg'], false];
- }
- }
- }
- public function query($refill_info)
- {
- $params['UserId'] = config::USER_ID;
- $params['TimesTamp'] = $this->getMillisecond();
- $params['UserOrderId'] = $refill_info['order_sn'];
- $content = "{$params['UserId']}{$params['TimesTamp']}{$params['UserOrderId']}".config::Key;
- $params['Sign'] = strtoupper(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, '系统错误'];
- }
- elseif ($resp['Code'] === 0)
- {
- $status = $resp['Data']['OrderStatus'];
- if ($status === 2) {
- $order_state = ORDER_STATE_SUCCESS;
- } elseif ($status === 3) {
- $order_state = ORDER_STATE_CANCEL;
- } elseif ($status === 1) {
- $order_state = ORDER_STATE_SEND;
- } else {
- return [false, $status];
- }
- return [true, $order_state];
- }
- elseif ($resp['Code'] === -240 && (time() - $refill_info['commit_time'] >= 600))
- {
- return [true, ORDER_STATE_NOEXIST];
- }
- else {
- return [false, $resp['Msg']];
- }
- }
- }
- public function balance()
- {
- $params['UserId'] = config::USER_ID;
- $params['TimesTamp'] = $this->getMillisecond();
- $content = "{$params['UserId']}{$params['TimesTamp']}".config::Key;
- $params['Sign'] = strtoupper(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['Code'] === 0)
- {
- return [true, $resp['Data'][0]['Balance']];
- }
- else {
- return [false, $resp['Msg']];
- }
- }
- }
- private function sign($params)
- {
- $key = config::Key;
- $content = "{$params['UserId']}{$params['Count']}{$params['NotifyUrl']}{$params['Account']}{$params['ClientIP']}{$params['UserOrderId']}{$params['ProductNumber']}";
- $content .= "{$params['TimesTamp']}{$key}";
- return strtoupper(md5($content));
- }
- private function getMillisecond()
- {
- $cur = microtime (true);
- $cur = intval($cur * 1000);
- return $cur;
- }
- }
|