123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace refill\mifeng_fast;
- require_once(BASE_HELPER_RAPI_PATH . '/mifeng_fast/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,int $regin_no): array
- {
- $operator_getter = function ($card_type)
- {
- if ($card_type == mtopcard\ChinaMobileCard) {
- return "移动";
- } elseif ($card_type == mtopcard\ChinaUnicomCard) {
- return "联通";
- } elseif ($card_type == mtopcard\ChinaTelecomCard) {
- return "电信";
- } else {
- return false;
- }
- };
- $operator_id = $operator_getter($card_type);
- $region = mtopcard\scard_region($regin_no);
- if ($operator_id === false) {
- return [];
- }
- $params = [
- 'app_key' => config::AppKey,
- 'timestamp' => time(),
- 'product_id' => config::PRODUCT_ID,
- 'third_id' => $order_sn,
- 'call_back_url' => config::NOTIFY_URL,
- 'datas' => [
- 'target' => "$phone",
- 'amount' => $amount,
- 'operator_id' => $operator_id
- ]
- ];
- if (!empty($region)) {
- $params['datas']['prov_code'] = $region;
- }
- $params['sign'] = config::sign($params);
- return $params;
- }
- //[$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, $params['regin_no'] ?? -1);
- if (empty($params)) {
- return [false, '提单参数不符合', false];
- }
- $resp = http_post_data(config::ORDER_URL, json_encode($params), config::ExtHeaders);
- if (empty($resp)) {
- return [false, '网络错误', true];
- }
- else
- {
- Log::record($resp, Log::DEBUG);
- $resp = json_decode($resp, true);
- if (empty($resp)) {
- return [false, '网络错误', true];
- }
- $code = $resp['code'];
- if ($code === 0) {
- return [true, $resp['data']['order_id'], false];
- } elseif (in_array($code, [10010, 10015])) {
- $net_errno = "HTTP-$code";
- $errmsg = config::ERRMSG[$code] ?? '';
- return [false, $errmsg, true];
- } else {
- $errmsg = config::ERRMSG[$code] ?? '';
- return [false, $errmsg, false];
- }
- }
- }
- private function query_params($refill_info)
- {
- $params = [
- 'app_key' => config::AppKey,
- 'timestamp' => time(),
- 'third_id' => $refill_info['order_sn'],
- 'time' => $refill_info['commit_time']
- ];
- $params['sign'] = config::sign($params);
- return $params;
- }
- public function query($refill_info): array
- {
- $params = $this->query_params($refill_info);
- $resp = http_post_data(config::QUERY_URL, json_encode($params), config::ExtHeaders);
- if (empty($resp)) {
- return [false, '系统错误', ''];
- }
- else
- {
- Log::record($resp, Log::DEBUG);
- $resp = json_decode($resp, true);
- if (empty($resp)) {
- return [false, '系统错误', ''];
- }
- $official_sn = '';
- $code = intval($resp['code']);
- if ($code === 0)
- {
- $order_id = $refill_info['order_id'];
- $data = $resp['data'];
- $state = $data['state'];
- if ($state === 3) {
- $official_sn = $data['voucher'] ?? '';
- $ch_trade_no = $data['order_id'] ?? '';
- Model('refill_order')->edit($order_id, ['ch_trade_no' => $ch_trade_no,'official_sn' => $official_sn]);
- $order_state = ORDER_STATE_SUCCESS;
- }
- elseif(in_array($state, [4,6])) {
- $ch_trade_no = $data['order_id'] ?? '';
- Model('refill_order')->edit($order_id, ['ch_trade_no' => $ch_trade_no]);
- $order_state = ORDER_STATE_CANCEL;
- }
- else {
- $order_state = ORDER_STATE_SEND;
- }
- }
- else {
- return [false, '其他,或网络错误', ''];
- }
- return [true, $order_state, $official_sn];
- }
- }
- private function balance_params()
- {
- $params = [
- 'app_key' => config::AppKey,
- 'timestamp' => time(),
- ];
- $params['sign'] = config::sign($params);
- return $params;
- }
- public function balance(): array
- {
- $params = $this->balance_params();
- $resp = http_post_data(config::BALANCE_URL, json_encode($params), 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, ncPriceFormat($resp['data']['amount'])];
- } else {
- return [false, '其它,失败'];
- }
- }
- }
- }
|