proxy.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. require_once(BASE_HELPER_PATH . '/refill/policy/transfer_timeout.php');
  9. class proxy
  10. {
  11. private function is_loop_order(refill\order $order)
  12. {
  13. if($order->is_third() || $order->is_oil()) {
  14. return false;
  15. }
  16. $card_no = $order->card_no();
  17. $spec = $order->spec();
  18. return util::loop_order_inc($card_no,$spec);
  19. }
  20. private function onEerror(refill\order $order,$need_callback,$errmsg)
  21. {
  22. $mod_refill = Model('refill_order');
  23. $mchid = $order->mchid();
  24. $mch_order = $order->mch_order();
  25. $last_order_id = $order->last_order_id();
  26. $order_time = $order->order_time();
  27. $order->finish();
  28. if ($last_order_id === 0) {
  29. $order_id = refill\RefillFactory::instance()->zero_order($order, $errmsg);
  30. $last_order_id = $order_id;
  31. } else {
  32. $mod_refill->partition(util::part_refill($order_time))->edit($last_order_id, ['notify_time' => time(), 'notify_state' => 1, 'is_retrying' => 0]);
  33. $mod_order = Model('vr_order');
  34. $mod_order->partition(util::part_vr_order_time($order_time))->editOrder(['close_reason' => $errmsg], ['order_id' => $last_order_id]);
  35. }
  36. refill\util::pop_queue_order($mchid, $mch_order,$order_time);
  37. QueueClient::push("NotifyMerchantComplete", ['order_id' => $last_order_id, 'manual' => false]);
  38. if($need_callback)
  39. {
  40. Log::record("onError monitor_callback",Log::DEBUG);
  41. $mch_amount = refill\RefillFactory::instance()->mch_amount($order);
  42. Log::record("onError monitor_callback mchid=$mchid mch_amount=$mch_amount", Log::DEBUG);
  43. if($mch_amount == false)
  44. {
  45. $refill_info = $mod_refill->getOrderInfo(['order_id' => $last_order_id]);
  46. if(!empty($refill_info)) {
  47. $mch_amount = $refill_info['mch_amount'];
  48. } else {
  49. $mch_amount = 0.0;
  50. }
  51. }
  52. refill\util::monitor_callback($mchid, $order->spec(), $order->card_type(), $mch_amount, 0, false, $order->order_time());
  53. $refill_info = Model('refill_order')->partition(util::part_refill($order_time))->getOrderInfo(['order_id' => $last_order_id]);
  54. $order_info = Model('refill_order')->partition(util::part_notify())->getOrderInfo(['order_id' => $last_order_id]);
  55. util::onEventComplete($refill_info, $order_info, false);
  56. }
  57. return true;
  58. }
  59. private function need_transfer(refill\order $order)
  60. {
  61. $mchid = $order->mchid();
  62. $card_type = $order->card_type();
  63. $ret = refill\transfer::instance()->need_transfer($mchid,$card_type,$lowest_ratio);
  64. if($ret) {
  65. $cur_ratio = refill\RefillFactory::instance()->merchant_ratio($mchid);
  66. Log::record("mchid=$mchid cur_ratio=$cur_ratio lowest_ratio=$lowest_ratio",Log::DEBUG);
  67. return $lowest_ratio > $cur_ratio;
  68. }
  69. return false;
  70. }
  71. private function need_transfer_timeout(refill\order $order)
  72. {
  73. $mchid = $order->mchid();
  74. $mch_order = $order->mch_order();
  75. $order_state = util::query_queue_order($mchid,$mch_order);
  76. return $order_state == ORDER_STATE_TIMEOUT;
  77. }
  78. private function transfer(refill\order $order, $time_out = false): bool
  79. {
  80. $order_canceler = function ($order_id,$err_msg) {
  81. $logic_vr_order = Logic("vr_order");
  82. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  83. $logic_vr_order->changeOrderStateCancel($order_info, '', $err_msg, true, true);
  84. };
  85. $transfer_order = function (refill\order $order,$time_out)
  86. {
  87. $mchid = $order->mchid();
  88. if ($time_out) {
  89. [$trans_mchid, $adminid] = refill\transfer_timeout::instance()->transfer_info($mchid);
  90. } else {
  91. [$trans_mchid, $adminid] = refill\transfer::instance()->transfer_info($mchid);
  92. }
  93. if($trans_mchid == 0 || $adminid == 0) {
  94. return false;
  95. }
  96. $order_time = time();
  97. $params = ['mchid' => $trans_mchid,
  98. 'buyer_id' => $adminid,
  99. 'amount' => $order->spec(),
  100. 'card_no' => $order->card_no(),
  101. 'mch_order' => $order->mch_order(),
  102. 'notify_url' => '',
  103. 'org_quality' => 0,
  104. 'order_time' => $order_time
  105. ];
  106. $mch_order = $order->mch_order();
  107. refill\util::push_queue_order($trans_mchid,$mch_order,ORDER_STATE_QUEUE);
  108. Model('refill_order')->add_detail($trans_mchid, $mch_order, $order_time, $params, ORDER_STATE_QUEUE);
  109. $state = refill\util::push_add($params);
  110. return $state;
  111. };
  112. $mchid = $order->mchid();
  113. $mch_order = $order->mch_order();
  114. [$success,$order_id,$errmsg] = refill\RefillFactory::instance()->success_order($order);
  115. if(!$success) {
  116. return false;
  117. }
  118. if (!$transfer_order($order, $time_out)) {
  119. $order_canceler($order_id, $errmsg);
  120. return false;
  121. }
  122. $order->finish();
  123. refill\util::pop_queue_order($mchid, $mch_order, $order->order_time());
  124. if(!$time_out) {
  125. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
  126. }
  127. return true;
  128. }
  129. public function add($params)
  130. {
  131. $order = refill\order::from_parameters($params);
  132. $need_callback = !$order->first_commit();
  133. if($order->is_black()) {
  134. return $this->onEerror($order, $need_callback,'黑名单卡号');
  135. }
  136. $mchid = $order->mchid();
  137. $mch_order = $order->mch_order();
  138. $order_time = $order->order_time();
  139. $mod_refill = Model('refill_order');
  140. if($order->first_commit())
  141. {
  142. if($this->is_loop_order($order)) {
  143. return $this->onEerror($order, $need_callback,'疑似循环单,提前结束.');
  144. }
  145. else {
  146. refill\util::push_queue_order($mchid, $mch_order, ORDER_STATE_SEND);
  147. $mod_refill->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
  148. }
  149. }
  150. 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);
  151. if ($this->canceled($mchid, $mch_order)) {
  152. refill\util::del_cancel_order($mchid,$mch_order);
  153. return $this->onEerror($order, $need_callback,'订单被拦截');
  154. }
  155. if (!$order->validate()) {
  156. return $this->onEerror($order, $need_callback,'空号或风险号');
  157. }
  158. if($this->need_intercept($order)) {
  159. return $this->onEerror($order, $need_callback,'转网,问题号码,运营商维护被拦截.');
  160. }
  161. if($this->successed($mchid,$mch_order)) {
  162. refill\util::del_next_order($mchid,$mch_order);
  163. $skip = true;
  164. } else {
  165. $skip = false;
  166. }
  167. if (!$order->first_commit() and $this->need_transfer_timeout($order))
  168. {
  169. $cur_quality = $order->cur_quality();
  170. if(in_array($cur_quality,[2,3])) {
  171. $this->transfer($order, true);
  172. } else {
  173. $this->onEerror($order, $need_callback, "普充超时提前失败.");
  174. }
  175. return true;
  176. }
  177. [$org_quality, $quality] = refill\RefillFactory::instance()->find_quality($order, $skip);
  178. $order->set_quality($org_quality,$quality);
  179. if($quality == 0)
  180. {
  181. if($order->first_commit()) {
  182. return $this->onEerror($order, $need_callback,'找不到合适质量的通道');
  183. }
  184. elseif ($this->need_transfer($order) && $this->transfer($order)) {
  185. return true;
  186. }
  187. else {
  188. return $this->onEerror($order, $need_callback,'再也找不到可用质量的通道');
  189. }
  190. }
  191. if($this->region_intercept($order)) {
  192. return $this->onEerror($order, $need_callback,'目前该省份维护中.');
  193. }
  194. if($order->first_commit())
  195. {
  196. if (!$order->match_card_type()) {
  197. return $this->onEerror($order, $need_callback,'卡类型和产品类型不符.');
  198. }
  199. if(refill\util::merchant_debt_stoped($mchid)) {
  200. Log::record("merchant_debt_stoped mchid=$mchid stoped=1",Log::DEBUG);
  201. return $this->onEerror($order, $need_callback,'米不够了.');
  202. } else {
  203. Log::record("merchant_debt_stoped mchid=$mchid stoped=0",Log::DEBUG);
  204. }
  205. $mch_amount = refill\RefillFactory::instance()->mch_amount($order);
  206. if($mch_amount === false) {
  207. return $this->onEerror($order, $need_callback,'没有协商商品购买价格.');
  208. }
  209. else
  210. {
  211. if(util::onEventBeforeSubmit($order) === false) {
  212. return true;
  213. }
  214. refill\util::monitor_submit($order->mchid(), $order->spec(), $order->card_type(), $mch_amount,$order->order_time());
  215. refill\util::onEventSubmit($order);
  216. }
  217. }
  218. $reAdder = function(refill\order $order,$order_id,$order_time,$mod_refill)
  219. {
  220. $mch_order = $order->mch_order();
  221. $cur_quality = $order->cur_quality();
  222. [$org_quality, $quality] = refill\RefillFactory::instance()->find_quality($order, true);
  223. Log::record("reAdder mch_order=$mch_order org_quality=$org_quality cur_quality=$cur_quality quality=$quality.", Log::DEBUG);
  224. //非混充不跳
  225. if(!PolicyUtil::mixed_quality($org_quality)) {
  226. return true;
  227. }
  228. //没有跳成功
  229. if($quality <= 0 || $quality === $cur_quality) {
  230. return true;
  231. }
  232. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  233. if($order_id > 0) {
  234. $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['is_retrying' => 1]);
  235. }
  236. [$must_change,$delay_secs] = refill\RefillFactory::instance()->must_changeto_quality($order, $quality);
  237. if($must_change === false)
  238. {
  239. //可以延迟切换质量
  240. $filter = $order->filter();
  241. $filter->restart($order->cur_quality());
  242. $params = $order->queue_params();
  243. if (!refill\util::async_add($params,$delay_secs)) {
  244. $fError = true;
  245. } else {
  246. $fError = false;
  247. Log::record("reAdder mch_order=$mch_order org_quality=$org_quality quality=$quality succ.", Log::DEBUG);
  248. }
  249. }
  250. else
  251. {
  252. $order->set_quality($org_quality, $quality);
  253. $params = $order->queue_params();
  254. if (!refill\util::push_add($params)) {
  255. $fError = true;
  256. } else {
  257. $fError = false;
  258. Log::record("reAdder mch_order=$mch_order org_quality=$org_quality quality=$quality succ.", Log::DEBUG);
  259. }
  260. }
  261. return $fError;
  262. };
  263. [$errcode, $errmsg, $order_id, $neterr, $net_errno] = refill\RefillFactory::instance()->add($order);
  264. if($errcode !== true)
  265. {
  266. //遇到网络错误情况,查询处理
  267. if($errcode === refill\errcode::MERCHANT_REFILL_NETERROR) {
  268. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],15);
  269. $fError = false;
  270. }
  271. elseif ($errcode === refill\errcode::MERCHANT_REFILL_CHLIMIT) {
  272. $fError = $reAdder($order, $order_id, $order_time, $mod_refill);
  273. }
  274. elseif($errcode === refill\errcode::MERCHANT_REFILL_DBERROR) {
  275. Log::record('MERCHANT_REFILL_DBERROR', Log::DEBUG);
  276. //如果有数据库错误,靠这行来修复。
  277. Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  278. $fError = true;
  279. }
  280. else {
  281. $fError = true;
  282. }
  283. if ($fError) {
  284. return $this->onEerror($order,true, $errmsg);
  285. }
  286. } else {
  287. $fError = false;
  288. }
  289. //超时预回调,需要在下订单号对订单进行监控。
  290. if(!$fError and $order->first_commit() and $this->need_tmout_monitor($order)) {
  291. refill\transfer_timeout::instance()->monitor($order);
  292. }
  293. return true;
  294. }
  295. private function need_tmout_monitor(refill\order $order)
  296. {
  297. $mchid = $order->mchid();
  298. $ret = refill\transfer_timeout::instance()->need_monitor($mchid);
  299. return $ret;
  300. }
  301. private function need_intercept(refill\order $order)
  302. {
  303. if($order->is_third()) return false;
  304. $mchid = $order->mchid();
  305. $card_state = $order->card_state();
  306. $is_transfer = $order->is_transfer();
  307. $card_type = $order->card_type();
  308. $card_no = $order->card_no();
  309. return refill\RefillFactory::instance()->need_intercept($mchid,$card_type,$card_state,$is_transfer,$card_no);
  310. }
  311. private function region_intercept(refill\order $order)
  312. {
  313. if($order->is_third() || $order->is_oil()) return false;
  314. $quality = $order->cur_quality();
  315. $card_type = $order->card_type();
  316. $reqion = $order->region_no();
  317. return refill\RefillFactory::instance()->region_intercept($quality,$card_type,$reqion);
  318. }
  319. private function canceled($mchid,$mch_order)
  320. {
  321. $order_state = refill\util::query_cancel_order($mchid,$mch_order);
  322. if($order_state == 1) {
  323. return true;
  324. } else {
  325. return false;
  326. }
  327. }
  328. private function successed($mchid,$mch_order)
  329. {
  330. $order_state = refill\util::query_next_order($mchid,$mch_order);
  331. if($order_state == 1) {
  332. return true;
  333. } else {
  334. return false;
  335. }
  336. }
  337. public function add_zero($params)
  338. {
  339. $order = refill\order::from_parameters($params);
  340. $mchid = $order->mchid();
  341. $mch_order = $order->mch_order();
  342. $order_time = $order->order_time();
  343. $order->finish();
  344. refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
  345. Model('refill_order')->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
  346. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($order);
  347. 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);
  348. $order_id = refill\RefillFactory::instance()->zero_order($order,"手动0元订单");
  349. refill\util::pop_queue_order($mchid, $mch_order, $order_time);
  350. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
  351. }
  352. public function notify($channel,$input)
  353. {
  354. return refill\RefillFactory::instance()->notify($channel,$input);
  355. }
  356. public function notify_merchant($order_id,$manual)
  357. {
  358. return refill\RefillFactory::instance()->notify_merchant($order_id,$manual);
  359. }
  360. public function notify_merchant_early($mchid, $mch_order)
  361. {
  362. return refill\RefillFactory::instance()->notify_merchant_early($mchid, $mch_order);
  363. }
  364. public function query($order_id)
  365. {
  366. return refill\RefillFactory::instance()->query($order_id);
  367. }
  368. public function query_auto($order_id,$query_times)
  369. {
  370. return refill\RefillFactory::instance()->query_auto($order_id,$query_times);
  371. }
  372. public function query_net($order_id)
  373. {
  374. return refill\RefillFactory::instance()->query_net($order_id);
  375. }
  376. public function query_timeout($mchid, $mch_order)
  377. {
  378. return refill\RefillFactory::instance()->query_timeout($mchid, $mch_order);
  379. }
  380. public function manual_success($order_id)
  381. {
  382. refill\RefillFactory::instance()->manual_success($order_id);
  383. }
  384. public function manual_cancel($order_id)
  385. {
  386. refill\RefillFactory::instance()->manual_cancel($order_id);
  387. }
  388. public function addthird($params)
  389. {
  390. $is_closed = function ($pcode) {
  391. $thrid_refill = Model('thrid_refill');
  392. $product = $thrid_refill->getProduct(['system_code' => $pcode,'opened' => 1]);
  393. return empty($product);
  394. };
  395. $order = refill\order::from_parameters($params);
  396. $need_callback = !$order->first_commit();
  397. if($order->is_black()) {
  398. return $this->onEerror($order, $need_callback,'黑名单卡号');
  399. }
  400. if($is_closed($order->pcode())) {
  401. return $this->onEerror($order, $need_callback,"{$order->pcode()} has closed.");
  402. }
  403. $mchid = $order->mchid();
  404. $mch_order = $order->mch_order();
  405. $order_time = $order->order_time();
  406. $mod_refill = Model('refill_order');
  407. refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
  408. $mod_refill->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
  409. if ($this->canceled($mchid, $mch_order)) {
  410. refill\util::del_cancel_order($mchid,$mch_order);
  411. return $this->onEerror($order, $need_callback,'订单被拦截');
  412. }
  413. $mch_amount = refill\RefillFactory::instance()->mch_amount($order);
  414. if ($mch_amount === false) {
  415. return $this->onEerror($order, $need_callback,'没有协商商品购买价格.');
  416. } else {
  417. refill\util::monitor_submit($order->mchid(), $order->spec(), $order->card_type(), $mch_amount, $order->order_time());
  418. refill\util::onEventSubmit($order);
  419. }
  420. [$errcode, $errmsg, $order_id, $neterr,$net_errno] = refill\RefillFactory::instance()->add($order);
  421. if($errcode !== true)
  422. {
  423. if ($errcode === refill\errcode::MERCHANT_REFILL_NETERROR) {
  424. return QueueClient::async_push("QueryOrderNeterr", ['order_id' => $order_id], 15);
  425. } else {
  426. return $this->onEerror($order, $need_callback, $errmsg);
  427. }
  428. }
  429. return true;
  430. }
  431. }