RefillPhone.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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, [], $net_errno);
  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. $resp = $this->url_request($params,'order.phone.charge');
  60. if (empty($resp)) {
  61. return [false, '系统错误', true];
  62. }
  63. else
  64. {
  65. Log::record($resp, Log::DEBUG);
  66. $resp = json_decode($resp ,true);
  67. $code = $resp['code'];
  68. if (empty($resp)) {
  69. return [false, '系统错误', true];
  70. } elseif ($code == 'SUCCESS') {
  71. return [true, $resp['id'], false];
  72. } elseif (in_array($code, config::ORDER_ERR_CODE)) {
  73. return [false, $code, false];
  74. } elseif (in_array($code, ['SYSTEM_ERROR', 'ORDER_ID_EXIST'])) {
  75. $net_errno = "HTTP-{$code}";
  76. return [false, $code, false];
  77. } else {
  78. $err = 998;
  79. $net_errno = "HTTP-{$err}";
  80. return [false, $code, false];
  81. }
  82. }
  83. }
  84. public function query($refill_info)
  85. {
  86. //上游单号,可以为空,但需参与签名
  87. $params['id'] = '';
  88. $params['outerId'] = $refill_info['order_sn'];
  89. $resp = $this->url_request($params,'order.status.query');
  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. $resp = $this->url_request([],'user.balance.query');
  129. if (empty($resp)) {
  130. return [false, '系统错误'];
  131. }
  132. else
  133. {
  134. Log::record($resp, Log::DEBUG);
  135. $resp = json_decode($resp, true);
  136. if (empty($resp))
  137. {
  138. return [false, '系统错误'];
  139. }
  140. elseif ($resp['code'] == 'SUCCESS')
  141. {
  142. return [true, $resp['balance']];
  143. }
  144. else
  145. {
  146. return [false, $resp['code']];
  147. }
  148. }
  149. }
  150. private function sign($params,$get_params)
  151. {
  152. $userId = config::UserId;
  153. $api_key = config::ApiKey;
  154. $content = "service={$get_params['service']}&userId={$userId}&ts={$get_params['ts']}&";
  155. if(!empty($params)) {
  156. foreach ($params as $key => $value){
  157. $content .= "{$key}={$value}&";
  158. }
  159. $content = rtrim($content, '&');
  160. }
  161. $content .= "&key={$api_key}";
  162. return strtoupper(md5($content));
  163. }
  164. /**
  165. * 获取毫秒级别的时间戳
  166. */
  167. private function getMillisecond()
  168. {
  169. $cur = microtime (true);
  170. $cur = intval($cur * 1000);
  171. return $cur;
  172. }
  173. }