RefillBase.php 38 KB

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