RefillBase.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. if (empty($mch_order)) {
  182. $order->set_mchorder($order_sn);
  183. }
  184. //虚拟订单表信息扩展
  185. $orderext = $order->ZeroRefillParams($order_id,$order_sn,$mch_amount,'',0,$errmsg);
  186. $order->commit_times_inc();
  187. $mod_refill->add_refill($orderext);
  188. if($order->is_third()) {
  189. $thrid_refill = Model('thrid_refill');
  190. $ext = $order->third_extparams($order_id,$order_sn);
  191. $thrid_refill->addExt($ext);
  192. }
  193. return $order_id;
  194. }
  195. else {
  196. return 0;
  197. }
  198. }
  199. //返回值:[ 错误码,错误信息,订单ID,是否是网络错误]
  200. //说明:错误码为true 表示成功
  201. // 其它情况,则需要判断订单ID
  202. public function add(order $order)
  203. {
  204. $last_orderid = $order->last_order_id();
  205. $mchid = $order->mchid();
  206. $mch_order = $order->mch_order();
  207. [$providers, $overload] = $this->mPolicy->find_providers($order);
  208. $chfilters = new channel_filter($mchid,$mch_order,$order->cur_quality(),$order->card_type());
  209. $providers = $chfilters->getProviders($providers);
  210. if (empty($providers)) {
  211. Log::record("canot find any providers", Log::DEBUG);
  212. return [errcode::CANNOT_MATCH_PROVIDER, "匹配不到合适的充值通道", $last_orderid, false, 0];
  213. }
  214. try
  215. {
  216. $minfo = new member_info($order->buyer_id());
  217. $org_quality = $order->org_quality();
  218. if(PolicyUtil::mixed_quality($org_quality)) {
  219. $calc = new CalcMerchantPrice($mchid, $order->spec(), $order->card_type(),$org_quality,$this->mPolicy,$order->thrid_params());
  220. }
  221. else {
  222. $calc = new CalcMerchantPrice($mchid, $order->spec(), $order->card_type(),$order->cur_quality(),$this->mPolicy,$order->thrid_params());
  223. }
  224. $mch_price = $calc->calc_vgoods_price([]);
  225. $mch_amount = $mch_price * $order->quantity();
  226. }
  227. catch (Exception $ex) {
  228. return [errcode::MERCHANT_PRICE_UNSETTING, "没有协商商品价格",$last_orderid,false,0];
  229. }
  230. $available = $minfo->available_predeposit();
  231. if ($mch_amount > $available) {
  232. Log::record("下单时机构余额不足,可用余额为:{$available}", Log::DEBUG);
  233. return [errcode::MERCHANT_SHORT_MONEY, "余额不足支付订单",$last_orderid,false,0];
  234. }
  235. $refill_state = false;
  236. $order_success = false;
  237. $net_errno = 0;
  238. foreach ($providers as $provider)
  239. {
  240. $channel_name = $provider->name();
  241. [$goods_id, $price] = $provider->goods($order->cur_quality(),$order->spec(),$order->card_type(),$order->region_no(),$order->thrid_params());
  242. if ($goods_id <= 0) continue;
  243. //非组合通道,以原始质量算价格. //通道价格大于客户价格,换通道.
  244. if(PolicyUtil::mixed_quality($org_quality) == false && $price > $mch_price) {
  245. continue;
  246. }
  247. $mod_refill = Model('refill_order');
  248. $channel_amount = $price * $order->quantity();
  249. [$order_success,$order_id,$order_sn] = $this->create_refill_order($order,$goods_id,$minfo,$calc,$channel_name,$channel_amount,$mch_amount);
  250. $last_orderid = $order_id;
  251. if(!$order_success) continue;
  252. util::incr_commit_pre($channel_name, $order->card_type(), $order->spec(), $order->cur_quality());
  253. util::decr_amount_lock($mchid, $order->card_type(), $order->spec());
  254. $start = microtime(true);
  255. $net_errno = "";
  256. $params = $order->channel_params($order_id,$order_sn,$goods_id);
  257. $card_no = $order->card_no();
  258. $card_type = $order->card_type();
  259. $spec = $order->spec();
  260. $quality = $order->cur_quality();
  261. [$state, $errmsg, $neterr] = $provider->add($card_no, $card_type, $spec, $params,$net_errno);
  262. Log::record(sprintf(" %s add request time=%.6f", $channel_name,microtime(true) - $start), Log::DEBUG);
  263. if ($state)
  264. {
  265. $chfilters->add_channel($channel_name,true);
  266. util::incr_commit($channel_name,$card_type,$spec,$quality,true);
  267. $trade_no = $errmsg;
  268. $refill_type = $provider->refill_type();
  269. if ($refill_type == 'api') {
  270. $logic_vr_order = Logic("vr_order");
  271. $logic_vr_order->changeOrderStateSend($order_id,true);
  272. } elseif($refill_type == 'fetch') {
  273. $logic_vr_order = Logic("vr_order");
  274. $logic_vr_order->changeOrderStateSend($order_id,true);
  275. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  276. $mod_fetch_order = Model('fetch_order');
  277. $fetch_datas = ['order_id' => $order_id, 'order_sn' => $order_sn, 'store_id' => $order_info['store_id'],'channel_name' => $channel_name,
  278. 'fetch_status' => 1, 'card_no' => $card_no, 'card_type' => $card_type, 'refill_amount' => $spec,
  279. 'add_time' => time()];
  280. $mod_fetch_order->add($fetch_datas);
  281. }
  282. else {
  283. Log::record("Err refill_type = {$refill_type}",Log::ERR);
  284. }
  285. $data = ['commit_time' => time(), 'ch_trade_no' => $trade_no];
  286. $mod_refill->edit($order_id, $data);
  287. $refill_state = true;
  288. //如果对方没有回调能力,则启动主动查询.
  289. if($provider->callback() === false) {
  290. QueueClient::async_push("QueryRefillState",['order_id' => $order_id],60);
  291. }
  292. break;
  293. }
  294. else
  295. {
  296. $chfilters->add_channel($channel_name,false);
  297. if(!empty($neterr) && util::need_check($net_errno)) {
  298. $mod_refill->edit($order_id, ['commit_time' => time(),'neterr' => 1,'err_msg' => "neterr={$net_errno}"]);
  299. break;
  300. } else {
  301. $neterr = false;
  302. $net_errno = 0;
  303. }
  304. util::incr_commit($channel_name,$card_type,$spec,$order->cur_quality(),false);
  305. util::incr_amount_lock($mchid,$card_type,$spec);
  306. Log::record("channel:{$channel_name} err:{$errmsg}");
  307. $logic_vr_order = Logic("vr_order");
  308. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  309. $logic_vr_order->changeOrderStateCancel($order_info, '', "调用{$channel_name}接口失败",true,true);
  310. if(!is_string($errmsg)) {
  311. $errmsg = "{$errmsg}";
  312. }
  313. $mod_refill->edit($order_id, ['commit_time' => time(),'err_msg' => $errmsg]);
  314. }
  315. }
  316. if ($refill_state) {
  317. return [true, '', $last_orderid, false, 0];
  318. } elseif ($order_success) {
  319. return [errcode::MERCHANT_REFILL_ERROR, "充值失败", $last_orderid, $neterr, $net_errno];
  320. } else {
  321. return [errcode::MERCHANT_REFILL_ERROR, "充值失败", $last_orderid, false, 0];
  322. }
  323. }
  324. private function create_refill_order(order $order,$goods_id,$minfo,$calc,$ch_name,$ch_amount,$mch_amount)
  325. {
  326. try
  327. {
  328. $fTrans = false;
  329. $logic_buy_virtual = Logic('buy_virtual');
  330. $mod_refill = Model('refill_order');
  331. if($fTrans) {
  332. $trans = new trans_wapper($mod_refill,__METHOD__);
  333. } else {
  334. $start = microtime(true);
  335. }
  336. $input['goods_id'] = $goods_id;
  337. $input['quantity'] = $order->quantity();
  338. $input['buyer_phone'] = $minfo->mobile();
  339. $input['buyer_name'] = $minfo->truename();
  340. $input['buyer_msg'] = $_POST['buyer_msg'] ?? '';
  341. $input['order_from'] = 1;
  342. $input['pd_pay'] = true;
  343. $result = $logic_buy_virtual->buyStep3($input, $order->buyer_id(), [$calc, 'calc_vorder_amount'], true, false);
  344. if ($result['state'] === true)
  345. {
  346. $order_sn = $result['data']['order_sn'];
  347. $order_id = $result['data']['order_id'];
  348. $last_orderid = $order->last_order_id();
  349. if($last_orderid > 0) {
  350. $mod_refill->edit($last_orderid, ['inner_status' => 1]);
  351. }
  352. $order->set_last_orderid($order_id);
  353. $thrid_refill = Model('thrid_refill');
  354. if($order->is_third()) {
  355. $product = $thrid_refill->getProduct(['system_code' => $order->pcode()]);
  356. $refill_amount = $product['refill_amount'];
  357. } else {
  358. $refill_amount = $order->spec();
  359. }
  360. if(empty($order->mch_order())) {
  361. $order->set_mchorder($order_sn);
  362. }
  363. $order->commit_times_inc();
  364. $orderext = $order->refill_params($order_id,$order_sn,$refill_amount, $ch_name, $ch_amount,$mch_amount);
  365. $mod_refill->add_refill($orderext);
  366. if($order->is_third()) {
  367. $ext = $order->third_extparams($order_id,$order_sn);
  368. $thrid_refill->addExt($ext);
  369. }
  370. if(!$this->pay_completed($order_sn))
  371. {
  372. $logic_vr_order = Logic("vr_order");
  373. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  374. $logic_vr_order->changeOrderStateCancel($order_info, '', "预存款不足以支付该订单",true,true);
  375. if($fTrans) {
  376. $trans->commit();
  377. }
  378. return [false,$order_id,$order_sn];
  379. }
  380. elseif($fTrans) {
  381. $trans->commit();
  382. }
  383. else {
  384. Log::record(sprintf(__METHOD__ . " request time=%.6f", microtime(true) - $start), Log::DEBUG);
  385. }
  386. return [true,$order_id,$order_sn];
  387. }
  388. else
  389. {
  390. if ($fTrans) $trans->commit();
  391. Log::record("{$result['msg']}", Log::ERR);
  392. return [false,0,''];
  393. }
  394. }
  395. catch (Exception $ex)
  396. {
  397. Log::record($ex->getMessage(), Log::ERR);
  398. if($fTrans) {
  399. $trans->rollback();
  400. }
  401. return [false,0,''];
  402. }
  403. }
  404. private function pay_completed($order_sn)
  405. {
  406. $logic_payment = Logic('payment');
  407. $order = $logic_payment->getVrOrderInfo($order_sn,'',true);
  408. $api_pay_amount = $order['data']['api_pay_amount'];
  409. return ($api_pay_amount == ncPriceFormat(0.00));
  410. }
  411. public function notify_merchant($order_id,$manual)
  412. {
  413. if ($order_id <= 0) {
  414. return [false, "订单ID小于0"];
  415. }
  416. $vr_order = Model('vr_order');
  417. $refill_order = Model('refill_order');
  418. $order_info = $vr_order->getOrderInfo(['order_id' => $order_id]);
  419. $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id,'inner_status' => 0,'is_retrying' => 0]);
  420. if (empty($order_info) || empty($refill_info)) {
  421. return [false, "无此订单"];
  422. }
  423. //手动通知,之所以不做尝试,是担心客户方状态处理不当
  424. if (!$manual && $refill_info['mch_notify_state'] != 0) {
  425. return [false, "已经通知客户方"];
  426. }
  427. $notify_url = $refill_info['notify_url'];
  428. if (empty($notify_url)) {
  429. $refill_order->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => 0]);
  430. return [false, "回调地址为空"];
  431. }
  432. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  433. $order_state = intval($order_info['order_state']);
  434. if ($order_state !== ORDER_STATE_CANCEL && $order_state !== ORDER_STATE_SUCCESS) {
  435. return [false, "错误的订单状态,不能通知."];
  436. }
  437. $resp = $this->mPolicy->notify($order_info,$refill_info);
  438. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  439. if ($resp) {
  440. $refill_order->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  441. return [true, ""];
  442. }
  443. else
  444. {
  445. $refill_order->edit($order_id, ['mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  446. $times = $refill_info['mch_notify_times'] + 1;
  447. if ($times > 100) {
  448. $refill_order->edit($order_id, ['mch_notify_state' => 2]);
  449. } else {
  450. $period = 5;
  451. QueueClient::async_push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false], $period);
  452. }
  453. return [false, "通知{$times}次,失败."];
  454. }
  455. }
  456. public function query($order_id)
  457. {
  458. $mod_order = Model('vr_order');
  459. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  460. if(empty($order_info)) return false;
  461. $mod_refill = Model('refill_order');
  462. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  463. $chname = $refill_info['channel_name'];
  464. if($refill_info['notify_state'] == 1) {
  465. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  466. return true;
  467. }
  468. if($order_info['order_state'] == ORDER_STATE_SEND) {
  469. $query_able = true;
  470. }
  471. else {
  472. $query_able = false;
  473. }
  474. if($query_able)
  475. {
  476. if(empty($chname)) return false;
  477. $provider = $this->mPolicy->provider($chname);
  478. if(empty($provider)) return false;
  479. [$state, $order_state] = $provider->query($refill_info);
  480. if(!$state) {
  481. return false;
  482. }
  483. elseif($order_state == ORDER_STATE_SUCCESS) {
  484. $this->proc_notify($order_id,true,false,$chname);
  485. }
  486. elseif($order_state == ORDER_STATE_CANCEL) {
  487. $this->proc_notify($order_id,false,true,$chname);
  488. }
  489. else {
  490. Log::record("RefillBase::query order_state={$order_state}",Log::DEBUG);
  491. }
  492. }
  493. return true;
  494. }
  495. public function query_net($order_id)
  496. {
  497. $mod_order = Model('vr_order');
  498. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  499. if(empty($order_info)) return false;
  500. $mod_refill = Model('refill_order');
  501. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  502. $chname = $refill_info['channel_name'];
  503. $mchid = $refill_info['mchid'];
  504. $mch_order = $refill_info['mch_order'];
  505. $card_type = intval($refill_info['card_type']);
  506. if($order_info['order_state'] == ORDER_STATE_PAY) {
  507. $query_able = true;
  508. }
  509. else {
  510. $query_able = false;
  511. }
  512. if($query_able)
  513. {
  514. if(empty($chname)) return false;
  515. $provider = $this->mPolicy->provider($chname);
  516. if(empty($provider)) return false;
  517. [$state, $order_state] = $provider->query($refill_info);
  518. if(!$state) {
  519. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30);
  520. return false;
  521. }
  522. elseif($order_state == ORDER_STATE_SUCCESS || $order_state == ORDER_STATE_CANCEL)
  523. {
  524. $logic_vr_order = Logic("vr_order");
  525. $logic_vr_order->changeOrderStateSend($order_id,true);
  526. $data = ['commit_time' => time()];
  527. $mod_refill->edit($order_id, $data);
  528. QueueClient::async_push("QueryRefillState",['order_id' => $order_id],1);
  529. }
  530. elseif ($order_state == ORDER_STATE_NOEXIST) {
  531. $logic_vr_order = Logic("vr_order");
  532. $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}查询订单不存在.",true,true);
  533. $mod_refill->edit($order_id, ['notify_time' => time(), 'notify_state' => 1]);
  534. util::pop_queue_order($mchid,$mch_order);
  535. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  536. }
  537. else {
  538. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30);
  539. }
  540. }
  541. return true;
  542. }
  543. public function manual_success($order_id)
  544. {
  545. $order_id = intval($order_id);
  546. if($order_id <= 0) return false;
  547. try {
  548. $mod_order = Model('vr_order');
  549. $tran = new trans_wapper($mod_order,'manual_success state trans');
  550. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true);
  551. if(!empty($order_info) && $order_info['order_state'] == ORDER_STATE_SEND) {
  552. $tran->commit();
  553. $logic_vr_order = Logic("vr_order");
  554. $logic_vr_order->changeOrderStateSuccess($order_id,true);
  555. $refill_order = Model('refill_order');
  556. $refill_order->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]);
  557. mtopcard\cards_helper::assign($order_id);
  558. $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id]);
  559. util::pop_queue_order($refill_info['mchid'],$refill_info['mch_order']);
  560. }
  561. else {
  562. $tran->commit();
  563. }
  564. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  565. return true;
  566. }
  567. catch (Exception $ex) {
  568. $tran->rollback();
  569. Log::record("manual_success exception:{$ex->getMessage()}",Log::ERR);
  570. return false;
  571. }
  572. }
  573. public function manual_cancel($order_id)
  574. {
  575. $order_id = intval($order_id);
  576. if($order_id <= 0) return false;
  577. try {
  578. $mod_order = Model('vr_order');
  579. $tran = new trans_wapper($mod_order,'manual_cancel state trans');
  580. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true);
  581. if(!empty($order_info) && $order_info['order_state'] == ORDER_STATE_SEND) {
  582. $tran->commit();
  583. $logic_vr_order = Logic("vr_order");
  584. $logic_vr_order->changeOrderStateCancel($order_info, '', "后台手动回调通知失败",true,true);
  585. $refill_order = Model('refill_order');
  586. $refill_order->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]);
  587. mtopcard\cards_helper::reuse($order_id);
  588. $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id]);
  589. util::pop_queue_order($refill_info['mchid'],$refill_info['mch_order']);
  590. }
  591. else {
  592. $tran->commit();
  593. }
  594. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  595. return true;
  596. }
  597. catch (Exception $ex) {
  598. $tran->rollback();
  599. Log::record("manual_cancel exception:{$ex->getMessage()}",Log::ERR);
  600. return false;
  601. }
  602. }
  603. public function UpdateRatio($ratios)
  604. {
  605. $this->mPolicy->update_ratios($ratios);
  606. }
  607. public function UpdatMchRatios($ratios)
  608. {
  609. $this->mPolicy->update_mchratios($ratios);
  610. }
  611. }