RefillPhone.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace refill\yuntian_fs;
  3. require_once(BASE_HELPER_RAPI_PATH . '/yuntian_fs/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 $card_type, string $order_sn)
  26. {
  27. $params['account'] = config::ACCOUNT;
  28. $params['consumerNo'] = $order_sn;
  29. $params['mobile'] = $phone;
  30. $params['ispCode'] = config::operator[$card_type];
  31. $params['cbkUrl'] = config::NOTIFY_URL;
  32. $params['timestamp'] = $this->get_millisecond();
  33. return $params;
  34. }
  35. public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
  36. {
  37. $regin_no = $params['regin_no'] ?? -1;
  38. if($regin_no <= 0) {
  39. return [false, '省份获取错误', false];
  40. }
  41. $params = $this->req_params($card_no, $card_type, $params['order_sn']);
  42. $params['flowCode'] = config::ProductIDS[$card_type][$regin_no][$amount];
  43. if (empty($params['flowCode'])) {
  44. return [false, '产品编号错误', false];
  45. }
  46. $sign = $this->sign($params);
  47. $params['sign'] = $sign;
  48. $resp = http_request(config::ORDER_URL, $params, 'GET', false, [], $net_errno);
  49. if (empty($resp)) {
  50. return [false, '系统错误', true];
  51. }
  52. else
  53. {
  54. Log::record($resp, Log::DEBUG);
  55. $resp = json_decode($resp ,true);
  56. if (empty($resp)) {
  57. return [false, '系统错误', true];
  58. } elseif ($resp['status'] === '001') {
  59. return [true, $resp['orderNo'], false];
  60. } else {
  61. return [false, $resp['status'], false];
  62. }
  63. }
  64. }
  65. public function query($refill_info)
  66. {
  67. $params['account'] = config::ACCOUNT;
  68. $params['consumerNo'] = $refill_info['order_sn'];
  69. $params['timestamp'] = $this->get_millisecond();
  70. $params['sign'] = $this->sign($params);
  71. $resp = http_request(config::QUERY_URL, $params);
  72. if (empty($resp)) {
  73. return [false, '系统错误'];
  74. }
  75. else
  76. {
  77. Log::record($resp, Log::DEBUG);
  78. $resp = json_decode($resp, true);
  79. if (empty($resp)) {
  80. return [false, '系统错误'];
  81. }
  82. $status = $resp['status'];
  83. if ($status === '001')
  84. {
  85. $order_state = ORDER_STATE_SUCCESS;
  86. $save['official_sn'] = strtolower($resp['voucherNo']) == 'null' ? '' : $resp['voucherNo'];
  87. Model('refill_order')->edit($refill_info['order_id'], $save);
  88. }
  89. elseif (in_array($status,config::QueryErrCodes, true))
  90. {
  91. if ($status === '026')
  92. {
  93. //订单不存在状态,三小时内查询可失败,超过则返回充值中.不可以在下单后的同时立马发起查询,否则返回该状态处理为充值中,至少隔30秒以上再发起查询
  94. $commit_secs = time() - $refill_info['commit_time'];
  95. if ($commit_secs > 0 && $commit_secs <= 180) {
  96. return [false, '时长不足'];
  97. } elseif ($commit_secs > 180 && $commit_secs <= 10800) {
  98. $order_state = ORDER_STATE_NOEXIST;
  99. } else {
  100. $order_state = ORDER_STATE_SEND;
  101. }
  102. }
  103. else {
  104. $order_state = ORDER_STATE_CANCEL;
  105. }
  106. }
  107. elseif ($status === '002') {
  108. $order_state = ORDER_STATE_SEND;
  109. }
  110. else {
  111. return [false, $status];
  112. }
  113. return [true, $order_state];
  114. }
  115. }
  116. public function balance()
  117. {
  118. $params['account'] = config::ACCOUNT;
  119. $params['timestamp'] = $this->get_millisecond();
  120. $params['sign'] = $this->sign($params);
  121. $resp = http_request(config::BALANCE_URL, $params);
  122. if (empty($resp)) {
  123. return [false, '系统错误'];
  124. }
  125. else
  126. {
  127. Log::record($resp, Log::DEBUG);
  128. $resp = json_decode($resp, true);
  129. if (empty($resp)) {
  130. return [false, '系统错误'];
  131. } elseif ($resp['status'] === 'success') {
  132. return [true, $resp['balance']];
  133. } else {
  134. return [false, $resp['descriptor']];
  135. }
  136. }
  137. }
  138. private function sign($params)
  139. {
  140. $signature = [config::KEY, $params['timestamp'], config::ACCOUNT];
  141. sort($signature, SORT_STRING);
  142. $signature = implode($signature);
  143. return sha1($signature);
  144. }
  145. /**
  146. * 获取毫秒级别的时间戳
  147. */
  148. private function get_millisecond()
  149. {
  150. $cur = microtime (true);
  151. return intval($cur * 1000);
  152. }
  153. }