123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947 |
- <?php
- namespace refill;
- require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
- require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
- require_once(BASE_HELPER_PATH . '/refill/IRefill.php');
- require_once(BASE_HELPER_PATH . '/refill/IRefillOil.php');
- require_once(BASE_HELPER_PATH . '/refill/IRefillPhone.php');
- require_once(BASE_HELPER_PATH . '/refill/IRefillThird.php');
- require_once(BASE_HELPER_PATH . '/refill/IRefillCallBack.php');
- require_once(BASE_HELPER_PATH . '/refill/ProviderManager.php');
- require_once(BASE_HELPER_PATH . '/refill/CalcMerchantPrice.php');
- require_once(BASE_HELPER_PATH . '/refill/util.php');
- require_once(BASE_HELPER_PATH . '/refill/errcode.php');
- use Log;
- use scope_trace;
- use mtopcard;
- use QueueClient;
- use member_info;
- use Exception;
- use trans_wapper;
- use Swoole;
- class RefillBase
- {
- protected $mPolicy;
- protected function __construct($policy)
- {
- $this->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))
- {
- $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:{$order_id}", Log::ERR);
- }
- }
- else {
- $orgdata = json_encode($input);
- Log::record("{$chname} 签名失败:input={$orgdata}",Log::ERR);
- }
- return true;
- }
- private function risksn_check($refill_info,$org_official_sn)
- {
- $card_type = intval($refill_info['card_type']);
- $chk_types = [mtopcard\ChinaMobileCard , mtopcard\ChinaUnicomCard , mtopcard\ChinaTelecomCard];
- if(!in_array($card_type,$chk_types)) {
- return false;
- }
- if ($org_official_sn !== false and count($org_official_sn) > 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 = ['0095', 'SP', 'J98', 'WX', '11010336J'];
- $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 && $this->risksn_check($refill_info, $official_sn)) {
- $tran->commit();
- $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']];
- QueueClient::async_push("OnRiskSN", $params, 1);
- return false;
- }
- $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']);
- if ($success) {
- $logic_vr_order->changeOrderStateSuccess($order_id,true);
- $tran->commit();
- util::onOrderSuccess($refill_info,$order_info);
- util::monitor_callback($mchid, $spec, $card_type, $refill_info['mch_amount'], $refill_info['channel_amount'], true, $order_time);
- }
- 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();
- [$can_retry,$params] = $this->retry($refill_info, $order_info);
- if ($can_retry)
- {
- if(util::push_add($params)) {
- return true;
- }
- }
- util::monitor_callback($mchid, $spec, $card_type, $refill_info['mch_amount'], 0, false, $order_time);
- }
- else {
- $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,不可重试.",true,true);
- $tran->commit();
- util::monitor_callback($mchid, $spec, $card_type, $refill_info['mch_amount'], 0, false, $order_time);
- }
- $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['notify_time' => time(), 'is_retrying' => 0,'notify_state' => 1]);
- 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(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 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());
- $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 [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());
- return [true,$order_id,''];
- }
- }
- 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, 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)
- {
- try
- {
- $mchid = $order->mchid();
- $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();
- return $mch_amount;
- } catch (Exception $ex) {
- 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);
- $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 = '';
- $neterr = false;
- 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;
- $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)
- {
- $commit_time = time();
- $chfilters->add_channel($channel_name,true);
- util::monitor_commit($channel_name, $spec, $card_type, $channel_amount, $commit_time);
- $trade_no = $errmsg;
- $refill_type = $provider->refill_type();
- $vr_part = util::part_vr_order_time($order_time);
- 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);
- $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->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);
- break;
- } else {
- $neterr = false;
- $net_errno = 0;
- }
- 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]);
- }
- }
- 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) {
- $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 (!$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 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;
- $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 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);
- $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::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) && $order_info['order_state'] == 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 UpdateChctl($params)
- {
- $this->mPolicy->update_chctl($params);
- }
- public function UpdateMaxSpeed($params)
- {
- $this->mPolicy->update_maxspeeds($params);
- }
- }
|