RefillBase.php 27 KB

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