RefillBase.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. util::incr_user_fail($mchid,$card_type, $spec,$quality);
  129. }
  130. else {
  131. $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,不可重试.",true,true);
  132. $tran->commit();
  133. util::incr_notify($chname, $card_type, $spec, $quality, false);
  134. util::incr_amount_lock($mchid,$card_type,$spec);
  135. util::incr_user_fail($mchid,$card_type, $spec,$quality);
  136. }
  137. $mod_refill->edit($order_id, ['notify_time' => time(), 'is_retrying' => 0,'notify_state' => 1]);
  138. util::pop_queue_order($mchid,$mch_order);
  139. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  140. return true;
  141. }
  142. catch (Exception $ex) {
  143. $tran->rollback();
  144. Log::record("Error:" . $ex->getMessage(), Log::ERR);
  145. Swoole\Coroutine::sleep(5);
  146. util::push_notify($chname,$input);
  147. return false;
  148. }
  149. }
  150. private function retry(array $refill_info, array $order_info)
  151. {
  152. $trace = new scope_trace(__METHOD__);
  153. $order = order::from_db($refill_info,$order_info);
  154. if($order->is_third()) {
  155. return [false,null];
  156. }
  157. [$org_quality,$quality] = $this->find_quality($order);
  158. if($quality <= 0) {
  159. return [false,null];
  160. }
  161. else {
  162. $params = $order->queue_params();
  163. return [true,$params];
  164. }
  165. }
  166. public function zero_order(order $order,$errmsg='')
  167. {
  168. $buyer_id = $order->buyer_id();
  169. $minfo = new member_info($buyer_id);
  170. $calc = new ZeroMerchantPrice($order->mchid(), $order->spec(), $order->card_type(),$order->cur_quality());
  171. $mch_amount = $calc->calc_vgoods_price([]);
  172. $input['goods_id'] = ZERO_GOODS_ID;
  173. $input['quantity'] = 1; //数量
  174. $input['buyer_phone'] = $minfo->mobile();
  175. $input['buyer_name'] = $minfo->truename();
  176. $input['buyer_msg'] = $_POST['buyer_msg'] ?? '';
  177. $input['order_from'] = 1;
  178. $input['pd_pay'] = true;
  179. $logic_buy_virtual = Logic('buy_virtual');
  180. $result = $logic_buy_virtual->buyStep3($input, $buyer_id, [$calc, 'calc_vorder_amount'], true,false);
  181. $mod_refill = Model('refill_order');
  182. if ($result['state'] === true)
  183. {
  184. $order_sn = $result['data']['order_sn'];
  185. $order_id = $result['data']['order_id'];
  186. $logic_vr_order = Logic("vr_order");
  187. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  188. $logic_vr_order->changeOrderStateCancel($order_info, '', '无法下单创建0元订单',true,true);
  189. $mch_order = $order->mch_order();
  190. if (empty($mch_order)) {
  191. $order->set_mchorder($order_sn);
  192. }
  193. $thrid_refill = Model('thrid_refill');
  194. if($order->is_third()) {
  195. $product = $thrid_refill->getProduct(['system_code' => $order->pcode()]);
  196. $refill_amount = $product['refill_amount'];
  197. } else {
  198. $refill_amount = $order->spec();
  199. }
  200. //虚拟订单表信息扩展
  201. $orderext = $order->ZeroRefillParams($order_id,$order_sn,$refill_amount,$mch_amount,'',0,$errmsg);
  202. $order->commit_times_inc();
  203. $mod_refill->add_refill($orderext);
  204. if($order->is_third()) {
  205. $thrid_refill = Model('thrid_refill');
  206. $ext = $order->third_extparams($order_id,$order_sn);
  207. $thrid_refill->addExt($ext);
  208. }
  209. return $order_id;
  210. }
  211. else {
  212. return 0;
  213. }
  214. }
  215. //返回值:[ 错误码,错误信息,订单ID,是否是网络错误]
  216. //说明:错误码为true 表示成功
  217. // 其它情况,则需要判断订单ID
  218. public function add(order $order)
  219. {
  220. $last_orderid = $order->last_order_id();
  221. $mchid = $order->mchid();
  222. $mch_order = $order->mch_order();
  223. [$providers, $overload] = $this->mPolicy->find_providers($order);
  224. $chfilters = new channel_filter($mchid,$mch_order,$order->cur_quality(),$order->card_type());
  225. $providers = $chfilters->getProviders($providers);
  226. if (empty($providers)) {
  227. Log::record("canot find any providers", Log::DEBUG);
  228. return [errcode::CANNOT_MATCH_PROVIDER, "匹配不到合适的充值通道", $last_orderid, false, 0];
  229. }
  230. try
  231. {
  232. $minfo = new member_info($order->buyer_id());
  233. $org_quality = $order->org_quality();
  234. if(PolicyUtil::mixed_quality($org_quality)) {
  235. $calc = new CalcMerchantPrice($mchid, $order->spec(), $order->card_type(),$org_quality,$this->mPolicy,$order->thrid_params());
  236. }
  237. else {
  238. $calc = new CalcMerchantPrice($mchid, $order->spec(), $order->card_type(),$order->cur_quality(),$this->mPolicy,$order->thrid_params());
  239. }
  240. $mch_price = $calc->calc_vgoods_price([]);
  241. $mch_amount = $mch_price * $order->quantity();
  242. }
  243. catch (Exception $ex) {
  244. return [errcode::MERCHANT_PRICE_UNSETTING, "没有协商商品价格",$last_orderid,false,0];
  245. }
  246. $available = $minfo->available_predeposit();
  247. if ($mch_amount > $available) {
  248. Log::record("下单时机构余额不足,可用余额为:{$available}", Log::DEBUG);
  249. return [errcode::MERCHANT_SHORT_MONEY, "余额不足支付订单",$last_orderid,false,0];
  250. }
  251. $refill_state = false;
  252. $order_success = false;
  253. $net_errno = '';
  254. $neterr = false;
  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. // $mod_refill->edit($order_id, ['commit_time' => time(), 'notify_time' => time(), 'err_msg' => $errmsg]);
  332. }
  333. }
  334. if ($refill_state) {
  335. return [true, '', $last_orderid, false, 0];
  336. } elseif ($order_success) {
  337. return [errcode::MERCHANT_REFILL_ERROR, "充值失败", $last_orderid, $neterr, $net_errno];
  338. } else {
  339. //订单创建失败,不需要再重试了,可能已经是数据库等系统错误
  340. return [errcode::MERCHANT_REFILL_ERROR, "充值失败", $last_orderid, false, 0];
  341. // return [errcode::MERCHANT_REFILL_ERROR, "充值失败", $last_orderid, false, 'ORDER_CREATE_FAIL'];
  342. }
  343. }
  344. private function create_order(order $order, $goods_id, $minfo, $calc, $ch_name, $ch_amount, $mch_amount)
  345. {
  346. $refill_creater = function (order $order,$last_orderid,$order_id,$order_sn,$mod_refill) use($ch_name, $ch_amount,$mch_amount)
  347. {
  348. try
  349. {
  350. if($last_orderid > 0) {
  351. $mod_refill->edit($last_orderid, ['inner_status' => 1]);
  352. }
  353. $order->set_last_orderid($order_id);
  354. $thrid_refill = Model('thrid_refill');
  355. if($order->is_third()) {
  356. $product = $thrid_refill->getProduct(['system_code' => $order->pcode()]);
  357. $refill_amount = $product['refill_amount'];
  358. } else {
  359. $refill_amount = $order->spec();
  360. }
  361. if(empty($order->mch_order())) {
  362. $order->set_mchorder($order_sn);
  363. }
  364. $order->commit_times_inc();
  365. $orderext = $order->refill_params($order_id,$order_sn,$refill_amount, $ch_name, $ch_amount,$mch_amount);
  366. $fInsert = $mod_refill->add_refill($orderext);
  367. if($order->is_third()) {
  368. $ext = $order->third_extparams($order_id,$order_sn);
  369. $thrid_refill->addExt($ext);
  370. }
  371. if(!$fInsert) {
  372. return false;
  373. } else {
  374. return true;
  375. }
  376. }
  377. catch (Exception $ex)
  378. {
  379. Log::record($ex->getMessage(),Log::ERR);
  380. return false;
  381. }
  382. };
  383. $order_canceler = function ($order_id,$err_msg) {
  384. $logic_vr_order = Logic("vr_order");
  385. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  386. $logic_vr_order->changeOrderStateCancel($order_info, '', $err_msg, true, true);
  387. };
  388. try
  389. {
  390. $logic_buy_virtual = Logic('buy_virtual');
  391. $mod_refill = Model('refill_order');
  392. $input['goods_id'] = $goods_id;
  393. $input['quantity'] = $order->quantity();
  394. $input['buyer_phone'] = $minfo->mobile();
  395. $input['buyer_name'] = $minfo->truename();
  396. $input['buyer_msg'] = $_POST['buyer_msg'] ?? '';
  397. $input['order_from'] = 1;
  398. $input['pd_pay'] = true;
  399. $start = microtime(true);
  400. $result = $logic_buy_virtual->buyStep3($input, $order->buyer_id(), [$calc, 'calc_vorder_amount'], true, true);
  401. if ($result['state'] === true)
  402. {
  403. $order_sn = $result['data']['order_sn'];
  404. $order_id = $result['data']['order_id'];
  405. $last_orderid = $order->last_order_id();
  406. $fSuccess = $refill_creater($order, $last_orderid, $order_id, $order_sn, $mod_refill);
  407. if(!$fSuccess) {
  408. Log::record("refill_creater fail:order_sn={$order_sn}",Log::ERR);
  409. $order_canceler($order_id,'refill_order 记录创建失败');
  410. return [false, $order_id, $order_sn];
  411. }
  412. if (!$this->pay_completed($order_sn)) {
  413. $order_canceler($order_id,'预存款不足以支付该订单');
  414. return [false, $order_id, $order_sn];
  415. }
  416. Log::record(sprintf(__METHOD__ . " request time=%.6f", microtime(true) - $start), Log::DEBUG);
  417. return [true,$order_id,$order_sn];
  418. }
  419. else
  420. {
  421. Log::record(__METHOD__ . " fail:buyStep3 err msg={$result['msg']}", Log::ERR);
  422. return [false,0,''];
  423. }
  424. }
  425. catch (Exception $ex)
  426. {
  427. Log::record($ex->getMessage(), Log::ERR);
  428. return [false,0,''];
  429. }
  430. }
  431. private function pay_completed($order_sn)
  432. {
  433. $logic_payment = Logic('payment');
  434. $order = $logic_payment->getVrOrderInfo($order_sn,'',true);
  435. $api_pay_amount = $order['data']['api_pay_amount'];
  436. return ($api_pay_amount == ncPriceFormat(0.0000));
  437. }
  438. public function notify_merchant($order_id,$manual)
  439. {
  440. if ($order_id <= 0) {
  441. return [false, "订单ID小于0"];
  442. }
  443. $vr_order = Model('vr_order');
  444. $refill_order = Model('refill_order');
  445. $order_info = $vr_order->getOrderInfo(['order_id' => $order_id]);
  446. $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id,'inner_status' => 0,'is_retrying' => 0]);
  447. if (empty($order_info) || empty($refill_info)) {
  448. return [false, "无此订单"];
  449. }
  450. //手动通知,之所以不做尝试,是担心客户方状态处理不当
  451. if (!$manual && $refill_info['mch_notify_state'] != 0) {
  452. return [false, "已经通知客户方"];
  453. }
  454. $notify_url = $refill_info['notify_url'];
  455. if (empty($notify_url)) {
  456. $refill_order->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => 0]);
  457. return [false, "回调地址为空"];
  458. }
  459. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  460. $order_state = intval($order_info['order_state']);
  461. if ($order_state !== ORDER_STATE_CANCEL && $order_state !== ORDER_STATE_SUCCESS) {
  462. return [false, "错误的订单状态,不能通知."];
  463. }
  464. $resp = $this->mPolicy->notify($order_info,$refill_info);
  465. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  466. if ($resp) {
  467. $refill_order->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  468. return [true, ""];
  469. }
  470. else
  471. {
  472. $refill_order->edit($order_id, ['mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  473. $times = $refill_info['mch_notify_times'] + 1;
  474. if ($times > 100) {
  475. $refill_order->edit($order_id, ['mch_notify_state' => 2]);
  476. } else {
  477. $period = 5;
  478. QueueClient::async_push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false], $period);
  479. }
  480. return [false, "通知{$times}次,失败."];
  481. }
  482. }
  483. public function query($order_id)
  484. {
  485. $mod_order = Model('vr_order');
  486. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  487. if(empty($order_info)) return false;
  488. $mod_refill = Model('refill_order');
  489. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  490. $chname = $refill_info['channel_name'];
  491. if ($refill_info['notify_state'] == 1 && in_array($order_info['order_state'], [ORDER_STATE_SUCCESS, ORDER_STATE_CANCEL])) {
  492. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  493. return true;
  494. }
  495. if($order_info['order_state'] == ORDER_STATE_SEND) {
  496. $query_able = true;
  497. }
  498. else {
  499. $query_able = false;
  500. }
  501. if($query_able)
  502. {
  503. if(empty($chname)) return false;
  504. $provider = $this->mPolicy->provider($chname);
  505. if(empty($provider)) return false;
  506. [$state, $order_state] = $provider->query($refill_info);
  507. if(!$state) {
  508. return false;
  509. }
  510. elseif($order_state == ORDER_STATE_SUCCESS) {
  511. $this->proc_notify($order_id,true,false,$chname);
  512. }
  513. elseif($order_state == ORDER_STATE_CANCEL) {
  514. $this->proc_notify($order_id,false,true,$chname);
  515. }
  516. else {
  517. Log::record("RefillBase::query order_state={$order_state}",Log::DEBUG);
  518. }
  519. }
  520. return true;
  521. }
  522. public function query_net($order_id)
  523. {
  524. $mod_order = Model('vr_order');
  525. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  526. if(empty($order_info)) return false;
  527. $mod_refill = Model('refill_order');
  528. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  529. $chname = $refill_info['channel_name'];
  530. $mchid = $refill_info['mchid'];
  531. $mch_order = $refill_info['mch_order'];
  532. if($order_info['order_state'] == ORDER_STATE_PAY) {
  533. $query_able = true;
  534. }
  535. else {
  536. $query_able = false;
  537. }
  538. if($query_able)
  539. {
  540. if(empty($chname)) return false;
  541. $provider = $this->mPolicy->provider($chname);
  542. if(empty($provider)) return false;
  543. [$state, $order_state] = $provider->query($refill_info);
  544. if(!$state) {
  545. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30);
  546. return false;
  547. }
  548. elseif($order_state == ORDER_STATE_SUCCESS || $order_state == ORDER_STATE_CANCEL)
  549. {
  550. $logic_vr_order = Logic("vr_order");
  551. $logic_vr_order->changeOrderStateSend($order_id, true);
  552. $data = ['commit_time' => time()];
  553. $mod_refill->edit($order_id, $data);
  554. QueueClient::async_push("QueryRefillState", ['order_id' => $order_id], 1);
  555. }
  556. elseif ($order_state == ORDER_STATE_NOEXIST) {
  557. $logic_vr_order = Logic("vr_order");
  558. $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}查询订单不存在.",true,true);
  559. $mod_refill->edit($order_id, ['notify_time' => time(), 'notify_state' => 1]);
  560. util::pop_queue_order($mchid,$mch_order);
  561. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
  562. }
  563. else {
  564. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30);
  565. }
  566. }
  567. return true;
  568. }
  569. public function manual_success($order_id)
  570. {
  571. $order_id = intval($order_id);
  572. if($order_id <= 0) return false;
  573. try {
  574. $mod_order = Model('vr_order');
  575. $tran = new trans_wapper($mod_order,'manual_success state trans');
  576. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true);
  577. if(!empty($order_info) && $order_info['order_state'] == ORDER_STATE_SEND) {
  578. $tran->commit();
  579. $logic_vr_order = Logic("vr_order");
  580. $logic_vr_order->changeOrderStateSuccess($order_id,true);
  581. $refill_order = Model('refill_order');
  582. $refill_order->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]);
  583. mtopcard\cards_helper::assign($order_id);
  584. $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id]);
  585. util::pop_queue_order($refill_info['mchid'],$refill_info['mch_order']);
  586. }
  587. else {
  588. $tran->commit();
  589. }
  590. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  591. return true;
  592. }
  593. catch (Exception $ex) {
  594. $tran->rollback();
  595. Log::record("manual_success exception:{$ex->getMessage()}",Log::ERR);
  596. return false;
  597. }
  598. }
  599. public function manual_cancel($order_id)
  600. {
  601. $order_id = intval($order_id);
  602. if($order_id <= 0) return false;
  603. try {
  604. $mod_order = Model('vr_order');
  605. $tran = new trans_wapper($mod_order,'manual_cancel state trans');
  606. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true);
  607. if(!empty($order_info) && $order_info['order_state'] == ORDER_STATE_SEND) {
  608. $tran->commit();
  609. $logic_vr_order = Logic("vr_order");
  610. $logic_vr_order->changeOrderStateCancel($order_info, '', "后台手动回调通知失败",true,true);
  611. $refill_order = Model('refill_order');
  612. $refill_order->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]);
  613. mtopcard\cards_helper::reuse($order_id);
  614. $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id]);
  615. util::pop_queue_order($refill_info['mchid'],$refill_info['mch_order']);
  616. }
  617. else {
  618. $tran->commit();
  619. }
  620. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  621. return true;
  622. }
  623. catch (Exception $ex) {
  624. $tran->rollback();
  625. Log::record("manual_cancel exception:{$ex->getMessage()}",Log::ERR);
  626. return false;
  627. }
  628. }
  629. public function need_intercept($mchid,$card_type,$card_state,$is_transfer)
  630. {
  631. return $this->mPolicy->need_intercept($mchid,$card_type,$card_state,$is_transfer);
  632. }
  633. public function region_intercept($quality,$card_type,$region_no)
  634. {
  635. return $this->mPolicy->region_intercept($quality,$card_type,$region_no);
  636. }
  637. public function UpdateRatio($ratios)
  638. {
  639. $this->mPolicy->update_ratios($ratios);
  640. }
  641. public function UpdateMchRatios($gross,$detail)
  642. {
  643. $this->mPolicy->update_mchratios($gross,$detail);
  644. }
  645. public function UpdateSpeeds($speeds)
  646. {
  647. $this->mPolicy->update_chspeeds($speeds);
  648. }
  649. }