RefillBase.php 44 KB

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