RefillBase.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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. //return [是否必须切换质量, 等待时间(如果必须,等待时间为0,如果不必是延迟秒数)]
  46. public function must_changeto_quality(order $order, $quality)
  47. {
  48. return $this->mPolicy->must_changeto_quality($order, $quality);
  49. }
  50. private function third_can_retry(order $order) : array
  51. {
  52. return $this->mPolicy->third_retry($order);
  53. }
  54. public function notify($chname, $input)
  55. {
  56. $caller = $this->mPolicy->getCaller($chname);
  57. if($caller === false) {
  58. return false;
  59. }
  60. elseif ($caller->verify($input))
  61. {
  62. $values = $caller->notify($input);
  63. if(count($values) == 4) { //老的接口返回四个参数
  64. [$order_id, $success, $can_try, $need_handle] = $values;
  65. $official_sn = false;
  66. } else {
  67. [$order_id, $success, $can_try, $need_handle, $official_sn] = $values;
  68. }
  69. if (!$need_handle) {
  70. return true;
  71. }
  72. if ($order_id !== false) {
  73. return $this->proc_notify($order_id, $success, $can_try, $chname, $input, $official_sn);
  74. } else {
  75. Log::record("$chname callback 系统无此订单ID.", Log::WARING);
  76. }
  77. }
  78. else {
  79. $orgdata = json_encode($input);
  80. Log::record("$chname 签名失败:input=$orgdata",Log::WARING);
  81. }
  82. return true;
  83. }
  84. private function risksn_check($refill_info, $order_info, $org_official_sn)
  85. {
  86. $in_exclude = function ($store_id)
  87. {
  88. $store_id = intval($store_id);
  89. if($store_id <= 0) {
  90. return false;
  91. }
  92. global $config;
  93. $risk_official_sn = $config['risk_official_sn'] ?? [];
  94. $exclude_stores = $risk_official_sn['exclude_stores'] ?? [];
  95. return in_array($store_id,$exclude_stores);
  96. };
  97. $card_type = intval($refill_info['card_type']);
  98. $chk_types = [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard];
  99. $store_id = intval($order_info['store_id']);
  100. if($in_exclude($store_id)) {
  101. return false;
  102. }
  103. if(!in_array($card_type,$chk_types)) {
  104. return false;
  105. }
  106. if ($org_official_sn !== false)
  107. {
  108. $length = strlen($org_official_sn);
  109. if($length < 6 or $length > 48) {
  110. return true;
  111. }
  112. }
  113. $quality = intval($refill_info['quality']);
  114. $official_sn = $refill_info['official_sn'];
  115. if($quality === Quality::Normal && empty($official_sn)) {
  116. return true;
  117. }
  118. $start_with = function ($haystack, $needle) {
  119. $length = strlen($needle);
  120. return (substr($haystack, 0, $length) === $needle);
  121. };
  122. $headers = ['SP', 'J98', 'WX', '110110']; //, '11010336J' '0095','ZF2023'
  123. $spcheker = function () use ($start_with, $headers, $official_sn)
  124. {
  125. $official_sn = strtoupper($official_sn);
  126. foreach ($headers as $header)
  127. {
  128. if ($start_with($official_sn, $header)) {
  129. return true;
  130. }
  131. }
  132. return false;
  133. };
  134. return $spcheker();
  135. }
  136. private function proc_notify($order_id, $success, $can_try, $chname, $input = [], $official_sn = false)
  137. {
  138. $mod_order = Model('vr_order');
  139. $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id]);
  140. $order_state = intval($order_info['order_state']);
  141. if ($order_state == ORDER_STATE_PAY)
  142. {
  143. if(!empty($input)) {
  144. Log::record(__METHOD__ . " recv notify when order_state=ORDER_STATE_PAY" ,Log::DEBUG);
  145. Swoole\Coroutine::sleep(5);
  146. util::push_notify($chname,$input);
  147. }
  148. return false;
  149. }
  150. if ($order_state != ORDER_STATE_SEND) {
  151. return false;
  152. }
  153. $logic_vr_order = Logic("vr_order");
  154. $mod_refill = Model('refill_order');
  155. try
  156. {
  157. $tran = new trans_wapper($mod_order,'notify change order state trans');
  158. $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id], '*', true, true);
  159. $order_state = intval($order_info['order_state']);
  160. if ($order_state != ORDER_STATE_SEND) {
  161. $tran->commit();
  162. return false;
  163. }
  164. $refill_info = $mod_refill->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0]);
  165. if(empty($refill_info)) {
  166. $tran->commit();
  167. return false;
  168. }
  169. $quality = intval($refill_info['quality']);
  170. $card_type = intval($refill_info['card_type']);
  171. $spec = intval($refill_info['refill_amount']);
  172. $mchid = intval($refill_info['mchid']);
  173. $mch_order = $refill_info['mch_order'];
  174. $order_time = intval($refill_info['order_time']);
  175. if ($success)
  176. {
  177. $params = ['store_id' => $order_info['store_id'],
  178. 'channel_name' => $refill_info['channel_name'],
  179. 'order_sn' => $refill_info['order_sn'],
  180. 'order_id' => $refill_info['order_id'],
  181. 'official_sn' => $refill_info['official_sn']];
  182. if ($this->risksn_check($refill_info, $order_info, $official_sn)) {
  183. $tran->commit();
  184. QueueClient::async_push("OnRiskSN", $params, 1);
  185. return false;
  186. } else {
  187. QueueClient::async_push("OnRefillSuccess", $params, 1);
  188. }
  189. }
  190. $commit_time = intval($refill_info['commit_time']);
  191. $period = time() - $commit_time;
  192. util::monitor_notify($chname,$spec,$card_type,$refill_info['channel_amount'],$period,$success,$commit_time,$refill_info['mch_amount']);
  193. util::onEventNotify($refill_info,$order_info,$success);
  194. if ($success) {
  195. $logic_vr_order->changeOrderStateSuccess($order_id,true);
  196. $tran->commit();
  197. util::onOrderSuccess($refill_info,$order_info);
  198. }
  199. elseif ($can_try)
  200. {
  201. $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,正在重试", true, true);
  202. $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['is_retrying' => 1,'notify_time' => time()]);
  203. $tran->commit();
  204. if ($card_type == mtopcard\ThirdRefillCard) {
  205. $thrid_refill = Model('thrid_refill');
  206. $third_info = $thrid_refill->getThird($order_id);
  207. $third_info = $thrid_refill->trans_order($third_info);
  208. } else {
  209. $third_info = [];
  210. }
  211. $order = order::from_db($refill_info, $order_info, $third_info);
  212. $ch_filter = $order->filter();
  213. $ch_filter->notify($refill_info['quality'], $chname);
  214. [$can_retry,$params] = $this->retry($order);
  215. if ($can_retry)
  216. {
  217. if($order->is_third())
  218. {
  219. if(util::push_addthird($params)) {
  220. return true;
  221. }
  222. }
  223. elseif(util::push_add($params)) {
  224. return true;
  225. }
  226. }
  227. else {
  228. $order->finish();
  229. }
  230. }
  231. else {
  232. $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,不可重试.",true,true);
  233. $tran->commit();
  234. }
  235. $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['notify_time' => time(), 'is_retrying' => 0,'notify_state' => 1]);
  236. util::monitor_callback($mchid, $spec, $card_type, $refill_info['mch_amount'], $refill_info['channel_amount'], $success, $order_time);
  237. util::onEventComplete($refill_info, $order_info, $success);
  238. util::pop_queue_order($mchid,$mch_order,$order_time);
  239. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  240. return true;
  241. }
  242. catch (Exception $ex) {
  243. $tran->rollback();
  244. Log::record("Error:" . $ex->getMessage(), Log::ERR);
  245. Swoole\Coroutine::sleep(5);
  246. util::push_notify($chname,$input);
  247. return false;
  248. }
  249. }
  250. private function retry(order $order)
  251. {
  252. $trace = new scope_trace(__METHOD__);
  253. if($order->is_third())
  254. {
  255. [$can_retry,$next_pcode] = $this->third_can_retry($order);
  256. if($can_retry) {
  257. $params = $order->third_requeue_params($next_pcode);
  258. return [$can_retry,$params];
  259. }
  260. else {
  261. return [false,null];
  262. }
  263. }
  264. $params = $order->queue_params();
  265. return [true,$params];
  266. }
  267. public function success_order(order $order) : array
  268. {
  269. $last_orderid = $order->last_order_id();
  270. $mchid = $order->mchid();
  271. $order_time = $order->order_time();
  272. try
  273. {
  274. $minfo = new member_info($order->buyer_id());
  275. $calc = $this->getMerchantPriceCalc($order);
  276. $mch_price = $calc->calc_vgoods_price([]);
  277. $mch_amount = $mch_price * $order->quantity();
  278. }
  279. catch (Exception $ex) {
  280. return [false,$last_orderid,'没有协商商品价格'];
  281. }
  282. $available = $minfo->available_predeposit();
  283. if ($mch_amount > $available) {
  284. return [false, $last_orderid,"余额不足支付订单"];
  285. }
  286. $goods_id = ZERO_GOODS_ID;
  287. $channel_name = 'cbproxy';
  288. $channel_amount = $mch_amount;
  289. $mod_refill = Model('refill_order');
  290. [$order_success, $order_id, $order_sn] = $this->create_order($order, $goods_id, $minfo, $calc, $channel_name, $channel_amount, $mch_amount);
  291. if(!$order_success) {
  292. return [false,$last_orderid,'订单创建失败'];
  293. }
  294. else {
  295. $logic_vr_order = Logic("vr_order");
  296. $logic_vr_order->changeOrderStateSuccess($order_id,true);
  297. $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['commit_time' => time(),'notify_time' => time(), 'is_retrying' => 0,'notify_state' => 1]);
  298. util::monitor_callback($mchid, $order->spec(), $order->card_type(), $mch_amount, $channel_amount, true, $order->order_time());
  299. $refill_info = $mod_refill->partition(util::part_refill($order_time))->getOrderInfo(['order_id' => $order_id]);
  300. $mod_order = Model('vr_order');
  301. $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id]);
  302. util::onEventComplete($refill_info, $order_info, true);
  303. return [true,$order_id,''];
  304. }
  305. }
  306. public function zero_order(order $order,$errmsg='')
  307. {
  308. $buyer_id = $order->buyer_id();
  309. $minfo = new member_info($order->buyer_id());
  310. // try
  311. // {
  312. // $calc = $this->getMerchantPriceCalc($order);
  313. // $mch_price = $calc->calc_vgoods_price([]);
  314. // $mch_amount = $mch_price * $order->quantity();
  315. // }
  316. // catch (Exception $ex) {
  317. // $mch_price = 0.00;
  318. // $mch_amount = 0.00;
  319. // }
  320. $calc = new ZeroMerchantPrice($order->mchid(), $order->spec(), $order->card_type(),$order->cur_quality());
  321. $mch_amount = $calc->calc_vgoods_price([]);
  322. $input['goods_id'] = ZERO_GOODS_ID;
  323. $input['quantity'] = 1; //数量
  324. $input['buyer_phone'] = $minfo->mobile();
  325. $input['buyer_name'] = $minfo->truename();
  326. $input['buyer_msg'] = $_POST['buyer_msg'] ?? '';
  327. $input['order_from'] = 1;
  328. $input['pd_pay'] = true;
  329. $logic_buy_virtual = Logic('buy_virtual');
  330. $result = $logic_buy_virtual->buyStep3($input, $buyer_id, [$calc, 'calc_vorder_amount'], true, true);
  331. $mod_refill = Model('refill_order');
  332. if ($result['state'] === true)
  333. {
  334. $order_sn = $result['data']['order_sn'];
  335. $order_id = $result['data']['order_id'];
  336. $logic_vr_order = Logic("vr_order");
  337. $order_info = Model('vr_order')->partition(util::part_vr_create())->getOrderInfo(['order_id' => $order_id]);
  338. $logic_vr_order->changeOrderStateCancel($order_info, '', '无法下单创建0元订单',true,true);
  339. $mch_order = $order->mch_order();
  340. if (empty($mch_order)) {
  341. $order->set_mchorder($order_sn);
  342. }
  343. $thrid_refill = Model('thrid_refill');
  344. if($order->is_third()) {
  345. $product = $thrid_refill->getProduct(['system_code' => $order->pcode()]);
  346. $refill_amount = $product['refill_amount'];
  347. } else {
  348. $refill_amount = $order->spec();
  349. }
  350. //虚拟订单表信息扩展
  351. $orderext = $order->ZeroRefillParams($order_id,$order_sn,$refill_amount,$mch_amount,'',0,$errmsg);
  352. $order->commit_times_inc();
  353. $mod_refill->add_refill($orderext);
  354. if($order->is_third()) {
  355. $thrid_refill = Model('thrid_refill');
  356. $ext = $order->third_extparams($order_id,$order_sn);
  357. $thrid_refill->addExt($ext);
  358. }
  359. return $order_id;
  360. }
  361. else {
  362. return 0;
  363. }
  364. }
  365. public function mch_amount(order $order)
  366. {
  367. $mchid = $order->mchid();
  368. $org_quality = $order->org_quality();
  369. try
  370. {
  371. $calc = $this->getMerchantPriceCalc($order);
  372. $mch_price = $calc->calc_vgoods_price([]);
  373. $mch_amount = $mch_price * $order->quantity();
  374. return $mch_amount;
  375. } catch (Exception $ex) {
  376. $spec = $order->spec();
  377. $card_type = $order->card_type();
  378. $cur_quality = $order->cur_quality();
  379. Log::record("onError mchid=$mchid org_quality=$org_quality cur_qua=$cur_quality spec=$spec card_type=$card_type :" . $ex->getMessage(), Log::ERR);
  380. return false;
  381. }
  382. }
  383. //返回值:[ 错误码,错误信息,订单ID,是否是网络错误]
  384. //说明:错误码为true 表示成功
  385. // 其它情况,则需要判断订单ID
  386. public function add(order $order)
  387. {
  388. $last_orderid = $order->last_order_id();
  389. $order_time = $order->order_time();
  390. $mchid = $order->mchid();
  391. $mch_order = $order->mch_order();
  392. [$providers, $overload] = $this->mPolicy->find_providers($order);
  393. $ch_times = $this->mPolicy->get_times();
  394. $chfilters = $order->filter();
  395. $cur_quality = $order->cur_quality();
  396. $providers = $chfilters->getProviders($cur_quality, $providers, $ch_times);
  397. if (empty($providers)) {
  398. Log::record("canot find any providers", Log::DEBUG);
  399. return [errcode::MERCHANT_REFILL_CHLIMIT, "匹配不到任何通道", $last_orderid, false, 0];
  400. }
  401. try
  402. {
  403. $minfo = new member_info($order->buyer_id());
  404. $org_quality = $order->org_quality();
  405. $calc = $this->getMerchantPriceCalc($order);
  406. $mch_price = $calc->calc_vgoods_price([]);
  407. $mch_amount = $mch_price * $order->quantity();
  408. }
  409. catch (Exception $ex) {
  410. return [errcode::MERCHANT_PRICE_UNSETTING, "没有协商商品价格",$last_orderid,false,0];
  411. }
  412. $available = $minfo->available_predeposit();
  413. if ($mch_amount > $available) {
  414. $errmsg = "余额不足=$available";
  415. Log::record($errmsg, Log::DEBUG);
  416. return [errcode::MERCHANT_SHORT_MONEY, $errmsg, $last_orderid, false, 0];
  417. }
  418. foreach ($providers as $provider)
  419. {
  420. $channel_name = $provider->name();
  421. $can_commit = util::onEventBeforeCommit($order, $channel_name);
  422. if ($can_commit === false) {
  423. continue;
  424. }
  425. if($this->mPolicy->is_over_chspeed($channel_name)) {
  426. Log::record("chspeed is over:$channel_name",Log::DEBUG);
  427. continue;
  428. }
  429. [$goods_id, $ch_price] = $provider->goods($order->cur_quality(), $order->spec(), $order->card_type(), $order->region_no(), $order->thrid_params());
  430. if ($goods_id <= 0) continue;
  431. if($ch_price > $mch_price)
  432. {
  433. //非组合通道,以原始质量算价格. //通道价格大于客户价格,换通道.
  434. if($order->is_third())
  435. {
  436. $third_mixed = $this->mPolicy->third_mixed($order->mchid(),$order->pcode());
  437. if(!$third_mixed) {
  438. continue;
  439. }
  440. }
  441. elseif(PolicyUtil::mixed_quality($org_quality))
  442. {
  443. if (!$order->can_over_price()) {
  444. Log::record("can_over_price is false", Log::DEBUG);
  445. continue;
  446. } else {
  447. Log::record("can_over_price is true", Log::DEBUG);
  448. }
  449. }
  450. else {
  451. continue;
  452. }
  453. }
  454. $mod_refill = Model('refill_order');
  455. $channel_amount = $ch_price * $order->quantity();
  456. [$order_success, $order_id, $order_sn] = $this->create_order($order, $goods_id, $minfo, $calc, $channel_name, $channel_amount, $mch_amount);
  457. $last_orderid = $order_id;
  458. if(!$order_success) {
  459. return [errcode::MERCHANT_REFILL_DBERROR, "充值失败", $last_orderid, false, 0];
  460. }
  461. $start = microtime(true);
  462. $net_errno = "";
  463. $params = $order->channel_params($order_id, $order_sn, $goods_id, $ch_price);
  464. $card_no = $order->card_no();
  465. $card_type = $order->card_type();
  466. $spec = $order->spec();
  467. $quality = $order->cur_quality();
  468. [$state, $errmsg, $neterr] = $provider->add($card_no, $card_type, $spec, $params, $net_errno);
  469. Log::record(sprintf(" %s add request time=%.6f: state=$state neterr=$neterr,net_errno=$net_errno", $channel_name,microtime(true) - $start), Log::DEBUG);
  470. if ($state)
  471. {
  472. $commit_time = time();
  473. $chfilters->add($cur_quality,$channel_name,true);
  474. util::monitor_commit($channel_name, $spec, $card_type, $channel_amount, $commit_time);
  475. util::onEventCommit($order,$channel_name);
  476. $trade_no = $errmsg;
  477. $refill_type = $provider->refill_type();
  478. $vr_part = util::part_vr_order_time($order_time);
  479. //api回调模式
  480. if ($refill_type == 'api') {
  481. $logic_vr_order = Logic("vr_order");
  482. $logic_vr_order->changeOrderStateSend($order_id, true, $vr_part);
  483. }
  484. //抢单子模式
  485. elseif($refill_type == 'fetch') {
  486. $logic_vr_order = Logic("vr_order");
  487. $logic_vr_order->changeOrderStateSend($order_id, true, $vr_part);
  488. $order_info = Model('vr_order')->partition($vr_part)->getOrderInfo(['order_id' => $order_id]);
  489. $mod_fetch_order = Model('fetch_order');
  490. $fetch_datas = ['order_id' => $order_id, 'order_sn' => $order_sn, 'store_id' => $order_info['store_id'],'channel_name' => $channel_name,
  491. 'fetch_status' => 1, 'card_no' => $card_no, 'card_type' => $card_type, 'refill_amount' => $spec,
  492. 'add_time' => time()];
  493. $mod_fetch_order->add($fetch_datas);
  494. }
  495. else {
  496. Log::record("Err refill_type = $refill_type",Log::ERR);
  497. }
  498. $data = ['commit_time' => $commit_time, 'ch_trade_no' => $trade_no];
  499. $mod_refill->partition(util::part_refill($order_time))->edit($order_id, $data);
  500. //主动查询模式。如果对方没有回调能力,则启动主动查询.
  501. if($provider->callback() === false) {
  502. QueueClient::async_push("QueryAutoRefillState",['order_id' => $order_id,'query_times' => 0],3);
  503. }
  504. return [true, '', $last_orderid, false, 0];
  505. }
  506. else
  507. {
  508. $chfilters->add($cur_quality, $channel_name, false);
  509. if(!empty($neterr) && util::need_check($net_errno)) {
  510. $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['commit_time' => time(),'neterr' => 1,'err_msg' => "neterr=$net_errno"]);
  511. util::monitor_netchk($channel_name,false);
  512. util::onEventNeterror($order,$channel_name);
  513. return [errcode::MERCHANT_REFILL_NETERROR, "充值失败", $last_orderid, $neterr, $net_errno];
  514. }
  515. //可以再提单
  516. Log::record("channel:$channel_name err:$errmsg");
  517. $logic_vr_order = Logic("vr_order");
  518. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  519. $logic_vr_order->changeOrderStateCancel($order_info, '', "调用{$channel_name}接口失败",true,true);
  520. if(!is_string($errmsg)) {
  521. $errmsg = "$errmsg";
  522. }
  523. $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['commit_time' => time(), 'err_msg' => $errmsg]);
  524. }
  525. }
  526. return [errcode::MERCHANT_REFILL_CHLIMIT, "无可用通道", $last_orderid, false, 0];
  527. }
  528. private function create_order(order $order, $goods_id, $minfo, $calc, $ch_name, $ch_amount, $mch_amount)
  529. {
  530. $refill_creater = function (order $order,$last_orderid,$order_id,$order_sn,$mod_refill) use($ch_name, $ch_amount,$mch_amount)
  531. {
  532. try
  533. {
  534. if($last_orderid > 0) {
  535. $order_time = $order->order_time();
  536. $mod_refill->partition(util::part_refill($order_time))->edit($last_orderid, ['inner_status' => 1]);
  537. }
  538. $order->set_last_orderid($order_id);
  539. $thrid_refill = Model('thrid_refill');
  540. if($order->is_third()) {
  541. $product = $thrid_refill->getProduct(['system_code' => $order->pcode(),'opened' => 1]);
  542. $refill_amount = $product['refill_amount'];
  543. } else {
  544. $refill_amount = $order->spec();
  545. }
  546. if(empty($order->mch_order())) {
  547. $order->set_mchorder($order_sn);
  548. }
  549. $order->commit_times_inc();
  550. $orderext = $order->refill_params($order_id, $order_sn, $refill_amount, $ch_name, $ch_amount, $mch_amount);
  551. $fInsert = $mod_refill->add_refill($orderext);
  552. if($order->is_third()) {
  553. $ext = $order->third_extparams($order_id,$order_sn);
  554. $thrid_refill->addExt($ext);
  555. }
  556. if(!$fInsert) {
  557. return false;
  558. } else {
  559. return true;
  560. }
  561. }
  562. catch (Exception $ex)
  563. {
  564. Log::record($ex->getMessage(),Log::ERR);
  565. return false;
  566. }
  567. };
  568. $order_canceler = function ($order_id,$err_msg) {
  569. $logic_vr_order = Logic("vr_order");
  570. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  571. $logic_vr_order->changeOrderStateCancel($order_info, '', $err_msg, true, true);
  572. };
  573. try
  574. {
  575. $logic_buy_virtual = Logic('buy_virtual');
  576. $mod_refill = Model('refill_order');
  577. $input['goods_id'] = $goods_id;
  578. $input['quantity'] = $order->quantity();
  579. $input['buyer_phone'] = $minfo->mobile();
  580. $input['buyer_name'] = $minfo->truename();
  581. $input['buyer_msg'] = $_POST['buyer_msg'] ?? '';
  582. $input['order_from'] = 1;
  583. $input['pd_pay'] = true;
  584. $start = microtime(true);
  585. $result = $logic_buy_virtual->buyStep3($input, $order->buyer_id(), [$calc, 'calc_vorder_amount'], true, true);
  586. if ($result['state'] === true)
  587. {
  588. $order_sn = $result['data']['order_sn'];
  589. $order_id = $result['data']['order_id'];
  590. $last_orderid = $order->last_order_id();
  591. $fSuccess = $refill_creater($order, $last_orderid, $order_id, $order_sn, $mod_refill);
  592. if(!$fSuccess) {
  593. Log::record("refill_creater fail: order_sn=$order_sn", Log::ERR);
  594. $order_canceler($order_id,'refill_order 记录创建失败');
  595. return [false, $order_id, $order_sn];
  596. }
  597. if (!$this->pay_completed($order_sn)) {
  598. $order_canceler($order_id,'预存款不足以支付该订单');
  599. return [false, $order_id, $order_sn];
  600. }
  601. Log::record(sprintf(__METHOD__ . " request time=%.6f", microtime(true) - $start), Log::DEBUG);
  602. return [true, $order_id, $order_sn];
  603. }
  604. else
  605. {
  606. Log::record(__METHOD__ . " fail:buyStep3 err msg={$result['msg']}", Log::ERR);
  607. return [false, 0, ''];
  608. }
  609. }
  610. catch (Exception $ex)
  611. {
  612. Log::record($ex->getMessage(), Log::ERR);
  613. return [false, 0, ''];
  614. }
  615. }
  616. private function pay_completed($order_sn)
  617. {
  618. $logic_payment = Logic('payment');
  619. $order = $logic_payment->getVrOrderInfo($order_sn,'',true);
  620. $api_pay_amount = $order['data']['api_pay_amount'];
  621. return ($api_pay_amount == ncPriceFormat(0.0000));
  622. }
  623. public function notify_merchant($order_id,$manual)
  624. {
  625. if ($order_id <= 0) {
  626. return [false, "订单ID小于0"];
  627. }
  628. $mod_order = Model('vr_order');
  629. $mod_refill = Model('refill_order');
  630. if ($manual) {
  631. $part = '';
  632. } else {
  633. $part = util::part_notify();
  634. }
  635. $order_info = $mod_order->partition($part)->getOrderInfo(['order_id' => $order_id]);
  636. $refill_info = $mod_refill->partition($part)->getOrderInfo(['order_id' => $order_id,'inner_status' => 0,'is_retrying' => 0]);
  637. if (empty($order_info) || empty($refill_info)) {
  638. return [false, "无此订单"];
  639. }
  640. if($refill_info['mch_notify_times'] == 0) {
  641. $card_no = $refill_info['card_no'];
  642. $spec = $refill_info['refill_amount'];
  643. util::loop_order_dec($card_no,$spec);
  644. }
  645. //手动通知,之所以不做尝试,是担心客户方状态处理不当
  646. if (!$manual && $refill_info['mch_notify_state'] != 0) {
  647. return [false, "已经通知客户方"];
  648. }
  649. $notify_url = $refill_info['notify_url'];
  650. $order_time = intval($refill_info['order_time']);
  651. $part = util::part_refill($order_time);
  652. if (empty($notify_url)) {
  653. $mod_refill->partition($part)->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => 0]);
  654. return [false, "回调地址为空"];
  655. }
  656. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  657. $order_state = intval($order_info['order_state']);
  658. if ($order_state !== ORDER_STATE_CANCEL && $order_state !== ORDER_STATE_SUCCESS) {
  659. return [false, "错误的订单状态,不能通知."];
  660. }
  661. $resp = $this->mPolicy->notify($order_info,$refill_info);
  662. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  663. if ($resp) {
  664. $mod_refill->partition($part)->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  665. return [true, ""];
  666. }
  667. else
  668. {
  669. $mod_refill->partition($part)->edit($order_id, ['mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  670. $times = $refill_info['mch_notify_times'] + 1;
  671. if ($times > 100) {
  672. $mod_refill->partition($part)->edit($order_id, ['mch_notify_state' => 2]);
  673. } else {
  674. $period = 5;
  675. QueueClient::async_push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false], $period);
  676. }
  677. return [false, "通知{$times}次,失败."];
  678. }
  679. }
  680. //发预回调通知消息
  681. public function notify_merchant_early($mchid, $mch_order)
  682. {
  683. $part = util::part_notify();
  684. $mod_refill = Model('refill_order');
  685. $refill_info = $mod_refill->partition($part)->getOrderInfo(['mch_order' => $mch_order,'mchid' => $mchid,'inner_status' => 0]);
  686. if(empty($refill_info)) {
  687. return [false, "refill_order table cannot find order when mchid=$mchid mch_order=$mch_order"];
  688. }
  689. $order_sn = $refill_info['order_sn'];
  690. $order_id = $refill_info['order_id'];
  691. $vr_order = Model('vr_order');
  692. $order_info = $vr_order->partition($part)->getOrderInfo(['order_sn' => $order_sn]);
  693. if (empty($order_info)) {
  694. return [false, "refill_order table cannot find order when order_sn=$order_sn."];
  695. }
  696. $cur_quality = intval($refill_info['quality']);
  697. $success = in_array($cur_quality, [2, 3]); //在卡密或者快充通道时,预回调成功。
  698. $resp = $this->mPolicy->notify_early($order_info, $refill_info, $success);
  699. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  700. if ($resp) {
  701. $mod_refill->partition($part)->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  702. return [true, ""];
  703. }
  704. else
  705. {
  706. $mod_refill->partition($part)->edit($order_id, ['mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  707. $times = $refill_info['mch_notify_times'] + 1;
  708. if ($times > 100) {
  709. $mod_refill->partition($part)->edit($order_id, ['mch_notify_state' => 2]);
  710. } else {
  711. $period = 5;
  712. QueueClient::async_push("NotifyMerchantEarly", ['mchid' => $mchid, 'mch_order' => $mch_order], $period);
  713. }
  714. return [false, "通知{$times}次,失败."];
  715. }
  716. }
  717. public function query($order_id)
  718. {
  719. $mod_order = Model('vr_order');
  720. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  721. if(empty($order_info)) return false;
  722. $mod_refill = Model('refill_order');
  723. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  724. $chname = $refill_info['channel_name'];
  725. if ($refill_info['notify_state'] == 1 && in_array($order_info['order_state'], [ORDER_STATE_SUCCESS, ORDER_STATE_CANCEL])) {
  726. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  727. return true;
  728. }
  729. if($order_info['order_state'] == ORDER_STATE_SEND) {
  730. $query_able = true;
  731. }
  732. else {
  733. $query_able = false;
  734. }
  735. if($query_able)
  736. {
  737. if(empty($chname)) {
  738. Log::record("chname={$chname}", Log::DEBUG);
  739. return false;
  740. }
  741. $provider = $this->mPolicy->provider($chname);
  742. if(empty($provider)) return false;
  743. $values = $provider->query($refill_info);
  744. if (count($values) == 2) {
  745. [$state, $order_state] = $values;
  746. $official_sn = false;
  747. } else {
  748. [$state, $order_state, $official_sn] = $values;
  749. }
  750. if (!$state) {
  751. return false;
  752. } elseif ($order_state == ORDER_STATE_SUCCESS) {
  753. $this->proc_notify($order_id, true, false, $chname, $official_sn);
  754. } elseif ($order_state == ORDER_STATE_CANCEL) {
  755. $this->proc_notify($order_id, false, true, $chname, $official_sn);
  756. } else {
  757. Log::record("RefillBase::query order_state={$order_state}", Log::DEBUG);
  758. }
  759. }
  760. return true;
  761. }
  762. public function query_net($order_id)
  763. {
  764. $query_handler = function ($order_id, $order_info, $state, $order_state, $chname,$order_time)
  765. {
  766. $mod_refill = Model('refill_order');
  767. if ($order_info['order_state'] == ORDER_STATE_PAY) {
  768. $query_able = true;
  769. } else {
  770. $query_able = false;
  771. }
  772. $vr_part = util::part_vr_order(intval($order_info['add_time']));
  773. $can_try = false;
  774. if($query_able)
  775. {
  776. if(!$state) {
  777. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30);
  778. $neterr = true;
  779. }
  780. elseif($order_state == ORDER_STATE_SUCCESS || $order_state == ORDER_STATE_CANCEL)
  781. {
  782. $neterr = false;
  783. $logic_vr_order = Logic("vr_order");
  784. $logic_vr_order->changeOrderStateSend($order_id, true, $vr_part);
  785. $data = ['commit_time' => time()];
  786. $mod_refill->partition(util::part_refill($order_time))->edit($order_id, $data);
  787. QueueClient::async_push("QueryRefillState", ['order_id' => $order_id], 1);
  788. }
  789. elseif ($order_state == ORDER_STATE_NOEXIST) {
  790. $neterr = false;
  791. $logic_vr_order = Logic("vr_order");
  792. $logic_vr_order->changeOrderStateSend($order_id, true, $vr_part);
  793. $can_try = true;
  794. }
  795. else {
  796. $neterr = true;
  797. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30);
  798. }
  799. util::monitor_netchk($chname,$neterr);
  800. }
  801. return [true,$can_try];
  802. };
  803. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  804. $mod_order = Model('vr_order');
  805. $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id]);
  806. if (empty($order_info) || $order_info['order_state'] != ORDER_STATE_PAY) return false;
  807. $mod_refill = Model('refill_order');
  808. $refill_info = $mod_refill->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  809. $chname = $refill_info['channel_name'];
  810. $provider = $this->mPolicy->provider($chname);
  811. if(empty($provider)) return false;
  812. $values = $provider->query($refill_info);
  813. if (count($values) == 2) {
  814. [$state, $order_state] = $values;
  815. $official_sn = false;
  816. } else {
  817. [$state, $order_state, $official_sn] = $values;
  818. }
  819. try {
  820. $can_try = false;
  821. $tran = new trans_wapper($mod_order, 'query_net change order state trans');
  822. $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id], '*', true, true);
  823. $order_time = intval($refill_info['order_time']);
  824. [$ret, $can_try] = $query_handler($order_id, $order_info, $state, $order_state, $chname,$order_time);
  825. $tran->commit();
  826. $trans_succ = true;
  827. }
  828. catch (Exception $ex) {
  829. Log::record("Error:" . $ex->getMessage(), Log::ERR);
  830. $trans_succ = false;
  831. $tran->rollback();
  832. $ret = false;
  833. }
  834. if($can_try)
  835. {
  836. if($trans_succ) {
  837. $this->proc_notify($order_id, false, true, $chname, $official_sn);
  838. } else {
  839. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30);
  840. }
  841. }
  842. return $ret;
  843. }
  844. public function query_timeout($mchid, $mch_order)
  845. {
  846. $has_tmout = transfer_timeout::instance()->has_tmout($mchid, $mch_order);
  847. if($has_tmout)
  848. {
  849. //标记为超时,并且准备提前回调。
  850. util::push_queue_order($mchid, $mch_order, ORDER_STATE_TIMEOUT);
  851. $part = util::part_notify();
  852. $mod_refill = Model('refill_order');
  853. $refill_info = $mod_refill->partition($part)->getOrderInfo(['mch_order' => $mch_order,'mchid' => $mchid,'inner_status' => 0]);
  854. if(empty($refill_info)) {
  855. return false;
  856. }
  857. $order_id = $refill_info['order_id'];
  858. $mod_refill->partition($part)->edit($order_id, ['notify_time' => time()]);
  859. QueueClient::push("NotifyMerchantEarly", ['mchid' => $mchid, 'mch_order' => $mch_order]);
  860. }
  861. return true;
  862. }
  863. public function query_auto($order_id,$query_times)
  864. {
  865. $perioder = function ($times)
  866. {
  867. if($times > 10) {
  868. return 300;
  869. }
  870. elseif($times > 5) {
  871. return 120;
  872. }
  873. else {
  874. return 5;
  875. }
  876. };
  877. $query_handler = function ($order_id, $order_info, $state, $order_state, $chname,$order_time,$query_times) use ($perioder)
  878. {
  879. $query_times += 1;
  880. $mod_refill = Model('refill_order');
  881. $vr_part = util::part_vr_order(intval($order_info['add_time']));
  882. $peroid = $perioder($query_times);
  883. $can_try = false;
  884. if (!$state) {
  885. QueueClient::async_push("QueryAutoRefillState", ['order_id' => $order_id, 'query_times' => $query_times], $peroid);
  886. $neterr = true;
  887. } elseif ($order_state == ORDER_STATE_SUCCESS || $order_state == ORDER_STATE_CANCEL) {
  888. $neterr = false;
  889. QueueClient::async_push("QueryRefillState", ['order_id' => $order_id], 1);
  890. } elseif ($order_state == ORDER_STATE_NOEXIST) {
  891. $neterr = false;
  892. QueueClient::async_push("QueryRefillState", ['order_id' => $order_id], 1);
  893. $can_try = true;
  894. } else {
  895. $neterr = true;
  896. QueueClient::async_push("QueryAutoRefillState", ['order_id' => $order_id, 'query_times' => $query_times], $peroid);
  897. }
  898. util::monitor_netchk($chname, $neterr);
  899. return [true, $can_try];
  900. };
  901. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  902. $mod_order = Model('vr_order');
  903. $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id]);
  904. if (empty($order_info)) return false;
  905. $mod_refill = Model('refill_order');
  906. $refill_info = $mod_refill->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  907. $chname = $refill_info['channel_name'];
  908. $provider = $this->mPolicy->provider($chname);
  909. if(empty($provider)) return false;
  910. $values = $provider->query($refill_info);
  911. if (count($values) == 2) {
  912. [$state, $order_state] = $values;
  913. $official_sn = false;
  914. } else {
  915. [$state, $order_state, $official_sn] = $values;
  916. }
  917. try {
  918. $can_try = false;
  919. $tran = new trans_wapper($mod_order, 'query_net change order state trans');
  920. $order_info = $mod_order->partition(util::part_notify())->getOrderInfo(['order_id' => $order_id], '*', true, true);
  921. $order_time = intval($refill_info['order_time']);
  922. [$ret, $can_try] = $query_handler($order_id, $order_info, $state, $order_state, $chname,$order_time,$query_times);
  923. $tran->commit();
  924. $trans_succ = true;
  925. }
  926. catch (Exception $ex) {
  927. Log::record("Error:" . $ex->getMessage(), Log::ERR);
  928. $trans_succ = false;
  929. $tran->rollback();
  930. $ret = false;
  931. }
  932. if($can_try)
  933. {
  934. if($trans_succ) {
  935. $this->proc_notify($order_id, false, true, $chname, $official_sn);
  936. } else {
  937. QueueClient::async_push("QueryOrderNeterr",['order_id' => $order_id],30);
  938. }
  939. }
  940. return $ret;
  941. }
  942. public function manual_success($order_id)
  943. {
  944. $order_id = intval($order_id);
  945. if($order_id <= 0) return false;
  946. try
  947. {
  948. $mod_order = Model('vr_order');
  949. $tran = new trans_wapper($mod_order,'manual_success state trans');
  950. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true);
  951. if(!empty($order_info) && in_array($order_info['order_state'],[ORDER_STATE_PAY,ORDER_STATE_SEND]))
  952. {
  953. $tran->commit();
  954. $logic_vr_order = Logic("vr_order");
  955. $logic_vr_order->changeOrderStateSuccess($order_id,true);
  956. $mod_refill = Model('refill_order');
  957. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id]);
  958. $order_time = intval($refill_info['order_time']);
  959. util::onOrderSuccess($refill_info,$order_info);
  960. $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]);
  961. mtopcard\cards_helper::assign($order_id);
  962. util::pop_queue_order($refill_info['mchid'], $refill_info['mch_order'], $order_time);
  963. }
  964. else {
  965. $tran->commit();
  966. }
  967. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  968. return true;
  969. }
  970. catch (Exception $ex) {
  971. $tran->rollback();
  972. Log::record("manual_success exception:{$ex->getMessage()}",Log::ERR);
  973. return false;
  974. }
  975. }
  976. public function manual_cancel($order_id)
  977. {
  978. $order_id = intval($order_id);
  979. if($order_id <= 0) return false;
  980. try {
  981. $mod_order = Model('vr_order');
  982. $tran = new trans_wapper($mod_order,'manual_cancel state trans');
  983. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true);
  984. if(!empty($order_info) && in_array($order_info['order_state'],[ORDER_STATE_PAY,ORDER_STATE_SEND]))
  985. {
  986. $tran->commit();
  987. $logic_vr_order = Logic("vr_order");
  988. $logic_vr_order->changeOrderStateCancel($order_info, '', "后台手动回调通知失败",true,true);
  989. $mod_refill = Model('refill_order');
  990. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id]);
  991. $order_time = intval($refill_info['order_time']);
  992. $mod_refill->partition(util::part_refill($order_time))->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]);
  993. mtopcard\cards_helper::reuse($order_id);
  994. util::pop_queue_order($refill_info['mchid'], $refill_info['mch_order'], $order_time);
  995. }
  996. else {
  997. $tran->commit();
  998. }
  999. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  1000. return true;
  1001. }
  1002. catch (Exception $ex) {
  1003. $tran->rollback();
  1004. Log::record("manual_cancel exception:{$ex->getMessage()}",Log::ERR);
  1005. return false;
  1006. }
  1007. }
  1008. public function need_intercept($mchid,$card_type,$card_state,$is_transfer,$card_no)
  1009. {
  1010. return $this->mPolicy->need_intercept($mchid,$card_type,$card_state,$is_transfer,$card_no);
  1011. }
  1012. public function region_intercept($quality,$card_type,$region_no)
  1013. {
  1014. return $this->mPolicy->region_intercept($quality,$card_type,$region_no);
  1015. }
  1016. public function UpdateMchRatios($gross,$detail,$types)
  1017. {
  1018. $this->mPolicy->update_mchratios($gross, $detail, $types);
  1019. }
  1020. public function merchant_ratio($mchid)
  1021. {
  1022. return $this->mPolicy->merchant_ratio($mchid);
  1023. }
  1024. public function UpdateChctl($params)
  1025. {
  1026. $this->mPolicy->update_chctl($params);
  1027. }
  1028. public function UpdateChspeed($params)
  1029. {
  1030. $this->mPolicy->update_chspeeds($params);
  1031. }
  1032. public function UpdateMaxSpeed($params)
  1033. {
  1034. $this->mPolicy->update_maxspeeds($params);
  1035. }
  1036. public function isOverChspeed($chname)
  1037. {
  1038. return $this->mPolicy->is_over_chspeed($chname);
  1039. }
  1040. /**
  1041. * @param order $order
  1042. * @param $mchid
  1043. * @return CalcMerchantPrice
  1044. * @throws Exception
  1045. */
  1046. private function getMerchantPriceCalc(order $order)
  1047. {
  1048. $org_quality = $order->org_quality();
  1049. if (PolicyUtil::mixed_quality($org_quality)) {
  1050. $calc = new CalcMerchantMixedPrice($order, $org_quality, $this->mPolicy, $order->thrid_params());
  1051. } else {
  1052. $calc = new CalcMerchantPrice($order, $order->cur_quality(), $this->mPolicy, $order->thrid_params());
  1053. }
  1054. return $calc;
  1055. }
  1056. }