RefillPhone.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace refill\douxun;
  3. require_once(BASE_HELPER_RAPI_PATH . '/douxun/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. $head['custInteId'] = config::custInteId;
  59. $head['echo'] = rand(100000,999999);
  60. $head['timestamp'] = date("YmdHis");
  61. $head['version'] = 1;
  62. $content = $head['custInteId'] . config::KEY . $head['echo'] . $head['timestamp'];
  63. $head['chargeSign'] = base64_encode(md5($content, true));
  64. $params['orderIds'] = $refill_info['order_sn'];
  65. $data = [
  66. 'head' => $head,
  67. 'body' => [
  68. 'item' => $params
  69. ]
  70. ];
  71. $xmlData = $this->toXmlData($data);
  72. $resp = http_post_data(config::QUERY_URL, $xmlData, config::ExtHeaders);
  73. if (empty($resp)) {
  74. return [false, '网络错误'];
  75. }
  76. else
  77. {
  78. Log::record($resp, Log::DEBUG);
  79. $resp = refill\util::xmlToArray($resp);
  80. if (empty($resp)) {
  81. return [false, '网络错误'];
  82. } elseif ($resp['head']['result'] === '0000') {
  83. $item = $resp['body']['item'];
  84. $status = $item['state'];
  85. if ($status === '1') {
  86. $order_state = ORDER_STATE_SUCCESS;
  87. $updata['official_sn'] = $item['operatorNo'];
  88. Model('refill_order')->edit($refill_info['order_id'], $updata);
  89. } elseif ($status === '2') {
  90. $order_state = ORDER_STATE_CANCEL;
  91. } elseif ($status === '0') {
  92. $order_state = ORDER_STATE_SEND;
  93. } elseif ($status === '-1' && (time() - $refill_info['commit_time'] > 600)) {
  94. $order_state = ORDER_STATE_NOEXIST;
  95. } else {
  96. return [false, $item['desc']];
  97. }
  98. return [true, $order_state];
  99. } else {
  100. return [false, $resp['head']['desc']];
  101. }
  102. }
  103. }
  104. public function balance()
  105. {
  106. $head['custInteId'] = config::custInteId;
  107. $head['echo'] = rand(100000,999999);
  108. $head['timestamp'] = date("YmdHis");
  109. $head['version'] = 1;
  110. $content = $head['custInteId'] . config::KEY . $head['echo'] . $head['timestamp'];
  111. $head['chargeSign'] = base64_encode(md5($content, true));
  112. $data = [
  113. 'head' => $head,
  114. ];
  115. $xmlData = $this->toXmlData($data);
  116. $resp = http_post_data(config::BALANCE_URL, $xmlData, config::ExtHeaders);
  117. if (empty($resp)) {
  118. return [false, '网络错误'];
  119. }
  120. else
  121. {
  122. Log::record($resp, Log::DEBUG);
  123. $resp = refill\util::xmlToArray($resp);
  124. if (empty($resp)) {
  125. return [false, '网络错误', true];
  126. } elseif ($resp['result'] === '0000') {
  127. return [true, $resp['balance'], false];
  128. } else {
  129. return [false, $resp['desc'], false];
  130. }
  131. }
  132. }
  133. private function sign($params)
  134. {
  135. $content = $params['custInteId'] . $params['orderId'] . config::KEY. $params['echo'] . $params['timestamp'];
  136. return base64_encode(md5($content, true));
  137. }
  138. private function toXmlData($data)
  139. {
  140. return $this->xml_encode($data);
  141. }
  142. private function xml_encode($data, $encoding='utf-8') {
  143. $xml = '<?xml version="1.0" encoding="' . $encoding . '"?>';
  144. $xml .= '<request>';
  145. $xml .= $this->data_to_xml($data);
  146. $xml .= '</request>';
  147. return $xml;
  148. }
  149. private function data_to_xml($data) {
  150. $xml = '';
  151. foreach ($data as $key => $val) {
  152. is_numeric($key) && $key = "item id=\"$key\"";
  153. $xml .= "<$key>";
  154. $xml .= ( is_array($val) || is_object($val)) ? $this->data_to_xml($val) : $val;
  155. list($key, ) = explode(' ', $key);
  156. $xml .= "</$key>";
  157. }
  158. return $xml;
  159. }
  160. }