RefillBase.php 36 KB

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