RefillBase.php 30 KB

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