RefillOil.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace refill\gftd;
  3. require_once(BASE_HELPER_RAPI_PATH . '/gftd/config.php');
  4. use refill;
  5. use mtopcard;
  6. use Log;
  7. class RefillOil extends refill\IRefillOil
  8. {
  9. public function __construct($cfgs)
  10. {
  11. parent::__construct($cfgs);
  12. }
  13. private function req_params(string $card_no,int $card_type,int $amount,array $other)
  14. {
  15. $params['appid'] = config::APP_ID;
  16. $params['ts'] = time();
  17. $params['productId'] = $this->product_id($amount,$card_type);
  18. $params['outOrderNumber'] = $other['order_sn'];
  19. $params['fuelCardNumber'] = $card_no;
  20. $params['fuelCardUserName'] = "";
  21. $params['fuelCardUserID'] = "";
  22. $card_info = refill\util::read_card($card_no,$card_type);
  23. $params['telephone'] = $card_info['bind_phone'];
  24. $params['phoneNumber'] = $card_info['bind_phone'];
  25. return [$params, $card_info];
  26. }
  27. private function product_id(int $amount,int $cart_type)
  28. {
  29. if($cart_type === mtopcard\PetroChinaCard)
  30. {
  31. switch ($amount) {
  32. case 100: return 1;
  33. case 200: return 2;
  34. case 500: return 3;
  35. case 1000: return 4;
  36. }
  37. }
  38. else
  39. {
  40. switch ($amount) {
  41. case 100: return 5;
  42. case 200: return 6;
  43. case 500: return 7;
  44. case 1000: return 8;
  45. }
  46. }
  47. return false;
  48. }
  49. private function sign($params)
  50. {
  51. $body = $this->body($params);
  52. $body .= config::APP_SECRET;
  53. return md5($body);
  54. }
  55. protected function check_empty($value)
  56. {
  57. if (!isset($value))
  58. return true;
  59. if ($value === null)
  60. return true;
  61. if (trim($value) === "")
  62. return true;
  63. return false;
  64. }
  65. private function body($params)
  66. {
  67. ksort($params);
  68. $body = "";
  69. $i = 0;
  70. foreach ($params as $k => $v)
  71. {
  72. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1))
  73. {
  74. if ($i == 0) {
  75. $body .= "{$k}" . "=" . $v;
  76. } else {
  77. $body .= "&" . "{$k}" . "=" . $v;
  78. }
  79. $i++;
  80. }
  81. }
  82. return $body;
  83. }
  84. public function add($card_no,$card_type,$amount,$input,&$net_errno = 0)
  85. {
  86. refill\util::send_normal($params['order_sn']);
  87. return [true , '',false];
  88. }
  89. private function query_params($order_sn)
  90. {
  91. $params['appid'] = config::APP_ID;
  92. $params['ts'] = time();
  93. $params['outOrderNumber'] = $order_sn;
  94. return $params;
  95. }
  96. public function query($refill_info)
  97. {
  98. $order_sn = $refill_info['order_sn'];
  99. $order_id = $refill_info['order_id'];
  100. $params = $this->query_params($order_sn);
  101. $sign = $this->sign($params);
  102. $params['signature'] = $sign;
  103. $uri = config::ORDER_URL . "/fuelRecharge/order/detail";
  104. $resp = http_post_data($uri,json_encode($params), config::ExtHeaders);
  105. Log::record("query resp={$resp}",Log::DEBUG);
  106. if(empty($resp)) {
  107. return [false,'网络错误'];
  108. }
  109. Log::record($resp,Log::DEBUG);
  110. $resp = json_decode($resp,true);
  111. if(empty($resp)) {
  112. return [false,'网络错误'];
  113. }
  114. elseif($resp['code'] == 0)
  115. {
  116. $data = $resp['data'];
  117. $status = intval($data['orderStatus']);
  118. if ($status === 101) {
  119. $updata['official_sn'] = $data['voucher'];
  120. $updata['ch_trade_no'] = $data['orderNumber'];
  121. $updata['err_msg'] = $resp['message'];
  122. Model('refill_order')->edit($order_id, $updata);
  123. return [true, ORDER_STATE_SUCCESS];
  124. }
  125. elseif ($status === 102) {
  126. $updata['official_sn'] = $data['voucher'];
  127. $updata['ch_trade_no'] = $data['orderNumber'];
  128. $updata['err_msg'] = $resp['message'];
  129. Model('refill_order')->edit($order_id, $updata);
  130. $this->onError($resp['message'],$order_id);
  131. return [true, ORDER_STATE_CANCEL];
  132. }
  133. elseif($status === 100 || $status === 106) {
  134. return [true, ORDER_STATE_SEND];
  135. }
  136. else {
  137. return [false, $resp['message']];
  138. }
  139. }
  140. else {
  141. return [false,$resp['message']];
  142. }
  143. }
  144. public function onError($msg,$order_id)
  145. {
  146. if(in_array($msg,config::BlackMsgs))
  147. {
  148. $refill = Model('refill_order');
  149. $order = $refill->getOrderInfo(['order_id' => $order_id]);
  150. if(empty($order)) return false;
  151. $card_no = $order['card_no'];
  152. if(!empty($card_no)) {
  153. refill\util::set_black($card_no);
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. public function balance()
  160. {
  161. return [false, '暂无余额接口'];
  162. }
  163. }