RefillBase.php 37 KB

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