util.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. <?php
  2. namespace refill;
  3. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  4. require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
  5. require_once(BASE_HELPER_PATH . '/queue/monitor.php');
  6. require_once(BASE_HELPER_PATH . '/refill/policy/rlock.php');
  7. use queue;
  8. use mtopcard;
  9. use Log;
  10. use Exception;
  11. use Cache;
  12. use QueueClient;
  13. use refill;
  14. use trans_wapper;
  15. class util
  16. {
  17. const ThirdRefillAmount = 100;
  18. static function make_mobile()
  19. {
  20. static $prefix = ["139", "138", "137", "136", "135", "134", "159", "158", "157", "150", "151", "152",
  21. "188", "187", "182", "183", "184", "178", "130", "131", "132", "156", "155", "186", "185",
  22. "176", "133", "153", "189", "180", "181", "177"];
  23. $pos = mt_rand(0, count($prefix) - 1);
  24. $no = "{$prefix[$pos]}" . mt_rand(10000000, 99999999);
  25. return $no;
  26. }
  27. public static function can_refill($card_no, $card_type)
  28. {
  29. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  30. $result = rcache('card_expired', '', "{$card_no}");
  31. if (empty($result)) {
  32. wcache("card_expired", [$card_no => time()], '');
  33. return [true, 0];
  34. } else {
  35. $latest = current($result);
  36. $cur = time();
  37. $success = ($cur - $latest) > 2;
  38. if ($success) {
  39. wcache("card_expired", [$card_no => time()], '');
  40. }
  41. return [$success, $latest + 2 - $cur];
  42. }
  43. } else {
  44. return [true, 0];
  45. }
  46. }
  47. public static function can_commit($card_no, $card_type)
  48. {
  49. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard)
  50. {
  51. $result = rcache('card_expired', '', "{$card_no}");
  52. if (empty($result)) {
  53. wcache("card_expired", [$card_no => time()], '');
  54. return [true, 0];
  55. }
  56. else
  57. {
  58. $latest = current($result);
  59. $cur = time();
  60. $lowest = 30;
  61. if ($cur > $latest && ($cur - $latest) >= $lowest) {
  62. wcache("card_expired", [$card_no => time()], '');
  63. return [true, 0];
  64. } else {
  65. wcache("card_expired", [$card_no => $latest + $lowest], '');
  66. return [false, $latest + $lowest - $cur];
  67. }
  68. }
  69. }
  70. else {
  71. return [true, 0];
  72. }
  73. }
  74. static function write_card($card_no, $card_type,$bind_phone)
  75. {
  76. if(empty($bind_phone)) {
  77. return false;
  78. }
  79. if ($card_type !== mtopcard\SinopecCard && $card_type !== mtopcard\PetroChinaCard) {
  80. return false;
  81. }
  82. $mobile_types = [mtopcard\ChinaMobileCard,mtopcard\ChinaUnicomCard,mtopcard\ChinaTelecomCard];
  83. $ctype = mtopcard\simple_card_type($bind_phone);
  84. if (!in_array($ctype,$mobile_types)) {
  85. return false;
  86. }
  87. $mod_topcard = Model('topcard');
  88. $ret = $mod_topcard->get_card($card_no);
  89. if (empty($ret)) {
  90. $mod_topcard->add($card_no, $card_type, time(), $bind_phone);
  91. } else {
  92. $mod_topcard->edit($card_no,$bind_phone);
  93. }
  94. dcache($card_no, 'cardrefill-');
  95. return true;
  96. }
  97. static function read_card($card_no, $card_type = 0)
  98. {
  99. if (empty($card_no)) return false;
  100. $data = rcache($card_no, 'cardrefill-');
  101. if (empty($data)) {
  102. $mod_topcard = Model('topcard');
  103. $ret = $mod_topcard->get_card($card_no);
  104. if (empty($ret)) {
  105. if ($card_type === 0) {
  106. $card_type = mtopcard\card_type($card_no,$regin_no);
  107. }
  108. $bind_phone = util::make_mobile();
  109. $mod_topcard->add($card_no, $card_type, time(), $bind_phone);
  110. $data['bind_phone'] = $bind_phone;
  111. $data['refill_time'] = time();
  112. $data['times'] = 0;
  113. $data['black_card'] = 0;
  114. wcache($card_no, $data, 'cardrefill-');
  115. } else {
  116. $val = $ret[0];
  117. $data['bind_phone'] = $val['bind_phone'];
  118. $data['black_card'] = $val['black_card'];
  119. $data['refill_time'] = time();
  120. $data['times'] = 0;
  121. }
  122. }
  123. //之前没加black_card处理,这个字段不存在.
  124. if (!array_key_exists('black_card', $data)) {
  125. $data['black_card'] = 0;
  126. }
  127. return $data;
  128. }
  129. static function inc_card($card_no, $card_info)
  130. {
  131. $card_info['times'] += 1;
  132. $card_info['refill_time'] = time();
  133. wcache($card_no, $card_info, 'cardrefill-');
  134. }
  135. public static function del_card($card_no)
  136. {
  137. dcache($card_no, 'cardrefill-');
  138. }
  139. public static function set_black($card_no)
  140. {
  141. if (empty($card_no)) return false;
  142. $card_info = util::read_card($card_no);
  143. if (!empty($card_info)) {
  144. $card_info['black_card'] = 1;
  145. $mod_topcard = Model('topcard');
  146. $mod_topcard->table('topcard')->where(['card_no' => $card_no])->update(['black_card' => 1]);
  147. wcache($card_no, $card_info, 'cardrefill-');
  148. return true;
  149. } else {
  150. return false;
  151. }
  152. }
  153. private static function black_order($order_sn, $msg)
  154. {
  155. static $errMsgs = ["只能给主卡且卡状态正常的加油卡充值", "加油卡卡号错误或不支持"];
  156. if (empty($msg)) return false;
  157. if (in_array($msg, $errMsgs)) {
  158. $refill = Model('refill_order');
  159. $order = $refill->getOrderInfo(['order_sn' => $order_sn]);
  160. if (empty($order)) return false;
  161. $card_no = $order['card_no'];
  162. return util::set_black($card_no);
  163. }
  164. }
  165. public static function black_from_log($file_name)
  166. {
  167. $fn = fopen($file_name, "r");
  168. if (empty($fn)) {
  169. Log::record("Open File {$file_name} error.", Log::ERR);
  170. return false;
  171. } else {
  172. Log::record("{$file_name} start woring", Log::DEBUG);
  173. }
  174. $errs = [];
  175. while (!feof($fn)) {
  176. $line = trim(fgets($fn));
  177. $ret = preg_match('/[\w\W]+"channelOrderNumber":"(?P<order_sn>[^"]+)"[\w\W]+"message":"(?P<message>[\x{4e00}-\x{9fa5}]+)"[\w\W]+"status":109/u', $line, $matches);
  178. if ($ret) {
  179. $order_sn = $matches['order_sn'];
  180. $message = $matches['message'];
  181. self::black_order($order_sn, $message);
  182. $errs[$message] = empty($errs[$message]) ? 1 : $errs[$message] + 1;
  183. }
  184. }
  185. foreach ($errs as $msg => $count) {
  186. Log::record("msg:{$msg} count:{$count}", Log::DEBUG);
  187. }
  188. fclose($fn);
  189. return true;
  190. }
  191. public static function async_add($params, $period = 10)
  192. {
  193. try {
  194. QueueClient::async_push("AysncAddDispatcher", ['method' => 'add', 'params' => $params], $period);
  195. return true;
  196. } catch (Exception $ex) {
  197. return false;
  198. }
  199. }
  200. public static function async_notify($chname,$data, $period)
  201. {
  202. try {
  203. QueueClient::async_push("AysncAddDispatcher", ['method' => 'notify', 'params' => ['channel' => $chname, 'params' => $data]], $period);
  204. return true;
  205. } catch (Exception $ex) {
  206. return false;
  207. }
  208. }
  209. public static function push_add($params)
  210. {
  211. try
  212. {
  213. $ret = self::push_queue('add', $params);
  214. return $ret !== false;
  215. }
  216. catch (Exception $ex) {
  217. return false;
  218. }
  219. }
  220. public static function push_add_zero($params)
  221. {
  222. try
  223. {
  224. $ret = self::push_queue('add_zero', $params);
  225. return $ret !== false;
  226. }
  227. catch (Exception $ex) {
  228. return false;
  229. }
  230. }
  231. public static function push_addthird($params)
  232. {
  233. try
  234. {
  235. $ret = self::push_queue('addthird', $params);
  236. return $ret !== false;
  237. }
  238. catch (Exception $ex) {
  239. return false;
  240. }
  241. }
  242. public static function push_notify($chname, $params)
  243. {
  244. try
  245. {
  246. $ret = self::push_queue('notify', ['channel' => $chname, 'params' => $params]);
  247. return $ret !== false;
  248. }
  249. catch (Exception $ex) {
  250. return false;
  251. }
  252. }
  253. public static function push_notify_merchant($order_id, $manual)
  254. {
  255. try
  256. {
  257. $ret = self::push_queue('notify_mechant', ['order_id' => $order_id, 'manual' => $manual]);
  258. return $ret !== false;
  259. }
  260. catch (Exception $ex) {
  261. return false;
  262. }
  263. }
  264. public static function push_query($order_id)
  265. {
  266. try
  267. {
  268. $ret = self::push_queue('query', ['order_id' => $order_id]);
  269. return $ret !== false;
  270. }
  271. catch (Exception $ex) {
  272. return false;
  273. }
  274. }
  275. public static function push_query_net($order_id)
  276. {
  277. try {
  278. $ret = self::push_queue('query_net', ['order_id' => $order_id]);
  279. return $ret !== false;
  280. }
  281. catch (Exception $ex) {
  282. return false;
  283. }
  284. }
  285. public static function manual_success($order_id)
  286. {
  287. try
  288. {
  289. $ret = self::push_queue('manual_success', ['order_id' => $order_id]);
  290. return $ret !== false;
  291. }
  292. catch (Exception $ex) {
  293. return false;
  294. }
  295. }
  296. public static function manual_cancel($order_id)
  297. {
  298. try
  299. {
  300. $ret = self::push_queue('manual_cancel', ['order_id' => $order_id]);
  301. return $ret !== false;
  302. }
  303. catch (Exception $ex) {
  304. return false;
  305. }
  306. }
  307. public static function push_queue($method, $value)
  308. {
  309. if (defined('USE_COROUTINE') && USE_COROUTINE && defined('COROUTINE_HOOK_TCP') && COROUTINE_HOOK_TCP) {
  310. $queue_name = 'QUEUE_DISPATCHER_CO';
  311. $ins = Cache::getInstance('cacheredis');
  312. return $ins->lpush($queue_name, serialize([$method => $value]));
  313. }
  314. else {
  315. return queue\DispatcherClient::instance()->push($method,$value);
  316. }
  317. }
  318. public static function dispatcher_queue_length()
  319. {
  320. $ins = Cache::getInstance('cacheredis');
  321. return $ins->lLen('QUEUE_DISPATCHER_CO');
  322. }
  323. public static function monitor_queue_length()
  324. {
  325. $ins = Cache::getInstance('cacheredis');
  326. return $ins->lLen('REFILL_MONITOR_QUEUE');
  327. }
  328. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  329. public static function monitor_submit($mchid, $spec, $card_type, $mch_amount, $time)
  330. {
  331. $time = intval($time);
  332. queue\MonitorClient::instance()->onSubmit($mchid,$time,$spec,$card_type,$mch_amount);
  333. }
  334. public static function monitor_callback($mchid,$spec,$card_type,$mch_amount,$channel_amount,$succ,$time)
  335. {
  336. queue\MonitorClient::instance()->onCallback($mchid,$time,$spec,$card_type,floatval($mch_amount),floatval($channel_amount),$succ);
  337. }
  338. public static function monitor_netchk($chname,$succ)
  339. {
  340. queue\MonitorClient::instance()->onNetCheck($chname, time(),$succ);
  341. }
  342. public static function monitor_commit($chname, $spec, $card_type, $channel_amount,$commit_time)
  343. {
  344. queue\MonitorClient::instance()->onCommit($chname, $commit_time, $spec, $card_type, $channel_amount);
  345. }
  346. public static function monitor_notify($chname, $spec, $card_type, $channel_amount, $period, $succ,$commit_time)
  347. {
  348. queue\MonitorClient::instance()->onNotify($chname, $commit_time, $spec, $card_type, floatval($channel_amount), $period, $succ);
  349. }
  350. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  351. public static function set_order_channels($mchid,$mchorder,$datas)
  352. {
  353. $ins = Cache::getInstance('cacheredis');
  354. $name = 'order_channels';
  355. $key = "{$mchid}-{$mchorder}";
  356. $ins->hset($name, '', [$key => serialize($datas)]);
  357. }
  358. public static function get_order_channels($mchid, $mchorder)
  359. {
  360. //old-name oil_exclude_channels
  361. $ins = Cache::getInstance('cacheredis');
  362. $name = 'order_channels';
  363. $key = "{$mchid}-{$mchorder}";
  364. $chnames = $ins->hget($name, '', $key);
  365. $chnames = unserialize($chnames);
  366. if(is_array($chnames)) {
  367. return $chnames;
  368. } else {
  369. return [];
  370. }
  371. }
  372. public static function del_order_channels($mchid, $mchorder)
  373. {
  374. $ins = Cache::getInstance('cacheredis');
  375. $name = 'order_channels';
  376. $key = "{$mchid}-{$mchorder}";
  377. $ins->hdel($name, '', $key);
  378. }
  379. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  380. public static function set_cancel_order($mchid,$mch_order)
  381. {
  382. $ins = Cache::getInstance('cacheredis');
  383. $name = 'order_cancel_hash';
  384. $key = "{$mchid}-{$mch_order}";
  385. $ins->hset($name, '', [$key=> 1]);
  386. }
  387. public static function query_cancel_order($mchid,$mch_order)
  388. {
  389. $ins = Cache::getInstance('cacheredis');
  390. $name = 'order_cancel_hash';
  391. $key = "{$mchid}-{$mch_order}";
  392. $value = $ins->hget($name,'',$key);
  393. return $value;
  394. }
  395. public static function del_cancel_order($mchid,$mch_order)
  396. {
  397. $ins = Cache::getInstance('cacheredis');
  398. $name = 'order_cancel_hash';
  399. $key = "{$mchid}-{$mch_order}";
  400. $ins->hdel($name, $key);
  401. }
  402. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  403. public static function set_next_order($mchid,$mch_order)
  404. {
  405. $ins = Cache::getInstance('cacheredis');
  406. $name = 'order_next_hash';
  407. $key = "{$mchid}-{$mch_order}";
  408. $ins->hset($name, '', [$key=> 1]);
  409. }
  410. public static function query_next_order($mchid,$mch_order)
  411. {
  412. $ins = Cache::getInstance('cacheredis');
  413. $name = 'order_next_hash';
  414. $key = "{$mchid}-{$mch_order}";
  415. $value = $ins->hget($name,'',$key);
  416. return $value;
  417. }
  418. public static function del_next_order($mchid,$mch_order)
  419. {
  420. $ins = Cache::getInstance('cacheredis');
  421. $name = 'order_success_hash';
  422. $key = "{$mchid}-{$mch_order}";
  423. $ins->hdel($name, $key);
  424. }
  425. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  426. public static function merchant_debt_stoped($mchid)
  427. {
  428. if($mchid > 0)
  429. {
  430. $ret = rcache('merchant-debt-judge', 'refill-',"{$mchid}");
  431. if(empty($ret)) {
  432. return false;
  433. }
  434. $stoped = intval($ret[$mchid]);
  435. return ($stoped === 1);
  436. }
  437. else {
  438. return false;
  439. }
  440. }
  441. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  442. public static function push_queue_order($mchid,$mch_order,$order_state)
  443. {
  444. if(empty($mch_order)) return;
  445. $ins = Cache::getInstance('cacheredis');
  446. $name = 'merchant_order_query';
  447. $key = "{$mchid}-{$mch_order}";
  448. $ins->hset($name, '', [$key => $order_state]);
  449. }
  450. public static function del_queue_order($mchid,$mch_order)
  451. {
  452. if(empty($mch_order)) return;
  453. $ins = Cache::getInstance('cacheredis');
  454. $name = 'merchant_order_query';
  455. $key = "{$mchid}-{$mch_order}";
  456. $ret = $ins->hdel($name, '', $key);
  457. }
  458. public static function pop_queue_order($mchid,$mch_order)
  459. {
  460. util::del_order_channels($mchid,$mch_order);
  461. Model('refill_order')->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_HANDLED]);
  462. $ins = Cache::getInstance('cacheredis');
  463. $name = 'merchant_order_query';
  464. $key = "{$mchid}-{$mch_order}";
  465. $ret = $ins->hdel($name, '', $key);
  466. }
  467. public static function query_queue_order($mchid,$mch_order)
  468. {
  469. $ins = Cache::getInstance('cacheredis');
  470. $name = 'merchant_order_query';
  471. $key = "{$mchid}-{$mch_order}";
  472. $value = $ins->hget($name,'',$key);
  473. return $value;
  474. }
  475. public static function need_check($net_errno)
  476. {
  477. if(empty($net_errno)) return false;
  478. [$type,$code] = explode('-',$net_errno);
  479. $code = intval($code);
  480. if($type == "CURL") {
  481. static $errors = [CURLE_GOT_NOTHING,CURLE_RECV_ERROR];
  482. return in_array($code,$errors);
  483. } elseif($type == "HTTP") {
  484. static $excludes = [404];
  485. return !in_array($code,$excludes);
  486. } else {
  487. return false;
  488. }
  489. }
  490. public static function order_errflag($net_errno) {
  491. return ($net_errno === 'ORDER_CREATE_FAIL');
  492. }
  493. public static function onOrderSuccess($refill_info,$order_info)
  494. {
  495. $data = store_member::instance()->get_member($order_info['store_id']);
  496. if(empty($data)) {
  497. Log::record("cannot find member when store_id={$order_info['store_id']}",Log::ERR);
  498. return false;
  499. }
  500. $data['order_sn'] = $refill_info['order_sn'];
  501. $data['amount'] = $refill_info['channel_amount'];
  502. $model_pd = Model('predeposit');
  503. $model_pd->changePd('order_pay',$data,true);
  504. return true;
  505. }
  506. public static function getProvider($name,$type = 'RefillPhone')
  507. {
  508. $file = BASE_HELPER_RAPI_PATH . "/$name/{$type}.php";
  509. if(!file_exists($file)){
  510. Log::record("provider api file={$file} not exist.",Log::DEBUG);
  511. return false;
  512. } else {
  513. require_once($file);
  514. Log::record("file={$file} load success.",Log::DEBUG);
  515. }
  516. $class_name = "refill\\{$name}\\{$type}";
  517. if (class_exists($class_name, false)) {
  518. $caller = new $class_name([]);
  519. return $caller;
  520. } else {
  521. $error = "Base Error: class {$class_name} isn't exists!";
  522. Log::record($error, Log::ERR);
  523. return false;
  524. }
  525. }
  526. public static function xmlToArray($xml)
  527. {
  528. $object = simplexml_load_string($xml);
  529. $val = json_decode(json_encode($object), true);
  530. $msg = json_encode($val);
  531. Log::record("xmlToArray result={$msg}", Log::DEBUG);
  532. return $val;
  533. }
  534. //for tester
  535. public static function send_normal($order_sn)
  536. {
  537. $status = mt_rand(1,100);
  538. if($status > 97) {
  539. $status = 1;
  540. } else {
  541. $status = 0;
  542. }
  543. $url = BASE_SITE_URL . "/mobile/callback/refill_baidu.php";
  544. go(function () use ($url, $status,$order_sn)
  545. {
  546. sleep(3);
  547. while (true)
  548. {
  549. $resp = http_request($url,['status' => $status,'order_sn' => $order_sn],'GET', false, [], $net_errno);
  550. if($resp == 'SUCCESS') {
  551. break;
  552. }
  553. }
  554. Log::record("resp = {$resp}",Log::DEBUG);
  555. });
  556. }
  557. public static function send_quick($order_sn)
  558. {
  559. $status = mt_rand(1,10);
  560. if($status > 3) {
  561. $status = 1;
  562. } else {
  563. $status = 0;
  564. }
  565. $url = BASE_SITE_URL . "/mobile/callback/refill_baidu.php";
  566. go(function () use ($url, $status,$order_sn)
  567. {
  568. sleep(3);
  569. while (true)
  570. {
  571. $resp = http_request($url,['status' => $status,'order_sn' => $order_sn],'GET', false, [], $net_errno);
  572. if($resp == 'SUCCESS') {
  573. break;
  574. }
  575. }
  576. Log::record("resp = {$resp}",Log::DEBUG);
  577. });
  578. }
  579. public static function retry_canceled_order($order_id, $skip)
  580. {
  581. $mod_order = Model('vr_order');
  582. $mod_refill = Model('refill_order');
  583. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  584. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  585. if(empty($refill_info) || empty($order_info)) {
  586. return [false,'无此订单或者订单已经重试中了...'];
  587. }
  588. $tran = new trans_wapper($mod_order,'notify change order state trans');
  589. try
  590. {
  591. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id], '*', true, true);
  592. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0], '*', true, true);
  593. $order_state = intval($order_info['order_state']);
  594. if(empty($refill_info) || $refill_info['is_retrying'] == 1 || $order_state != ORDER_STATE_CANCEL) {
  595. $tran->commit();
  596. return [false,'订单已经在重试'];
  597. }
  598. $mod_refill->edit($order_id, ['is_retrying' => 1]);
  599. $tran->commit();
  600. $order = refill\order::from_db($refill_info,$order_info);
  601. $params = $order->queue_params();
  602. $params['order_time'] = time();
  603. if ($skip) {
  604. $mchid = $refill_info['mchid'];
  605. $mch_order = $refill_info['mch_order'];
  606. refill\util::set_next_order($mchid, $mch_order);
  607. }
  608. if(util::push_add($params)) {
  609. return [true,''];
  610. } else {
  611. return [false,'加入队列出错'];
  612. }
  613. }
  614. catch (Exception $ex) {
  615. $tran->rollback();
  616. Log::record($ex->getMessage(),Log::ERR);
  617. return [false,"{$ex->getMessage()}"];
  618. }
  619. }
  620. public function transfer_success_order($order_id, $manual_recharge_amount)
  621. {
  622. $mod_order = Model('vr_order');
  623. $mod_refill = Model('refill_order');
  624. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  625. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  626. if(empty($refill_info) || empty($order_info)) {
  627. return [false,'无此订单'];
  628. }
  629. $tran = new trans_wapper($mod_order,'notify change order state trans');
  630. try
  631. {
  632. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id], '*', true, true);
  633. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0], '*', true, true);
  634. $order_state = intval($order_info['order_state']);
  635. if(empty($refill_info) || $refill_info['is_retrying'] == 1 || $order_state != ORDER_STATE_CANCEL) {
  636. $tran->commit();
  637. return [false,'订单已经在重试'];
  638. }
  639. $mod_refill->edit($order_id, ['is_retrying' => 1]);
  640. $order = refill\order::from_db($refill_info,$order_info);
  641. $mchid = $order->mchid();
  642. [$success,$success_order_id,$errmsg] = refill\RefillFactory::instance()->success_order($order);
  643. if(!$success) {
  644. $tran->rollback();
  645. return [false, $errmsg];
  646. }
  647. $mod_refill->edit($success_order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  648. $mod_refill->edit($order_id, ['is_retrying' => 0]);
  649. if($manual_recharge_amount > 0) {
  650. $mod_refill->edit($success_order_id, ['channel_amount' => $manual_recharge_amount]);
  651. }
  652. $tran->commit();
  653. return [true,''];
  654. }
  655. catch (Exception $ex) {
  656. $tran->rollback();
  657. Log::record($ex->getMessage(),Log::ERR);
  658. return [false,"{$ex->getMessage()}"];
  659. }
  660. }
  661. }