mPolicy = $policy; } public function allow($mchid,$card_type,$amount,$quality) { return $this->mPolicy->allow($mchid,$card_type,$amount,$quality); } public function goods() { return $this->mPolicy->goods(); } public function load() { $this->mPolicy->load(); } public function find_quality(order $order,$skip = false) { return $this->mPolicy->find_quality($order,$skip); } //return [是否必须切换质量, 等待时间(如果必须,等待时间为0,如果不必是延迟秒数)] public function must_changeto_quality(order $order, $quality) { return $this->mPolicy->must_changeto_quality($order, $quality); } private function third_can_retry(order $order) : array { return $this->mPolicy->third_retry($order); } public function notify($chname, $input) { $caller = $this->mPolicy->getCaller($chname); if($caller === false) { return false; } elseif ($caller->verify($input)) { $values = $caller->notify($input); if(count($values) == 4) { //老的接口返回四个参数 [$order_id, $success, $can_try, $need_handle] = $values; $official_sn = false; } else { [$order_id, $success, $can_try, $need_handle, $official_sn] = $values; } if (!$need_handle) { return true; } if ($order_id !== false) { return $this->proc_notify($order_id, $success, $can_try, $chname, $input, $official_sn); } else { Log::record("$chname callback 系统无此订单ID.", Log::WARING); } } else { $orgdata = json_encode($input); Log::record("$chname 签名失败:input=$orgdata",Log::WARING); } return true; } private function risksn_check($refill_info, $order_info, $org_official_sn) { $in_exclude = function ($store_id) { $store_id = intval($store_id); if($store_id <= 0) { return false; } global $config; $risk_official_sn = $config['risk_official_sn'] ?? []; $exclude_stores = $risk_official_sn['exclude_stores'] ?? []; return in_array($store_id,$exclude_stores); }; $card_type = intval($refill_info['card_type']); $chk_types = [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard]; $store_id = intval($order_info['store_id']); if($in_exclude($store_id)) { return false; } if(!in_array($card_type,$chk_types)) { return false; } if ($org_official_sn !== false) { $length = strlen($org_official_sn); if($length < 6 or $length > 48) { return true; } } $quality = intval($refill_info['quality']); $official_sn = $refill_info['official_sn']; if($quality === Quality::Normal && empty($official_sn)) { return true; } $start_with = function ($haystack, $needle) { $length = strlen($needle); return (substr($haystack, 0, $length) === $needle); }; $headers = ['SP', 'J98', 'WX', '110110']; //, '11010336J' '0095','ZF2023' $spcheker = function () use ($start_with, $headers, $official_sn) { $official_sn = strtoupper($official_sn); foreach ($headers as $header) { if ($start_with($official_sn, $header)) { return true; } } return false; }; return $spcheker(); } private function proc_notify($order_id, $success, $can_try, $chname, $input = [], $official_sn = false) { $mod_order = Model('vr_order'); $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id]); $order_state = intval($order_info['order_state']); if ($order_state == ORDER_STATE_PAY) { if(!empty($input)) { Log::record(__METHOD__ . " recv notify when order_state=ORDER_STATE_PAY" ,Log::DEBUG); Swoole\Coroutine::sleep(5); util::push_notify($chname,$input); } return false; } if ($order_state != ORDER_STATE_SEND) { return false; } $logic_vr_order = Logic("vr_order"); $mod_refill = Model('refill_order'); try { $tran = new trans_wapper($mod_order,'notify change order state trans'); $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id], '*', true, true); $order_state = intval($order_info['order_state']); if ($order_state != ORDER_STATE_SEND) { $tran->commit(); return false; } $refill_info = $mod_refill->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0]); if(empty($refill_info)) { $tran->commit(); return false; } $quality = intval($refill_info['quality']); $card_type = intval($refill_info['card_type']); $spec = intval($refill_info['refill_amount']); $mchid = intval($refill_info['mchid']); $mch_order = $refill_info['mch_order']; $order_time = intval($refill_info['order_time']); if ($success) { $params = ['store_id' => $order_info['store_id'], 'channel_name' => $refill_info['channel_name'], 'order_sn' => $refill_info['order_sn'], 'order_id' => $refill_info['order_id'], 'official_sn' => $refill_info['official_sn']]; if ($this->risksn_check($refill_info, $order_info, $official_sn)) { $tran->commit(); QueueClient::async_push("OnRiskSN", $params, 1); return false; } else { QueueClient::async_push("OnRefillSuccess", $params, 1); } } $commit_time = intval($refill_info['commit_time']); $period = time() - $commit_time; util::monitor_notify($chname,$spec,$card_type,$refill_info['channel_amount'],$period,$success,$commit_time,$refill_info['mch_amount']); util::onEventNotify($refill_info,$order_info,$success); if ($success) { $logic_vr_order->changeOrderStateSuccess($order_id,true); $tran->commit(); util::onOrderSuccess($refill_info,$order_info); } elseif ($can_try) { $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,正在重试", true, true); $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['is_retrying' => 1,'notify_time' => time()]); $tran->commit(); if ($card_type == mtopcard\ThirdRefillCard) { $thrid_refill = Model('thrid_refill'); $third_info = $thrid_refill->getThird($order_id); $third_info = $thrid_refill->trans_order($third_info); } else { $third_info = []; } $order = order::from_db($refill_info, $order_info, $third_info); $ch_filter = $order->filter(); $ch_filter->notify($refill_info['quality'], $chname); [$can_retry,$params] = $this->retry($order); if ($can_retry) { if($order->is_third()) { if(util::push_addthird($params)) { return true; } } elseif(util::push_add($params)) { return true; } } else { $order->finish(); } } else { $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,不可重试.",true,true); $tran->commit(); } $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['notify_time' => time(), 'is_retrying' => 0,'notify_state' => 1]); util::monitor_callback($mchid, $spec, $card_type, $refill_info['mch_amount'], $refill_info['channel_amount'], $success, $order_time); util::onEventComplete($refill_info, $order_info, $success); util::pop_queue_order($mchid,$mch_order,$order_time); QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]); return true; } catch (Exception $ex) { $tran->rollback(); Log::record("Error:" . $ex->getMessage(), Log::ERR); Swoole\Coroutine::sleep(5); util::push_notify($chname,$input); return false; } } private function retry(order $order) { $trace = new scope_trace(__METHOD__); if($order->is_third()) { [$can_retry,$next_pcode] = $this->third_can_retry($order); if($can_retry) { $params = $order->third_requeue_params($next_pcode); return [$can_retry,$params]; } else { return [false,null]; } } $params = $order->queue_params(); return [true,$params]; } public function success_order(order $order) : array { $last_orderid = $order->last_order_id(); $mchid = $order->mchid(); $order_time = $order->order_time(); try { $minfo = new member_info($order->buyer_id()); $calc = $this->getMerchantPriceCalc($order); $mch_price = $calc->calc_vgoods_price([]); $mch_amount = $mch_price * $order->quantity(); } catch (Exception $ex) { return [false,$last_orderid,'没有协商商品价格']; } $available = $minfo->available_predeposit(); if ($mch_amount > $available) { return [false, $last_orderid,"余额不足支付订单"]; } $goods_id = ZERO_GOODS_ID; $channel_name = 'cbproxy'; $channel_amount = $mch_amount; $mod_refill = Model('refill_order'); [$order_success, $order_id, $order_sn] = $this->create_order($order, $goods_id, $minfo, $calc, $channel_name, $channel_amount, $mch_amount); if(!$order_success) { return [false,$last_orderid,'订单创建失败']; } else { $logic_vr_order = Logic("vr_order"); $logic_vr_order->changeOrderStateSuccess($order_id,true); $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['commit_time' => time(),'notify_time' => time(), 'is_retrying' => 0,'notify_state' => 1]); util::monitor_callback($mchid, $order->spec(), $order->card_type(), $mch_amount, $channel_amount, true, $order->order_time()); $refill_info = $mod_refill->partition(util::part_refill($order_time))->getOrderInfo(['order_id' => $order_id]); $mod_order = Model('vr_order'); $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id]); util::onEventComplete($refill_info, $order_info, true); return [true,$order_id,'']; } } public function zero_order(order $order,$errmsg='') { $buyer_id = $order->buyer_id(); $minfo = new member_info($order->buyer_id()); $calc = new ZeroMerchantPrice($order); $mch_amount = $calc->calc_vgoods_price([]); $input['goods_id'] = ZERO_GOODS_ID; $input['quantity'] = 1; //数量 $input['buyer_phone'] = $minfo->mobile(); $input['buyer_name'] = $minfo->truename(); $input['buyer_msg'] = $_POST['buyer_msg'] ?? ''; $input['order_from'] = 1; $input['pd_pay'] = true; $logic_buy_virtual = Logic('buy_virtual'); $result = $logic_buy_virtual->buyStep3($input, $buyer_id, [$calc, 'calc_vorder_amount'], true, true); $mod_refill = Model('refill_order'); if ($result['state'] === true) { $order_sn = $result['data']['order_sn']; $order_id = $result['data']['order_id']; $logic_vr_order = Logic("vr_order"); $order_info = Model('vr_order')->partition(util::part_vr_create())->getOrderInfo(['order_id' => $order_id]); $logic_vr_order->changeOrderStateCancel($order_info, '', '无法下单创建0元订单',true,true); $mch_order = $order->mch_order(); if (empty($mch_order)) { $order->set_mchorder($order_sn); } $thrid_refill = Model('thrid_refill'); if($order->is_third()) { $product = $thrid_refill->getProduct(['system_code' => $order->pcode()]); $refill_amount = $product['refill_amount']; } else { $refill_amount = $order->spec(); } //虚拟订单表信息扩展 $orderext = $order->ZeroRefillParams($order_id,$order_sn,$refill_amount,$mch_amount,'',0,$errmsg); $order->commit_times_inc(); $mod_refill->add_refill($orderext); if($order->is_third()) { $thrid_refill = Model('thrid_refill'); $ext = $order->third_extparams($order_id,$order_sn); $thrid_refill->addExt($ext); } return $order_id; } else { return 0; } } public function mch_amount(order $order) { $mchid = $order->mchid(); $org_quality = $order->org_quality(); try { $calc = $this->getMerchantPriceCalc($order); $mch_price = $calc->calc_vgoods_price([]); $mch_amount = $mch_price * $order->quantity(); return $mch_amount; } catch (Exception $ex) { $spec = $order->spec(); $card_type = $order->card_type(); $cur_quality = $order->cur_quality(); Log::record("onError mchid=$mchid org_quality=$org_quality cur_qua=$cur_quality spec=$spec card_type=$card_type :" . $ex->getMessage(), Log::DEBUG); return false; } } //返回值:[ 错误码,错误信息,订单ID,是否是网络错误] //说明:错误码为true 表示成功 // 其它情况,则需要判断订单ID public function add(order $order) { $last_orderid = $order->last_order_id(); $order_time = $order->order_time(); $mchid = $order->mchid(); $mch_order = $order->mch_order(); [$providers, $overload] = $this->mPolicy->find_providers($order); $ch_times = $this->mPolicy->get_times(); $chfilters = $order->filter(); $cur_quality = $order->cur_quality(); $providers = $chfilters->getProviders($cur_quality, $providers, $ch_times); if (empty($providers)) { Log::record("canot find any providers", Log::DEBUG); return [errcode::MERCHANT_REFILL_CHLIMIT, "匹配不到任何通道", $last_orderid, false, 0]; } try { $minfo = new member_info($order->buyer_id()); $org_quality = $order->org_quality(); $calc = $this->getMerchantPriceCalc($order); $mch_price = $calc->calc_vgoods_price([]); $mch_amount = $mch_price * $order->quantity(); } catch (Exception $ex) { return [errcode::MERCHANT_PRICE_UNSETTING, "没有协商商品价格",$last_orderid,false,0]; } $available = $minfo->available_predeposit(); if ($mch_amount > $available) { $errmsg = "余额不足=$available"; Log::record($errmsg, Log::DEBUG); return [errcode::MERCHANT_SHORT_MONEY, $errmsg, $last_orderid, false, 0]; } foreach ($providers as $provider) { $channel_name = $provider->name(); $can_commit = util::onEventBeforeCommit($order, $channel_name); if ($can_commit === false) { continue; } if($this->mPolicy->is_over_chspeed($channel_name)) { Log::record("chspeed is over:$channel_name",Log::DEBUG); continue; } [$goods_id, $ch_price] = $provider->goods($order->cur_quality(), $order->spec(), $order->card_type(), $order->region_no(), $order->thrid_params()); if ($goods_id <= 0) continue; if($ch_price > $mch_price) { //非组合通道,以原始质量算价格. //通道价格大于客户价格,换通道. if($order->is_third()) { $third_mixed = $this->mPolicy->third_mixed($order->mchid(),$order->pcode()); if(!$third_mixed) { continue; } } elseif(PolicyUtil::mixed_quality($org_quality)) { if (!$order->can_over_price()) { Log::record("can_over_price is false", Log::DEBUG); continue; } else { Log::record("can_over_price is true", Log::DEBUG); } } else { continue; } } $mod_refill = Model('refill_order'); $channel_amount = $ch_price * $order->quantity(); [$order_success, $order_id, $order_sn] = $this->create_order($order, $goods_id, $minfo, $calc, $channel_name, $channel_amount, $mch_amount); $last_orderid = $order_id; if(!$order_success) { return [errcode::MERCHANT_REFILL_DBERROR, "充值失败", $last_orderid, false, 0]; } $start = microtime(true); $net_errno = ""; $params = $order->channel_params($order_id, $order_sn, $goods_id, $ch_price); $card_no = $order->card_no(); $card_type = $order->card_type(); $spec = $order->spec(); $quality = $order->cur_quality(); [$state, $errmsg, $neterr] = $provider->add($card_no, $card_type, $spec, $params, $net_errno); Log::record(sprintf(" %s add request time=%.6f: state=$state neterr=$neterr,net_errno=$net_errno", $channel_name,microtime(true) - $start), Log::DEBUG); if ($state) { $commit_time = time(); $chfilters->add($cur_quality,$channel_name,true); util::monitor_commit($channel_name, $spec, $card_type, $channel_amount, $commit_time); util::onEventCommit($order,$channel_name); $trade_no = $errmsg; $refill_type = $provider->refill_type(); $vr_part = util::part_vr_order_time($order_time); //api回调模式 if ($refill_type == 'api') { $logic_vr_order = Logic("vr_order"); $logic_vr_order->changeOrderStateSend($order_id, true, $vr_part); } //抢单子模式 elseif($refill_type == 'fetch') { $logic_vr_order = Logic("vr_order"); $logic_vr_order->changeOrderStateSend($order_id, true, $vr_part); $order_info = Model('vr_order')->partition($vr_part)->getOrderInfo(['order_id' => $order_id]); $mod_fetch_order = Model('fetch_order'); $fetch_datas = ['order_id' => $order_id, 'order_sn' => $order_sn, 'store_id' => $order_info['store_id'],'channel_name' => $channel_name, 'fetch_status' => 1, 'card_no' => $card_no, 'card_type' => $card_type, 'refill_amount' => $spec, 'add_time' => time()]; $mod_fetch_order->add($fetch_datas); } else { Log::record("Err refill_type = $refill_type",Log::ERR); } $data = ['commit_time' => $commit_time, 'ch_trade_no' => $trade_no]; $mod_refill->partition(util::part_refill($order_time))->edit($order_id, $data); //主动查询模式。如果对方没有回调能力,则启动主动查询. if($provider->callback() === false) { QueueClient::async_push("QueryAutoRefillState",['order_id' => $order_id,'query_times' => 0],3); } return [true, '', $last_orderid, false, 0]; } else { $chfilters->add($cur_quality, $channel_name, false); if(!empty($neterr) && util::need_check($net_errno)) { $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['commit_time' => time(),'neterr' => 1,'err_msg' => "neterr=$net_errno"]); util::monitor_netchk($channel_name,false); util::onEventNeterror($order,$channel_name); return [errcode::MERCHANT_REFILL_NETERROR, "充值失败", $last_orderid, $neterr, $net_errno]; } //可以再提单 Log::record("channel:$channel_name err:$errmsg"); $logic_vr_order = Logic("vr_order"); $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]); $logic_vr_order->changeOrderStateCancel($order_info, '', "调用{$channel_name}接口失败",true,true); if(!is_string($errmsg)) { $errmsg = "$errmsg"; } $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['commit_time' => time(), 'err_msg' => $errmsg]); } } return [errcode::MERCHANT_REFILL_CHLIMIT, "无可用通道", $last_orderid, false, 0]; } private function create_order(order $order, $goods_id, $minfo, $calc, $ch_name, $ch_amount, $mch_amount) { $refill_creater = function (order $order,$last_orderid,$order_id,$order_sn,$mod_refill) use($ch_name, $ch_amount,$mch_amount) { try { if($last_orderid > 0) { $order_time = $order->order_time(); $mod_refill->partition(util::part_refill($order_time))->edit($last_orderid, ['inner_status' => 1]); } $order->set_last_orderid($order_id); $thrid_refill = Model('thrid_refill'); if($order->is_third()) { $product = $thrid_refill->getProduct(['system_code' => $order->pcode(),'opened' => 1]); $refill_amount = $product['refill_amount']; } else { $refill_amount = $order->spec(); } if(empty($order->mch_order())) { $order->set_mchorder($order_sn); } $order->commit_times_inc(); $orderext = $order->refill_params($order_id, $order_sn, $refill_amount, $ch_name, $ch_amount, $mch_amount); $fInsert = $mod_refill->add_refill($orderext); if($order->is_third()) { $ext = $order->third_extparams($order_id,$order_sn); $thrid_refill->addExt($ext); } if(!$fInsert) { return false; } else { return true; } } catch (Exception $ex) { Log::record($ex->getMessage(),Log::ERR); return false; } }; $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); }; try { $logic_buy_virtual = Logic('buy_virtual'); $mod_refill = Model('refill_order'); $input['goods_id'] = $goods_id; $input['quantity'] = $order->quantity(); $input['buyer_phone'] = $minfo->mobile(); $input['buyer_name'] = $minfo->truename(); $input['buyer_msg'] = $_POST['buyer_msg'] ?? ''; $input['order_from'] = 1; $input['pd_pay'] = true; $start = microtime(true); $result = $logic_buy_virtual->buyStep3($input, $order->buyer_id(), [$calc, 'calc_vorder_amount'], true, true); if ($result['state'] === true) { $order_sn = $result['data']['order_sn']; $order_id = $result['data']['order_id']; $last_orderid = $order->last_order_id(); $fSuccess = $refill_creater($order, $last_orderid, $order_id, $order_sn, $mod_refill); if(!$fSuccess) { Log::record("refill_creater fail: order_sn=$order_sn", Log::ERR); $order_canceler($order_id,'refill_order 记录创建失败'); return [false, $order_id, $order_sn]; } if (!$this->pay_completed($order_sn)) { $order_canceler($order_id,'预存款不足以支付该订单'); return [false, $order_id, $order_sn]; } Log::record(sprintf(__METHOD__ . " request time=%.6f", microtime(true) - $start), Log::DEBUG); return [true, $order_id, $order_sn]; } else { Log::record(__METHOD__ . " fail:buyStep3 err msg={$result['msg']}", Log::ERR); return [false, 0, '']; } } catch (Exception $ex) { Log::record($ex->getMessage(), Log::ERR); return [false, 0, '']; } } private function pay_completed($order_sn) { $logic_payment = Logic('payment'); $order = $logic_payment->getVrOrderInfo($order_sn,'',true); $api_pay_amount = $order['data']['api_pay_amount']; return ($api_pay_amount == ncPriceFormat(0.0000)); } public function notify_merchant($order_id,$manual) { if ($order_id <= 0) { return [false, "订单ID小于0"]; } $mod_order = Model('vr_order'); $mod_refill = Model('refill_order'); if ($manual) { $part = ''; } else { $part = util::part_notify(); } $order_info = $mod_order->partition($part)->getOrderInfo(['order_id' => $order_id]); $refill_info = $mod_refill->partition($part)->getOrderInfo(['order_id' => $order_id,'inner_status' => 0,'is_retrying' => 0]); if (empty($order_info) || empty($refill_info)) { return [false, "无此订单"]; } if($refill_info['mch_notify_times'] == 0) { $card_no = $refill_info['card_no']; $spec = $refill_info['refill_amount']; util::loop_order_dec($card_no,$spec); } //手动通知,之所以不做尝试,是担心客户方状态处理不当 if (!$manual && $refill_info['mch_notify_state'] != 0) { return [false, "已经通知客户方"]; } $notify_url = $refill_info['notify_url']; $order_time = intval($refill_info['order_time']); $part = util::part_refill($order_time); if (empty($notify_url)) { $mod_refill->partition($part)->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => 0]); return [false, "回调地址为空"]; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// $order_state = intval($order_info['order_state']); if ($order_state !== ORDER_STATE_CANCEL && $order_state !== ORDER_STATE_SUCCESS) { return [false, "错误的订单状态,不能通知."]; } $resp = $this->mPolicy->notify($order_info,$refill_info); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// if ($resp) { $mod_refill->partition($part)->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]); return [true, ""]; } else { $mod_refill->partition($part)->edit($order_id, ['mch_notify_times' => ['exp', 'mch_notify_times+1']]); $times = $refill_info['mch_notify_times'] + 1; if ($times > 100) { $mod_refill->partition($part)->edit($order_id, ['mch_notify_state' => 2]); } else { $period = 5; QueueClient::async_push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false], $period); } return [false, "通知{$times}次,失败."]; } } //发预回调通知消息 public function notify_merchant_early($mchid, $mch_order) { $part = util::part_notify(); $mod_refill = Model('refill_order'); $refill_info = $mod_refill->partition($part)->getOrderInfo(['mch_order' => $mch_order,'mchid' => $mchid,'inner_status' => 0]); if(empty($refill_info)) { return [false, "refill_order table cannot find order when mchid=$mchid mch_order=$mch_order"]; } $order_sn = $refill_info['order_sn']; $order_id = $refill_info['order_id']; $vr_order = Model('vr_order'); $order_info = $vr_order->partition($part)->getOrderInfo(['order_sn' => $order_sn]); if (empty($order_info)) { return [false, "refill_order table cannot find order when order_sn=$order_sn."]; } $cur_quality = intval($refill_info['quality']); $success = in_array($cur_quality, [2, 3]); //在卡密或者快充通道时,预回调成功。 $resp = $this->mPolicy->notify_early($order_info, $refill_info, $success); //////////////////////////////////////////////////////////////////////////////////////////////////////////////// if ($resp) { $mod_refill->partition($part)->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]); return [true, ""]; } else { $mod_refill->partition($part)->edit($order_id, ['mch_notify_times' => ['exp', 'mch_notify_times+1']]); $times = $refill_info['mch_notify_times'] + 1; if ($times > 100) { $mod_refill->partition($part)->edit($order_id, ['mch_notify_state' => 2]); } else { $period = 5; QueueClient::async_push("NotifyMerchantEarly", ['mchid' => $mchid, 'mch_order' => $mch_order], $period); } return [false, "通知{$times}次,失败."]; } } public function query($order_id) { $mod_order = Model('vr_order'); $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]); if(empty($order_info)) return false; $mod_refill = Model('refill_order'); $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]); $chname = $refill_info['channel_name']; if ($refill_info['notify_state'] == 1 && in_array($order_info['order_state'], [ORDER_STATE_SUCCESS, ORDER_STATE_CANCEL])) { QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]); return true; } if($order_info['order_state'] == ORDER_STATE_SEND) { $query_able = true; } else { $query_able = false; } if($query_able) { if(empty($chname)) { Log::record("chname={$chname}", Log::DEBUG); return false; } $provider = $this->mPolicy->provider($chname); if(empty($provider)) return false; $values = $provider->query($refill_info); if (count($values) == 2) { [$state, $order_state] = $values; $official_sn = false; } else { [$state, $order_state, $official_sn] = $values; } if (!$state) { return false; } elseif ($order_state == ORDER_STATE_SUCCESS) { $this->proc_notify($order_id, true, false, $chname, $official_sn); } elseif ($order_state == ORDER_STATE_CANCEL) { $this->proc_notify($order_id, false, true, $chname, $official_sn); } else { Log::record("RefillBase::query order_state={$order_state}", Log::DEBUG); } } return true; } public function query_net($order_id) { $query_handler = function ($order_id, $order_info, $state, $order_state, $chname,$order_time) { $mod_refill = Model('refill_order'); if ($order_info['order_state'] == ORDER_STATE_PAY) { $query_able = true; } else { $query_able = false; } $vr_part = util::part_vr_order(intval($order_info['add_time'])); $can_try = false; if($query_able) { if(!$state) { QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30); $neterr = true; } elseif($order_state == ORDER_STATE_SUCCESS || $order_state == ORDER_STATE_CANCEL) { $neterr = false; $logic_vr_order = Logic("vr_order"); $logic_vr_order->changeOrderStateSend($order_id, true, $vr_part); $data = ['commit_time' => time()]; $mod_refill->partition(util::part_refill($order_time))->edit($order_id, $data); QueueClient::async_push("QueryRefillState", ['order_id' => $order_id], 1); } elseif ($order_state == ORDER_STATE_NOEXIST) { $neterr = false; $logic_vr_order = Logic("vr_order"); $logic_vr_order->changeOrderStateSend($order_id, true, $vr_part); $can_try = true; } else { $neterr = true; QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30); } util::monitor_netchk($chname,$neterr); } return [true,$can_try]; }; //////////////////////////////////////////////////////////////////////////////////////////////////////////////// $mod_order = Model('vr_order'); $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id]); if (empty($order_info) || $order_info['order_state'] != ORDER_STATE_PAY) return false; $mod_refill = Model('refill_order'); $refill_info = $mod_refill->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]); $chname = $refill_info['channel_name']; $provider = $this->mPolicy->provider($chname); if(empty($provider)) return false; $values = $provider->query($refill_info); if (count($values) == 2) { [$state, $order_state] = $values; $official_sn = false; } else { [$state, $order_state, $official_sn] = $values; } try { $can_try = false; $tran = new trans_wapper($mod_order, 'query_net change order state trans'); $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id], '*', true, true); $order_time = intval($refill_info['order_time']); [$ret, $can_try] = $query_handler($order_id, $order_info, $state, $order_state, $chname,$order_time); $tran->commit(); $trans_succ = true; } catch (Exception $ex) { Log::record("Error:" . $ex->getMessage(), Log::ERR); $trans_succ = false; $tran->rollback(); $ret = false; } if($can_try) { if($trans_succ) { $this->proc_notify($order_id, false, true, $chname, $official_sn); } else { QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30); } } return $ret; } public function query_timeout($mchid, $mch_order) { $has_tmout = transfer_timeout::instance()->has_tmout($mchid, $mch_order); if($has_tmout) { //标记为超时,并且准备提前回调。 util::push_queue_order($mchid, $mch_order, ORDER_STATE_TIMEOUT); $part = util::part_notify(); $mod_refill = Model('refill_order'); $refill_info = $mod_refill->partition($part)->getOrderInfo(['mch_order' => $mch_order,'mchid' => $mchid,'inner_status' => 0]); if(empty($refill_info)) { return false; } $order_id = $refill_info['order_id']; $mod_refill->partition($part)->edit($order_id, ['notify_time' => time()]); QueueClient::push("NotifyMerchantEarly", ['mchid' => $mchid, 'mch_order' => $mch_order]); } return true; } public function query_auto($order_id,$query_times) { $perioder = function ($times) { if($times > 10) { return 300; } elseif($times > 5) { return 120; } else { return 5; } }; $query_handler = function ($order_id, $order_info, $state, $order_state, $chname,$order_time,$query_times) use ($perioder) { $query_times += 1; $mod_refill = Model('refill_order'); $vr_part = util::part_vr_order(intval($order_info['add_time'])); $peroid = $perioder($query_times); $can_try = false; if (!$state) { QueueClient::async_push("QueryAutoRefillState", ['order_id' => $order_id, 'query_times' => $query_times], $peroid); $neterr = true; } elseif ($order_state == ORDER_STATE_SUCCESS || $order_state == ORDER_STATE_CANCEL) { $neterr = false; QueueClient::async_push("QueryRefillState", ['order_id' => $order_id], 1); } elseif ($order_state == ORDER_STATE_NOEXIST) { $neterr = false; QueueClient::async_push("QueryRefillState", ['order_id' => $order_id], 1); $can_try = true; } else { $neterr = true; QueueClient::async_push("QueryAutoRefillState", ['order_id' => $order_id, 'query_times' => $query_times], $peroid); } util::monitor_netchk($chname, $neterr); return [true, $can_try]; }; //////////////////////////////////////////////////////////////////////////////////////////////////////////////// $mod_order = Model('vr_order'); $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id]); if (empty($order_info)) return false; $mod_refill = Model('refill_order'); $refill_info = $mod_refill->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]); $chname = $refill_info['channel_name']; $provider = $this->mPolicy->provider($chname); if(empty($provider)) return false; $values = $provider->query($refill_info); if (count($values) == 2) { [$state, $order_state] = $values; $official_sn = false; } else { [$state, $order_state, $official_sn] = $values; } try { $can_try = false; $tran = new trans_wapper($mod_order, 'query_net change order state trans'); $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id], '*', true, true); $order_time = intval($refill_info['order_time']); [$ret, $can_try] = $query_handler($order_id, $order_info, $state, $order_state, $chname,$order_time,$query_times); $tran->commit(); $trans_succ = true; } catch (Exception $ex) { Log::record("Error:" . $ex->getMessage(), Log::ERR); $trans_succ = false; $tran->rollback(); $ret = false; } if($can_try) { if($trans_succ) { $this->proc_notify($order_id, false, true, $chname, $official_sn); } else { QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30); } } return $ret; } public function manual_success($order_id) { $order_id = intval($order_id); if($order_id <= 0) return false; try { $mod_order = Model('vr_order'); $tran = new trans_wapper($mod_order,'manual_success state trans'); $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true); if(!empty($order_info) && in_array($order_info['order_state'],[ORDER_STATE_PAY,ORDER_STATE_SEND])) { $tran->commit(); $logic_vr_order = Logic("vr_order"); $logic_vr_order->changeOrderStateSuccess($order_id,true); $mod_refill = Model('refill_order'); $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id]); $order_time = intval($refill_info['order_time']); util::onOrderSuccess($refill_info,$order_info); $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]); mtopcard\cards_helper::assign($order_id); util::pop_queue_order($refill_info['mchid'], $refill_info['mch_order'], $order_time); } else { $tran->commit(); } QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]); return true; } catch (Exception $ex) { $tran->rollback(); Log::record("manual_success exception:{$ex->getMessage()}",Log::ERR); return false; } } public function manual_cancel($order_id) { $order_id = intval($order_id); if($order_id <= 0) return false; try { $mod_order = Model('vr_order'); $tran = new trans_wapper($mod_order,'manual_cancel state trans'); $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true); if(!empty($order_info) && in_array($order_info['order_state'],[ORDER_STATE_PAY,ORDER_STATE_SEND])) { $tran->commit(); $logic_vr_order = Logic("vr_order"); $logic_vr_order->changeOrderStateCancel($order_info, '', "后台手动回调通知失败",true,true); $mod_refill = Model('refill_order'); $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id]); $order_time = intval($refill_info['order_time']); $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]); mtopcard\cards_helper::reuse($order_id); util::pop_queue_order($refill_info['mchid'], $refill_info['mch_order'], $order_time); } else { $tran->commit(); } QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]); return true; } catch (Exception $ex) { $tran->rollback(); Log::record("manual_cancel exception:{$ex->getMessage()}",Log::ERR); return false; } } public function need_intercept($mchid,$card_type,$card_state,$is_transfer,$card_no) { return $this->mPolicy->need_intercept($mchid,$card_type,$card_state,$is_transfer,$card_no); } public function region_intercept($quality,$card_type,$region_no) { return $this->mPolicy->region_intercept($quality,$card_type,$region_no); } public function UpdateMchRatios($gross,$detail,$types) { $this->mPolicy->update_mchratios($gross, $detail, $types); } public function merchant_ratio($mchid) { return $this->mPolicy->merchant_ratio($mchid); } public function UpdateChctl($params) { $this->mPolicy->update_chctl($params); } public function UpdateChspeed($params) { $this->mPolicy->update_chspeeds($params); } public function UpdateMaxSpeed($params) { $this->mPolicy->update_maxspeeds($params); } public function isOverChspeed($chname) { return $this->mPolicy->is_over_chspeed($chname); } /** * @param order $order * @param $mchid * @return CalcMerchantPrice * @throws Exception */ private function getMerchantPriceCalc(order $order) { $org_quality = $order->org_quality(); if (PolicyUtil::mixed_quality($org_quality)) { $calc = new CalcMerchantMixedPrice($order, $org_quality, $this->mPolicy, $order->thrid_params()); } else { $calc = new CalcMerchantPrice($order, $order->cur_quality(), $this->mPolicy, $order->thrid_params()); } return $calc; } }