RefillPhone.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace refill\canbin_fs;
  3. require_once(BASE_HELPER_RAPI_PATH . '/canbin_fs/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 $phone, int $amount, int $card_type, string $order_sn, string $regin_no)
  26. {
  27. $params['szAgentId'] = config::USER_ID;
  28. $params['szOrderId'] = $order_sn;
  29. $params['szPhoneNum'] = $phone;
  30. $params['nMoney'] = $amount;
  31. $params['nSortType'] = config::operator[$card_type];
  32. $params['szProductId'] = config::Product[$card_type][$regin_no][$amount];
  33. $params['nProductClass'] = 1;
  34. $params['nProductType'] = 1;
  35. $params['szTimeStamp'] = date("Y-m-d H:i:s");
  36. $params['szNotifyUrl'] = config::NOTIFY_URL;
  37. return $params;
  38. }
  39. public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
  40. {
  41. $order_sn = $params['order_sn'];
  42. $regin_no = $params['regin_no'] ?? -1;
  43. if($regin_no <= 0) {
  44. return [false, '省份获取错误', false];
  45. }
  46. $params = $this->req_params($card_no, $amount, $card_type, $order_sn, $regin_no);
  47. $sign = $this->sign($params);
  48. $params['szVerifyString'] = $sign;
  49. $resp = http_request(config::ORDER_URL, $params, 'POST', false, config::ExtHeaders, $net_errno);
  50. if (empty($resp)) {
  51. return [false, '网络错误', true];
  52. }
  53. else
  54. {
  55. Log::record($resp, Log::DEBUG);
  56. $resp = json_decode($resp, true);
  57. $nRtn = $resp['nRtn'];
  58. if (empty($resp)) {
  59. return [false, '网络错误', true];
  60. } elseif ($nRtn === 0) {
  61. return [true, '', false];
  62. } elseif (in_array($nRtn, config::ERR_NOS, true)) {
  63. return [false, $resp['szRtnCode'], false];
  64. } elseif (in_array($nRtn, [2050, 999], true)) {
  65. $net_errno = "HTTP-{$nRtn}";
  66. return [false, $resp['szRtnCode'], true];
  67. } else {
  68. $err = 998;
  69. $net_errno = "HTTP-{$err}";
  70. return [false, $resp['szRtnCode'], true];
  71. }
  72. }
  73. }
  74. public function query($refill_info)
  75. {
  76. $params['szAgentId'] = config::USER_ID;
  77. $params['szOrderId'] = $refill_info['order_sn'];
  78. $key = config::KEY;
  79. $content = "szAgentId={$params['szAgentId']}&szOrderId={$params['szOrderId']}&szKey={$key}";
  80. $params['szVerifyString'] = md5($content);
  81. $resp = http_request(config::QUERY_URL, $params, 'POST', false, config::ExtHeaders);
  82. if (empty($resp)) {
  83. return [false, '网络错误', ''];
  84. }
  85. else
  86. {
  87. Log::record($resp, Log::DEBUG);
  88. $resp = json_decode($resp, true);
  89. if (empty($resp)) {
  90. return [false, '网络错误', ''];
  91. }
  92. $official_sn = '';
  93. $status = $resp['nRtn'];
  94. if ($status === 5012) {
  95. $official_sn = $resp['szRtnMsg'];
  96. $updata['official_sn'] = $official_sn;
  97. Model('refill_order')->edit($refill_info['order_id'], $updata);
  98. $order_state = ORDER_STATE_SUCCESS;
  99. } elseif ($status === 5013) {
  100. $order_state = ORDER_STATE_CANCEL;
  101. } elseif (in_array($status, [5011,5019],true)) {
  102. $order_state = ORDER_STATE_SEND;
  103. } elseif ($status === 5005 && (time() - $refill_info['commit_time'] > 300)) {
  104. $order_state = ORDER_STATE_NOEXIST;
  105. } else {
  106. return [false, $resp['szRtnMsg'], ''];
  107. }
  108. return [true, $order_state, $official_sn];
  109. }
  110. }
  111. public function balance()
  112. {
  113. $params['szAgentId'] = config::USER_ID;
  114. $key = config::KEY;
  115. $content = "szAgentId={$params['szAgentId']}&szKey={$key}";
  116. $params['szVerifyString'] = md5($content);
  117. $resp = http_request(config::BALANCE_URL, $params, 'POST', false, config::ExtHeaders);
  118. if (empty($resp)) {
  119. return [false, '网络错误'];
  120. }
  121. else
  122. {
  123. Log::record($resp, Log::DEBUG);
  124. $resp = json_decode($resp, true);
  125. if (empty($resp)) {
  126. return [false, '网络错误'];
  127. } elseif ($resp['nRtn'] === 0) {
  128. return [true, $resp['fBalance']];
  129. } else {
  130. return [false, $resp['szRtnCode']];
  131. }
  132. }
  133. }
  134. private function sign($params)
  135. {
  136. $userid = config::USER_ID;
  137. $key = config::KEY;
  138. $content = "szAgentId={$userid}&szOrderId={$params['szOrderId']}&szPhoneNum={$params['szPhoneNum']}&nMoney={$params['nMoney']}&nSortType={$params['nSortType']}";
  139. $content .= "&nProductClass={$params['nProductClass']}&nProductType={$params['nProductType']}&szTimeStamp={$params['szTimeStamp']}&szKey={$key}";
  140. return md5($content);
  141. }
  142. }