RefillPhone.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace refill\beixt;
  3. require_once(BASE_HELPER_RAPI_PATH . '/beixt/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 $amount ,string $order_sn)
  13. {
  14. $params['phone'] = $phone;
  15. $params['product_id'] = $amount;
  16. $params['tradeNo'] = $order_sn;
  17. $params['notify_url'] = config::NOTIFY_URL;
  18. return json_encode($params);
  19. }
  20. public function add($card_no, $card_type,$amount,$params,&$net_errno = 0)
  21. {
  22. $order_sn = $params['order_sn'];
  23. $params = $this->req_params($card_no,$amount,$order_sn);
  24. $time = time();
  25. $api_user_name = config::API_USER_NAME;
  26. $sign = $this->sign($time);
  27. $header = [
  28. 'Content-Type: application/json',
  29. "API-USER-NAME: {$api_user_name}",
  30. "API-NAME: OrderCreate",
  31. "API-TIMESTAMP: {$time}",
  32. "API-SIGNATURE: {$sign}",
  33. ];
  34. $resp = http_post_data(config::REQUEST_URL,$params,$header,$net_errno);
  35. if (empty($resp)) {
  36. return [false,'系统错误',true];
  37. }
  38. else
  39. {
  40. Log::record($resp,Log::DEBUG);
  41. $resp = json_decode($resp,true);
  42. if (empty($resp)) {
  43. return [false, '系统错误', true];
  44. } elseif ($resp['ack'] == 'success') {
  45. return [true, $resp['message']['order_number'], false];
  46. } else {
  47. return [false, $resp['message'], false];
  48. }
  49. }
  50. }
  51. public function query($refill_info)
  52. {
  53. $params['order_number'] = $refill_info['ch_trade_no'];
  54. $params['tradeNo'] = $refill_info['order_sn'];
  55. $time = time();
  56. $api_user_name = config::API_USER_NAME;
  57. $sign = $this->sign($time);
  58. $header = [
  59. 'Content-Type: application/json',
  60. "API-USER-NAME: {$api_user_name}",
  61. "API-NAME: OrderQuery",
  62. "API-TIMESTAMP: {$time}",
  63. "API-SIGNATURE: {$sign}",
  64. ];
  65. $resp = http_post_data(config::REQUEST_URL,json_encode($params),$header);
  66. if (empty($resp)) {
  67. return [false,'系统错误'];
  68. }
  69. else
  70. {
  71. Log::record($resp,Log::DEBUG);
  72. $resp = json_decode($resp,true);
  73. if (empty($resp)) {
  74. return [false,'系统错误'];
  75. }
  76. elseif($resp['ack'] == 'success')
  77. {
  78. $data = $resp['order'];
  79. if ($data['shipping_status'] == 1) {
  80. $order_state = ORDER_STATE_SUCCESS;
  81. } elseif (in_array($data['shipping_status'], [0,3,4])) {
  82. $order_state = ORDER_STATE_CANCEL;
  83. } elseif (in_array($data['shipping_status'], [2,5])) {
  84. $order_state = ORDER_STATE_SEND;
  85. } else {
  86. return [false, $resp['message']];
  87. }
  88. return [true, $order_state];
  89. }
  90. else {
  91. return [false,$resp['message']];
  92. }
  93. }
  94. }
  95. private function sign($time)
  96. {
  97. $ip = config::API_IP;
  98. $cert = config::API_CERT;
  99. $content = $ip.$time.$cert;
  100. return md5($content);
  101. }
  102. }