RefillPhone.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace refill\dazhanggui_fs_gd;
  3. require_once(BASE_HELPER_RAPI_PATH . '/dazhanggui_fs_gd/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, $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. if(empty($params['szProductId'])) {
  48. return [false, '商品编号错误', false];
  49. }
  50. $sign = $this->sign($params);
  51. $params['szVerifyString'] = $sign;
  52. $resp = http_request(config::ORDER_URL, $params, 'POST', false, config::ExtHeaders, $net_errno);
  53. if (empty($resp)) {
  54. return [false, '网络错误', true];
  55. }
  56. else
  57. {
  58. Log::record($resp, Log::DEBUG);
  59. $resp = json_decode($resp, true);
  60. $nRtn = $resp['nRtn'];
  61. if (empty($resp)) {
  62. return [false, '网络错误', true];
  63. } elseif ($nRtn === 0) {
  64. return [true, '', false];
  65. } elseif (in_array($nRtn, config::ERR_NOS, true)) {
  66. return [false, $resp['szRtnCode'], false];
  67. } elseif (in_array($nRtn, [2050, 999], true)) {
  68. $net_errno = "HTTP-{$nRtn}";
  69. return [false, $resp['szRtnCode'], true];
  70. } else {
  71. $err = 998;
  72. $net_errno = "HTTP-{$err}";
  73. return [false, $resp['szRtnCode'], true];
  74. }
  75. }
  76. }
  77. public function query($refill_info)
  78. {
  79. $params['szAgentId'] = config::USER_ID;
  80. $params['szOrderId'] = $refill_info['order_sn'];
  81. $key = config::KEY;
  82. $content = "szAgentId={$params['szAgentId']}&szOrderId={$params['szOrderId']}&szKey={$key}";
  83. $params['szVerifyString'] = md5($content);
  84. $resp = http_request(config::QUERY_URL, $params, 'POST', false, config::ExtHeaders);
  85. if (empty($resp)) {
  86. return [false, '网络错误'];
  87. }
  88. else
  89. {
  90. Log::record($resp, Log::DEBUG);
  91. $resp = json_decode($resp, true);
  92. if (empty($resp)) {
  93. return [false, '网络错误'];
  94. }
  95. $status = $resp['nRtn'];
  96. if ($status === 5012) {
  97. $updata['official_sn'] = $resp['szRtnMsg'];
  98. Model('refill_order')->edit($refill_info['order_id'], $updata);
  99. $order_state = ORDER_STATE_SUCCESS;
  100. } elseif ($status === 5013) {
  101. $order_state = ORDER_STATE_CANCEL;
  102. } elseif (in_array($status, [5011,5019],true)) {
  103. $order_state = ORDER_STATE_SEND;
  104. } elseif ($status === 5005 && (time() - $refill_info['commit_time'] >= 300)) {
  105. $order_state = ORDER_STATE_NOEXIST;
  106. } else {
  107. return [false, $resp['szRtnMsg']];
  108. }
  109. return [true, $order_state];
  110. }
  111. }
  112. public function balance()
  113. {
  114. $params['szAgentId'] = config::USER_ID;
  115. $key = config::KEY;
  116. $content = "szAgentId={$params['szAgentId']}&szKey={$key}";
  117. $params['szVerifyString'] = md5($content);
  118. $resp = http_request(config::BALANCE_URL, $params, 'POST', false, config::ExtHeaders);
  119. if (empty($resp)) {
  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['nRtn'] === 0) {
  129. return [true, $resp['fBalance']];
  130. } else {
  131. return [false, $resp['szRtnCode']];
  132. }
  133. }
  134. }
  135. private function sign($params)
  136. {
  137. $userid = config::USER_ID;
  138. $key = config::KEY;
  139. $content = "szAgentId={$userid}&szOrderId={$params['szOrderId']}&szPhoneNum={$params['szPhoneNum']}&nMoney={$params['nMoney']}&nSortType={$params['nSortType']}";
  140. $content .= "&nProductClass={$params['nProductClass']}&nProductType={$params['nProductType']}&szTimeStamp={$params['szTimeStamp']}&szKey={$key}";
  141. return md5($content);
  142. }
  143. }