RefillBase.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. <?php
  2. namespace refill;
  3. require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
  4. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  5. require_once(BASE_HELPER_PATH . '/refill/IRefill.php');
  6. require_once(BASE_HELPER_PATH . '/refill/IRefillOil.php');
  7. require_once(BASE_HELPER_PATH . '/refill/IRefillPhone.php');
  8. require_once(BASE_HELPER_PATH . '/refill/IRefillCallBack.php');
  9. require_once(BASE_HELPER_PATH . '/refill/ProviderManager.php');
  10. require_once(BASE_HELPER_PATH . '/refill/CalcMerchantPrice.php');
  11. require_once(BASE_HELPER_PATH . '/refill/util.php');
  12. require_once(BASE_HELPER_PATH . '/refill/errcode.php');
  13. use Log;
  14. use scope_trace;
  15. use mtopcard;
  16. use QueueClient;
  17. use member_info;
  18. use Exception;
  19. use trans_wapper;
  20. use Swoole;
  21. class RefillBase
  22. {
  23. protected $mPolicy;
  24. protected $mLimits = [];
  25. protected function __construct($policy)
  26. {
  27. $this->mPolicy = $policy;
  28. }
  29. public function allow($mchid,$card_type,$amount,$quality)
  30. {
  31. return $this->mPolicy->allow($mchid,$card_type,$amount,$quality);
  32. }
  33. public function goods()
  34. {
  35. return $this->mPolicy->goods();
  36. }
  37. public function load()
  38. {
  39. $this->mPolicy->load();
  40. }
  41. public function find_quality(order $order,$skip = false)
  42. {
  43. return $this->mPolicy->find_quality($order,$skip);
  44. }
  45. public function notify($chname, $input)
  46. {
  47. $caller = $this->mPolicy->getCaller($chname);
  48. if($caller === false) {
  49. return false;
  50. }
  51. elseif ($caller->verify($input))
  52. {
  53. [$order_id, $success, $can_try, $need_handle] = $caller->notify($input);
  54. if (!$need_handle) {
  55. return true;
  56. }
  57. if ($order_id !== false) {
  58. return $this->proc_notify($order_id, $success, $can_try, $chname,$input);
  59. } else {
  60. Log::record("{$chname} callback 系统无此订单ID:{$order_id}", Log::ERR);
  61. }
  62. }
  63. else {
  64. $orgdata = json_encode($input);
  65. Log::record("{$chname} 签名失败:input={$orgdata}",Log::ERR);
  66. }
  67. return true;
  68. }
  69. private function proc_notify($order_id,$success, $can_try,$chname,$input=[])
  70. {
  71. $mod_order = Model('vr_order');
  72. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  73. $order_state = intval($order_info['order_state']);
  74. if ($order_state == ORDER_STATE_PAY)
  75. {
  76. if(!empty($input)) {
  77. Log::record(__METHOD__ . " recv notify when order_state=ORDER_STATE_PAY" ,Log::DEBUG);
  78. Swoole\Coroutine::sleep(5);
  79. util::push_notify($chname,$input);
  80. }
  81. return false;
  82. }
  83. if ($order_state != ORDER_STATE_SEND) {
  84. return false;
  85. }
  86. $logic_vr_order = Logic("vr_order");
  87. $mod_refill = Model('refill_order');
  88. try
  89. {
  90. $tran = new trans_wapper($mod_order,'notify change order state trans');
  91. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true);
  92. $order_state = intval($order_info['order_state']);
  93. if ($order_state != ORDER_STATE_SEND) {
  94. $tran->commit();
  95. return false;
  96. }
  97. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  98. if(empty($refill_info)) {
  99. $tran->commit();
  100. return false;
  101. }
  102. $quality = intval($refill_info['quality']);
  103. $card_type = intval($refill_info['card_type']);
  104. $spec = intval($refill_info['refill_amount']);
  105. $mchid = intval($refill_info['mchid']);
  106. $mch_order = $refill_info['mch_order'];
  107. if ($success) {
  108. $logic_vr_order->changeOrderStateSuccess($order_id,true);
  109. $tran->commit();
  110. util::incr_notify($chname, $card_type, $spec, $quality, true);
  111. util::incr_user_success($mchid,$card_type, $spec,$quality);
  112. util::onOrderSuccess($refill_info,$order_info);
  113. }
  114. elseif ($can_try)
  115. {
  116. $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,正在重试",true,true);
  117. $mod_refill->edit($order_id, ['is_retrying' => 1,'notify_time' => time()]);
  118. $tran->commit();
  119. util::incr_notify($chname, $card_type, $spec, $quality, false);
  120. util::incr_amount_lock($mchid,$card_type,$spec);
  121. [$can_retry,$params] = $this->retry($refill_info, $order_info);
  122. if ($can_retry)
  123. {
  124. if(util::push_add($params)) {
  125. return true;
  126. }
  127. }
  128. }
  129. else {
  130. $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,不可重试.",true,true);
  131. $tran->commit();
  132. util::incr_notify($chname, $card_type, $spec, $quality, false);
  133. util::incr_amount_lock($mchid,$card_type,$spec);
  134. }
  135. $mod_refill->edit($order_id, ['notify_time' => time(), 'is_retrying' => 0,'notify_state' => 1]);
  136. util::pop_queue_order($mchid,$mch_order);
  137. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  138. if(!$success) {
  139. util::incr_user_fail($mchid,$card_type, $spec,$quality);
  140. }
  141. return true;
  142. }
  143. catch (Exception $ex) {
  144. $tran->rollback();
  145. Log::record("Error:" . $ex->getMessage(), Log::ERR);
  146. Swoole\Coroutine::sleep(5);
  147. util::push_notify($chname,$input);
  148. return false;
  149. }
  150. }
  151. private function retry(array $refill_info, array $order_info)
  152. {
  153. $trace = new scope_trace(__METHOD__);
  154. $order = order::from_db($refill_info,$order_info);
  155. if($order->is_third()) {
  156. return [false,null];
  157. }
  158. [$org_quality,$quality] = $this->find_quality($order);
  159. if($quality <= 0) {
  160. return [false,null];
  161. }
  162. else {
  163. $params = $order->queue_params();
  164. return [true,$params];
  165. }
  166. }
  167. public function zero_order(order $order,$errmsg='')
  168. {
  169. $buyer_id = $order->buyer_id();
  170. $minfo = new member_info($buyer_id);
  171. $calc = new ZeroMerchantPrice($order->mchid(), $order->spec(), $order->card_type(),$order->cur_quality());
  172. $mch_amount = $calc->calc_vgoods_price([]);
  173. $input['goods_id'] = ZERO_GOODS_ID;
  174. $input['quantity'] = 1; //数量
  175. $input['buyer_phone'] = $minfo->mobile();
  176. $input['buyer_name'] = $minfo->truename();
  177. $input['buyer_msg'] = $_POST['buyer_msg'] ?? '';
  178. $input['order_from'] = 1;
  179. $input['pd_pay'] = true;
  180. $logic_buy_virtual = Logic('buy_virtual');
  181. $result = $logic_buy_virtual->buyStep3($input, $buyer_id, [$calc, 'calc_vorder_amount'], true,false);
  182. $mod_refill = Model('refill_order');
  183. if ($result['state'] === true)
  184. {
  185. $order_sn = $result['data']['order_sn'];
  186. $order_id = $result['data']['order_id'];
  187. $logic_vr_order = Logic("vr_order");
  188. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  189. $logic_vr_order->changeOrderStateCancel($order_info, '', '无法下单创建0元订单',true,true);
  190. $mch_order = $order->mch_order();
  191. if (empty($mch_order)) {
  192. $order->set_mchorder($order_sn);
  193. }
  194. $thrid_refill = Model('thrid_refill');
  195. if($order->is_third()) {
  196. $product = $thrid_refill->getProduct(['system_code' => $order->pcode()]);
  197. $refill_amount = $product['refill_amount'];
  198. } else {
  199. $refill_amount = $order->spec();
  200. }
  201. //虚拟订单表信息扩展
  202. $orderext = $order->ZeroRefillParams($order_id,$order_sn,$refill_amount,$mch_amount,'',0,$errmsg);
  203. $order->commit_times_inc();
  204. $mod_refill->add_refill($orderext);
  205. if($order->is_third()) {
  206. $thrid_refill = Model('thrid_refill');
  207. $ext = $order->third_extparams($order_id,$order_sn);
  208. $thrid_refill->addExt($ext);
  209. }
  210. return $order_id;
  211. }
  212. else {
  213. return 0;
  214. }
  215. }
  216. //返回值:[ 错误码,错误信息,订单ID,是否是网络错误]
  217. //说明:错误码为true 表示成功
  218. // 其它情况,则需要判断订单ID
  219. public function add(order $order)
  220. {
  221. $last_orderid = $order->last_order_id();
  222. $mchid = $order->mchid();
  223. $mch_order = $order->mch_order();
  224. [$providers, $overload] = $this->mPolicy->find_providers($order);
  225. $chfilters = new channel_filter($mchid,$mch_order,$order->cur_quality(),$order->card_type());
  226. $providers = $chfilters->getProviders($providers);
  227. if (empty($providers)) {
  228. Log::record("canot find any providers", Log::DEBUG);
  229. return [errcode::CANNOT_MATCH_PROVIDER, "匹配不到合适的充值通道", $last_orderid, false, 0];
  230. }
  231. try
  232. {
  233. $minfo = new member_info($order->buyer_id());
  234. $org_quality = $order->org_quality();
  235. if(PolicyUtil::mixed_quality($org_quality)) {
  236. $calc = new CalcMerchantPrice($mchid, $order->spec(), $order->card_type(),$org_quality,$this->mPolicy,$order->thrid_params());
  237. }
  238. else {
  239. $calc = new CalcMerchantPrice($mchid, $order->spec(), $order->card_type(),$order->cur_quality(),$this->mPolicy,$order->thrid_params());
  240. }
  241. $mch_price = $calc->calc_vgoods_price([]);
  242. $mch_amount = $mch_price * $order->quantity();
  243. }
  244. catch (Exception $ex) {
  245. return [errcode::MERCHANT_PRICE_UNSETTING, "没有协商商品价格",$last_orderid,false,0];
  246. }
  247. $available = $minfo->available_predeposit();
  248. if ($mch_amount > $available) {
  249. Log::record("下单时机构余额不足,可用余额为:{$available}", Log::DEBUG);
  250. return [errcode::MERCHANT_SHORT_MONEY, "余额不足支付订单",$last_orderid,false,0];
  251. }
  252. $refill_state = false;
  253. $order_success = false;
  254. $net_errno = 0;
  255. foreach ($providers as $provider)
  256. {
  257. $channel_name = $provider->name();
  258. [$goods_id, $price] = $provider->goods($order->cur_quality(), $order->spec(), $order->card_type(), $order->region_no(), $order->thrid_params());
  259. if ($goods_id <= 0) continue;
  260. //非组合通道,以原始质量算价格. //通道价格大于客户价格,换通道.
  261. if(PolicyUtil::mixed_quality($org_quality) == false && $price > $mch_price) {
  262. continue;
  263. }
  264. $mod_refill = Model('refill_order');
  265. $channel_amount = $price * $order->quantity();
  266. [$order_success, $order_id, $order_sn] = $this->create_order($order, $goods_id, $minfo, $calc, $channel_name, $channel_amount, $mch_amount);
  267. $last_orderid = $order_id;
  268. if(!$order_success) continue;
  269. util::incr_commit_pre($channel_name, $order->card_type(), $order->spec(), $order->cur_quality());
  270. util::decr_amount_lock($mchid, $order->card_type(), $order->spec());
  271. $start = microtime(true);
  272. $net_errno = "";
  273. $params = $order->channel_params($order_id,$order_sn,$goods_id);
  274. $card_no = $order->card_no();
  275. $card_type = $order->card_type();
  276. $spec = $order->spec();
  277. $quality = $order->cur_quality();
  278. [$state, $errmsg, $neterr] = $provider->add($card_no, $card_type, $spec, $params,$net_errno);
  279. Log::record(sprintf(" %s add request time=%.6f: state={$state} neterr={$neterr},net_errno={$net_errno}", $channel_name,microtime(true) - $start), Log::DEBUG);
  280. if ($state)
  281. {
  282. $chfilters->add_channel($channel_name,true);
  283. util::incr_commit($channel_name,$card_type,$spec,$quality,true);
  284. $trade_no = $errmsg;
  285. $refill_type = $provider->refill_type();
  286. if ($refill_type == 'api') {
  287. $logic_vr_order = Logic("vr_order");
  288. $logic_vr_order->changeOrderStateSend($order_id,true);
  289. } elseif($refill_type == 'fetch') {
  290. $logic_vr_order = Logic("vr_order");
  291. $logic_vr_order->changeOrderStateSend($order_id,true);
  292. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  293. $mod_fetch_order = Model('fetch_order');
  294. $fetch_datas = ['order_id' => $order_id, 'order_sn' => $order_sn, 'store_id' => $order_info['store_id'],'channel_name' => $channel_name,
  295. 'fetch_status' => 1, 'card_no' => $card_no, 'card_type' => $card_type, 'refill_amount' => $spec,
  296. 'add_time' => time()];
  297. $mod_fetch_order->add($fetch_datas);
  298. }
  299. else {
  300. Log::record("Err refill_type = {$refill_type}",Log::ERR);
  301. }
  302. $data = ['commit_time' => time(), 'ch_trade_no' => $trade_no];
  303. $mod_refill->edit($order_id, $data);
  304. $refill_state = true;
  305. //如果对方没有回调能力,则启动主动查询.
  306. if($provider->callback() === false) {
  307. QueueClient::async_push("QueryRefillState",['order_id' => $order_id],60);
  308. }
  309. break;
  310. }
  311. else
  312. {
  313. $chfilters->add_channel($channel_name,false);
  314. if(!empty($neterr) && util::need_check($net_errno)) {
  315. $mod_refill->edit($order_id, ['commit_time' => time(),'neterr' => 1,'err_msg' => "neterr={$net_errno}"]);
  316. break;
  317. } else {
  318. $neterr = false;
  319. $net_errno = 0;
  320. }
  321. util::incr_commit($channel_name,$card_type,$spec,$order->cur_quality(),false);
  322. util::incr_amount_lock($mchid,$card_type,$spec);
  323. Log::record("channel:{$channel_name} err:{$errmsg}");
  324. $logic_vr_order = Logic("vr_order");
  325. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  326. $logic_vr_order->changeOrderStateCancel($order_info, '', "调用{$channel_name}接口失败",true,true);
  327. if(!is_string($errmsg)) {
  328. $errmsg = "{$errmsg}";
  329. }
  330. $mod_refill->edit($order_id, ['commit_time' => time(),'err_msg' => $errmsg]);
  331. }
  332. }
  333. if ($refill_state) {
  334. return [true, '', $last_orderid, false, 0];
  335. } elseif ($order_success) {
  336. return [errcode::MERCHANT_REFILL_ERROR, "充值失败", $last_orderid, $neterr, $net_errno];
  337. } else {
  338. return [errcode::MERCHANT_REFILL_ERROR, "充值失败", $last_orderid, false, 0];
  339. }
  340. }
  341. private function create_order(order $order, $goods_id, $minfo, $calc, $ch_name, $ch_amount, $mch_amount)
  342. {
  343. $refill_creater = function (order $order,$last_orderid,$order_id,$order_sn,$mod_refill) use($ch_name, $ch_amount,$mch_amount)
  344. {
  345. try
  346. {
  347. if($last_orderid > 0) {
  348. $mod_refill->edit($last_orderid, ['inner_status' => 1]);
  349. }
  350. $order->set_last_orderid($order_id);
  351. $thrid_refill = Model('thrid_refill');
  352. if($order->is_third()) {
  353. $product = $thrid_refill->getProduct(['system_code' => $order->pcode()]);
  354. $refill_amount = $product['refill_amount'];
  355. } else {
  356. $refill_amount = $order->spec();
  357. }
  358. if(empty($order->mch_order())) {
  359. $order->set_mchorder($order_sn);
  360. }
  361. $order->commit_times_inc();
  362. $orderext = $order->refill_params($order_id,$order_sn,$refill_amount, $ch_name, $ch_amount,$mch_amount);
  363. $fInsert = $mod_refill->add_refill($orderext);
  364. if($order->is_third()) {
  365. $ext = $order->third_extparams($order_id,$order_sn);
  366. $thrid_refill->addExt($ext);
  367. }
  368. if(!$fInsert) {
  369. return false;
  370. } else {
  371. return true;
  372. }
  373. }
  374. catch (Exception $ex)
  375. {
  376. Log::record($ex->getMessage(),Log::ERR);
  377. return false;
  378. }
  379. };
  380. $order_canceler = function ($order_id,$err_msg) {
  381. $logic_vr_order = Logic("vr_order");
  382. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  383. $logic_vr_order->changeOrderStateCancel($order_info, '', $err_msg, true, true);
  384. };
  385. try
  386. {
  387. $logic_buy_virtual = Logic('buy_virtual');
  388. $mod_refill = Model('refill_order');
  389. $input['goods_id'] = $goods_id;
  390. $input['quantity'] = $order->quantity();
  391. $input['buyer_phone'] = $minfo->mobile();
  392. $input['buyer_name'] = $minfo->truename();
  393. $input['buyer_msg'] = $_POST['buyer_msg'] ?? '';
  394. $input['order_from'] = 1;
  395. $input['pd_pay'] = true;
  396. $start = microtime(true);
  397. $result = $logic_buy_virtual->buyStep3($input, $order->buyer_id(), [$calc, 'calc_vorder_amount'], true, true);
  398. if ($result['state'] === true)
  399. {
  400. $order_sn = $result['data']['order_sn'];
  401. $order_id = $result['data']['order_id'];
  402. $last_orderid = $order->last_order_id();
  403. $fSuccess = $refill_creater($order, $last_orderid, $order_id, $order_sn, $mod_refill);
  404. if(!$fSuccess) {
  405. Log::record("refill_creater fail:order_sn={$order_sn}",Log::ERR);
  406. $order_canceler($order_id,'refill_order 记录创建失败');
  407. return [false, $order_id, $order_sn];
  408. }
  409. if (!$this->pay_completed($order_sn)) {
  410. $order_canceler($order_id,'预存款不足以支付该订单');
  411. return [false, $order_id, $order_sn];
  412. }
  413. Log::record(sprintf(__METHOD__ . " request time=%.6f", microtime(true) - $start), Log::DEBUG);
  414. return [true,$order_id,$order_sn];
  415. }
  416. else
  417. {
  418. Log::record(__METHOD__ . " fail:buyStep3 err msg={$result['msg']}", Log::ERR);
  419. return [false,0,''];
  420. }
  421. }
  422. catch (Exception $ex)
  423. {
  424. Log::record($ex->getMessage(), Log::ERR);
  425. return [false,0,''];
  426. }
  427. }
  428. private function pay_completed($order_sn)
  429. {
  430. $logic_payment = Logic('payment');
  431. $order = $logic_payment->getVrOrderInfo($order_sn,'',true);
  432. $api_pay_amount = $order['data']['api_pay_amount'];
  433. return ($api_pay_amount == ncPriceFormat(0.0000));
  434. }
  435. public function notify_merchant($order_id,$manual)
  436. {
  437. if ($order_id <= 0) {
  438. return [false, "订单ID小于0"];
  439. }
  440. $vr_order = Model('vr_order');
  441. $refill_order = Model('refill_order');
  442. $order_info = $vr_order->getOrderInfo(['order_id' => $order_id]);
  443. $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id,'inner_status' => 0,'is_retrying' => 0]);
  444. if (empty($order_info) || empty($refill_info)) {
  445. return [false, "无此订单"];
  446. }
  447. //手动通知,之所以不做尝试,是担心客户方状态处理不当
  448. if (!$manual && $refill_info['mch_notify_state'] != 0) {
  449. return [false, "已经通知客户方"];
  450. }
  451. $notify_url = $refill_info['notify_url'];
  452. if (empty($notify_url)) {
  453. $refill_order->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => 0]);
  454. return [false, "回调地址为空"];
  455. }
  456. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  457. $order_state = intval($order_info['order_state']);
  458. if ($order_state !== ORDER_STATE_CANCEL && $order_state !== ORDER_STATE_SUCCESS) {
  459. return [false, "错误的订单状态,不能通知."];
  460. }
  461. $resp = $this->mPolicy->notify($order_info,$refill_info);
  462. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  463. if ($resp) {
  464. $refill_order->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  465. return [true, ""];
  466. }
  467. else
  468. {
  469. $refill_order->edit($order_id, ['mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  470. $times = $refill_info['mch_notify_times'] + 1;
  471. if ($times > 100) {
  472. $refill_order->edit($order_id, ['mch_notify_state' => 2]);
  473. } else {
  474. $period = 5;
  475. QueueClient::async_push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false], $period);
  476. }
  477. return [false, "通知{$times}次,失败."];
  478. }
  479. }
  480. public function query($order_id)
  481. {
  482. $mod_order = Model('vr_order');
  483. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  484. if(empty($order_info)) return false;
  485. $mod_refill = Model('refill_order');
  486. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  487. $chname = $refill_info['channel_name'];
  488. if ($refill_info['notify_state'] == 1 && in_array($order_info['order_state'], [ORDER_STATE_SUCCESS, ORDER_STATE_CANCEL])) {
  489. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  490. return true;
  491. }
  492. if($order_info['order_state'] == ORDER_STATE_SEND) {
  493. $query_able = true;
  494. }
  495. else {
  496. $query_able = false;
  497. }
  498. if($query_able)
  499. {
  500. if(empty($chname)) return false;
  501. $provider = $this->mPolicy->provider($chname);
  502. if(empty($provider)) return false;
  503. [$state, $order_state] = $provider->query($refill_info);
  504. if(!$state) {
  505. return false;
  506. }
  507. elseif($order_state == ORDER_STATE_SUCCESS) {
  508. $this->proc_notify($order_id,true,false,$chname);
  509. }
  510. elseif($order_state == ORDER_STATE_CANCEL) {
  511. $this->proc_notify($order_id,false,true,$chname);
  512. }
  513. else {
  514. Log::record("RefillBase::query order_state={$order_state}",Log::DEBUG);
  515. }
  516. }
  517. return true;
  518. }
  519. public function query_net($order_id)
  520. {
  521. $mod_order = Model('vr_order');
  522. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  523. if(empty($order_info)) return false;
  524. $mod_refill = Model('refill_order');
  525. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  526. $chname = $refill_info['channel_name'];
  527. $mchid = $refill_info['mchid'];
  528. $mch_order = $refill_info['mch_order'];
  529. if($order_info['order_state'] == ORDER_STATE_PAY) {
  530. $query_able = true;
  531. }
  532. else {
  533. $query_able = false;
  534. }
  535. if($query_able)
  536. {
  537. if(empty($chname)) return false;
  538. $provider = $this->mPolicy->provider($chname);
  539. if(empty($provider)) return false;
  540. [$state, $order_state] = $provider->query($refill_info);
  541. if(!$state) {
  542. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30);
  543. return false;
  544. }
  545. elseif($order_state == ORDER_STATE_SUCCESS || $order_state == ORDER_STATE_CANCEL)
  546. {
  547. $logic_vr_order = Logic("vr_order");
  548. $logic_vr_order->changeOrderStateSend($order_id, true);
  549. $data = ['commit_time' => time()];
  550. $mod_refill->edit($order_id, $data);
  551. QueueClient::async_push("QueryRefillState", ['order_id' => $order_id], 1);
  552. }
  553. elseif ($order_state == ORDER_STATE_NOEXIST) {
  554. $logic_vr_order = Logic("vr_order");
  555. $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}查询订单不存在.",true,true);
  556. $mod_refill->edit($order_id, ['notify_time' => time(), 'notify_state' => 1]);
  557. util::pop_queue_order($mchid,$mch_order);
  558. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
  559. }
  560. else {
  561. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30);
  562. }
  563. }
  564. return true;
  565. }
  566. public function manual_success($order_id)
  567. {
  568. $order_id = intval($order_id);
  569. if($order_id <= 0) return false;
  570. try {
  571. $mod_order = Model('vr_order');
  572. $tran = new trans_wapper($mod_order,'manual_success state trans');
  573. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true);
  574. if(!empty($order_info) && $order_info['order_state'] == ORDER_STATE_SEND) {
  575. $tran->commit();
  576. $logic_vr_order = Logic("vr_order");
  577. $logic_vr_order->changeOrderStateSuccess($order_id,true);
  578. $refill_order = Model('refill_order');
  579. $refill_order->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]);
  580. mtopcard\cards_helper::assign($order_id);
  581. $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id]);
  582. util::pop_queue_order($refill_info['mchid'],$refill_info['mch_order']);
  583. }
  584. else {
  585. $tran->commit();
  586. }
  587. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  588. return true;
  589. }
  590. catch (Exception $ex) {
  591. $tran->rollback();
  592. Log::record("manual_success exception:{$ex->getMessage()}",Log::ERR);
  593. return false;
  594. }
  595. }
  596. public function manual_cancel($order_id)
  597. {
  598. $order_id = intval($order_id);
  599. if($order_id <= 0) return false;
  600. try {
  601. $mod_order = Model('vr_order');
  602. $tran = new trans_wapper($mod_order,'manual_cancel state trans');
  603. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true);
  604. if(!empty($order_info) && $order_info['order_state'] == ORDER_STATE_SEND) {
  605. $tran->commit();
  606. $logic_vr_order = Logic("vr_order");
  607. $logic_vr_order->changeOrderStateCancel($order_info, '', "后台手动回调通知失败",true,true);
  608. $refill_order = Model('refill_order');
  609. $refill_order->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]);
  610. mtopcard\cards_helper::reuse($order_id);
  611. $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id]);
  612. util::pop_queue_order($refill_info['mchid'],$refill_info['mch_order']);
  613. }
  614. else {
  615. $tran->commit();
  616. }
  617. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  618. return true;
  619. }
  620. catch (Exception $ex) {
  621. $tran->rollback();
  622. Log::record("manual_cancel exception:{$ex->getMessage()}",Log::ERR);
  623. return false;
  624. }
  625. }
  626. public function need_intercept($mchid,$card_type,$card_state,$is_transfer)
  627. {
  628. return $this->mPolicy->need_intercept($mchid,$card_type,$card_state,$is_transfer);
  629. }
  630. public function region_intercept($quality,$card_type,$region_no)
  631. {
  632. return $this->mPolicy->region_intercept($quality,$card_type,$region_no);
  633. }
  634. public function UpdateRatio($ratios)
  635. {
  636. $this->mPolicy->update_ratios($ratios);
  637. }
  638. public function UpdateMchRatios($gross,$detail)
  639. {
  640. $this->mPolicy->update_mchratios($gross,$detail);
  641. }
  642. public function UpdateSpeeds($speeds)
  643. {
  644. $this->mPolicy->update_chspeeds($speeds);
  645. }
  646. }