RefillPhone.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace refill\douxun_copy;
  3. require_once(BASE_HELPER_RAPI_PATH . '/douxun_copy/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 req_params(int $phone, int $card_type, int $amount, string $order_sn)
  13. {
  14. $head['custInteId'] = config::custInteId;
  15. $head['orderId'] = $order_sn;
  16. $head['orderType'] = 1;
  17. $head['echo'] = rand(100000,999999);
  18. $head['timestamp'] = date("YmdHis");
  19. $head['version'] = 1;
  20. $params['packCode'] = config::PRODUCT[$card_type][$amount];
  21. $params['mobile'] = $phone;
  22. $params['effectType'] = 1;
  23. $params['mobileOper'] = config::operator[$card_type];
  24. return [$head,$params];
  25. }
  26. public function add($card_no, $card_type, $amount, $params, &$net_errno = 0)
  27. {
  28. $order_sn = $params['order_sn'];
  29. [$head,$params] = $this->req_params($card_no, $card_type, $amount, $order_sn);
  30. $sign = $this->sign($head);
  31. $head['chargeSign'] = $sign;
  32. $data = [
  33. 'head' => $head,
  34. 'body' => [
  35. 'item' => $params
  36. ]
  37. ];
  38. $xmlData = $this->toXmlData($data);
  39. $resp = http_post_data(config::ORDER_URL, $xmlData, config::ExtHeaders, $net_errno);
  40. if (empty($resp)) {
  41. return [false, '网络错误', true];
  42. }
  43. else
  44. {
  45. Log::record($resp, Log::DEBUG);
  46. $resp = refill\util::xmlToArray($resp);
  47. if (empty($resp)) {
  48. return [false, '网络错误', true];
  49. } elseif ($resp['result'] === '0000') {
  50. return [true, '', false];
  51. } else {
  52. return [false, $resp['desc'], false];
  53. }
  54. }
  55. }
  56. public function query($refill_info)
  57. {
  58. if (time() - $refill_info['commit_time'] > 86400*30) {
  59. return [false, '时效已过'];
  60. }
  61. $head['custInteId'] = config::custInteId;
  62. $head['echo'] = rand(100000,999999);
  63. $head['timestamp'] = date("YmdHis");
  64. $head['version'] = 1;
  65. $content = $head['custInteId'] . config::KEY . $head['echo'] . $head['timestamp'];
  66. $head['chargeSign'] = base64_encode(md5($content, true));
  67. $params['orderIds'] = $refill_info['order_sn'];
  68. $data = [
  69. 'head' => $head,
  70. 'body' => [
  71. 'item' => $params
  72. ]
  73. ];
  74. $xmlData = $this->toXmlData($data);
  75. $resp = http_post_data(config::QUERY_URL, $xmlData, config::ExtHeaders);
  76. if (empty($resp)) {
  77. return [false, '网络错误'];
  78. }
  79. else
  80. {
  81. Log::record($resp, Log::DEBUG);
  82. $resp = refill\util::xmlToArray($resp);
  83. if (empty($resp)) {
  84. return [false, '网络错误'];
  85. }
  86. elseif ($resp['head']['result'] === '0000')
  87. {
  88. $item = $resp['body']['item'];
  89. $status = $item['state'];
  90. if ($status === '1') {
  91. $order_state = ORDER_STATE_SUCCESS;
  92. $updata['official_sn'] = $item['operatorNo'];
  93. Model('refill_order')->edit($refill_info['order_id'], $updata);
  94. } elseif ($status === '2') {
  95. $order_state = ORDER_STATE_CANCEL;
  96. } elseif ($status === '0') {
  97. $order_state = ORDER_STATE_SEND;
  98. } elseif ($status === '-1' && (time() - $refill_info['commit_time'] > 600)) {
  99. $order_state = ORDER_STATE_NOEXIST;
  100. } else {
  101. return [false, $item['desc']];
  102. }
  103. return [true, $order_state];
  104. }
  105. else {
  106. return [false, $resp['head']['desc']];
  107. }
  108. }
  109. }
  110. public function balance()
  111. {
  112. $head['custInteId'] = config::custInteId;
  113. $head['echo'] = rand(100000,999999);
  114. $head['timestamp'] = date("YmdHis");
  115. $head['version'] = 1;
  116. $content = $head['custInteId'] . config::KEY . $head['echo'] . $head['timestamp'];
  117. $head['chargeSign'] = base64_encode(md5($content, true));
  118. $data = [
  119. 'head' => $head,
  120. ];
  121. $xmlData = $this->toXmlData($data);
  122. $resp = http_post_data(config::BALANCE_URL, $xmlData, config::ExtHeaders);
  123. if (empty($resp)) {
  124. return [false, '网络错误'];
  125. }
  126. else
  127. {
  128. Log::record($resp, Log::DEBUG);
  129. $resp = refill\util::xmlToArray($resp);
  130. if (empty($resp)) {
  131. return [false, '网络错误', true];
  132. } elseif ($resp['result'] === '0000') {
  133. return [true, $resp['balance'], false];
  134. } else {
  135. return [false, $resp['desc'], false];
  136. }
  137. }
  138. }
  139. private function sign($params)
  140. {
  141. $content = $params['custInteId'] . $params['orderId'] . config::KEY. $params['echo'] . $params['timestamp'];
  142. return base64_encode(md5($content, true));
  143. }
  144. private function toXmlData($data)
  145. {
  146. return $this->xml_encode($data);
  147. }
  148. private function xml_encode($data, $encoding='utf-8') {
  149. $xml = '<?xml version="1.0" encoding="' . $encoding . '"?>';
  150. $xml .= '<request>';
  151. $xml .= $this->data_to_xml($data);
  152. $xml .= '</request>';
  153. return $xml;
  154. }
  155. private function data_to_xml($data) {
  156. $xml = '';
  157. foreach ($data as $key => $val) {
  158. is_numeric($key) && $key = "item id=\"$key\"";
  159. $xml .= "<$key>";
  160. $xml .= ( is_array($val) || is_object($val)) ? $this->data_to_xml($val) : $val;
  161. list($key, ) = explode(' ', $key);
  162. $xml .= "</$key>";
  163. }
  164. return $xml;
  165. }
  166. }