123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254 |
- <?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);
- }
- //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());
- // try
- // {
- // $calc = $this->getMerchantPriceCalc($order);
- // $mch_price = $calc->calc_vgoods_price([]);
- // $mch_amount = $mch_price * $order->quantity();
- // }
- // catch (Exception $ex) {
- // $mch_price = 0.00;
- // $mch_amount = 0.00;
- // }
- $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)
- {
- $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::ERR);
- 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;
- }
- }
|