RefillPhone.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace refill\guochuang_nation;
  3. require_once(BASE_HELPER_RAPI_PATH . '/guochuang_nation/config.php');
  4. use refill;
  5. use Log;
  6. class RefillPhone extends refill\IRefillPhone
  7. {
  8. public function __construct($cfgs)
  9. {
  10. parent::__construct($cfgs);
  11. }
  12. private function none_support($card_no)
  13. {
  14. return strpos($card_no, '148') === 0;
  15. }
  16. private function add_params(int $phone, int $amount, int $card_type, string $order_sn,$regin_no)
  17. {
  18. $params['phone'] = $phone;
  19. $params['phoneType'] = config::operator[$card_type];
  20. $params['money'] = $amount;
  21. $params['outerId'] = $order_sn;
  22. $params['callBackUrl'] = config::NOTIFY_URL;
  23. $params['speed'] = 0;
  24. $params['provId'] = config::ProvinceMap[$regin_no];
  25. if(empty($params['provId'])) {
  26. return false;
  27. }
  28. else {
  29. return $params;
  30. }
  31. }
  32. private function form_uri($params, $service)
  33. {
  34. $get_params['service'] = $service;
  35. $get_params['userId'] = config::UserId;
  36. $get_params['ts'] = $this->getMillisecond();
  37. $sign = $this->sign($params,$get_params);
  38. $get_params['sign'] = $sign;
  39. $url = config::API_URL;
  40. $uri = $url . (strpos($url, '?') ? '&' : '?') . (is_array($get_params) ? http_build_query($get_params) : $get_params);
  41. return $uri;
  42. }
  43. public function add($card_no, $card_type, $amount, $input, &$net_errno = 0)
  44. {
  45. $order_sn = $input['order_sn'];
  46. $regin_no = $input['regin_no'] ?? -1;
  47. if($regin_no <= 0) {
  48. return [false, '省份获取错误', false];
  49. }
  50. if($this->none_support($card_no)) {
  51. return [false, '不支持物联网卡号', false];
  52. }
  53. $params = $this->add_params($card_no, $amount, $card_type, $order_sn,$regin_no);
  54. if($params === false) {
  55. return [false, '省份信息错误', false];
  56. }
  57. $uri = $this->form_uri($params,'order.phone.charge');
  58. $resp = http_request($uri, $params, 'POST', false, [], $net_errno);
  59. if (empty($resp)) {
  60. return [false, '系统错误', true];
  61. }
  62. else
  63. {
  64. Log::record($resp, Log::DEBUG);
  65. $resp = json_decode($resp ,true);
  66. $code = $resp['code'];
  67. if (empty($resp)) {
  68. return [false, '系统错误', true];
  69. } elseif ($code === 'SUCCESS') {
  70. return [true, $resp['id'], false];
  71. } elseif (in_array($code, config::ORDER_ERR_CODE)) {
  72. return [false, $code, false];
  73. } elseif (in_array($code, ['SYSTEM_ERROR', 'ORDER_ID_EXIST'])) {
  74. $net_errno = "HTTP-{$code}";
  75. return [false, $code, true];
  76. } else {
  77. $err = 998;
  78. $net_errno = "HTTP-{$err}";
  79. return [false, $code, true];
  80. }
  81. }
  82. }
  83. public function query($refill_info)
  84. {
  85. //上游单号,可以为空,但需参与签名
  86. $params['id'] = '';
  87. $params['outerId'] = $refill_info['order_sn'];
  88. $uri = $this->form_uri($params,'order.status.query');
  89. $resp = http_request($uri, $params, 'POST');
  90. if (empty($resp)) {
  91. return [false, '系统错误'];
  92. }
  93. else
  94. {
  95. Log::record($resp, Log::DEBUG);
  96. $resp = json_decode($resp, true);
  97. $code = $resp['code'];
  98. if (empty($resp))
  99. {
  100. return [false, '系统错误'];
  101. }
  102. elseif ($code === 'SUCCESS')
  103. {
  104. $status = $resp['status'];
  105. if ($status === 'SUCCESS') {
  106. $order_state = ORDER_STATE_SUCCESS;
  107. $save['official_sn'] = strtolower($resp['evidence']) == 'null' ? '' : $resp['evidence'];
  108. Model('refill_order')->edit($refill_info['order_id'], $save);
  109. } elseif ($status === 'FAIL') {
  110. $order_state = ORDER_STATE_CANCEL;
  111. } elseif ($status === 'PROCESSING') {
  112. $order_state = ORDER_STATE_SEND;
  113. } elseif ($status === 'ORDER_NOT_EXIST' && (time() - $refill_info['commit_time'] >= 600)) {
  114. $order_state = ORDER_STATE_NOEXIST;
  115. } else {
  116. return [false, $code];
  117. }
  118. return [true, $order_state];
  119. }
  120. else
  121. {
  122. return [false, "code={$code}"];
  123. }
  124. }
  125. }
  126. public function balance()
  127. {
  128. $uri = $this->form_uri([],'user.balance.query');
  129. $resp = http_request($uri, [], 'POST');
  130. if (empty($resp)) {
  131. return [false, '系统错误'];
  132. }
  133. else
  134. {
  135. Log::record($resp, Log::DEBUG);
  136. $resp = json_decode($resp, true);
  137. if (empty($resp)) {
  138. return [false, '系统错误'];
  139. } elseif ($resp['code'] === 'SUCCESS') {
  140. return [true, $resp['balance']];
  141. } else {
  142. return [false, $resp['code']];
  143. }
  144. }
  145. }
  146. private function sign($params,$get_params)
  147. {
  148. $userId = config::UserId;
  149. $api_key = config::ApiKey;
  150. $content = "service={$get_params['service']}&userId={$userId}&ts={$get_params['ts']}&";
  151. if(!empty($params))
  152. {
  153. foreach ($params as $key => $value){
  154. $content .= "{$key}={$value}&";
  155. }
  156. $content = rtrim($content, '&');
  157. }
  158. $content .= "&key={$api_key}";
  159. return strtoupper(md5($content));
  160. }
  161. /**
  162. * 获取毫秒级别的时间戳
  163. */
  164. private function getMillisecond()
  165. {
  166. $cur = microtime (true);
  167. $cur = intval($cur * 1000);
  168. return $cur;
  169. }
  170. }