RefillPhone.php 6.5 KB

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