RefillBase.php 43 KB

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