proxy.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. declare(strict_types=0);
  3. require_once (BASE_ROOT_PATH . '/helper/model_helper.php');
  4. class proxy
  5. {
  6. private function onEerror(refill\order $order,$errmsg)
  7. {
  8. $refill_order = Model('refill_order');
  9. $mchid = $order->mchid();
  10. $mch_order = $order->mch_order();
  11. $last_order_id = $order->last_order_id();
  12. if ($last_order_id === 0) {
  13. $order_id = refill\RefillFactory::instance()->zero_order($order, $errmsg);
  14. $last_order_id = $order_id;
  15. } else {
  16. $refill_order->edit($last_order_id, ['notify_time' => time(), 'notify_state' => 1, 'is_retrying' => 0]);
  17. }
  18. refill\util::pop_queue_order($mchid,$mch_order);
  19. QueueClient::push("NotifyMerchantComplete", ['order_id' => $last_order_id,'manual' => false]);
  20. return true;
  21. }
  22. public function add($params)
  23. {
  24. $order = refill\order::from_parameters($params);
  25. if($order->is_black()) {
  26. return $this->onEerror($order, '黑名单卡号');
  27. }
  28. $mchid = $order->mchid();
  29. $mch_order = $order->mch_order();
  30. $refill_order = Model('refill_order');
  31. if($order->first_commit()) {
  32. refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
  33. $refill_order->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
  34. }
  35. Log::record("proxy::add times={$order->commit_times()} mch_order={$mch_order} card_no = {$order->card_no()} regin_no={$order->region_no()} org_quality={$order->org_quality()} quality={$order->cur_quality()}",Log::DEBUG);
  36. if ($this->canceled($mchid, $mch_order)) {
  37. return $this->onEerror($order, '订单被拦截');
  38. }
  39. if (!$order->validate()) {
  40. return $this->onEerror($order, '空号或风险号');
  41. }
  42. if($this->need_intercept($order)) {
  43. return $this->onEerror($order, '转网,问题号码,运营商维护被拦截.');
  44. }
  45. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($order);
  46. $order->set_quality($org_quality,$quality);
  47. if($quality == 0) {
  48. return $this->onEerror($order, '找不到合适质量的通道');
  49. }
  50. if($this->region_intercept($order)) {
  51. return $this->onEerror($order, '运营商维护被全局拦截.');
  52. }
  53. if($order->first_commit()) {
  54. refill\util::incr_user_commit($mchid,$order->card_type(),$order->spec(),$org_quality);
  55. }
  56. [$errcode, $errmsg, $order_id, $neterr,$net_errno] = refill\RefillFactory::instance()->add($order);
  57. if($errcode !== true)
  58. {
  59. if($errcode === refill\errcode::MERCHANT_REFILL_ERROR && $neterr && refill\util::need_check($net_errno)) {
  60. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],15);
  61. $fError = false;
  62. }
  63. elseif(($errcode === refill\errcode::MERCHANT_REFILL_ERROR && $neterr))
  64. {
  65. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($order,true);
  66. $order->set_quality($org_quality,$quality);
  67. if ($quality > 0)
  68. {
  69. $params['order_id'] = $order_id;
  70. if($order_id > 0) {
  71. $refill_order->edit($order_id, ['is_retrying' => 1]);
  72. }
  73. $params = $order->queue_params();
  74. if (!refill\util::push_add($params)) {
  75. $fError = true;
  76. } else {
  77. $fError = false;
  78. }
  79. }
  80. }
  81. else {
  82. $fError = true;
  83. }
  84. if($fError) {
  85. return $this->onEerror($order, $errmsg);
  86. }
  87. }
  88. return true;
  89. }
  90. private function need_intercept(refill\order $order)
  91. {
  92. if($order->is_third()) return false;
  93. $mchid = $order->mchid();
  94. $card_state = $order->card_state();
  95. $is_transfer = $order->is_transfer();
  96. $card_type = $order->card_type();
  97. return refill\RefillFactory::instance()->need_intercept($mchid,$card_type,$card_state,$is_transfer);
  98. }
  99. private function region_intercept(refill\order $order)
  100. {
  101. if($order->is_third() || $order->is_oil()) return false;
  102. $quality = $order->cur_quality();
  103. $card_type = $order->card_type();
  104. $reqion = $order->region_no();
  105. return refill\RefillFactory::instance()->region_intercept($quality,$card_type,$reqion);
  106. }
  107. private function canceled($mchid,$mch_order)
  108. {
  109. $order_state = refill\util::query_cancel_order($mchid,$mch_order);
  110. if($order_state == 1) {
  111. return true;
  112. } else {
  113. return false;
  114. }
  115. }
  116. public function add_zero($params)
  117. {
  118. $order = refill\order::from_parameters($params);
  119. $mchid = $order->mchid();
  120. $mch_order = $order->mch_order();
  121. refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
  122. Model('refill_order')->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
  123. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($order);
  124. if($order->first_commit()) {
  125. refill\util::incr_user_commit($mchid,$order->card_type(),$order->spec(),$org_quality);
  126. }
  127. Log::record("proxy::add mch_order={$mch_order} card_no = {$order->card_no()} regin_no={$order->region_no()} org_quality={$org_quality} quality={$quality}",Log::DEBUG);
  128. $order_id = refill\RefillFactory::instance()->zero_order($order,"手动0元订单");
  129. refill\util::pop_queue_order($mchid, $mch_order);
  130. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
  131. }
  132. public function notify($channel,$input)
  133. {
  134. return refill\RefillFactory::instance()->notify($channel,$input);
  135. }
  136. public function notify_merchant($order_id,$manual)
  137. {
  138. return refill\RefillFactory::instance()->notify_merchant($order_id,$manual);
  139. }
  140. public function query($order_id)
  141. {
  142. return refill\RefillFactory::instance()->query($order_id);
  143. }
  144. public function query_net($order_id)
  145. {
  146. return refill\RefillFactory::instance()->query_net($order_id);
  147. }
  148. public function manual_success($order_id)
  149. {
  150. refill\RefillFactory::instance()->manual_success($order_id);
  151. }
  152. public function manual_cancel($order_id)
  153. {
  154. refill\RefillFactory::instance()->manual_cancel($order_id);
  155. }
  156. public function addthird($params)
  157. {
  158. $order = refill\order::from_parameters($params);
  159. $mchid = $order->mchid();
  160. $mch_order = $order->mch_order();
  161. $card_type = $order->card_type();
  162. $spec = $order->spec();
  163. $org_quality = $order->org_quality();
  164. $refill_order = Model('refill_order');
  165. refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
  166. $refill_order->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
  167. refill\util::incr_user_commit($mchid,$card_type,$spec,$org_quality);
  168. [$errcode, $errmsg, $order_id, $neterr,$net_errno] = refill\RefillFactory::instance()->add($order);
  169. if($errcode !== true)
  170. {
  171. if ($errcode === refill\errcode::MERCHANT_REFILL_ERROR && $neterr && refill\util::need_check($net_errno)) {
  172. return QueueClient::async_push("QueryOrderNeterr", ['order_id' => $order_id], 15);
  173. } else {
  174. return $this->onEerror($order, $errmsg);
  175. }
  176. }
  177. return true;
  178. }
  179. }