RefillPhone.php 6.7 KB

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