123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- <?php
- declare(strict_types=0);
- use refill\PolicyUtil;
- use refill\util;
- use refill\Quality;
- require_once(BASE_ROOT_PATH . '/helper/model_helper.php');
- require_once(BASE_HELPER_PATH . '/refill/policy/transfer.php');
- class proxy
- {
- private function is_loop_order(refill\order $order)
- {
- if($order->is_third() || $order->is_oil()) {
- return false;
- }
- $card_no = $order->card_no();
- $spec = $order->spec();
- return util::loop_order_inc($card_no,$spec);
- }
- private function onEerror(refill\order $order,$need_callback,$errmsg)
- {
- $mod_refill = Model('refill_order');
- $mchid = $order->mchid();
- $mch_order = $order->mch_order();
- $last_order_id = $order->last_order_id();
- $order_time = $order->order_time();
- if ($last_order_id === 0) {
- $order_id = refill\RefillFactory::instance()->zero_order($order, $errmsg);
- $last_order_id = $order_id;
- } else {
- $mod_refill->partition(util::part_refill($order_time))->edit($last_order_id, ['notify_time' => time(), 'notify_state' => 1, 'is_retrying' => 0]);
- $mod_order = Model('vr_order');
- $mod_order->partition(util::part_vr_order_time($order_time))->editOrder(['close_reason' => $errmsg], ['order_id' => $last_order_id]);
- }
- refill\util::pop_queue_order($mchid, $mch_order,$order_time);
- QueueClient::push("NotifyMerchantComplete", ['order_id' => $last_order_id, 'manual' => false]);
- if($need_callback)
- {
- Log::record("onEerror monitor_callback",Log::DEBUG);
- $mch_amount = refill\RefillFactory::instance()->mch_amount($order);
- if($mch_amount == false) {
- refill\util::monitor_callback($mchid, $order->spec(), $order->card_type(), 0.0, 0, false, $order->order_time());
- } else {
- refill\util::monitor_callback($mchid, $order->spec(), $order->card_type(), $mch_amount, 0, false, $order->order_time());
- }
- $refill_info = Model('refill_order')->partition(util::part_refill($order_time))->getOrderInfo(['order_id' => $order_id]);
- $order_info = Model('refill_order')->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id]);
- util::onEventComplete($refill_info, $order_info, false);
- }
- return true;
- }
- private function need_transfer(refill\order $order)
- {
- if($order->match_ratio()) return false;
- $order_time = $order->order_time();
- $mchid = $order->mchid();
- $card_type = $order->card_type();
- $ret = refill\transfer::instance()->need_transfer($mchid,$order_time,$card_type);
- return $ret;
- }
- private function transfer(refill\order $order) : bool
- {
- $order_canceler = function ($order_id,$err_msg) {
- $logic_vr_order = Logic("vr_order");
- $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
- $logic_vr_order->changeOrderStateCancel($order_info, '', $err_msg, true, true);
- };
- $transfer_order = function (refill\order $order)
- {
- $mchid = $order->mchid();
- [$trans_mchid,$adminid] = refill\transfer::instance()->transfer_info($mchid);
- if($trans_mchid == 0 || $adminid == 0) {
- return false;
- }
- $order_time = time();
- $params = ['mchid' => $trans_mchid,
- 'buyer_id' => $adminid,
- 'amount' => $order->spec(),
- 'card_no' => $order->card_no(),
- 'mch_order' => $order->mch_order(),
- 'notify_url' => '',
- 'org_quality' => 0,
- 'order_time' => $order_time
- ];
- $mch_order = $order->mch_order();
- refill\util::push_queue_order($trans_mchid,$mch_order,ORDER_STATE_QUEUE);
- Model('refill_order')->add_detail($trans_mchid, $mch_order, $order_time, $params, ORDER_STATE_QUEUE);
- $state = refill\util::push_add($params);
- return $state;
- };
- $mchid = $order->mchid();
- $mch_order = $order->mch_order();
- [$success,$order_id,$errmsg] = refill\RefillFactory::instance()->success_order($order);
- if(!$success) {
- return false;
- }
- if(!$transfer_order($order)) {
- $order_canceler($order_id,$errmsg);
- return false;
- }
- refill\util::pop_queue_order($mchid, $mch_order, $order->order_time());
- QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
- return true;
- }
- public function add($params)
- {
- $order = refill\order::from_parameters($params);
- $need_callback = !$order->first_commit();
- if($order->is_black()) {
- return $this->onEerror($order, $need_callback,'黑名单卡号');
- }
- $mchid = $order->mchid();
- $mch_order = $order->mch_order();
- $order_time = $order->order_time();
- $mod_refill = Model('refill_order');
- if($order->first_commit())
- {
- if($this->is_loop_order($order)) {
- return $this->onEerror($order, $need_callback,'疑似循环单,提前结束.');
- }
- else {
- refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
- $mod_refill->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
- }
- }
- 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);
- if ($this->canceled($mchid, $mch_order)) {
- refill\util::del_cancel_order($mchid,$mch_order);
- return $this->onEerror($order, $need_callback,'订单被拦截');
- }
- if (!$order->validate()) {
- return $this->onEerror($order, $need_callback,'空号或风险号');
- }
- if($this->need_intercept($order)) {
- return $this->onEerror($order, $need_callback,'转网,问题号码,运营商维护被拦截.');
- }
- if($this->successed($mchid,$mch_order)) {
- refill\util::del_next_order($mchid,$mch_order);
- $skip = true;
- } else {
- $skip = false;
- }
- [$org_quality, $quality] = refill\RefillFactory::instance()->find_quality($order, $skip);
- $order->set_quality($org_quality,$quality);
- if($quality == 0) {
- return $this->onEerror($order, $need_callback,'找不到合适质量的通道');
- }
- if($this->region_intercept($order)) {
- return $this->onEerror($order, $need_callback,'运营商维护被全局拦截.');
- }
- if(($order->commit_times() === 0) and (util::onEventBeforeSubmit($order) === false)) {
- return true;
- }
- if($order->first_commit())
- {
- if (!$order->match_card_type()) {
- return $this->onEerror($order, $need_callback,'卡类型和产品类型不符.');
- }
- if(refill\util::merchant_debt_stoped($mchid)) {
- Log::record("merchant_debt_stoped mchid={$mchid} stoped=1",Log::DEBUG);
- return $this->onEerror($order, $need_callback,'米不够了.');
- } else {
- Log::record("merchant_debt_stoped mchid={$mchid} stoped=0",Log::DEBUG);
- }
- $mch_amount = refill\RefillFactory::instance()->mch_amount($order);
- if($mch_amount === false) {
- return $this->onEerror($order, $need_callback,'没有协商商品购买价格.');
- }
- else {
- refill\util::monitor_submit($order->mchid(), $order->spec(), $order->card_type(), $mch_amount,$order->order_time());
- refill\util::onEventSubmit($order);
- }
- }
- elseif($this->need_transfer($order) && $this->transfer($order)) {
- return true;
- }
- $reAdder = function(refill\order $order,$order_id,$order_time,$mod_refill)
- {
- $mch_order = $order->mch_order();
- $cur_quality = $order->cur_quality();
- [$org_quality, $quality] = refill\RefillFactory::instance()->find_quality($order, true);
- Log::record("reAdder mch_order=$mch_order org_quality=$org_quality cur_quality=$cur_quality quality=$quality.", Log::DEBUG);
- //非混充不跳
- if(!PolicyUtil::mixed_quality($org_quality)) {
- return true;
- }
- //没有跳成功
- if($quality === 0 || $quality === $cur_quality) {
- return true;
- }
- $order->set_quality($org_quality,$quality);
- if ($quality > 0)
- {
- if($order_id > 0) {
- $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['is_retrying' => 1]);
- }
- $params = $order->queue_params();
- if (!refill\util::push_add($params)) {
- $fError = true;
- } else {
- $fError = false;
- Log::record("reAdder mch_order=$mch_order org_quality=$org_quality quality=$quality succ.", Log::DEBUG);
- }
- }
- else {
- $fError = true;
- }
- return $fError;
- };
- [$errcode, $errmsg, $order_id, $neterr,$net_errno] = refill\RefillFactory::instance()->add($order);
- if($errcode !== true)
- {
- //遇到网络错误情况,查询处理
- if($errcode === refill\errcode::MERCHANT_REFILL_NETERROR && $neterr && refill\util::need_check($net_errno)) {
- QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],15);
- $fError = false;
- }
- elseif (($errcode === refill\errcode::MERCHANT_REFILL_NETERROR && $neterr === false) || $errcode === refill\errcode::MERCHANT_REFILL_CHLIMIT) {
- $fError = $reAdder($order, $order_id, $order_time, $mod_refill);
- }
- else {
- $fError = true;
- }
- if ($fError) {
- return $this->onEerror($order,true, $errmsg);
- }
- }
- return true;
- }
- private function need_intercept(refill\order $order)
- {
- if($order->is_third()) return false;
- $mchid = $order->mchid();
- $card_state = $order->card_state();
- $is_transfer = $order->is_transfer();
- $card_type = $order->card_type();
- $card_no = $order->card_no();
- return refill\RefillFactory::instance()->need_intercept($mchid,$card_type,$card_state,$is_transfer,$card_no);
- }
- private function region_intercept(refill\order $order)
- {
- if($order->is_third() || $order->is_oil()) return false;
- $quality = $order->cur_quality();
- $card_type = $order->card_type();
- $reqion = $order->region_no();
- return refill\RefillFactory::instance()->region_intercept($quality,$card_type,$reqion);
- }
- private function canceled($mchid,$mch_order)
- {
- $order_state = refill\util::query_cancel_order($mchid,$mch_order);
- if($order_state == 1) {
- return true;
- } else {
- return false;
- }
- }
- private function successed($mchid,$mch_order)
- {
- $order_state = refill\util::query_next_order($mchid,$mch_order);
- if($order_state == 1) {
- return true;
- } else {
- return false;
- }
- }
- public function add_zero($params)
- {
- $order = refill\order::from_parameters($params);
- $mchid = $order->mchid();
- $mch_order = $order->mch_order();
- $order_time = $order->order_time();
- refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
- Model('refill_order')->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
- [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($order);
- 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);
- $order_id = refill\RefillFactory::instance()->zero_order($order,"手动0元订单");
- refill\util::pop_queue_order($mchid, $mch_order, $order_time);
- QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
- }
- public function notify($channel,$input)
- {
- return refill\RefillFactory::instance()->notify($channel,$input);
- }
- public function notify_merchant($order_id,$manual)
- {
- return refill\RefillFactory::instance()->notify_merchant($order_id,$manual);
- }
- public function query($order_id)
- {
- return refill\RefillFactory::instance()->query($order_id);
- }
- public function query_auto($order_id,$query_times)
- {
- return refill\RefillFactory::instance()->query_auto($order_id,$query_times);
- }
- public function query_net($order_id)
- {
- return refill\RefillFactory::instance()->query_net($order_id);
- }
- public function manual_success($order_id)
- {
- refill\RefillFactory::instance()->manual_success($order_id);
- }
- public function manual_cancel($order_id)
- {
- refill\RefillFactory::instance()->manual_cancel($order_id);
- }
- public function addthird($params)
- {
- $is_closed = function ($pcode) {
- $thrid_refill = Model('thrid_refill');
- $product = $thrid_refill->getProduct(['system_code' => $pcode,'opened' => 1]);
- return empty($product);
- };
- $order = refill\order::from_parameters($params);
- $need_callback = !$order->first_commit();
- if($is_closed($order->pcode())) {
- return $this->onEerror($order, $need_callback,"{$order->pcode()} has closed.");
- }
- $mchid = $order->mchid();
- $mch_order = $order->mch_order();
- $order_time = $order->order_time();
- $mod_refill = Model('refill_order');
- refill\util::push_queue_order($mchid,$mch_order,ORDER_STATE_SEND);
- $mod_refill->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_SEND]);
- $mch_amount = refill\RefillFactory::instance()->mch_amount($order);
- if ($mch_amount === false) {
- return $this->onEerror($order, $need_callback,'没有协商商品购买价格.');
- } else {
- refill\util::monitor_submit($order->mchid(), $order->spec(), $order->card_type(), $mch_amount, $order->order_time());
- refill\util::onEventSubmit($order);
- }
- [$errcode, $errmsg, $order_id, $neterr,$net_errno] = refill\RefillFactory::instance()->add($order);
- if($errcode !== true)
- {
- if ($errcode === refill\errcode::MERCHANT_REFILL_NETERROR && $neterr && refill\util::need_check($net_errno)) {
- return QueueClient::async_push("QueryOrderNeterr", ['order_id' => $order_id], 15);
- } else {
- return $this->onEerror($order, $need_callback, $errmsg);
- }
- }
- return true;
- }
- }
|