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); } public function notify($chname, $input) { $caller = $this->mPolicy->getCaller($chname); if($caller === false) { return false; } elseif ($caller->verify($input)) { [$order_id, $success, $can_try, $need_handle] = $caller->notify($input); if (!$need_handle) { return true; } if ($order_id !== false) { return $this->proc_notify($order_id, $success, $can_try, $chname,$input); } else { Log::record("{$chname} callback 系统无此订单ID:{$order_id}", Log::ERR); } } else { $orgdata = json_encode($input); Log::record("{$chname} 签名失败:input={$orgdata}",Log::ERR); } return true; } private function proc_notify($order_id,$success, $can_try,$chname,$input=[]) { $mod_order = Model('vr_order'); $order_info = $mod_order->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->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->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']; if ($success) { $logic_vr_order->changeOrderStateSuccess($order_id,true); $tran->commit(); util::incr_notify($chname, $card_type, $spec, $quality, true); util::incr_user_success($mchid,$card_type, $spec,$quality); util::onOrderSuccess($refill_info,$order_info); } elseif ($can_try) { $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,正在重试",true,true); $mod_refill->edit($order_id, ['is_retrying' => 1,'notify_time' => time()]); $tran->commit(); util::incr_notify($chname, $card_type, $spec, $quality, false); util::incr_amount_lock($mchid,$card_type,$spec); [$can_retry,$params] = $this->retry($refill_info, $order_info); if ($can_retry) { if(util::push_add($params)) { return true; } } } else { $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,不可重试.",true,true); $tran->commit(); util::incr_notify($chname, $card_type, $spec, $quality, false); util::incr_amount_lock($mchid,$card_type,$spec); } $mod_refill->edit($order_id, ['notify_time' => time(), 'is_retrying' => 0,'notify_state' => 1]); util::pop_queue_order($mchid,$mch_order); QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]); if(!$success) { util::incr_user_fail($mchid,$card_type, $spec,$quality); } 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(array $refill_info, array $order_info) { $trace = new scope_trace(__METHOD__); $order = order::from_db($refill_info,$order_info); if($order->is_third()) { return [false,null]; } [$org_quality,$quality] = $this->find_quality($order); if($quality <= 0) { return [false,null]; } else { $params = $order->queue_params(); return [true,$params]; } } public function zero_order(order $order,$errmsg='') { $buyer_id = $order->buyer_id(); $minfo = new member_info($buyer_id); $calc = new ZeroMerchantPrice($order->mchid(), $order->spec(), $order->card_type(),$order->cur_quality()); $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,false); $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')->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; } } //返回值:[ 错误码,错误信息,订单ID,是否是网络错误] //说明:错误码为true 表示成功 // 其它情况,则需要判断订单ID public function add(order $order) { $last_orderid = $order->last_order_id(); $mchid = $order->mchid(); $mch_order = $order->mch_order(); [$providers, $overload] = $this->mPolicy->find_providers($order); $chfilters = new channel_filter($mchid,$mch_order,$order->cur_quality(),$order->card_type()); $providers = $chfilters->getProviders($providers); if (empty($providers)) { Log::record("canot find any providers", Log::DEBUG); return [errcode::CANNOT_MATCH_PROVIDER, "匹配不到合适的充值通道", $last_orderid, false, 0]; } try { $minfo = new member_info($order->buyer_id()); $org_quality = $order->org_quality(); if(PolicyUtil::mixed_quality($org_quality)) { $calc = new CalcMerchantPrice($mchid, $order->spec(), $order->card_type(),$org_quality,$this->mPolicy,$order->thrid_params()); } else { $calc = new CalcMerchantPrice($mchid, $order->spec(), $order->card_type(),$order->cur_quality(),$this->mPolicy,$order->thrid_params()); } $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) { Log::record("下单时机构余额不足,可用余额为:{$available}", Log::DEBUG); return [errcode::MERCHANT_SHORT_MONEY, "余额不足支付订单",$last_orderid,false,0]; } $refill_state = false; $order_success = false; $net_errno = 0; foreach ($providers as $provider) { $channel_name = $provider->name(); [$goods_id, $price] = $provider->goods($order->cur_quality(), $order->spec(), $order->card_type(), $order->region_no(), $order->thrid_params()); if ($goods_id <= 0) continue; //非组合通道,以原始质量算价格. //通道价格大于客户价格,换通道. if(PolicyUtil::mixed_quality($org_quality) == false && $price > $mch_price) { continue; } $mod_refill = Model('refill_order'); $channel_amount = $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) continue; util::incr_commit_pre($channel_name, $order->card_type(), $order->spec(), $order->cur_quality()); util::decr_amount_lock($mchid, $order->card_type(), $order->spec()); $start = microtime(true); $net_errno = ""; $params = $order->channel_params($order_id,$order_sn,$goods_id); $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) { $chfilters->add_channel($channel_name,true); util::incr_commit($channel_name,$card_type,$spec,$quality,true); $trade_no = $errmsg; $refill_type = $provider->refill_type(); if ($refill_type == 'api') { $logic_vr_order = Logic("vr_order"); $logic_vr_order->changeOrderStateSend($order_id,true); } elseif($refill_type == 'fetch') { $logic_vr_order = Logic("vr_order"); $logic_vr_order->changeOrderStateSend($order_id,true); $order_info = Model('vr_order')->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' => time(), 'ch_trade_no' => $trade_no]; $mod_refill->edit($order_id, $data); $refill_state = true; //如果对方没有回调能力,则启动主动查询. if($provider->callback() === false) { QueueClient::async_push("QueryRefillState",['order_id' => $order_id],60); } break; } else { $chfilters->add_channel($channel_name,false); if(!empty($neterr) && util::need_check($net_errno)) { $mod_refill->edit($order_id, ['commit_time' => time(),'neterr' => 1,'err_msg' => "neterr={$net_errno}"]); break; } else { $neterr = false; $net_errno = 0; } util::incr_commit($channel_name,$card_type,$spec,$order->cur_quality(),false); util::incr_amount_lock($mchid,$card_type,$spec); 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->edit($order_id, ['commit_time' => time(),'err_msg' => $errmsg]); } } if ($refill_state) { return [true, '', $last_orderid, false, 0]; } elseif ($order_success) { return [errcode::MERCHANT_REFILL_ERROR, "充值失败", $last_orderid, $neterr, $net_errno]; } else { return [errcode::MERCHANT_REFILL_ERROR, "充值失败", $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) { $mod_refill->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()]); $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"]; } $vr_order = Model('vr_order'); $refill_order = Model('refill_order'); $order_info = $vr_order->getOrderInfo(['order_id' => $order_id]); $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id,'inner_status' => 0,'is_retrying' => 0]); if (empty($order_info) || empty($refill_info)) { return [false, "无此订单"]; } //手动通知,之所以不做尝试,是担心客户方状态处理不当 if (!$manual && $refill_info['mch_notify_state'] != 0) { return [false, "已经通知客户方"]; } $notify_url = $refill_info['notify_url']; if (empty($notify_url)) { $refill_order->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) { $refill_order->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]); return [true, ""]; } else { $refill_order->edit($order_id, ['mch_notify_times' => ['exp', 'mch_notify_times+1']]); $times = $refill_info['mch_notify_times'] + 1; if ($times > 100) { $refill_order->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 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)) return false; $provider = $this->mPolicy->provider($chname); if(empty($provider)) return false; [$state, $order_state] = $provider->query($refill_info); if(!$state) { return false; } elseif($order_state == ORDER_STATE_SUCCESS) { $this->proc_notify($order_id,true,false,$chname); } elseif($order_state == ORDER_STATE_CANCEL) { $this->proc_notify($order_id,false,true,$chname); } else { Log::record("RefillBase::query order_state={$order_state}",Log::DEBUG); } } return true; } public function query_net($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']; $mchid = $refill_info['mchid']; $mch_order = $refill_info['mch_order']; if($order_info['order_state'] == ORDER_STATE_PAY) { $query_able = true; } else { $query_able = false; } if($query_able) { if(empty($chname)) return false; $provider = $this->mPolicy->provider($chname); if(empty($provider)) return false; [$state, $order_state] = $provider->query($refill_info); if(!$state) { QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30); return false; } elseif($order_state == ORDER_STATE_SUCCESS || $order_state == ORDER_STATE_CANCEL) { $logic_vr_order = Logic("vr_order"); $logic_vr_order->changeOrderStateSend($order_id, true); $data = ['commit_time' => time()]; $mod_refill->edit($order_id, $data); QueueClient::async_push("QueryRefillState", ['order_id' => $order_id], 1); } elseif ($order_state == ORDER_STATE_NOEXIST) { $logic_vr_order = Logic("vr_order"); $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}查询订单不存在.",true,true); $mod_refill->edit($order_id, ['notify_time' => time(), 'notify_state' => 1]); util::pop_queue_order($mchid,$mch_order); QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]); } else { QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30); } } return true; } 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) && $order_info['order_state'] == ORDER_STATE_SEND) { $tran->commit(); $logic_vr_order = Logic("vr_order"); $logic_vr_order->changeOrderStateSuccess($order_id,true); $refill_order = Model('refill_order'); $refill_order->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]); mtopcard\cards_helper::assign($order_id); $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id]); util::pop_queue_order($refill_info['mchid'],$refill_info['mch_order']); } 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) && $order_info['order_state'] == ORDER_STATE_SEND) { $tran->commit(); $logic_vr_order = Logic("vr_order"); $logic_vr_order->changeOrderStateCancel($order_info, '', "后台手动回调通知失败",true,true); $refill_order = Model('refill_order'); $refill_order->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]); mtopcard\cards_helper::reuse($order_id); $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id]); util::pop_queue_order($refill_info['mchid'],$refill_info['mch_order']); } 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) { return $this->mPolicy->need_intercept($mchid,$card_type,$card_state,$is_transfer); } public function region_intercept($quality,$card_type,$region_no) { return $this->mPolicy->region_intercept($quality,$card_type,$region_no); } public function UpdateRatio($ratios) { $this->mPolicy->update_ratios($ratios); } public function UpdateMchRatios($gross,$detail) { $this->mPolicy->update_mchratios($gross,$detail); } public function UpdateSpeeds($speeds) { $this->mPolicy->update_chspeeds($speeds); } }