RefillPhone.php 5.8 KB

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