123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace refill\seven_kami;
- require_once(BASE_HELPER_RAPI_PATH . '/seven_kami/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
- {
- $nSortType_getter = function ($card_type)
- {
- if ($card_type == mtopcard\ChinaMobileCard) {
- return 1;
- } elseif ($card_type == mtopcard\ChinaUnicomCard) {
- return 2;
- } elseif ($card_type == mtopcard\ChinaTelecomCard) {
- return 3;
- } else {
- return false;
- }
- };
- $nSortType = $nSortType_getter($card_type);
- if($nSortType === false) {
- return [];
- }
- $params = [
- 'szAgentId' => config::szAgentId,
- 'szOrderId' => $order_sn,
- 'szPhoneNum'=> "$phone",
- 'nMoney' => $amount,
- 'nSortType' => $nSortType,
- 'nProductClass' => 1,
- 'nProductType' =>"1",
- 'szTimeStamp'=> date("Y-m-d H:i:s"),
- 'szNotifyUrl' => config::NOTIFY_URL
- ];
- $params['szVerifyString'] = config::sign($params,config::add_keys);
- 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);
- 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];
- }
- $nRtn = $resp['nRtn'];
- if ($nRtn === 0) { //下单成功
- return [true, $resp['szOrderId'], false];
- } elseif (in_array($nRtn, config::ERRCODES, true)) {
- return [false, config::ERRMSG[$nRtn], false];
- } elseif (in_array($nRtn, [2050, 999])) { //下单异常
- $net_errno = "HTTP-$nRtn";
- return [false, config::ERRMSG[$nRtn], true];
- } else {
- $net_errno = "HTTP-998";
- return [false, "其他异常", true];
- }
- }
- }
- private function query_params($refill_info)
- {
- $params = [
- 'szAgentId' => config::szAgentId,
- 'szOrderId' => $refill_info['order_sn'],
- 'szFormat' => 'JSON',
- ];
- $params['szVerifyString'] = config::sign($params, config::query_keys);
- return $params;
- }
- public function query($refill_info): array
- {
- $params = $this->query_params($refill_info);
- $resp = http_request(config::QUERY_URL, $params , 'POST');
- if (empty($resp)) {
- return [false, '系统错误', ''];
- }
- else
- {
- Log::record($resp, Log::DEBUG);
- $resp = json_decode($resp, true);
- if (empty($resp)) {
- return [false, '系统错误', ''];
- }
- $official_sn = '';
- $nRtn = intval($resp['nRtn']);
- if ($nRtn === 5012) {
- $official_sn = config::get_osn($resp['szRtnMsg'] ?? '');
- Model('refill_order')->edit($refill_info['order_id'], ['official_sn' => $official_sn]);
- $order_state = ORDER_STATE_SUCCESS;
- }
- elseif($nRtn === 5013) {
- $order_state = ORDER_STATE_CANCEL;
- }
- elseif (in_array($nRtn, [999, 5001, 5002, 5003, 5004, 5011, 5019])) {
- $order_state = ORDER_STATE_SEND;
- }
- elseif($nRtn === 5005 and (time() - $refill_info['commit_time'] >= 300)) {
- $order_state = ORDER_STATE_NOEXIST;
- }
- else {
- return [false, '其他,或网络错误', ''];
- }
- return [true, $order_state, $official_sn];
- }
- }
- private function balance_params()
- {
- $params = [
- 'szAgentId' => config::szAgentId
- ];
- $params['szVerifyString'] = config::sign($params, config::balance_keys);
- return $params;
- }
- public function balance(): array
- {
- $params = $this->balance_params();
- $resp = http_request(config::BALANCE_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['nRtn'] === 0) {
- return [true, ncPriceFormat($resp['fBalance'] + $resp['fCredit'])];
- } else {
- return [false, '其它,失败'];
- }
- }
- }
- }
|