RefillPhone.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace refill\lingzhman;
  3. require_once(BASE_HELPER_RAPI_PATH . '/lingzhman/config.php');
  4. use refill;
  5. use Log;
  6. use mtopcard;
  7. class RefillPhone extends refill\IRefillPhone
  8. {
  9. public function __construct($cfgs)
  10. {
  11. parent::__construct($cfgs);
  12. }
  13. private function req_params(int $phone, string $order_sn)
  14. {
  15. $params['account'] = config::ACCOUNT;
  16. $params['consumerNo'] = $order_sn;
  17. $params['mobile'] = $phone;
  18. $params['timestamp'] = date("YmdHis");
  19. return $params;
  20. }
  21. private function none_support($card_no)
  22. {
  23. [$validate,$card_type,$regin_no,$isTransfer,$card_state,$black] = mtopcard\valid_phone($card_no);
  24. return $isTransfer;
  25. }
  26. private function card_no_none_support($card_no)
  27. {
  28. $no = substr($card_no, 0, 2);
  29. if(in_array($no, [14, 16, 17, 19])) {
  30. return true;
  31. }
  32. $no = substr($card_no, 0, 3);
  33. if(in_array($no,[150, 151])){
  34. return true;
  35. }
  36. return false;
  37. }
  38. public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
  39. {
  40. if($this->none_support($card_no)) {
  41. return [false, '不支持转网', false];
  42. }
  43. if($this->card_no_none_support($card_no)) {
  44. return [false, '不支持虚拟号段', false];
  45. }
  46. $params = $this->req_params($card_no, $params['order_sn']);
  47. $params['flowCode'] = config::ProductIDS[$card_type][$amount];
  48. if (empty($params['flowCode'])) {
  49. return [false, '产品编号错误', false];
  50. }
  51. $sign = $this->sign($params);
  52. $params['sign'] = $sign;
  53. $resp = http_request(config::ORDER_URL, $params, 'GET', false, [], $net_errno);
  54. if (empty($resp)) {
  55. return [false, '系统错误', true];
  56. }
  57. else
  58. {
  59. Log::record($resp, Log::DEBUG);
  60. $resp = json_decode($resp ,true);
  61. if (empty($resp)) {
  62. return [false, '系统错误', true];
  63. } elseif ($resp['status'] === '001') {
  64. return [true, $resp['orderNo'], false];
  65. } else {
  66. return [false, $resp['status'], false];
  67. }
  68. }
  69. }
  70. public function query($refill_info)
  71. {
  72. $params['account'] = config::ACCOUNT;
  73. $params['consumerNo'] = $refill_info['order_sn'];
  74. $params['timestamp'] = date("YmdHis");
  75. $params['sign'] = $this->sign($params);
  76. $resp = http_request(config::QUERY_URL, $params);
  77. if (empty($resp)) {
  78. return [false, '系统错误'];
  79. }
  80. else
  81. {
  82. Log::record($resp, Log::DEBUG);
  83. $resp = json_decode($resp, true);
  84. if (empty($resp)) {
  85. return [false, '系统错误'];
  86. }
  87. $status = $resp['status'];
  88. if ($status === '001')
  89. {
  90. $order_state = ORDER_STATE_SUCCESS;
  91. $save['official_sn'] = strtolower($resp['voucherNo']) == 'null' ? '' : $resp['voucherNo'];
  92. Model('refill_order')->edit($refill_info['order_id'], $save);
  93. }
  94. elseif (in_array($status,config::QueryErrCodes))
  95. {
  96. if ($status === '026')
  97. {
  98. //订单不存在状态,三小时内查询可失败,超过则返回充值中.不可以在下单后的同时立马发起查询,否则返回该状态处理为充值中,至少隔30秒以上再发起查询
  99. $commit_secs = time() - $refill_info['commit_time'];
  100. if ($commit_secs > 0 && $commit_secs < 30) {
  101. return [false, '时长不足'];
  102. } elseif ($commit_secs <= 10800 && $commit_secs >= 30) {
  103. $order_state = ORDER_STATE_NOEXIST;
  104. } else {
  105. $order_state = ORDER_STATE_SEND;
  106. }
  107. }
  108. else {
  109. $order_state = ORDER_STATE_CANCEL;
  110. }
  111. }
  112. elseif ($status === '002') {
  113. $order_state = ORDER_STATE_SEND;
  114. }
  115. else {
  116. return [false, $status];
  117. }
  118. return [true, $order_state];
  119. }
  120. }
  121. public function balance()
  122. {
  123. $params['account'] = config::ACCOUNT;
  124. $params['timestamp'] = date("YmdHis");
  125. $params['sign'] = $this->sign($params);
  126. $resp = http_request(config::BALANCE_URL, $params);
  127. if (empty($resp)) {
  128. return [false, '系统错误'];
  129. }
  130. else
  131. {
  132. Log::record($resp, Log::DEBUG);
  133. $resp = json_decode($resp, true);
  134. if (empty($resp)) {
  135. return [false, '系统错误'];
  136. } elseif ($resp['status'] === 'success') {
  137. return [true, $resp['balance']];
  138. } else {
  139. return [false, $resp['descriptor']];
  140. }
  141. }
  142. }
  143. private function sign($params)
  144. {
  145. $data['account'] = $params['account'];
  146. $data['timestamp'] = $params['timestamp'];
  147. $data['secret'] = config::KEY;
  148. asort($data);
  149. $content = '';
  150. foreach ($data as $val){
  151. $content .= $val;
  152. }
  153. return sha1($content);
  154. }
  155. }