RefillBase.php 43 KB

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