util.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. //统计提交订单数据
  324. public static function incr_commit_pre($chname, $card_type, $spec, $quality)
  325. {
  326. $ins = Cache::getInstance('cacheredis');
  327. $name = 'commit_speed_monitor';
  328. $sec = time();
  329. $key_sec = "{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  330. $ins->hIncrBy($name, $key_sec, 1);
  331. }
  332. public static function hget_commit_pre_sec($chname, $card_type, $spec, $quality, $time_stamp)
  333. {
  334. $ins = Cache::getInstance('cacheredis');
  335. $name = 'commit_speed_monitor';
  336. $key_sec = "{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  337. $value = $ins->hget($name, '', $key_sec);
  338. return intval($value);
  339. }
  340. //统计用户提交数据
  341. public static function incr_user_commit($mchid,$card_type, $spec,$quality)
  342. {
  343. $ins = Cache::getInstance('cacheredis');
  344. $name = 'user_monitor_commit';
  345. $sec = time();
  346. $key_sec = "{$mchid}-{$quality}-{$card_type}-{$spec}-{$sec}";
  347. $ins->hIncrBy($name, $key_sec, 1);
  348. }
  349. public static function incr_user_success($mchid,$card_type, $spec,$quality)
  350. {
  351. $ins = Cache::getInstance('cacheredis');
  352. $name = 'user_monitor_success';
  353. $sec = time();
  354. $key_sec = "{$mchid}-{$quality}-{$card_type}-{$spec}-{$sec}";
  355. $ins->hIncrBy($name, $key_sec, 1);
  356. }
  357. public static function incr_user_fail($mchid,$card_type, $spec,$quality)
  358. {
  359. $ins = Cache::getInstance('cacheredis');
  360. $name = 'user_monitor_fail';
  361. $sec = time();
  362. $key_sec = "{$mchid}-{$quality}-{$card_type}-{$spec}-{$sec}";
  363. $ins->hIncrBy($name, $key_sec, 1);
  364. }
  365. public static function incr_commit($chname, $card_type, $spec, $quality, $fsuccess = true)
  366. {
  367. $ins = Cache::getInstance('cacheredis');
  368. $name = 'channel_monitor_commit';
  369. $sec = time();
  370. if ($fsuccess) {
  371. $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  372. } else {
  373. $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  374. }
  375. $ins->hIncrBy($name, $key_sec, 1);
  376. }
  377. public static function hget_commit_sec($chname, $card_type, $spec, $quality, $time_stamp, $fsuccess = true)
  378. {
  379. $ins = Cache::getInstance('cacheredis');
  380. $name = 'channel_monitor_commit';
  381. if ($fsuccess) {
  382. $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  383. } else {
  384. $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  385. }
  386. $value = $ins->hget($name, '', $key_sec);
  387. return intval($value);
  388. }
  389. //统计回调通知数据
  390. public static function incr_notify($chname, $card_type, $spec, $quality, $fsuccess = true)
  391. {
  392. $ins = Cache::getInstance('cacheredis');
  393. $name = 'channel_monitor_notify';
  394. $sec = time();
  395. if ($fsuccess) {
  396. $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  397. } else {
  398. $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$sec}";
  399. }
  400. $ins->hIncrBy($name, $key_sec, 1);
  401. }
  402. public static function hget_notify_sec($chname, $card_type, $spec, $quality, $time_stamp, $fsuccess = true)
  403. {
  404. $ins = Cache::getInstance('cacheredis');
  405. $name = 'channel_monitor_notify';
  406. if ($fsuccess) {
  407. $key_sec = "succ-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  408. } else {
  409. $key_sec = "fail-{$chname}-{$quality}-{$card_type}-{$spec}-{$time_stamp}";
  410. }
  411. $value = $ins->hget($name, '', $key_sec);
  412. return intval($value);
  413. }
  414. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  415. public static function incr_amount_lock($mchid, $card_type, $spec)
  416. {
  417. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  418. refill\rlock::incr_sys_storage($card_type,$spec,1);
  419. refill\rlock::incr_mch_total_storage($mchid,$card_type,$spec);
  420. refill\rlock::incr_mch_storage($mchid,$card_type,$spec,1);
  421. }
  422. }
  423. public static function decr_amount_lock($mchid, $card_type, $spec)
  424. {
  425. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  426. refill\rlock::decr_sys_storage($card_type,$spec,1);
  427. refill\rlock::decr_mch_total_storage($mchid,$card_type,$spec);
  428. refill\rlock::decr_mch_storage($mchid,$card_type,$spec,1);
  429. }
  430. }
  431. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  432. public static function monitor_submit($mchid,$mch_amount)
  433. {
  434. queue\MonitorClient::instance()->onSubmit($mchid,time(),$mch_amount);
  435. }
  436. public static function monitor_callback($mchid,$mch_amount,$channel_amount,$succ)
  437. {
  438. queue\MonitorClient::instance()->onCallback($mchid,time(),floatval($mch_amount),floatval($channel_amount),$succ);
  439. }
  440. public static function monitor_neterr($chname)
  441. {
  442. queue\MonitorClient::instance()->onNetError($chname, time());
  443. }
  444. public static function monitor_notify($chname,$spec,$card_type,$period,$succ)
  445. {
  446. queue\MonitorClient::instance()->onNotify($chname, time(),$spec,$card_type,$period,$succ);
  447. }
  448. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  449. public static function set_order_channels($mchid,$mchorder,$datas)
  450. {
  451. $ins = Cache::getInstance('cacheredis');
  452. $name = 'order_channels';
  453. $key = "{$mchid}-{$mchorder}";
  454. $ins->hset($name, '', [$key => serialize($datas)]);
  455. }
  456. public static function get_order_channels($mchid, $mchorder)
  457. {
  458. //old-name oil_exclude_channels
  459. $ins = Cache::getInstance('cacheredis');
  460. $name = 'order_channels';
  461. $key = "{$mchid}-{$mchorder}";
  462. $chnames = $ins->hget($name, '', $key);
  463. $chnames = unserialize($chnames);
  464. if(is_array($chnames)) {
  465. return $chnames;
  466. } else {
  467. return [];
  468. }
  469. }
  470. public static function del_order_channels($mchid, $mchorder)
  471. {
  472. $ins = Cache::getInstance('cacheredis');
  473. $name = 'order_channels';
  474. $key = "{$mchid}-{$mchorder}";
  475. $ins->hdel($name, '', $key);
  476. }
  477. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  478. public static function set_cancel_order($mchid,$mch_order)
  479. {
  480. $ins = Cache::getInstance('cacheredis');
  481. $name = 'order_cancel_hash';
  482. $key = "{$mchid}-{$mch_order}";
  483. $ins->hset($name, '', [$key=> 1]);
  484. }
  485. public static function query_cancel_order($mchid,$mch_order)
  486. {
  487. $ins = Cache::getInstance('cacheredis');
  488. $name = 'order_cancel_hash';
  489. $key = "{$mchid}-{$mch_order}";
  490. $value = $ins->hget($name,'',$key);
  491. return $value;
  492. }
  493. public static function del_cancel_order($mchid,$mch_order)
  494. {
  495. $ins = Cache::getInstance('cacheredis');
  496. $name = 'order_cancel_hash';
  497. $key = "{$mchid}-{$mch_order}";
  498. $ins->hdel($name, $key);
  499. }
  500. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  501. public static function set_next_order($mchid,$mch_order)
  502. {
  503. $ins = Cache::getInstance('cacheredis');
  504. $name = 'order_next_hash';
  505. $key = "{$mchid}-{$mch_order}";
  506. $ins->hset($name, '', [$key=> 1]);
  507. }
  508. public static function query_next_order($mchid,$mch_order)
  509. {
  510. $ins = Cache::getInstance('cacheredis');
  511. $name = 'order_next_hash';
  512. $key = "{$mchid}-{$mch_order}";
  513. $value = $ins->hget($name,'',$key);
  514. return $value;
  515. }
  516. public static function del_next_order($mchid,$mch_order)
  517. {
  518. $ins = Cache::getInstance('cacheredis');
  519. $name = 'order_success_hash';
  520. $key = "{$mchid}-{$mch_order}";
  521. $ins->hdel($name, $key);
  522. }
  523. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  524. public static function push_queue_order($mchid,$mch_order,$order_state)
  525. {
  526. if(empty($mch_order)) return;
  527. $ins = Cache::getInstance('cacheredis');
  528. $name = 'merchant_order_query';
  529. $key = "{$mchid}-{$mch_order}";
  530. $ins->hset($name, '', [$key => $order_state]);
  531. }
  532. public static function del_queue_order($mchid,$mch_order)
  533. {
  534. if(empty($mch_order)) return;
  535. $ins = Cache::getInstance('cacheredis');
  536. $name = 'merchant_order_query';
  537. $key = "{$mchid}-{$mch_order}";
  538. $ret = $ins->hdel($name, '', $key);
  539. }
  540. public static function pop_queue_order($mchid,$mch_order)
  541. {
  542. util::del_order_channels($mchid,$mch_order);
  543. Model('refill_order')->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_HANDLED]);
  544. $ins = Cache::getInstance('cacheredis');
  545. $name = 'merchant_order_query';
  546. $key = "{$mchid}-{$mch_order}";
  547. $ret = $ins->hdel($name, '', $key);
  548. }
  549. public static function query_queue_order($mchid,$mch_order)
  550. {
  551. $ins = Cache::getInstance('cacheredis');
  552. $name = 'merchant_order_query';
  553. $key = "{$mchid}-{$mch_order}";
  554. $value = $ins->hget($name,'',$key);
  555. return $value;
  556. }
  557. public static function need_check($net_errno)
  558. {
  559. if(empty($net_errno)) return false;
  560. [$type,$code] = explode('-',$net_errno);
  561. $code = intval($code);
  562. if($type == "CURL") {
  563. static $errors = [CURLE_GOT_NOTHING,CURLE_RECV_ERROR];
  564. return in_array($code,$errors);
  565. } elseif($type == "HTTP") {
  566. static $excludes = [404];
  567. return !in_array($code,$excludes);
  568. } else {
  569. return false;
  570. }
  571. }
  572. public static function order_errflag($net_errno) {
  573. return ($net_errno === 'ORDER_CREATE_FAIL');
  574. }
  575. public static function onOrderSuccess($refill_info,$order_info)
  576. {
  577. $data = store_member::instance()->get_member($order_info['store_id']);
  578. if(empty($data)) {
  579. Log::record("cannot find member when store_id={$order_info['store_id']}",Log::ERR);
  580. return false;
  581. }
  582. $data['order_sn'] = $refill_info['order_sn'];
  583. $data['amount'] = $refill_info['channel_amount'];
  584. $model_pd = Model('predeposit');
  585. $model_pd->changePd('order_pay',$data,true);
  586. return true;
  587. }
  588. public static function getProvider($name,$type = 'RefillPhone')
  589. {
  590. $file = BASE_HELPER_RAPI_PATH . "/$name/{$type}.php";
  591. if(!file_exists($file)){
  592. Log::record("provider api file={$file} not exist.",Log::DEBUG);
  593. return false;
  594. } else {
  595. require_once($file);
  596. Log::record("file={$file} load success.",Log::DEBUG);
  597. }
  598. $class_name = "refill\\{$name}\\{$type}";
  599. if (class_exists($class_name, false)) {
  600. $caller = new $class_name([]);
  601. return $caller;
  602. } else {
  603. $error = "Base Error: class {$class_name} isn't exists!";
  604. Log::record($error, Log::ERR);
  605. return false;
  606. }
  607. }
  608. public static function xmlToArray($xml)
  609. {
  610. $object = simplexml_load_string($xml);
  611. $val = json_decode(json_encode($object), true);
  612. $msg = json_encode($val);
  613. Log::record("xmlToArray result={$msg}", Log::DEBUG);
  614. return $val;
  615. }
  616. //for tester
  617. public static function send_normal($order_sn)
  618. {
  619. $status = mt_rand(1,100);
  620. if($status > 97) {
  621. $status = 1;
  622. } else {
  623. $status = 0;
  624. }
  625. $url = BASE_SITE_URL . "/mobile/callback/refill_baidu.php";
  626. go(function () use ($url, $status,$order_sn)
  627. {
  628. sleep(3);
  629. while (true)
  630. {
  631. $resp = http_request($url,['status' => $status,'order_sn' => $order_sn],'GET', false, [], $net_errno);
  632. if($resp == 'SUCCESS') {
  633. break;
  634. }
  635. }
  636. Log::record("resp = {$resp}",Log::DEBUG);
  637. });
  638. }
  639. public static function send_quick($order_sn)
  640. {
  641. $status = mt_rand(1,10);
  642. if($status > 3) {
  643. $status = 1;
  644. } else {
  645. $status = 0;
  646. }
  647. $url = BASE_SITE_URL . "/mobile/callback/refill_baidu.php";
  648. go(function () use ($url, $status,$order_sn)
  649. {
  650. sleep(3);
  651. while (true)
  652. {
  653. $resp = http_request($url,['status' => $status,'order_sn' => $order_sn],'GET', false, [], $net_errno);
  654. if($resp == 'SUCCESS') {
  655. break;
  656. }
  657. }
  658. Log::record("resp = {$resp}",Log::DEBUG);
  659. });
  660. }
  661. public static function retry_canceled_order($order_id, $skip)
  662. {
  663. $mod_order = Model('vr_order');
  664. $mod_refill = Model('refill_order');
  665. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  666. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  667. if(empty($refill_info) || empty($order_info)) {
  668. return [false,'无此订单或者订单已经重试中了...'];
  669. }
  670. $tran = new trans_wapper($mod_order,'notify change order state trans');
  671. try
  672. {
  673. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id], '*', true, true);
  674. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0], '*', true, true);
  675. $order_state = intval($order_info['order_state']);
  676. if(empty($refill_info) || $refill_info['is_retrying'] == 1 || $order_state != ORDER_STATE_CANCEL) {
  677. $tran->commit();
  678. return [false,'订单已经在重试'];
  679. }
  680. $mod_refill->edit($order_id, ['is_retrying' => 1]);
  681. $tran->commit();
  682. $order = refill\order::from_db($refill_info,$order_info);
  683. $params = $order->queue_params();
  684. $params['order_time'] = time();
  685. if ($skip) {
  686. $mchid = $refill_info['mchid'];
  687. $mch_order = $refill_info['mch_order'];
  688. refill\util::set_next_order($mchid, $mch_order);
  689. }
  690. if(util::push_add($params)) {
  691. return [true,''];
  692. } else {
  693. return [false,'加入队列出错'];
  694. }
  695. }
  696. catch (Exception $ex) {
  697. $tran->rollback();
  698. Log::record($ex->getMessage(),Log::ERR);
  699. return [false,"{$ex->getMessage()}"];
  700. }
  701. }
  702. }