RefillBase.php 34 KB

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