RefillPhone.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace refill\zhongst_doubi;
  3. require_once(BASE_HELPER_RAPI_PATH . '/zhongst_doubi/config.php');
  4. use refill;
  5. use Log;
  6. class RefillPhone extends refill\IRefillThird
  7. {
  8. public function __construct($cfgs)
  9. {
  10. parent::__construct($cfgs);
  11. }
  12. private function req_params($phone, int $amount, string $order_sn, $product_code)
  13. {
  14. $params['szAgentId'] = config::USER_ID;
  15. $params['szOrderId'] = $order_sn;
  16. $params['szPhoneNum'] = $phone;
  17. $params['nMoney'] = $amount;
  18. $params['nSortType'] = '1031';
  19. $params['szProductId'] = $product_code;
  20. $params['nProductClass'] = 1;
  21. $params['nProductType'] = 1;
  22. $params['szTimeStamp'] = date("Y-m-d H:i:s");
  23. $params['szNotifyUrl'] = config::NOTIFY_URL;
  24. return $params;
  25. }
  26. private function getProductAmount($sys_pcode)
  27. {
  28. $thrid_refill = Model('thrid_refill');
  29. $product = $thrid_refill->getProduct(['system_code' => $sys_pcode]);
  30. if (empty($product)) {
  31. return false;
  32. } else {
  33. return $product['refill_amount'];
  34. }
  35. }
  36. private function getProductCode($goods_id, $sys_pcode)
  37. {
  38. $thrid_refill = Model('thrid_refill');
  39. $product = $thrid_refill->getProviderProduct($this->mStoreID,$goods_id,$sys_pcode);
  40. if (empty($product)) {
  41. return false;
  42. } else {
  43. return $product['channel_code'];
  44. }
  45. }
  46. public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
  47. {
  48. $order_sn = $params['order_sn'];
  49. $goods_id = intval($params['goods_id']);
  50. $product_code = $this->getProductCode($goods_id, $params['product_code']);
  51. Model('thrid_refill')->edit_third($params['order_id'], ['chcode' => $product_code]);
  52. $amount = $this->getProductAmount($params['product_code']);
  53. if(empty($amount)) {
  54. return [false, '产品未开启', false];
  55. }
  56. $params = $this->req_params($card_no, $amount, $order_sn, $product_code);
  57. if(empty($params['szProductId'])) {
  58. return [false, '商品编号错误', false];
  59. }
  60. $sign = $this->sign($params);
  61. $params['szVerifyString'] = $sign;
  62. $resp = http_request(config::ORDER_URL, $params, 'POST', false, config::ExtHeaders, $net_errno);
  63. if (empty($resp)) {
  64. return [false, '网络错误', true];
  65. }
  66. else
  67. {
  68. Log::record($resp, Log::DEBUG);
  69. $resp = json_decode($resp, true);
  70. $nRtn = $resp['nRtn'];
  71. if (empty($resp)) {
  72. return [false, '网络错误', true];
  73. } elseif ($nRtn === 0) {
  74. return [true, '', false];
  75. } elseif (in_array($nRtn, config::ERR_NOS, true)) {
  76. return [false, $resp['szRtnCode'], false];
  77. } elseif (in_array($nRtn, [2050, 999], true)) {
  78. $net_errno = "HTTP-{$nRtn}";
  79. return [false, $resp['szRtnCode'], true];
  80. } else {
  81. $err = 998;
  82. $net_errno = "HTTP-{$err}";
  83. return [false, $resp['szRtnCode'], true];
  84. }
  85. }
  86. }
  87. public function query($refill_info)
  88. {
  89. $params['szAgentId'] = config::USER_ID;
  90. $params['szOrderId'] = $refill_info['order_sn'];
  91. $key = config::KEY;
  92. $content = "szAgentId={$params['szAgentId']}&szOrderId={$params['szOrderId']}&szKey={$key}";
  93. $params['szVerifyString'] = md5($content);
  94. $resp = http_request(config::QUERY_URL, $params, 'POST', false, config::ExtHeaders);
  95. if (empty($resp)) {
  96. return [false, '网络错误'];
  97. }
  98. else
  99. {
  100. Log::record($resp, Log::DEBUG);
  101. $resp = json_decode($resp, true);
  102. if (empty($resp)) {
  103. return [false, '网络错误'];
  104. }
  105. $status = $resp['nRtn'];
  106. if ($status === 5012) {
  107. $updata['official_sn'] = $resp['szRtnMsg'];
  108. Model('refill_order')->edit($refill_info['order_id'], $updata);
  109. $order_state = ORDER_STATE_SUCCESS;
  110. } elseif ($status === 5013) {
  111. $order_state = ORDER_STATE_CANCEL;
  112. } elseif (in_array($status, [5011,5019],true)) {
  113. $order_state = ORDER_STATE_SEND;
  114. } elseif ($status === 5005 && (time() - $refill_info['commit_time'] >= 300)) {
  115. $order_state = ORDER_STATE_NOEXIST;
  116. } else {
  117. return [false, $resp['szRtnMsg']];
  118. }
  119. return [true, $order_state];
  120. }
  121. }
  122. public function balance()
  123. {
  124. $params['szAgentId'] = config::USER_ID;
  125. $key = config::KEY;
  126. $content = "szAgentId={$params['szAgentId']}&szKey={$key}";
  127. $params['szVerifyString'] = md5($content);
  128. $resp = http_request(config::BALANCE_URL, $params, 'POST', false, config::ExtHeaders);
  129. if (empty($resp)) {
  130. return [false, '网络错误'];
  131. }
  132. else
  133. {
  134. Log::record($resp, Log::DEBUG);
  135. $resp = json_decode($resp, true);
  136. if (empty($resp)) {
  137. return [false, '网络错误'];
  138. } elseif ($resp['nRtn'] === 0) {
  139. return [true, $resp['fBalance']];
  140. } else {
  141. return [false, $resp['szRtnCode']];
  142. }
  143. }
  144. }
  145. private function sign($params)
  146. {
  147. $userid = config::USER_ID;
  148. $key = config::KEY;
  149. $content = "szAgentId={$userid}&szOrderId={$params['szOrderId']}&szPhoneNum={$params['szPhoneNum']}&nMoney={$params['nMoney']}&nSortType={$params['nSortType']}";
  150. $content .= "&nProductClass={$params['nProductClass']}&nProductType={$params['nProductType']}&szTimeStamp={$params['szTimeStamp']}&szKey={$key}";
  151. return md5($content);
  152. }
  153. }