RefillBase.php 28 KB

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