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($mchid,$spec,$card_type,$org_quality,$cur_quality,$times,$period,$pcode = '',$regin_no = -1): array { return $this->mPolicy->find_quality($mchid,$spec,$card_type,$org_quality,$cur_quality,$times,$period,$pcode,$regin_no); } 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']); $org_quality = intval($refill_info['org_quality']); $mch_order = $refill_info['mch_order']; if ($success) { util::incr_notify($chname, $card_type, $spec, $quality, true); util::incr_user_success($mchid,$card_type, $spec,$org_quality); $logic_vr_order->changeOrderStateSuccess($order_id,true); } elseif ($can_try) { util::incr_notify($chname, $card_type, $spec, $quality, false); util::incr_amount_lock($mchid,$card_type,$spec); $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,正在重试",true,true); [$can_retry,$params] = $this->retry($refill_info, $order_info); if ($can_retry) { $mod_refill->edit($order_id, ['is_retrying' => 1,'notify_time' => time()]); $tran->commit(); if(util::push_add($params)) { return true; } } } else { util::incr_notify($chname, $card_type, $spec, $quality, false); util::incr_amount_lock($mchid,$card_type,$spec); $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,不可重试.",true,true); } $tran->commit(); } catch (Exception $ex) { $tran->rollback(); Log::record("Error:" . $ex->getMessage(), Log::ERR); } $mod_refill->edit($order_id, ['notify_time' => time(), 'is_retrying' => 0,'notify_state' => 1]); Log::record("proc_notify pop_queue_order 1",Log::DEBUG); util::pop_queue_order($mchid,$mch_order); Log::record("proc_notify pop_queue_order 2",Log::DEBUG); QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]); Log::record("proc_notify pop_queue_order 3",Log::DEBUG); return true; } private function retry(array $refill_info, array $order_info) { $mchid = intval($refill_info['mchid']); $spec = intval($refill_info['refill_amount']); $card_type = intval($refill_info['card_type']); $org_quality = intval($refill_info['org_quality']); $cur_quality = intval($refill_info['quality']); //从数据库中读取的字段。 $used_time = time() - intval($refill_info['order_time']); $commit_times = intval($refill_info['commit_times']); $regin_no = $refill_info['regin_no']; //三方充值,不做重试 if($card_type == mtopcard\ThirdRefillCard) { return [false,null]; } [$org_quality,$quality] = $this->find_quality($mchid,$spec,$card_type,$org_quality,$cur_quality,$commit_times,$used_time,"",$regin_no); if($quality <= 0) return [false,null]; $params = [ 'mchid' => $refill_info['mchid'], 'buyer_id' => $order_info['buyer_id'], 'amount' => $refill_info['refill_amount'], 'card_no' => $refill_info['card_no'], 'card_type' => $refill_info['card_type'], 'regin_no' => $refill_info['regin_no'], 'org_quality' => $org_quality, 'quality' => $cur_quality, 'mch_order' => $refill_info['mch_order'], 'notify_url' => $refill_info['notify_url'], 'idcard' => $refill_info['idcard'] ?? '', 'card_name' => $refill_info['card_name'] ?? '', 'order_time' => $refill_info['order_time'], 'commit_times' => $refill_info['commit_times'] + 1, 'order_id' => $refill_info['order_id'] ]; return [true,$params]; } public function zero_order($mchid, $buyer_id, $amount, $card_no, $mch_order, $idcard, $card_name, $notify_url,$quality,$org_quality, $order_time = 0, $commit_times = 0,$errmsg='',$card_type = 0,$quantity = 1,$third_params = []) { if($card_type == mtopcard\UnknownCard) { $card_type = mtopcard\card_type($card_no,$regin_no); } $minfo = new member_info($buyer_id); $calc = new ZeroMerchantPrice($mchid, $amount, $card_type,$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); if (empty($mch_order)) { $mch_order = $order_sn; } //虚拟订单表信息扩展 $orderext = ['order_id' => $order_id, 'order_sn' => $order_sn, 'mchid' => $mchid, 'refill_amount' => $amount, 'mch_order' => $mch_order, 'idcard' => $idcard, 'card_name' => $card_name, 'notify_url' => $notify_url, 'channel_name' => '', 'mch_amount' => $mch_amount, 'channel_amount' => 0, 'order_time' => $order_time, 'commit_times' => $commit_times, 'commit_time' => time(),'notify_state' => 1,'notify_time' => time(), 'card_type' => $card_type, 'card_no' => $card_no, 'quality' => $quality, 'org_quality' => $org_quality, 'err_msg' => $errmsg]; $mod_refill->add_refill($orderext); if($card_type == mtopcard\ThirdRefillCard) { $thrid_refill = Model('thrid_refill'); $ext = ['order_id' => $order_id, 'order_sn' => $order_sn, 'account_type' => $third_params['third_card_type'], 'quantity' => $quantity, 'user_account' => $card_no, 'pcode' => $third_params['product_code'] ]; $thrid_refill->addExt($ext); } return $order_id; } else { return 0; } } //$quality,质量 //返回值:[ 错误码,错误信息,订单ID,是否是网络错误] //说明:错误码为true 表示成功 // 其它情况,则需要判断订单ID public function add($mchid, $buyer_id, $amount, $card_no, $mch_order, $idcard, $card_name, $notify_url, $quality, $org_quality, $order_time, $commit_times, $last_orderid = 0, $card_type = 0, $regin_no = 0, $quantity = 1, $third_params = []) { if($card_type == mtopcard\ThirdRefillCard) { $pcode = $third_params['product_code']; } else { $pcode = ""; } Log::record("RefillBase::add regin_no={$regin_no}",Log::DEBUG); [$providers, $overload] = $this->mPolicy->find_providers($mchid, $amount, $card_type, $org_quality, $quality, $regin_no, $pcode, $order_time, $commit_times); $chfilters = new channel_filter($mchid,$mch_order,$quality,$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]; } if (empty($notify_url)) { $notify_url = ""; } try { $minfo = new member_info($buyer_id); if(PolicyUtil::mixed_quality($org_quality)) { $calc = new CalcMerchantPrice($mchid, $amount, $card_type,$org_quality,$this->mPolicy,$third_params); } else { $calc = new CalcMerchantPrice($mchid, $amount, $card_type,$quality,$this->mPolicy,$third_params); } $mch_price = $calc->calc_vgoods_price([]); $mch_amount = $mch_price * $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($quality,$amount,$card_type,$regin_no,$third_params); if ($goods_id <= 0) continue; if(PolicyUtil::mixed_quality($org_quality) == false && $price > $mch_price) { //组合通道,以原始质量算价格. //通道价格大于客户价格,换通道. continue; } Log::record("start create order",Log::DEBUG); $input['goods_id'] = $goods_id; $input['quantity'] = $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; $logic_buy_virtual = Logic('buy_virtual'); $mod_refill = Model('refill_order'); try { $fTrans = false; if($fTrans) { $trans = new trans_wapper($mod_refill,__METHOD__); } else { $start = microtime(true); } $result = $logic_buy_virtual->buyStep3($input, $buyer_id, [$calc, 'calc_vorder_amount'], true, false); if ($result['state'] === true) { $order_sn = $result['data']['order_sn']; $order_id = $result['data']['order_id']; if($last_orderid > 0) { $mod_refill->edit($last_orderid, ['inner_status' => 1]); } $last_orderid = $order_id; if (empty($mch_order)) { $mch_order = $order_sn; } $thrid_refill = Model('thrid_refill'); if($card_type == mtopcard\ThirdRefillCard) { $product = $thrid_refill->getProduct(['system_code' => $third_params['product_code']]); $refill_amount = $product['refill_amount']; } else { $refill_amount = $amount; } //虚拟订单表信息扩展 $orderext = ['order_id' => $order_id, 'order_sn' => $order_sn, 'mchid' => $mchid, 'refill_amount' => $refill_amount, 'mch_order' => $mch_order, 'idcard' => $idcard, 'card_name' => $card_name, 'notify_url' => $notify_url, 'channel_name' => $channel_name, 'mch_amount' => $mch_amount, 'channel_amount' => $price * $quantity, 'order_time' => $order_time, 'commit_times' => $commit_times, 'card_type' => $card_type, 'regin_no' => $regin_no, 'card_no' => $card_no, 'quality' => $quality, 'org_quality' => $org_quality]; $mod_refill->add_refill($orderext); if($card_type == mtopcard\ThirdRefillCard) { $ext = ['order_id' => $order_id, 'order_sn' => $order_sn, 'account_type' => $third_params['third_card_type'], 'quantity' => $quantity, 'user_account' => $card_no, 'pcode' => $third_params['product_code'] ]; $thrid_refill->addExt($ext); } if(!$this->pay_completed($order_sn)) { $logic_vr_order = Logic("vr_order"); $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]); $logic_vr_order->changeOrderStateCancel($order_info, '', "预存款不足以支付该订单",true,true); if($fTrans) { $trans->commit(); } break; } elseif($fTrans) { $trans->commit(); } else { Log::record(sprintf(__METHOD__ . "request time=%.6f", microtime(true) - $start), Log::DEBUG); } } else { if($fTrans) { $trans->commit(); } Log::record("{$result['msg']}",Log::ERR); continue; } } catch (Exception $ex) { Log::record($ex->getMessage(), Log::ERR); if($fTrans) { $trans->rollback(); } continue; } $order_success = true; $params = ['order_sn' => $order_sn, 'idcard' => $idcard, 'card_name' => $card_name, 'buyer_id' => $buyer_id, 'quality' => $quality, 'order_id' => $order_id, 'regin_no' => $regin_no]; if($card_type == mtopcard\ThirdRefillCard) { $params['quantity'] = $quantity; $params['product_code'] = $third_params['product_code']; $params['third_card_type'] = $third_params['third_card_type']; $params['goods_id'] = $goods_id; } util::incr_commit_pre($channel_name,$card_type,$amount,$quality); util::decr_amount_lock($mchid,$card_type,$amount); $start = microtime(true); Log::record("start commit order card_no={$card_no}", Log::DEBUG); $net_errno = ""; [$state, $errmsg, $neterr] = $provider->add($card_no, $card_type, $amount, $params,$net_errno); Log::record(sprintf(" %s add request time=%.6f", $channel_name,microtime(true) - $start), Log::DEBUG); if ($state) { $chfilters->add_channel($channel_name,true); //提交成功 util::incr_commit($channel_name,$card_type,$amount,$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' => $amount, '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; } //提交失败 $commit_times += 1; util::incr_commit($channel_name,$card_type,$amount,$quality,false); util::incr_amount_lock($mchid,$card_type,$amount); 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 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.00)); } 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) { 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']; $card_type = intval($refill_info['card_type']); 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 UpdateRatio($ratios) { $this->mPolicy->update_ratios($ratios); } }