RefillBase.php 25 KB

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