proxy.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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->commit_times() === 0) and (util::onEventBeforeSubmit($order) === false)) {
  151. return true;
  152. }
  153. if($order->first_commit())
  154. {
  155. if (!$order->match_card_type()) {
  156. return $this->onEerror($order, $need_callback,'卡类型和产品类型不符.');
  157. }
  158. if(refill\util::merchant_debt_stoped($mchid)) {
  159. Log::record("merchant_debt_stoped mchid={$mchid} stoped=1",Log::DEBUG);
  160. return $this->onEerror($order, $need_callback,'米不够了.');
  161. } else {
  162. Log::record("merchant_debt_stoped mchid={$mchid} stoped=0",Log::DEBUG);
  163. }
  164. $mch_amount = refill\RefillFactory::instance()->mch_amount($order);
  165. if($mch_amount === false) {
  166. return $this->onEerror($order, $need_callback,'没有协商商品购买价格.');
  167. }
  168. else {
  169. refill\util::monitor_submit($order->mchid(), $order->spec(), $order->card_type(), $mch_amount,$order->order_time());
  170. refill\util::onEventSubmit($order);
  171. }
  172. }
  173. elseif($this->need_transfer($order) && $this->transfer($order)) {
  174. return true;
  175. }
  176. $reAdder = function(refill\order $order,$order_id,$order_time,$mod_refill)
  177. {
  178. $mch_order = $order->mch_order();
  179. $cur_quality = $order->cur_quality();
  180. [$org_quality, $quality] = refill\RefillFactory::instance()->find_quality($order, true);
  181. Log::record("reAdder mch_order=$mch_order org_quality=$org_quality cur_quality=$cur_quality quality=$quality.", Log::DEBUG);
  182. //非混充不跳
  183. if(!PolicyUtil::mixed_quality($org_quality)) {
  184. return true;
  185. }
  186. //没有跳成功
  187. if($quality === 0 || $quality === $cur_quality) {
  188. return true;
  189. }
  190. $order->set_quality($org_quality,$quality);
  191. if ($quality > 0)
  192. {
  193. if($order_id > 0) {
  194. $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['is_retrying' => 1]);
  195. }
  196. $params = $order->queue_params();
  197. if (!refill\util::push_add($params)) {
  198. $fError = true;
  199. } else {
  200. $fError = false;
  201. Log::record("reAdder mch_order=$mch_order org_quality=$org_quality quality=$quality succ.", Log::DEBUG);
  202. }
  203. }
  204. else {
  205. $fError = true;
  206. }
  207. return $fError;
  208. };
  209. [$errcode, $errmsg, $order_id, $neterr,$net_errno] = refill\RefillFactory::instance()->add($order);
  210. if($errcode !== true)
  211. {
  212. //遇到网络错误情况,查询处理
  213. if($errcode === refill\errcode::MERCHANT_REFILL_NETERROR && $neterr && refill\util::need_check($net_errno)) {
  214. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],15);
  215. $fError = false;
  216. }
  217. elseif (($errcode === refill\errcode::MERCHANT_REFILL_NETERROR && $neterr === false) || $errcode === refill\errcode::MERCHANT_REFILL_CHLIMIT) {
  218. $fError = $reAdder($order, $order_id, $order_time, $mod_refill);
  219. }
  220. else {
  221. $fError = true;
  222. }
  223. if ($fError) {
  224. return $this->onEerror($order,true, $errmsg);
  225. }
  226. }
  227. return true;
  228. }
  229. private function need_intercept(refill\order $order)
  230. {
  231. if($order->is_third()) return false;
  232. $mchid = $order->mchid();
  233. $card_state = $order->card_state();
  234. $is_transfer = $order->is_transfer();
  235. $card_type = $order->card_type();
  236. $card_no = $order->card_no();
  237. return refill\RefillFactory::instance()->need_intercept($mchid,$card_type,$card_state,$is_transfer,$card_no);
  238. }
  239. private function region_intercept(refill\order $order)
  240. {
  241. if($order->is_third() || $order->is_oil()) return false;
  242. $quality = $order->cur_quality();
  243. $card_type = $order->card_type();
  244. $reqion = $order->region_no();
  245. return refill\RefillFactory::instance()->region_intercept($quality,$card_type,$reqion);
  246. }
  247. private function canceled($mchid,$mch_order)
  248. {
  249. $order_state = refill\util::query_cancel_order($mchid,$mch_order);
  250. if($order_state == 1) {
  251. return true;
  252. } else {
  253. return false;
  254. }
  255. }
  256. private function successed($mchid,$mch_order)
  257. {
  258. $order_state = refill\util::query_next_order($mchid,$mch_order);
  259. if($order_state == 1) {
  260. return true;
  261. } else {
  262. return false;
  263. }
  264. }
  265. public function add_zero($params)
  266. {
  267. $order = refill\order::from_parameters($params);
  268. $mchid = $order->mchid();
  269. $mch_order = $order->mch_order();
  270. $order_time = $order->order_time();
  271. refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
  272. Model('refill_order')->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
  273. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($order);
  274. 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);
  275. $order_id = refill\RefillFactory::instance()->zero_order($order,"手动0元订单");
  276. refill\util::pop_queue_order($mchid, $mch_order, $order_time);
  277. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
  278. }
  279. public function notify($channel,$input)
  280. {
  281. return refill\RefillFactory::instance()->notify($channel,$input);
  282. }
  283. public function notify_merchant($order_id,$manual)
  284. {
  285. return refill\RefillFactory::instance()->notify_merchant($order_id,$manual);
  286. }
  287. public function query($order_id)
  288. {
  289. return refill\RefillFactory::instance()->query($order_id);
  290. }
  291. public function query_auto($order_id,$query_times)
  292. {
  293. return refill\RefillFactory::instance()->query_auto($order_id,$query_times);
  294. }
  295. public function query_net($order_id)
  296. {
  297. return refill\RefillFactory::instance()->query_net($order_id);
  298. }
  299. public function manual_success($order_id)
  300. {
  301. refill\RefillFactory::instance()->manual_success($order_id);
  302. }
  303. public function manual_cancel($order_id)
  304. {
  305. refill\RefillFactory::instance()->manual_cancel($order_id);
  306. }
  307. public function addthird($params)
  308. {
  309. $is_closed = function ($pcode) {
  310. $thrid_refill = Model('thrid_refill');
  311. $product = $thrid_refill->getProduct(['system_code' => $pcode,'opened' => 1]);
  312. return empty($product);
  313. };
  314. $order = refill\order::from_parameters($params);
  315. $need_callback = !$order->first_commit();
  316. if($is_closed($order->pcode())) {
  317. return $this->onEerror($order, $need_callback,"{$order->pcode()} has closed.");
  318. }
  319. $mchid = $order->mchid();
  320. $mch_order = $order->mch_order();
  321. $order_time = $order->order_time();
  322. $mod_refill = Model('refill_order');
  323. refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
  324. $mod_refill->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
  325. $mch_amount = refill\RefillFactory::instance()->mch_amount($order);
  326. if ($mch_amount === false) {
  327. return $this->onEerror($order, $need_callback,'没有协商商品购买价格.');
  328. } else {
  329. refill\util::monitor_submit($order->mchid(), $order->spec(), $order->card_type(), $mch_amount, $order->order_time());
  330. refill\util::onEventSubmit($order);
  331. }
  332. [$errcode, $errmsg, $order_id, $neterr,$net_errno] = refill\RefillFactory::instance()->add($order);
  333. if($errcode !== true)
  334. {
  335. if ($errcode === refill\errcode::MERCHANT_REFILL_NETERROR && $neterr && refill\util::need_check($net_errno)) {
  336. return QueueClient::async_push("QueryOrderNeterr", ['order_id' => $order_id], 15);
  337. } else {
  338. return $this->onEerror($order, $need_callback, $errmsg);
  339. }
  340. }
  341. return true;
  342. }
  343. }