RefillPhone.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace refill\seven_kami;
  3. require_once(BASE_HELPER_RAPI_PATH . '/seven_kami/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): array
  14. {
  15. $nSortType_getter = function ($card_type)
  16. {
  17. if ($card_type == mtopcard\ChinaMobileCard) {
  18. return 1;
  19. } elseif ($card_type == mtopcard\ChinaUnicomCard) {
  20. return 2;
  21. } elseif ($card_type == mtopcard\ChinaTelecomCard) {
  22. return 3;
  23. } else {
  24. return false;
  25. }
  26. };
  27. $nSortType = $nSortType_getter($card_type);
  28. if($nSortType === false) {
  29. return [];
  30. }
  31. $params = [
  32. 'szAgentId' => config::szAgentId,
  33. 'szOrderId' => $order_sn,
  34. 'szPhoneNum'=> "$phone",
  35. 'nMoney' => $amount,
  36. 'nSortType' => $nSortType,
  37. 'nProductClass' => 1,
  38. 'nProductType' =>"1",
  39. 'szTimeStamp'=> date("Y-m-d H:i:s"),
  40. 'szNotifyUrl' => config::NOTIFY_URL
  41. ];
  42. $params['szVerifyString'] = config::sign($params,config::add_keys);
  43. return $params;
  44. }
  45. //[$state, $errmsg, $neterr]
  46. public function add($card_no, $card_type, $amount, $params, &$net_errno = 0): array
  47. {
  48. $params = $this->add_params($card_no, $amount, $params['order_sn'], $card_type);
  49. if(empty($params)) {
  50. return [false, '提单参数不符合', false];
  51. }
  52. $resp = http_request(config::ORDER_URL, $params , 'POST' , false , [] , $net_errno);
  53. if (empty($resp)) {
  54. return [false, '网络错误', true];
  55. }
  56. else
  57. {
  58. Log::record($resp, Log::DEBUG);
  59. $resp = json_decode($resp, true);
  60. if (empty($resp)) {
  61. return [false, '网络错误', true];
  62. }
  63. $nRtn = $resp['nRtn'];
  64. if ($nRtn === 0) { //下单成功
  65. return [true, $resp['szOrderId'], false];
  66. } elseif (in_array($nRtn, config::ERRCODES, true)) {
  67. return [false, config::ERRMSG[$nRtn], false];
  68. } elseif (in_array($nRtn, [2050, 999])) { //下单异常
  69. $net_errno = "HTTP-$nRtn";
  70. return [false, config::ERRMSG[$nRtn], true];
  71. } else {
  72. $net_errno = "HTTP-998";
  73. return [false, "其他异常", true];
  74. }
  75. }
  76. }
  77. private function query_params($refill_info)
  78. {
  79. $params = [
  80. 'szAgentId' => config::szAgentId,
  81. 'szOrderId' => $refill_info['order_sn'],
  82. 'szFormat' => 'JSON',
  83. ];
  84. $params['szVerifyString'] = config::sign($params, config::query_keys);
  85. return $params;
  86. }
  87. public function query($refill_info): array
  88. {
  89. $params = $this->query_params($refill_info);
  90. $resp = http_request(config::QUERY_URL, $params , 'POST');
  91. if (empty($resp)) {
  92. return [false, '系统错误', ''];
  93. }
  94. else
  95. {
  96. Log::record($resp, Log::DEBUG);
  97. $resp = json_decode($resp, true);
  98. if (empty($resp)) {
  99. return [false, '系统错误', ''];
  100. }
  101. $official_sn = '';
  102. $nRtn = intval($resp['nRtn']);
  103. if ($nRtn === 5012) {
  104. $official_sn = config::get_osn($resp['szRtnMsg'] ?? '');
  105. Model('refill_order')->edit($refill_info['order_id'], ['official_sn' => $official_sn]);
  106. $order_state = ORDER_STATE_SUCCESS;
  107. }
  108. elseif($nRtn === 5013) {
  109. $order_state = ORDER_STATE_CANCEL;
  110. }
  111. elseif (in_array($nRtn, [999, 5001, 5002, 5003, 5004, 5011, 5019])) {
  112. $order_state = ORDER_STATE_SEND;
  113. }
  114. elseif($nRtn === 5005 and (time() - $refill_info['commit_time'] >= 300)) {
  115. $order_state = ORDER_STATE_NOEXIST;
  116. }
  117. else {
  118. return [false, '其他,或网络错误', ''];
  119. }
  120. return [true, $order_state, $official_sn];
  121. }
  122. }
  123. private function balance_params()
  124. {
  125. $params = [
  126. 'szAgentId' => config::szAgentId
  127. ];
  128. $params['szVerifyString'] = config::sign($params, config::balance_keys);
  129. return $params;
  130. }
  131. public function balance(): array
  132. {
  133. $params = $this->balance_params();
  134. $resp = http_request(config::BALANCE_URL, $params , 'POST');
  135. if (empty($resp)) {
  136. return [false, '系统错误'];
  137. }
  138. else
  139. {
  140. Log::record($resp, Log::DEBUG);
  141. $resp = json_decode($resp, true);
  142. if (empty($resp)) {
  143. return [false, '系统错误'];
  144. } elseif ($resp['nRtn'] === 0) {
  145. return [true, ncPriceFormat($resp['fBalance'] + $resp['fCredit'])];
  146. } else {
  147. return [false, '其它,失败'];
  148. }
  149. }
  150. }
  151. }