RefillPhone.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace refill\mifeng_fast;
  3. require_once(BASE_HELPER_RAPI_PATH . '/mifeng_fast/config.php');
  4. use mtopcard;
  5. use refill;
  6. use Log;
  7. class RefillPhone extends refill\IRefillPhone
  8. {
  9. public function __construct($cfgs)
  10. {
  11. parent::__construct($cfgs);
  12. }
  13. private function add_params(int $phone, int $amount, string $order_sn, int $card_type,int $regin_no): array
  14. {
  15. $operator_getter = function ($card_type)
  16. {
  17. if ($card_type == mtopcard\ChinaMobileCard) {
  18. return "移动";
  19. } elseif ($card_type == mtopcard\ChinaUnicomCard) {
  20. return "联通";
  21. } elseif ($card_type == mtopcard\ChinaTelecomCard) {
  22. return "电信";
  23. } else {
  24. return false;
  25. }
  26. };
  27. $operator_id = $operator_getter($card_type);
  28. $region = mtopcard\scard_region($regin_no);
  29. if ($operator_id === false) {
  30. return [];
  31. }
  32. $params = [
  33. 'app_key' => config::AppKey,
  34. 'timestamp' => time(),
  35. 'product_id' => config::PRODUCT_ID,
  36. 'third_id' => $order_sn,
  37. 'call_back_url' => config::NOTIFY_URL,
  38. 'datas' => [
  39. 'target' => "$phone",
  40. 'amount' => $amount,
  41. 'operator_id' => $operator_id
  42. ]
  43. ];
  44. if (!empty($region)) {
  45. $params['datas']['prov_code'] = $region;
  46. }
  47. $params['sign'] = config::sign($params);
  48. return $params;
  49. }
  50. //[$state, $errmsg, $neterr]
  51. public function add($card_no, $card_type, $amount, $params, &$net_errno = 0): array
  52. {
  53. $params = $this->add_params($card_no, $amount, $params['order_sn'], $card_type, $params['regin_no'] ?? -1);
  54. if (empty($params)) {
  55. return [false, '提单参数不符合', false];
  56. }
  57. $resp = http_post_data(config::ORDER_URL, json_encode($params), config::ExtHeaders);
  58. if (empty($resp)) {
  59. return [false, '网络错误', true];
  60. }
  61. else
  62. {
  63. Log::record($resp, Log::DEBUG);
  64. $resp = json_decode($resp, true);
  65. if (empty($resp)) {
  66. return [false, '网络错误', true];
  67. }
  68. $code = $resp['code'];
  69. if ($code === 0) {
  70. return [true, $resp['data']['order_id'], false];
  71. } elseif (in_array($code, [10010, 10015])) {
  72. $net_errno = "HTTP-$code";
  73. $errmsg = config::ERRMSG[$code] ?? '';
  74. return [false, $errmsg, true];
  75. } else {
  76. $errmsg = config::ERRMSG[$code] ?? '';
  77. return [false, $errmsg, false];
  78. }
  79. }
  80. }
  81. private function query_params($refill_info)
  82. {
  83. $params = [
  84. 'app_key' => config::AppKey,
  85. 'timestamp' => time(),
  86. 'third_id' => $refill_info['order_sn'],
  87. 'time' => $refill_info['commit_time']
  88. ];
  89. $params['sign'] = config::sign($params);
  90. return $params;
  91. }
  92. public function query($refill_info): array
  93. {
  94. $params = $this->query_params($refill_info);
  95. $resp = http_post_data(config::QUERY_URL, json_encode($params), config::ExtHeaders);
  96. if (empty($resp)) {
  97. return [false, '系统错误', ''];
  98. }
  99. else
  100. {
  101. Log::record($resp, Log::DEBUG);
  102. $resp = json_decode($resp, true);
  103. if (empty($resp)) {
  104. return [false, '系统错误', ''];
  105. }
  106. $official_sn = '';
  107. $code = intval($resp['code']);
  108. if ($code === 0)
  109. {
  110. $order_id = $refill_info['order_id'];
  111. $data = $resp['data'];
  112. $state = $data['state'];
  113. if ($state === 3) {
  114. $official_sn = $data['voucher'] ?? '';
  115. $ch_trade_no = $data['order_id'] ?? '';
  116. Model('refill_order')->edit($order_id, ['ch_trade_no' => $ch_trade_no,'official_sn' => $official_sn]);
  117. $order_state = ORDER_STATE_SUCCESS;
  118. }
  119. elseif(in_array($state, [4,6])) {
  120. $ch_trade_no = $data['order_id'] ?? '';
  121. Model('refill_order')->edit($order_id, ['ch_trade_no' => $ch_trade_no]);
  122. $order_state = ORDER_STATE_CANCEL;
  123. }
  124. else {
  125. $order_state = ORDER_STATE_SEND;
  126. }
  127. }
  128. else {
  129. return [false, '其他,或网络错误', ''];
  130. }
  131. return [true, $order_state, $official_sn];
  132. }
  133. }
  134. private function balance_params()
  135. {
  136. $params = [
  137. 'app_key' => config::AppKey,
  138. 'timestamp' => time(),
  139. ];
  140. $params['sign'] = config::sign($params);
  141. return $params;
  142. }
  143. public function balance(): array
  144. {
  145. $params = $this->balance_params();
  146. $resp = http_post_data(config::BALANCE_URL, json_encode($params), config::ExtHeaders);
  147. if (empty($resp)) {
  148. return [false, '系统错误'];
  149. }
  150. else
  151. {
  152. Log::record($resp, Log::DEBUG);
  153. $resp = json_decode($resp, true);
  154. if (empty($resp)) {
  155. return [false, '系统错误'];
  156. } elseif ($resp['code'] === 0) {
  157. return [true, ncPriceFormat($resp['data']['amount'])];
  158. } else {
  159. return [false, '其它,失败'];
  160. }
  161. }
  162. }
  163. }