RefillPhone.php 4.5 KB

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