proxy.php 16 KB

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