123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace refill\dixin_normal;
- require_once(BASE_HELPER_RAPI_PATH . '/dixin_normal/config.php');
- use mtopcard;
- use refill;
- use Log;
- class RefillPhone extends refill\IRefillPhone
- {
- public function __construct($cfgs)
- {
- parent::__construct($cfgs);
- }
- private function add_params(int $phone, int $amount, string $order_sn, int $card_type): array
- {
- $oper_getter = function ($card_type)
- {
- if ($card_type == mtopcard\ChinaMobileCard) {
- return 'YD';
- } elseif ($card_type == mtopcard\ChinaUnicomCard) {
- return 'LT';
- } elseif ($card_type == mtopcard\ChinaTelecomCard) {
- return 'DX';
- } else {
- return false;
- }
- };
- $operator = $oper_getter($card_type);
- $sku_code = config::sku_code($card_type,$amount);
- if($operator === false || empty($sku_code)) {
- return [];
- }
- $params = [
- 'merchant_no' => $order_sn,
- 'sku_code' => $sku_code,
- 'format' => ['account' => $phone],
- "format_type" => "common",
- 'notice_url' => config::NOTIFY_URL,
- "uid" => "123456"
- ];
- $result = $this->method('create.order',$params);
- return $result;
- }
- private function method($method,$data = [],$attach = '')
- {
- $param = [
- 'access_token' => config::ACCESS_TOKEN,
- 'once' => uniqid(),
- 'timestamp' => $this->getUnixTimestamp(),
- 'attach' => $attach,
- 'format' => 'JSON',
- 'sign_type' => 'MD5',
- 'version' => '1.0.0',
- 'method' => $method,
- 'data' => json_encode($data, 256),
- ];
- $param = array_merge($param, ['sign' => config::sign($param)]);
- return $param;
- }
- private function getUnixTimestamp ()
- {
- list($s1, $s2) = explode(' ', microtime());
- return (float)sprintf('%.0f',(floatval($s1) + floatval($s2)) * 1000);
- }
- //[$state, $errmsg, $neterr]
- public function add($card_no, $card_type, $amount, $params, &$net_errno = 0): array
- {
- $params = $this->add_params($card_no, $amount, $params['order_sn'],$card_type);
- if(empty($params)) {
- return [false, '提单参数不符合', false];
- }
- $resp = http_request(config::ORDER_URL, $params , 'POST' , false , [] , $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'] === 200) {
- return [true, '', false];
- } else {
- return [false, $resp['message'], false];
- }
- }
- }
- public function query($refill_info): array
- {
- $params['merchant_no'] = $refill_info['order_sn']; //sn码
- $params = $this->method('query.order',$params);//查数据库情况,查询全部数据结构包括orderid
- $resp = http_request(config::ORDER_URL, $params , 'POST');
- if (empty($resp)) {
- return [false, '系统错误', ''];
- }
- else
- {
- Log::record($resp, Log::DEBUG);
- $resp = json_decode($resp, true);
- if (empty($resp)) {
- return [false, '系统错误', ''];
- }
- elseif ($resp['code'] === 200)
- {
- $val = $resp['data'][0];
- $status = $val['recharge_status'];
- $official_sn = '';//strtolower($val['official_sn']) == 'null' ? '' : $val['official_sn'];
- //充值状态:1=待充值,2=充值中,3=充值完成,4=充值失败,5=运营商维护,8=部分到账
- if ($status == '3') {
- $save['ch_trade_no'] = $val['order_no'];//平台的订单号
- $save['official_sn'] = $official_sn;
- Model('refill_order')->edit($refill_info['order_id'], $save);
- $order_state = ORDER_STATE_SUCCESS;
- }
- elseif (in_array($status,[4,5])) {
- Model('refill_order')->edit($refill_info['order_id'], ['ch_trade_no' => $val['order_no']]);
- $order_state = ORDER_STATE_CANCEL;
- }
- else {
- $order_state = ORDER_STATE_SEND;
- }
- return [true, $order_state, $official_sn];
- }
- else
- {
- return [false, $resp['message'], ''];
- }
- }
- }
- public function balance(): array
- {
- $params = $this->method('query.balance',[]);
- $resp = http_request(config::ORDER_URL, $params , 'POST');
- if (empty($resp)) {
- return [false, '系统错误'];
- }
- else
- {
- Log::record($resp, Log::DEBUG);
- $resp = json_decode($resp, true);
- if (empty($resp)) {
- return [false, '系统错误'];
- } elseif ($resp['code'] === 200) {
- return [true, ncPriceFormat($resp['data']['predict_money'])];
- } else {
- return [false, $resp['message']];
- }
- }
- }
- }
|