RefillPhone.php 6.5 KB

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