RefillPhone.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace refill\zhixin_fs;
  3. require_once(BASE_HELPER_RAPI_PATH . '/zhixin_fs/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. public function goods($quality,int $amount,int $card_type,$regin_no,$other)
  14. {
  15. [$goods_id, $price] = parent::goods($quality,$amount,$card_type,$regin_no,$other);
  16. if($goods_id <= 0) return [0,0];
  17. $key = "$card_type-$amount-$regin_no";
  18. $price = config::Price[$key] ?? false;
  19. if($price === false) {
  20. Log::record("channel cannot find price where name=$this->mName, goods_id = $goods_id card_type=$card_type amount=$amount regin_no=$regin_no",Log::ERR);
  21. return [0,0];
  22. } else {
  23. return [$goods_id, ncPriceFormat($price)];
  24. }
  25. }
  26. private function add_params(int $phone, int $amount, string $order_sn, int $card_type): array
  27. {
  28. $cp_getter = function ($card_type)
  29. {
  30. if ($card_type == mtopcard\ChinaMobileCard) {
  31. return "01";
  32. } elseif ($card_type == mtopcard\ChinaUnicomCard) {
  33. return "03";
  34. } elseif ($card_type == mtopcard\ChinaTelecomCard) {
  35. return "02";
  36. } else {
  37. return false;
  38. }
  39. };
  40. $cp = $cp_getter($card_type);
  41. if ($cp === false) {
  42. return [];
  43. }
  44. $params = [
  45. 'mrch_no' => config::MRCH_NO,
  46. 'request_time' => date('YmdHis'),
  47. 'client_order_no' => $order_sn,
  48. 'product_type' => 1,
  49. 'phone_no' => "$phone",
  50. 'cp' => $cp,
  51. 'city_code' => "",
  52. 'recharge_amount' => $amount * 100,
  53. 'recharge_type' => 0,
  54. 'recharge_desc' => "",
  55. 'notify_url' => config::NOTIFY_URL,
  56. ];
  57. $params['sign'] = config::sign($params);
  58. return $params;
  59. }
  60. //[$state, $errmsg, $neterr]
  61. public function add($card_no, $card_type, $amount, $params, &$net_errno = 0): array
  62. {
  63. $params = $this->add_params($card_no, $amount, $params['order_sn'], $card_type);
  64. if (empty($params)) {
  65. return [false, '提单参数不符合', false];
  66. }
  67. $resp = http_post_data(config::ORDER_URL, json_encode($params), config::ExtHeaders);
  68. if (empty($resp)) {
  69. return [false, '网络错误', true];
  70. }
  71. else
  72. {
  73. Log::record($resp, Log::DEBUG);
  74. $resp = json_decode($resp, true);
  75. if (empty($resp)) {
  76. return [false, '网络错误', true];
  77. }
  78. $nRtn = intval($resp['code']);
  79. if ($nRtn === 2) {
  80. return [true, $resp['data']['up_order_no'], false];
  81. } elseif (in_array($nRtn, config::ERRCODES, true)) {
  82. return [false, config::ERRMSG[$nRtn], false];
  83. } elseif (in_array($nRtn, [625])) {
  84. $net_errno = "HTTP-$nRtn";
  85. return [false, "重复订单号", true];
  86. } else {
  87. $net_errno = "HTTP-998";
  88. return [false, "其他异常", true];
  89. }
  90. }
  91. }
  92. private function query_params($refill_info)
  93. {
  94. $params = [
  95. 'mrch_no' => config::MRCH_NO,
  96. 'request_time' => date('YmdHis'),
  97. 'client_order_no' => $refill_info['order_sn'],
  98. 'order_time' => date('YmdHis')
  99. ];
  100. $params['sign'] = config::sign($params);
  101. return $params;
  102. }
  103. public function query($refill_info): array
  104. {
  105. $params = $this->query_params($refill_info);
  106. $resp = http_post_data(config::QUERY_URL, json_encode($params), config::ExtHeaders);
  107. if (empty($resp)) {
  108. return [false, '系统错误', ''];
  109. }
  110. else
  111. {
  112. Log::record($resp, Log::DEBUG);
  113. $resp = json_decode($resp, true);
  114. if (empty($resp)) {
  115. return [false, '系统错误', ''];
  116. }
  117. $official_sn = '';
  118. $nRtn = intval($resp['code']);
  119. if ($nRtn === 2)
  120. {
  121. $order_id = $refill_info['order_id'];
  122. $recharge_status = $resp['data']['recharge_status'];
  123. if ($recharge_status == "2") {
  124. $official_sn = $resp['data']['elecardID'] ?? '';
  125. $ch_trade_no = $resp['data']['up_order_no'] ?? '';
  126. Model('refill_order')->edit($order_id, ['ch_trade_no' => $ch_trade_no,'official_sn' => $official_sn]);
  127. $order_state = ORDER_STATE_SUCCESS;
  128. }
  129. elseif($recharge_status == "6") {
  130. $ch_trade_no = $resp['data']['up_order_no'] ?? '';
  131. Model('refill_order')->edit($order_id, ['ch_trade_no' => $ch_trade_no]);
  132. $order_state = ORDER_STATE_CANCEL;
  133. }
  134. else {
  135. $order_state = ORDER_STATE_SEND;
  136. }
  137. }
  138. elseif ($nRtn == 626 && (time() - $refill_info['commit_time'] >= 600)) {
  139. $order_state = ORDER_STATE_NOEXIST;
  140. }
  141. else {
  142. return [false, '其他,或网络错误', ''];
  143. }
  144. return [true, $order_state, $official_sn];
  145. }
  146. }
  147. private function balance_params()
  148. {
  149. $params = [
  150. 'mrch_no' => config::MRCH_NO,
  151. 'request_time' => date('YmdHis')
  152. ];
  153. $params['sign'] = config::sign($params);
  154. return $params;
  155. }
  156. public function balance(): array
  157. {
  158. $params = $this->balance_params();
  159. $resp = http_post_data(config::BALANCE_URL, json_encode($params), config::ExtHeaders);
  160. if (empty($resp)) {
  161. return [false, '系统错误'];
  162. }
  163. else
  164. {
  165. Log::record($resp, Log::DEBUG);
  166. $resp = json_decode($resp, true);
  167. if (empty($resp)) {
  168. return [false, '系统错误'];
  169. } elseif (intval($resp['code']) === 2) {
  170. return [true, ncPriceFormat($resp['data']['balance'])];
  171. } else {
  172. return [false, '其它,失败'];
  173. }
  174. }
  175. }
  176. }