proxy.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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($order->first_commit()) {
  48. refill\util::incr_user_commit($mchid,$order->card_type(),$order->spec(),$org_quality);
  49. }
  50. [$errcode, $errmsg, $order_id, $neterr,$net_errno] = refill\RefillFactory::instance()->add($order);
  51. if($errcode !== true)
  52. {
  53. if($errcode === refill\errcode::MERCHANT_REFILL_ERROR && $neterr && refill\util::need_check($net_errno)) {
  54. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],15);
  55. $fError = false;
  56. }
  57. elseif(($errcode === refill\errcode::MERCHANT_REFILL_ERROR && $neterr))
  58. {
  59. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($order,true);
  60. $order->set_quality($org_quality,$quality);
  61. if ($quality > 0)
  62. {
  63. $params['order_id'] = $order_id;
  64. if($order_id > 0) {
  65. $refill_order->edit($order_id, ['is_retrying' => 1]);
  66. }
  67. $params = $order->queue_params();
  68. if (!refill\util::push_add($params)) {
  69. $fError = true;
  70. } else {
  71. $fError = false;
  72. }
  73. }
  74. }
  75. else {
  76. $fError = true;
  77. }
  78. if($fError) {
  79. return $this->onEerror($order, $errmsg);
  80. }
  81. }
  82. return true;
  83. }
  84. private function need_intercept(refill\order $order)
  85. {
  86. if($order->is_third()) return false;
  87. $mchid = $order->mchid();
  88. $card_state = $order->card_state();
  89. $is_transfer = $order->is_transfer();
  90. $is_transfer = $order->is_transfer();
  91. $card_type = $order->card_type();
  92. return refill\RefillFactory::instance()->need_intercept($mchid,$card_type,$card_state,$is_transfer);
  93. // global $config;
  94. // $mintercepts = $config['merchant_intercept'] ?? [];
  95. // if(array_key_exists($mchid,$mintercepts))
  96. // {
  97. // $mintercepts = $mintercepts[$mchid];
  98. // if(in_array($card_state,$mintercepts['card_states'])) {
  99. // return true;
  100. // }
  101. //
  102. // if($mintercepts['is_transfer'] && $is_transfer) {
  103. // return true;
  104. // }
  105. // }
  106. //
  107. // return false;
  108. }
  109. private function canceled($mchid,$mch_order)
  110. {
  111. $order_state = refill\util::query_cancel_order($mchid,$mch_order);
  112. if($order_state == 1) {
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. public function add_zero($params)
  119. {
  120. $order = refill\order::from_parameters($params);
  121. $mchid = $order->mchid();
  122. $mch_order = $order->mch_order();
  123. refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
  124. Model('refill_order')->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
  125. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($order);
  126. if($order->first_commit()) {
  127. refill\util::incr_user_commit($mchid,$order->card_type(),$order->spec(),$org_quality);
  128. }
  129. 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);
  130. $order_id = refill\RefillFactory::instance()->zero_order($order,"手动0元订单");
  131. refill\util::pop_queue_order($mchid, $mch_order);
  132. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
  133. }
  134. public function notify($channel,$input)
  135. {
  136. return refill\RefillFactory::instance()->notify($channel,$input);
  137. }
  138. public function notify_merchant($order_id,$manual)
  139. {
  140. return refill\RefillFactory::instance()->notify_merchant($order_id,$manual);
  141. }
  142. public function query($order_id)
  143. {
  144. return refill\RefillFactory::instance()->query($order_id);
  145. }
  146. public function query_net($order_id)
  147. {
  148. return refill\RefillFactory::instance()->query_net($order_id);
  149. }
  150. public function manual_success($order_id)
  151. {
  152. refill\RefillFactory::instance()->manual_success($order_id);
  153. }
  154. public function manual_cancel($order_id)
  155. {
  156. refill\RefillFactory::instance()->manual_cancel($order_id);
  157. }
  158. public function addthird($params)
  159. {
  160. $order = refill\order::from_parameters($params);
  161. $mchid = $order->mchid();
  162. $mch_order = $order->mch_order();
  163. $card_type = $order->card_type();
  164. $spec = $order->spec();
  165. $org_quality = $order->org_quality();
  166. $refill_order = Model('refill_order');
  167. refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
  168. $refill_order->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
  169. refill\util::incr_user_commit($mchid,$card_type,$spec,$org_quality);
  170. [$errcode, $errmsg, $order_id, $neterr,$net_errno] = refill\RefillFactory::instance()->add($order);
  171. if($errcode !== true)
  172. {
  173. if ($errcode === refill\errcode::MERCHANT_REFILL_ERROR && $neterr && refill\util::need_check($net_errno)) {
  174. return QueueClient::async_push("QueryOrderNeterr", ['order_id' => $order_id], 15);
  175. } else {
  176. return $this->onEerror($order, $errmsg);
  177. }
  178. }
  179. return true;
  180. }
  181. }