RefillPhone.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace refill\nanjingpushang_hf;
  3. require_once(BASE_HELPER_RAPI_PATH . '/nanjingpushang_hf/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. public function goods($quality,int $amount,int $card_type,$regin_no,$other)
  13. {
  14. [$goods_id, $price] = parent::goods($quality,$amount,$card_type,$regin_no,$other);
  15. if($goods_id <= 0) return [0,0];
  16. $key = "$card_type-$amount-$regin_no";
  17. $price = config::Price[$key];
  18. if(empty($price)) {
  19. Log::record("channel cannot find price where name=$this->mName, goods_id = $goods_id card_type=$card_type amount=$amount regin_no=$regin_no",Log::ERR);
  20. return [0,0];
  21. } else {
  22. return [$goods_id,ncPriceFormat($price)];
  23. }
  24. }
  25. private function req_params(int $card_no, int $amount, int $card_type, string $order_sn)
  26. {
  27. $params['merchantId'] = config::USER_ID;
  28. $params['amt'] = $amount;
  29. $params['phone'] = $card_no;
  30. $params['extOrderId'] = $order_sn;
  31. $params['notifyUrl'] = config::NOTIFY_URL;
  32. $params['operator'] = config::Cardtype2Name[$card_type];
  33. $sign = config::sign($params,['operator']);
  34. $params['md5'] = $sign;
  35. return $params;
  36. }
  37. public function add($card_no, $card_type, $amount, $params, &$net_errno = 0)
  38. {
  39. $order_sn = $params['order_sn'];
  40. $params = $this->req_params($card_no, $amount, $card_type, $order_sn);
  41. $resp = http_request(config::ORDER_URL, $params, 'POST', false, config::ExtHeaders, $net_errno);
  42. if (empty($resp))
  43. {
  44. return [false, '网络错误', true];
  45. }
  46. else
  47. {
  48. Log::record($resp, Log::DEBUG);
  49. $resp = json_decode($resp, true);
  50. if (empty($resp)) {
  51. return [false, '网络错误', true];
  52. }
  53. $code = $resp['code'];
  54. if ($code === 0) {
  55. return [true, '', false];
  56. }
  57. elseif (in_array($code, config::ERR_NOS, true)) {
  58. return [false, $resp['msg'], false];
  59. }
  60. else {
  61. $err = 998;
  62. $net_errno = "HTTP-$err";
  63. return [false, $resp['msg'], true];
  64. }
  65. }
  66. }
  67. public function query($refill_info)
  68. {
  69. $params['merchantId'] = config::USER_ID;
  70. $params['extOrderId'] = $refill_info['order_sn'];
  71. $params['sign'] = config::sign($params);
  72. $resp = http_request(config::QUERY_URL, $params, 'POST', false, config::ExtHeaders);
  73. if (empty($resp))
  74. {
  75. return [false, '网络错误'];
  76. }
  77. else
  78. {
  79. Log::record($resp, Log::DEBUG);
  80. $resp = json_decode($resp, true);
  81. if (empty($resp)) {
  82. return [false, '网络错误'];
  83. }
  84. $code = $resp['code'];
  85. $order = $resp['ext'];
  86. if ($code === 0)
  87. {
  88. $status = $resp['data'];
  89. if ($status === 4) {
  90. $updata['official_sn'] = $order['exchangeTraded'];
  91. Model('refill_order')->edit($refill_info['order_id'], $updata);
  92. return [true, ORDER_STATE_SUCCESS];
  93. } elseif ($status === 5) {
  94. return [true, ORDER_STATE_CANCEL];
  95. } elseif ($status === 3) {
  96. return [true, ORDER_STATE_SEND];
  97. }
  98. //9未查到订单(如果下单成功但是未查到订单不可以置为失败,需要进行核对)
  99. // elseif ($status === 9 && time() - $refill_info['commit_time'] > 600) {
  100. // return [true, ORDER_STATE_NOEXIST];
  101. // }
  102. else {
  103. return [false, $order['rechargeFailedReason']];
  104. }
  105. }
  106. else
  107. {
  108. return [false, $order['rechargeFailedReason'] ?? $resp['msg']];
  109. }
  110. }
  111. }
  112. public function balance()
  113. {
  114. $params['merchantId'] = config::USER_ID;
  115. $sign = config::sign($params);
  116. $params['sign'] = $sign;
  117. $resp = http_request(config::BALANCE_URL, $params, 'POST', false, config::ExtHeaders);
  118. if (empty($resp))
  119. {
  120. return [false, '网络错误'];
  121. }
  122. else
  123. {
  124. Log::record($resp, Log::DEBUG);
  125. $resp = json_decode($resp, true);
  126. if (empty($resp)) {
  127. return [false, '网络错误'];
  128. } elseif ($resp['code'] === 0) {
  129. $account = $resp['ext'];
  130. return [true, $account['balance']];
  131. } else {
  132. return [false, $resp['msg']];
  133. }
  134. }
  135. }
  136. }