RefillOil.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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)
  85. {
  86. [$params,$card_info] = $this->req_params($card_no,$card_type,$amount,$input);
  87. $sign = $this->sign($params);
  88. $params['signature'] = $sign;
  89. $uri = config::ORDER_URL . "/fuelRecharge/create";
  90. $resp = http_post_data($uri,json_encode($params), config::ExtHeaders);
  91. if($resp === false) {
  92. return [false,'系统错误',true];
  93. }
  94. else
  95. {
  96. Log::record($resp,Log::DEBUG);
  97. $resp = json_decode($resp,true);
  98. if($resp['code'] == 0)
  99. {
  100. refill\util::inc_card($card_no,$card_info);
  101. $data = $resp['data'];
  102. return [true,$data['orderNumber'], false];
  103. }
  104. else {
  105. return [false,$resp['message'], false];
  106. }
  107. }
  108. }
  109. private function query_params($order_sn)
  110. {
  111. $params['appid'] = config::APP_ID;
  112. $params['ts'] = time();
  113. $params['outOrderNumber'] = $order_sn;
  114. return $params;
  115. }
  116. public function query($refill_info)
  117. {
  118. $order_sn = $refill_info['order_sn'];
  119. $params = $this->query_params($order_sn);
  120. $sign = $this->sign($params);
  121. $params['signature'] = $sign;
  122. $uri = config::ORDER_URL . "/fuelRecharge/order/detail";
  123. $resp = http_post_data($uri,json_encode($params), config::ExtHeaders);
  124. Log::record("query resp={$resp}",Log::DEBUG);
  125. if($resp === false) {
  126. return [false,'网络错误'];
  127. }
  128. else
  129. {
  130. Log::record($resp,Log::DEBUG);
  131. $resp = json_decode($resp,true);
  132. if($resp == false) {
  133. return [false,'网络错误'];
  134. }
  135. elseif($resp['code'] == 0)
  136. {
  137. $data = $resp['data'];
  138. $status = intval($data['orderStatus']);
  139. if ($status === 101) {
  140. return [true, ORDER_STATE_SUCCESS];
  141. }
  142. elseif ($status === 102) {
  143. return [true, ORDER_STATE_CANCEL];
  144. }
  145. elseif($status === 100 || $status === 106) {
  146. return [true, ORDER_STATE_SEND];
  147. }
  148. else {
  149. return [false, $resp['message']];
  150. }
  151. }
  152. else {
  153. return [false,$resp['message']];
  154. }
  155. }
  156. }
  157. }