proxy.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. declare(strict_types=0);
  3. use refill\PolicyUtil;
  4. use refill\util;
  5. use refill\Quality;
  6. require_once(BASE_ROOT_PATH . '/helper/model_helper.php');
  7. require_once(BASE_HELPER_PATH . '/refill/policy/transfer.php');
  8. class proxy
  9. {
  10. private function is_loop_order(refill\order $order)
  11. {
  12. if($order->is_third() || $order->is_oil()) {
  13. return false;
  14. }
  15. $card_no = $order->card_no();
  16. $spec = $order->spec();
  17. return util::loop_order_inc($card_no,$spec);
  18. }
  19. private function onEerror(refill\order $order,$need_callback,$errmsg)
  20. {
  21. $mod_refill = Model('refill_order');
  22. $mchid = $order->mchid();
  23. $mch_order = $order->mch_order();
  24. $last_order_id = $order->last_order_id();
  25. $order_time = $order->order_time();
  26. if ($last_order_id === 0) {
  27. $order_id = refill\RefillFactory::instance()->zero_order($order, $errmsg);
  28. $last_order_id = $order_id;
  29. } else {
  30. $mod_refill->partition(util::part_refill($order_time))->edit($last_order_id, ['notify_time' => time(), 'notify_state' => 1, 'is_retrying' => 0]);
  31. $mod_order = Model('vr_order');
  32. $mod_order->partition(util::part_vr_order_time($order_time))->editOrder(['close_reason' => $errmsg], ['order_id' => $last_order_id]);
  33. }
  34. refill\util::pop_queue_order($mchid, $mch_order,$order_time);
  35. QueueClient::push("NotifyMerchantComplete", ['order_id' => $last_order_id, 'manual' => false]);
  36. if($need_callback)
  37. {
  38. Log::record("onEerror monitor_callback",Log::DEBUG);
  39. $mch_amount = refill\RefillFactory::instance()->mch_amount($order);
  40. if($mch_amount == false) {
  41. refill\util::monitor_callback($mchid, $order->spec(), $order->card_type(), 0.0, 0, false, $order->order_time());
  42. } else {
  43. refill\util::monitor_callback($mchid, $order->spec(), $order->card_type(), $mch_amount, 0, false, $order->order_time());
  44. }
  45. $refill_info = Model('refill_order')->partition(util::part_refill($order_time))->getOrderInfo(['order_id' => $order_id]);
  46. $order_info = Model('refill_order')->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id]);
  47. util::onEventComplete($refill_info, $order_info, false);
  48. }
  49. return true;
  50. }
  51. private function need_transfer(refill\order $order)
  52. {
  53. if($order->match_ratio()) return false;
  54. $order_time = $order->order_time();
  55. $mchid = $order->mchid();
  56. $card_type = $order->card_type();
  57. $ret = refill\transfer::instance()->need_transfer($mchid,$order_time,$card_type);
  58. return $ret;
  59. }
  60. private function transfer(refill\order $order) : bool
  61. {
  62. $order_canceler = function ($order_id,$err_msg) {
  63. $logic_vr_order = Logic("vr_order");
  64. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  65. $logic_vr_order->changeOrderStateCancel($order_info, '', $err_msg, true, true);
  66. };
  67. $transfer_order = function (refill\order $order)
  68. {
  69. $mchid = $order->mchid();
  70. [$trans_mchid,$adminid] = refill\transfer::instance()->transfer_info($mchid);
  71. if($trans_mchid == 0 || $adminid == 0) {
  72. return false;
  73. }
  74. $order_time = time();
  75. $params = ['mchid' => $trans_mchid,
  76. 'buyer_id' => $adminid,
  77. 'amount' => $order->spec(),
  78. 'card_no' => $order->card_no(),
  79. 'mch_order' => $order->mch_order(),
  80. 'notify_url' => '',
  81. 'org_quality' => 0,
  82. 'order_time' => $order_time
  83. ];
  84. $mch_order = $order->mch_order();
  85. refill\util::push_queue_order($trans_mchid,$mch_order,ORDER_STATE_QUEUE);
  86. Model('refill_order')->add_detail($trans_mchid, $mch_order, $order_time, $params, ORDER_STATE_QUEUE);
  87. $state = refill\util::push_add($params);
  88. return $state;
  89. };
  90. $mchid = $order->mchid();
  91. $mch_order = $order->mch_order();
  92. [$success,$order_id,$errmsg] = refill\RefillFactory::instance()->success_order($order);
  93. if(!$success) {
  94. return false;
  95. }
  96. if(!$transfer_order($order)) {
  97. $order_canceler($order_id,$errmsg);
  98. return false;
  99. }
  100. refill\util::pop_queue_order($mchid, $mch_order, $order->order_time());
  101. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
  102. return true;
  103. }
  104. public function add($params)
  105. {
  106. $order = refill\order::from_parameters($params);
  107. $need_callback = !$order->first_commit();
  108. if($order->is_black()) {
  109. return $this->onEerror($order, $need_callback,'黑名单卡号');
  110. }
  111. $mchid = $order->mchid();
  112. $mch_order = $order->mch_order();
  113. $order_time = $order->order_time();
  114. $mod_refill = Model('refill_order');
  115. if($order->first_commit())
  116. {
  117. if($this->is_loop_order($order)) {
  118. return $this->onEerror($order, $need_callback,'疑似循环单,提前结束.');
  119. }
  120. else {
  121. refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
  122. $mod_refill->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
  123. }
  124. }
  125. 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);
  126. if ($this->canceled($mchid, $mch_order)) {
  127. refill\util::del_cancel_order($mchid,$mch_order);
  128. return $this->onEerror($order, $need_callback,'订单被拦截');
  129. }
  130. if (!$order->validate()) {
  131. return $this->onEerror($order, $need_callback,'空号或风险号');
  132. }
  133. if($this->need_intercept($order)) {
  134. return $this->onEerror($order, $need_callback,'转网,问题号码,运营商维护被拦截.');
  135. }
  136. if($this->successed($mchid,$mch_order)) {
  137. refill\util::del_next_order($mchid,$mch_order);
  138. $skip = true;
  139. } else {
  140. $skip = false;
  141. }
  142. [$org_quality, $quality] = refill\RefillFactory::instance()->find_quality($order, $skip);
  143. $order->set_quality($org_quality,$quality);
  144. if($quality == 0) {
  145. return $this->onEerror($order, $need_callback,'找不到合适质量的通道');
  146. }
  147. if($this->region_intercept($order)) {
  148. return $this->onEerror($order, $need_callback,'运营商维护被全局拦截.');
  149. }
  150. if($order->first_commit())
  151. {
  152. if (!$order->match_card_type()) {
  153. return $this->onEerror($order, $need_callback,'卡类型和产品类型不符.');
  154. }
  155. if(refill\util::merchant_debt_stoped($mchid)) {
  156. Log::record("merchant_debt_stoped mchid={$mchid} stoped=1",Log::DEBUG);
  157. return $this->onEerror($order, $need_callback,'米不够了.');
  158. } else {
  159. Log::record("merchant_debt_stoped mchid={$mchid} stoped=0",Log::DEBUG);
  160. }
  161. $mch_amount = refill\RefillFactory::instance()->mch_amount($order);
  162. if($mch_amount === false) {
  163. return $this->onEerror($order, $need_callback,'没有协商商品购买价格.');
  164. }
  165. else
  166. {
  167. if(util::onEventBeforeSubmit($order) === false) {
  168. return true;
  169. }
  170. refill\util::monitor_submit($order->mchid(), $order->spec(), $order->card_type(), $mch_amount,$order->order_time());
  171. refill\util::onEventSubmit($order);
  172. }
  173. }
  174. elseif($this->need_transfer($order) && $this->transfer($order)) {
  175. return true;
  176. }
  177. $reAdder = function(refill\order $order,$order_id,$order_time,$mod_refill)
  178. {
  179. $mch_order = $order->mch_order();
  180. $cur_quality = $order->cur_quality();
  181. [$org_quality, $quality] = refill\RefillFactory::instance()->find_quality($order, true);
  182. Log::record("reAdder mch_order=$mch_order org_quality=$org_quality cur_quality=$cur_quality quality=$quality.", Log::DEBUG);
  183. //非混充不跳
  184. if(!PolicyUtil::mixed_quality($org_quality)) {
  185. return true;
  186. }
  187. //没有跳成功
  188. if($quality === 0 || $quality === $cur_quality) {
  189. return true;
  190. }
  191. $order->set_quality($org_quality,$quality);
  192. if ($quality > 0)
  193. {
  194. if($order_id > 0) {
  195. $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['is_retrying' => 1]);
  196. }
  197. $params = $order->queue_params();
  198. if (!refill\util::push_add($params)) {
  199. $fError = true;
  200. } else {
  201. $fError = false;
  202. Log::record("reAdder mch_order=$mch_order org_quality=$org_quality quality=$quality succ.", Log::DEBUG);
  203. }
  204. }
  205. else {
  206. $fError = true;
  207. }
  208. return $fError;
  209. };
  210. [$errcode, $errmsg, $order_id, $neterr,$net_errno] = refill\RefillFactory::instance()->add($order);
  211. if($errcode !== true)
  212. {
  213. //遇到网络错误情况,查询处理
  214. if($errcode === refill\errcode::MERCHANT_REFILL_NETERROR && $neterr && refill\util::need_check($net_errno)) {
  215. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],15);
  216. $fError = false;
  217. }
  218. elseif (($errcode === refill\errcode::MERCHANT_REFILL_NETERROR && $neterr === false) || $errcode === refill\errcode::MERCHANT_REFILL_CHLIMIT) {
  219. $fError = $reAdder($order, $order_id, $order_time, $mod_refill);
  220. }
  221. else {
  222. $fError = true;
  223. }
  224. if ($fError) {
  225. return $this->onEerror($order,true, $errmsg);
  226. }
  227. }
  228. return true;
  229. }
  230. private function need_intercept(refill\order $order)
  231. {
  232. if($order->is_third()) return false;
  233. $mchid = $order->mchid();
  234. $card_state = $order->card_state();
  235. $is_transfer = $order->is_transfer();
  236. $card_type = $order->card_type();
  237. $card_no = $order->card_no();
  238. return refill\RefillFactory::instance()->need_intercept($mchid,$card_type,$card_state,$is_transfer,$card_no);
  239. }
  240. private function region_intercept(refill\order $order)
  241. {
  242. if($order->is_third() || $order->is_oil()) return false;
  243. $quality = $order->cur_quality();
  244. $card_type = $order->card_type();
  245. $reqion = $order->region_no();
  246. return refill\RefillFactory::instance()->region_intercept($quality,$card_type,$reqion);
  247. }
  248. private function canceled($mchid,$mch_order)
  249. {
  250. $order_state = refill\util::query_cancel_order($mchid,$mch_order);
  251. if($order_state == 1) {
  252. return true;
  253. } else {
  254. return false;
  255. }
  256. }
  257. private function successed($mchid,$mch_order)
  258. {
  259. $order_state = refill\util::query_next_order($mchid,$mch_order);
  260. if($order_state == 1) {
  261. return true;
  262. } else {
  263. return false;
  264. }
  265. }
  266. public function add_zero($params)
  267. {
  268. $order = refill\order::from_parameters($params);
  269. $mchid = $order->mchid();
  270. $mch_order = $order->mch_order();
  271. $order_time = $order->order_time();
  272. refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
  273. Model('refill_order')->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
  274. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($order);
  275. 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);
  276. $order_id = refill\RefillFactory::instance()->zero_order($order,"手动0元订单");
  277. refill\util::pop_queue_order($mchid, $mch_order, $order_time);
  278. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
  279. }
  280. public function notify($channel,$input)
  281. {
  282. return refill\RefillFactory::instance()->notify($channel,$input);
  283. }
  284. public function notify_merchant($order_id,$manual)
  285. {
  286. return refill\RefillFactory::instance()->notify_merchant($order_id,$manual);
  287. }
  288. public function query($order_id)
  289. {
  290. return refill\RefillFactory::instance()->query($order_id);
  291. }
  292. public function query_auto($order_id,$query_times)
  293. {
  294. return refill\RefillFactory::instance()->query_auto($order_id,$query_times);
  295. }
  296. public function query_net($order_id)
  297. {
  298. return refill\RefillFactory::instance()->query_net($order_id);
  299. }
  300. public function manual_success($order_id)
  301. {
  302. refill\RefillFactory::instance()->manual_success($order_id);
  303. }
  304. public function manual_cancel($order_id)
  305. {
  306. refill\RefillFactory::instance()->manual_cancel($order_id);
  307. }
  308. public function addthird($params)
  309. {
  310. $is_closed = function ($pcode) {
  311. $thrid_refill = Model('thrid_refill');
  312. $product = $thrid_refill->getProduct(['system_code' => $pcode,'opened' => 1]);
  313. return empty($product);
  314. };
  315. $order = refill\order::from_parameters($params);
  316. $need_callback = !$order->first_commit();
  317. if($is_closed($order->pcode())) {
  318. return $this->onEerror($order, $need_callback,"{$order->pcode()} has closed.");
  319. }
  320. $mchid = $order->mchid();
  321. $mch_order = $order->mch_order();
  322. $order_time = $order->order_time();
  323. $mod_refill = Model('refill_order');
  324. refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
  325. $mod_refill->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
  326. $mch_amount = refill\RefillFactory::instance()->mch_amount($order);
  327. if ($mch_amount === false) {
  328. return $this->onEerror($order, $need_callback,'没有协商商品购买价格.');
  329. } else {
  330. refill\util::monitor_submit($order->mchid(), $order->spec(), $order->card_type(), $mch_amount, $order->order_time());
  331. refill\util::onEventSubmit($order);
  332. }
  333. [$errcode, $errmsg, $order_id, $neterr,$net_errno] = refill\RefillFactory::instance()->add($order);
  334. if($errcode !== true)
  335. {
  336. if ($errcode === refill\errcode::MERCHANT_REFILL_NETERROR && $neterr && refill\util::need_check($net_errno)) {
  337. return QueueClient::async_push("QueryOrderNeterr", ['order_id' => $order_id], 15);
  338. } else {
  339. return $this->onEerror($order, $need_callback, $errmsg);
  340. }
  341. }
  342. return true;
  343. }
  344. }