RefillBase.php 38 KB

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