proxy.php 7.5 KB

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