util.php 27 KB

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