RefillPhone.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace refill\weisanhuo_normal;
  3. require_once(BASE_HELPER_RAPI_PATH . '/weisanhuo_normal/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 $card_no, int $card_type, string $order_sn, int $amount): array
  13. {
  14. $params['outTradeNo'] = $order_sn;
  15. $params['secretId'] = config::SECRET_ID;
  16. $params['account'] = $card_no;
  17. $product_id = config::ProductIdS[$card_type][$amount] ?? false;
  18. if ($product_id === false){
  19. return [];
  20. }
  21. $params['packageCode'] = $product_id;
  22. return $params;
  23. }
  24. public function add($card_no, $card_type, $amount, $params, &$net_errno = 0): array
  25. {
  26. $params = $this->req_params($card_no, $card_type, $params['order_sn'], $amount);
  27. if (empty($params)){
  28. return [false, '产品编码错误', false];
  29. }
  30. $sign = config::sign($params,['outTradeNo']);
  31. $params['sign'] = $sign;
  32. $params = json_encode($params);
  33. $resp = http_post_data(config::ORDER_URL, $params , config::ExtHeaders, $net_errno);
  34. if (empty($resp)) {
  35. return [false, '系统错误', true];
  36. }
  37. else
  38. {
  39. Log::record($resp, Log::DEBUG);
  40. $resp = json_decode($resp ,true);
  41. if (empty($resp)) {
  42. return [false, '系统错误', true];
  43. }
  44. if ($resp['success'] === false){
  45. return [false, $resp['returnMessage'], false];
  46. }
  47. $status = $resp['data']['status'];
  48. if ($status == 'waiting') {
  49. return [true, $resp['data']['inTradeNo'], false];
  50. } else {
  51. return [false, $resp['data']['statusDesc'], false];
  52. }
  53. }
  54. }
  55. public function query($refill_info): array
  56. {
  57. $params['secretId'] = config::SECRET_ID;
  58. $params['outTradeNo'] = $refill_info['order_sn'];
  59. $sign = config::sign($params, ['outTradeNo']);
  60. $params['sign'] = $sign;
  61. $resp = http_request(config::QUERY_URL, $params, 'GET');
  62. if (empty($resp)) {
  63. return [false, '网络错误', ''];
  64. }
  65. else
  66. {
  67. Log::record($resp, Log::DEBUG);
  68. $resp = json_decode($resp, true);
  69. if (empty($resp))
  70. {
  71. return [false, '网络错误', ''];
  72. }
  73. elseif ($resp['success'] === true)
  74. {
  75. $offical_sn = '';
  76. if(!empty($resp['data']))
  77. {
  78. $status = $resp['data']['status'];
  79. if ($status == 'success') {
  80. $offical_sn = $resp['data']['operatorTradeNo'];
  81. $ch_trade_no = $resp['data']['inTradeNo'];
  82. Model('refill_order')->edit($refill_info['order_id'], ['official_sn' => $offical_sn,'ch_trade_no' => $ch_trade_no]);
  83. $order_state = ORDER_STATE_SUCCESS;
  84. } elseif ($status == 'failed') {
  85. $ch_trade_no = $resp['data']['inTradeNo'];
  86. Model('refill_order')->edit($refill_info['order_id'], ['ch_trade_no' => $ch_trade_no]);
  87. $order_state = ORDER_STATE_CANCEL;
  88. } elseif ($status == 'waiting') {
  89. $order_state = ORDER_STATE_SEND;
  90. } else {
  91. return [false, $status, $offical_sn];
  92. }
  93. return [true, $order_state, $offical_sn];
  94. }
  95. elseif ((time() - $refill_info['commit_time']) >= 600) {
  96. return [true, ORDER_STATE_NOEXIST, $offical_sn];
  97. }
  98. else {
  99. return [true, ORDER_STATE_SEND, $offical_sn];
  100. }
  101. }
  102. else
  103. {
  104. return [false, $resp['returnMessage'], ''];
  105. }
  106. }
  107. }
  108. public function balance(): array
  109. {
  110. $params['secretId'] = config::SECRET_ID;
  111. $sign = config::sign($params);
  112. $params['sign'] = $sign;
  113. $resp = http_request(config::BALANCE_URL, $params);
  114. if (empty($resp)) {
  115. return [false, '网络错误'];
  116. } else {
  117. Log::record($resp, Log::DEBUG);
  118. $resp = json_decode($resp, true);
  119. if ($resp['success'] === true) {
  120. return [true, $resp['data']];
  121. } else {
  122. return [false, $resp['returnMessage']];
  123. }
  124. }
  125. }
  126. }