RefillBase.php 31 KB

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