12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118 |
- <?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);
- }
- 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:{$order_id}", Log::ERR);
- }
- }
- else {
- $orgdata = json_encode($input);
- Log::record("{$chname} 签名失败:input={$orgdata}",Log::ERR);
- }
- 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',
- $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']);
- 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();
- 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);
- [$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;
- }
- }
- 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(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];
- }
- }
- [$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, $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)) {
- 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) continue;
- $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_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("QueryAutoRefillState",['order_id' => $order_id,'query_times' => 0],3);
- }
- 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($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 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_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 UpdateChctl($params)
- {
- $this->mPolicy->update_chctl($params);
- }
- public function UpdateMaxSpeed($params)
- {
- $this->mPolicy->update_maxspeeds($params);
- }
- }
|