RefillBase.php 43 KB

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