proxy.php 14 KB

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