123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace refill\huafutong_doubi;
- require_once(BASE_HELPER_RAPI_PATH . '/huafutong_doubi/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 req_params(int $phone, int $amount, string $order_sn)
- {
- $data['ChargeAccount'] = $phone;
- $data['SupBuyNum'] = 1;
- $data['ChargeValue'] = $amount;
- $data['OrderType'] = 1;
- $data['ChargeType'] = 10;
- $data['Id'] = $order_sn;
- $params['mchid'] = config::MCH_ID;
- $params['notifyurl'] = config::NOTIFY_URL;
- $params['data'] = $this->aes_encrypt($data);
- return $params;
- }
- public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
- {
- $params = $this->req_params($card_no, $amount, $params['order_sn']);
- $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'] === 'success') {
- return [true, $resp['data']['trans_sn'], false];
- } else {
- return [false, $resp['msg'], false];
- }
- }
- }
- public function query($refill_info)
- {
- $params['mchid'] = config::MCH_ID;
- $params['order_id'] = $refill_info['order_sn'];
- $params['time'] = time();
- $content = "{$params['mchid']}{$params['order_id']}{$params['time']}".config::UserKey;
- $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, '系统错误'];
- }
- elseif ($resp['code'] === '3')
- {
- $status = $resp['data']['status'];
- if ($status === 3) {
- Model('refill_order')->edit($refill_info['order_id'], ['official_sn' => $resp['data']['certificate']]);
- $order_state = ORDER_STATE_SUCCESS;
- } elseif (in_array($status, [0, 2], true)) {
- $order_state = ORDER_STATE_CANCEL;
- } elseif ($status === 1) {
- $order_state = ORDER_STATE_SEND;
- } else {
- return [false, $status];
- }
- return [true, $order_state];
- }
- elseif ($resp['code'] === '2' && time() - $refill_info['commit_time'] > 600) {
- return [true, ORDER_STATE_NOEXIST];
- }
- else {
- return [false, $resp['msg']];
- }
- }
- }
- public function balance()
- {
- $params['mchid'] = config::MCH_ID;
- $params['time'] = time();
- $content = "{$params['mchid']}{$params['time']}".config::UserKey;
- $params['sign'] =md5($content) ;
- $resp = http_request(config::BALANCE_URL, $params, 'POST', false, config::ExtHeaders);
- Log::record($resp, Log::DEBUG);
- $resp = json_decode($resp, true);
- if (empty($resp)) {
- return [false, '系统错误'];
- } elseif ($resp['code'] === '3') {
- return [true, $resp['data']['money']];
- } else {
- return [false, $resp['msg']];
- }
- }
- private function aes_encrypt($params): string
- {
- $encrypt = json_encode($params);
- return base64_encode(openssl_encrypt($encrypt, 'AES-128-CBC', config::UserKey, OPENSSL_RAW_DATA, config::aesIV));
- }
- }
|