RefillBase.php 43 KB

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