WalletService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\AccountLogModel;
  13. use App\Models\BalanceLogModel;
  14. use App\Models\MemberModel;
  15. use App\Models\WalletLogModel;
  16. use App\Models\WalletModel;
  17. use App\Services\Api\FinanceService;
  18. use App\Services\Api\PledgeOrderService;
  19. use Illuminate\Support\Facades\DB;
  20. use Tighten\SolanaPhpSdk\Connection;
  21. use Tighten\SolanaPhpSdk\PublicKey;
  22. use Tighten\SolanaPhpSdk\SolanaRpcClient;
  23. /**
  24. * 钱包管理-服务类
  25. * @package App\Services
  26. */
  27. class WalletService extends BaseService
  28. {
  29. protected $apiUrl = '';
  30. protected $bianUrl = 'http://p2p.binance.com/bapi/c2c/v2/public/c2c/adv/quoted-price';
  31. protected $ouyiApi = 'https://www.okx.com/priapi/v3/b2c/deposit/quotedPrice';
  32. protected $config = [];
  33. protected $tokenAddress = 'So11111111111111111111111111111111111111111'; // solana
  34. protected $usdtTokenAddress = 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB'; // solana-usdt
  35. // 静态对象
  36. protected static $instance = null;
  37. protected $apiUrls = [
  38. // 代币账号
  39. 'tokenAccount' => '/v2.0/account/token-accounts',
  40. // 账户交易记录
  41. 'accountTransfer' => '/v2.0/account/transfer?address=%s&activity_type[]=%s&block_time[]=%s&page=%s&page_size=%s',
  42. // 代币账户
  43. 'tokenAccounts'=>'/v2.0/account/token-accounts?address=%s&type=%s&hide_zero=true'
  44. ];
  45. /**
  46. * 构造函数
  47. */
  48. public function __construct()
  49. {
  50. $this->config = ConfigService::make()->getConfigOptionByGroup(14);
  51. if (empty($this->config)) {
  52. return false;
  53. }
  54. }
  55. /**
  56. * 静态入口
  57. * @return static|null
  58. */
  59. public static function make()
  60. {
  61. if (!self::$instance) {
  62. self::$instance = (new static());
  63. }
  64. return self::$instance;
  65. }
  66. /**
  67. * 获取平台钱包地址
  68. * @param int $type 钱包类型:1-充值入账,2-提现出账,默认充值
  69. * @param int $amount 提币时,提币金额验证,充值钱包可忽略
  70. * @param int $coinType 币种:1-USDT
  71. * @param boolean $privateKey 是否返回解码的私钥
  72. * @return array|false|mixed、
  73. */
  74. public function getWallet($type = 1, $amount = 0, $coinType = 1, $privateKey = false)
  75. {
  76. $cacheKey = "caches:wallet:platform:{$type}";
  77. $wallets = RedisService::get($cacheKey);
  78. if (empty($wallets)) {
  79. $wallets = WalletModel::where(['type' => $type, 'status' => 1, 'mark' => 1])->select(['address', 'private_key'])->get();
  80. $wallets = $wallets ? $wallets->toArray() : [];
  81. if ($wallets) {
  82. RedisService::set($cacheKey, $wallets, rand(300, 600));
  83. }
  84. }
  85. if (empty($wallets)) {
  86. $this->error = $type == 1 ? 1036 : 1037;
  87. return false;
  88. }
  89. // 需要验证余额
  90. if ($amount > 0) {
  91. foreach ($wallets as $item) {
  92. $address = isset($item['address']) ? trim($item['address']) : '';
  93. $privateKey = isset($item['private_key']) ? trim($item['private_key']) : '';
  94. $privateKey = $this->getAddressPrivateKey($privateKey, $address, 2);
  95. if ($coinType == 1) {
  96. $accountData = $this->getUsdtBalance($address);
  97. $balance = isset($accountData['amount'])? $accountData['amount'] : 0;
  98. } else {
  99. $accountData = $this->getUsdtBalance($address);
  100. $balance = isset($accountData['amount'])? $accountData['amount'] : 0;
  101. }
  102. if ($balance >= $amount) {
  103. $tokenAccount = isset($accountData['tokenAccount'])? $accountData['tokenAccount'] : '';
  104. return ['address' => $address,'tokenAccount'=>$tokenAccount, 'private_key' => $privateKey];
  105. }
  106. }
  107. $this->error = 1038;
  108. return false;
  109. } else {
  110. $rand = rand(0, count($wallets) - 1);
  111. $wallet = $wallets[$rand];
  112. if ($privateKey) {
  113. $wallet['private_key'] = $this->getAddressPrivateKey($wallet['private_key'], $wallet['address'], 2);
  114. }
  115. return $wallet;
  116. }
  117. }
  118. /**
  119. * 获取所有钱包地址
  120. * @return array|mixed
  121. */
  122. public function getWalletList()
  123. {
  124. try {
  125. $cacheKey = "caches:wallet:platform:all";
  126. $wallets = RedisService::get($cacheKey);
  127. if ($wallets) {
  128. return $wallets;
  129. }
  130. $wallets = WalletModel::where(['status' => 1, 'mark' => 1])->select(['id', 'address', 'type'])->get();
  131. $wallets = $wallets ? $wallets->toArray() : [];
  132. if ($wallets) {
  133. RedisService::set($cacheKey, $wallets, rand(300, 600));
  134. }
  135. return $wallets;
  136. } catch (\Exception $exception){
  137. $this->error = $exception->getMessage();
  138. return false;
  139. }
  140. }
  141. /**
  142. * 加解密密钥
  143. * @param $key 密钥或加密密钥
  144. * @param $address 地址
  145. * @param $type 类型:1-加密,2-解密
  146. * @return mixed|string|string[]
  147. */
  148. public function getAddressPrivateKey($key, $address, $type)
  149. {
  150. // 加密
  151. if ($type == 1) {
  152. $baseStr = base64_encode($key);
  153. $baseStr = str_replace(['==', '='], ['-2', '-1'], $baseStr);
  154. $str = substr($baseStr, -6, 7) . substr(md5($address), 2, 6) . substr($baseStr, 0, -6);
  155. return $str;
  156. } // 解密
  157. else {
  158. $str1 = substr($key, 12) . substr($key, 0, 6);
  159. $str1 = str_replace(['-1', '-2'], ['=', '=='], $str1);
  160. return base64_decode($str1);
  161. }
  162. }
  163. /**
  164. * 验证地址
  165. * @param $address
  166. * @return string
  167. */
  168. public function checkAddress($address)
  169. {
  170. try{
  171. $publicKey = new PublicKey($address);
  172. return PublicKey::isOnCurve($publicKey);
  173. } catch (\Exception $exception){
  174. $this->error = $exception->getMessage();
  175. return false;
  176. }
  177. }
  178. /**
  179. * solana余额
  180. * @param $address
  181. * @return false|float|string
  182. */
  183. public function getBalance($address, $cache = false)
  184. {
  185. if (empty($address)) {
  186. $this->error = '1018';
  187. return false;
  188. }
  189. $cacheKey = "caches:wallet:balance:sol_{$address}";
  190. if ($data = RedisService::get($cacheKey) && $cache) {
  191. return $data;
  192. }
  193. if (RedisService::get($cacheKey . '_lock') && $cache) {
  194. return false;
  195. }
  196. try {
  197. $sdk = new Connection(new SolanaRpcClient(SolanaRpcClient::MAINNET_ENDPOINT));
  198. $publicKey = new PublicKey($address);
  199. $balance = $sdk->getBalance($publicKey);
  200. RedisService::set($cacheKey, $balance, rand(3, 5));
  201. RedisService::set($cacheKey . '_lock', true, rand(3, 5));
  202. return $balance;
  203. } catch (\Exception $exception) {
  204. $this->error = $exception->getMessage();
  205. return false;
  206. }
  207. }
  208. /**
  209. * solana-USDT余额
  210. * @param $address
  211. * @return false|float|string
  212. */
  213. public function getUsdtBalance($address, $cache = false)
  214. {
  215. if (empty($address)) {
  216. $this->error = 1018;
  217. return false;
  218. }
  219. $cacheKey = "caches:wallet:balance:sol_usdt_{$address}";
  220. $data = RedisService::get($cacheKey);
  221. if ($data && !$cache) {
  222. return $data;
  223. }
  224. try {
  225. $apiUrl = ConfigService::make()->getConfigByCode('solana_scan_api', '');
  226. if (empty($apiUrl)) {
  227. $this->error = 1040;
  228. return false;
  229. }
  230. $token = ConfigService::make()->getConfigByCode('solana_scan_api_key', '');
  231. if (empty($token)) {
  232. $this->error = 1039;
  233. return false;
  234. }
  235. $headers = [
  236. "token: {$token}",
  237. ];
  238. $url = $apiUrl . sprintf($this->apiUrls['tokenAccounts'],$address,'token');
  239. $result = curl_get($url, '', $headers, 5);
  240. $data = $result ? json_decode($result, true) : [];
  241. RedisService::set($cacheKey.'_result', ['url'=>$url,'address'=>$address,'data'=>$data,'result'=>$result,'headers'=>$headers], 600);
  242. $accounts = isset($data['data']) ? $data['data'] : [];
  243. if(empty($accounts)){
  244. $this->error = 2208;
  245. return false;
  246. }
  247. $balance = 0;
  248. $tokenAccount = '';
  249. foreach ($accounts as $item){
  250. if($item['token_address'] = $this->usdtTokenAddress){
  251. $tokenAccount = $item['token_account'];
  252. $balance = moneyFormat($item['amount']/pow(10, $item['token_decimals']), 2);
  253. }
  254. }
  255. RedisService::set($cacheKey, ['amount'=> $balance,'tokenAccount'=>$tokenAccount], rand(5, 10));
  256. return ['amount'=> $balance,'tokenAccount'=>$tokenAccount];
  257. } catch (\Exception $exception) {
  258. $this->error = $exception->getMessage();
  259. RedisService::set($cacheKey.'_error', ['error'=> $exception->getMessage(),'address'=>$address], 600);
  260. return false;
  261. }
  262. }
  263. /**
  264. * solana usdt 转账
  265. * @param $to 进账账户
  266. * @param $amount 转账金额
  267. */
  268. public function usdtTransfer($to, $amount, $from = '', $fromPrivateKey = '')
  269. {
  270. if ($amount <= 0) {
  271. $this->error = '2205';
  272. return false;
  273. }
  274. }
  275. /**
  276. * 监听充值存币处理
  277. * @param $address 钱包地址
  278. * @param int $accountType 账户类型:1-USDT充值,2-USDT提现
  279. * @param int $page 分页:默认0-轮询分页20页,每页100条记录
  280. * @return array|false
  281. */
  282. public function listenWallet($address, $accountType = 1, $page = 0)
  283. {
  284. // 获取钱包参数
  285. try {
  286. $apiUrl = ConfigService::make()->getConfigByCode('solana_scan_api', '');
  287. if (empty($apiUrl)) {
  288. $this->error = 1040;
  289. return false;
  290. }
  291. $token = ConfigService::make()->getConfigByCode('solana_scan_api_key', '');
  292. if (empty($token)) {
  293. $this->error = 1039;
  294. return false;
  295. }
  296. $headers = [
  297. "token: {$token}",
  298. ];
  299. if ($page <= 0) {
  300. $page = RedisService::get('caches:wallet:listen_recharge_page_' . $address);
  301. $maxPage = ConfigService::make()->getConfigByCode('wallet_listen_max_page', 20);
  302. $page = $page > 0 && $page <= $maxPage ? $page : 1;
  303. }
  304. $limit = ConfigService::make()->getConfigByCode('wallet_listen_limit', 300);
  305. $limit = $limit > 10 && $limit <= 100 ? $limit : 100;
  306. $time = ConfigService::make()->getConfigByCode('wallet_listen_time', 48);
  307. $startTime = $time > 0 ? time() - $time * 3600 : time() - 7200;
  308. $url = $apiUrl . sprintf($this->apiUrls['accountTransfer'], $address, 'ACTIVITY_SPL_TRANSFER', $startTime, $page, $limit);
  309. $result = curl_get($url, [], $headers, 5);
  310. $result = $result ? json_decode($result, true) : [];
  311. $datas = isset($result['data']) && is_array($result['data']) ? $result['data'] : [];
  312. $status = isset($result['success']) ? $result['success'] : '';
  313. $error = isset($result['error_message']) ? $result['error_message'] : '';
  314. RedisService::set("caches:wallet:listen_{$address}", ['url' => $url, 'page' => $page, 'result' => $result], 7200);
  315. if ($status != true || empty($datas)) {
  316. $this->error = $error ? '第{$page}页接口错误-' . $error : "第{$page}页没有取到数据";
  317. return false;
  318. }
  319. RedisService::set('caches:wallet:listen_recharge_page', $page + 1, rand(3600, 7200));
  320. $coinInMin = ConfigService::make()->getConfigByCode('recharge_min_money');
  321. $coinInMin = $coinInMin > 0 ? $coinInMin : 0;
  322. $cacheKey = "caches:wallet:listen:{$address}_{$accountType}:";
  323. $dateTime = date('Y-m-d H:i:s');
  324. $success = 0;
  325. if ($datas) {
  326. foreach ($datas as $v) {
  327. $amount = isset($v['amount']) ? intval($v['amount']) : 0;
  328. $tokenDecimals = isset($v['token_decimals']) && $v['token_decimals'] ? intval($v['token_decimals']) : 6;
  329. $amount = moneyFormat($amount / pow(10, $tokenDecimals), 6);
  330. $time = isset($v['block_time']) ? intval($v['block_time']) : 0;
  331. $txid = isset($v['trans_id']) ? $v['trans_id'] : '';
  332. $ownerAddress = isset($v['from_address']) ? $v['from_address'] : '';
  333. $toAddress = isset($v['to_address']) ? $v['to_address'] : '';
  334. $tokenAddress = isset($v['token_address']) ? $v['token_address'] : '';
  335. $coinType = 0;
  336. $coinName = '';
  337. if ($tokenAddress == $this->tokenAddress) {
  338. $coinType = 2;
  339. $coinName = 'Sol';
  340. } else if ($tokenAddress == $this->usdtTokenAddress) {
  341. $coinType = 1;
  342. $coinName = 'USDT';
  343. }
  344. if($amount<=0){
  345. echo "【{$dateTime} wallet】账户[{$ownerAddress}]到钱包[{$toAddress}]记录[{$txid}]金额[{$amount}]{$coinName},金额较小不处理\n";
  346. RedisService::set($cacheKey . "{$txid}:error", ['error' => '金额较小不处理', 'data' => $v], 7200);
  347. continue;
  348. }
  349. // 处理交易记录
  350. if (!WalletLogModel::checkExists($txid)) {
  351. $log = [
  352. 'owner_address' => $ownerAddress,
  353. 'to_address' => $toAddress,
  354. 'token_address' => $tokenAddress,
  355. 'token_type' => $coinType,
  356. 'hash' => $txid,
  357. 'amount' => $amount,
  358. 'create_time' => $time ? $time : time(),
  359. 'update_time' => time(),
  360. 'status' => 1,
  361. 'mark' => 1,
  362. ];
  363. WalletLogModel::insert($log);
  364. }
  365. // 充值处理(solana-usdt)
  366. if ($toAddress == $address && $ownerAddress && $accountType != 2) {
  367. if ($amount < $coinInMin) {
  368. echo "【{$dateTime} wallet】账户[{$ownerAddress}]到钱包[{$toAddress}]记录[{$txid}]金额[{$amount}]{$coinName},金额较小不处理\n";
  369. RedisService::set($cacheKey . "{$txid}:error", ['error' => '金额较小不处理', 'data' => $v], 7200);
  370. continue;
  371. }
  372. if ($tokenAddress != $this->usdtTokenAddress) {
  373. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]{$coinName}币种不匹配\n";
  374. RedisService::set($cacheKey . "{$txid}:error", ['error' => '币种不匹配', 'data' => $v], 7200);
  375. continue;
  376. }
  377. if (BalanceLogModel::checkExists($txid)) {
  378. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]{$coinName}交易已处理过\n";
  379. RedisService::set($cacheKey . "{$txid}:error", ['error' => '交易已处理过', 'data' => $v], 7200);
  380. continue;
  381. }
  382. $memberInfo = MemberModel::where(['wallet_url' => $ownerAddress, 'mark' => 1])
  383. ->select(['id', 'nickname', 'wallet_url', 'usdt', 'sbt'])
  384. ->orderBy('id', 'asc')
  385. ->first();
  386. $trcUrl = isset($memberInfo['wallet_url']) ? trim($memberInfo['wallet_url']) : '';
  387. $usdt = isset($memberInfo['usdt']) ? floatval($memberInfo['usdt']) : 0;
  388. $userId = isset($memberInfo['id']) ? intval($memberInfo['id']) : 0;
  389. if (empty($memberInfo)) {
  390. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]{$coinName}未绑定用户\n";
  391. RedisService::set($cacheKey . "{$txid}:error", ['error' => '未绑定用户', 'data' => $v], 7200);
  392. continue;
  393. }
  394. // 入账
  395. try {
  396. DB::beginTransaction();
  397. $updateData = ['update_time' => time()];
  398. $total = $amount;
  399. $updateData['usdt'] = DB::raw("usdt + {$total}");
  400. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  401. DB::rollBack();
  402. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]入账处理失败\n";
  403. RedisService::set($cacheKey . "{$txid}:error", ['error' => '入账处理失败', 'update' => $updateData, 'member' => $memberInfo, 'data' => $v], 7200);
  404. continue;
  405. }
  406. // 充值明细
  407. $data = [
  408. 'user_id' => $userId,
  409. 'order_no' => get_order_num('SR'),
  410. 'type' => 1,
  411. 'coin_type' => 1,
  412. 'money' => $amount,
  413. 'actual_money' => $total,
  414. 'pay_type' => 20,
  415. 'pay_status' => 20,
  416. 'pay_at' => date('Y-m-d H:i:s', $time),
  417. 'hash' => $txid,
  418. 'wallet_url' => $trcUrl,
  419. 'pt_wallet_url' => $address,
  420. 'date' => date('Y-m-d'),
  421. 'create_time' => time(),
  422. 'update_time' => time(),
  423. 'status' => 2,
  424. 'mark' => 1,
  425. ];
  426. if (!BalanceLogModel::insert($data)) {
  427. DB::rollBack();
  428. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]充值明细处理失败\n";
  429. RedisService::set($cacheKey . "{$txid}:error", ['error' => '充值明细处理失败', 'log' => $data, 'member' => $memberInfo, 'data' => $v], 7200);
  430. continue;
  431. }
  432. // 用户账户明细
  433. $log = [
  434. 'user_id' => $userId,
  435. 'order_no' => $data['order_no'],
  436. 'user_type' => 1,
  437. 'type' => 3,
  438. 'coin_type' => 1,
  439. 'hash' => $txid,
  440. 'money' => $amount,
  441. 'before_money' => $usdt,
  442. 'create_time' => time(),
  443. 'update_time' => date('Y-m-d H:i:s'),
  444. 'remark' => 'USDT充值',
  445. 'status' => 1,
  446. 'mark' => 1,
  447. ];
  448. if (!AccountLogModel::insert($log)) {
  449. DB::rollBack();
  450. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]账户明细处理失败\n";
  451. RedisService::set($cacheKey . "{$txid}:error", ['error' => '账户明细处理失败', 'log' => $log, 'member' => $memberInfo, 'data' => $v], 7200);
  452. continue;
  453. }
  454. // 平台进账
  455. FinanceService::make()->saveLog(0, $amount, 1);
  456. DB::commit();
  457. $amount = moneyFormat($amount, 2);
  458. RedisService::set($cacheKey . "{$txid}:success", ['error' => '处理成功', 'log' => $data, 'member' => $memberInfo, 'data' => $v], 7200);
  459. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]充值成功\n";
  460. RedisService::clear("caches:wallet:balanceLog:{$txid}");
  461. RedisService::keyDel("caches:balanceLog:total*");
  462. $success++;
  463. } catch (\Exception $exception) {
  464. DB::rollBack();
  465. RedisService::clear("caches:wallet:balanceLog:{$txid}");
  466. $this->error = "处理错误-" . $exception->getMessage();
  467. echo "处理错误-" . $exception->getMessage();
  468. continue;
  469. }
  470. } // 提现处理
  471. else if ($ownerAddress == $address && $toAddress && $accountType == 2) {
  472. // 处理锁跳过
  473. if (RedisService::get($cacheKey . "{$txid}:lock")) {
  474. echo "【{$dateTime} withdraw】提现记录[{$txid}]金额[{$amount}]间隔时间不处理\n";
  475. RedisService::set($cacheKey . "{$txid}:error", ['error' => '间隔时间内不处理', 'data' => $v], 600);
  476. continue;
  477. }
  478. $info = BalanceLogModel::checkExists($txid);
  479. $logId = isset($info['id']) ? $info['id'] : 0;
  480. $userId = isset($info['user_id']) ? $info['user_id'] : 0;
  481. $money = isset($info['money']) ? $info['money'] : 0;
  482. $actualMoney = isset($info['actual_money']) ? $info['actual_money'] : 0;
  483. $payStatus = isset($info['pay_status']) ? $info['pay_status'] : 0;
  484. $status = isset($info['status']) ? $info['status'] : 0;
  485. $trcUrl = isset($info['wallet_url']) ? trim($info['wallet_url']) : '';
  486. $walletUrl = isset($info['pt_wallet_url']) ? trim($info['pt_wallet_url']) : '';
  487. if (empty($info) || $logId <= 0 || $userId <= 0) {
  488. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}]提现记录不存在\n";
  489. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现记录不存在', 'data' => $v], 7200);
  490. RedisService::set($cacheKey . "{$txid}:lock", ['error' => '提现记录不存在', 'data' => $v], rand(30, 60));
  491. continue;
  492. }
  493. if ($status != 2) {
  494. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}]提现记录未审核\n";
  495. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现记录未审核', 'data' => $v], 7200);
  496. RedisService::set($cacheKey . "{$txid}:lock", ['error' => '提现记录未审核', 'data' => $v], rand(30, 60));
  497. continue;
  498. }
  499. if ($payStatus == 20) {
  500. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}]提现记录已打款\n";
  501. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现记录已打款', 'data' => $v], 7200);
  502. RedisService::set($cacheKey . "{$txid}:lock", ['error' => '提现记录已打款', 'data' => $v], rand(30, 60));
  503. continue;
  504. }
  505. if ($trcUrl != $toAddress || $walletUrl != $ownerAddress) {
  506. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}],提现订单交易地址不匹配\n";
  507. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现订单交易地址不匹配', 'info' => $info, 'data' => $v], 7200);
  508. continue;
  509. }
  510. // 金额验证
  511. if (abs($actualMoney - $amount) > 1) {
  512. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}],提现金额与交易金额不匹配\n";
  513. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现金额与交易金额不匹配', 'info' => $info, 'data' => $v], 7200);
  514. RedisService::set($cacheKey . "{$txid}:lock", ['error' => '提现金额与交易金额不匹配', 'data' => $v], rand(30, 60));
  515. continue;
  516. }
  517. // 更新状态
  518. try {
  519. DB::beginTransaction();
  520. if (!BalanceLogModel::where(['id' => $logId])->update(['pay_status' => 20, 'pay_at' => $dateTime, 'update_time' => time()])) {
  521. DB::rollBack();
  522. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}],提现状态更新失败\n";
  523. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现状态更新失败', 'info' => $info, 'data' => $v], 7200);
  524. continue;
  525. }
  526. // 平台出账
  527. FinanceService::make()->saveLog(0, $amount, 2);
  528. DB::commit();
  529. $amount = moneyFormat($amount, 2);
  530. RedisService::set($cacheKey . "{$txid}:success", ['error' => '处理成功', 'log' => $log, 'info' => $info, 'data' => $v], 7200);
  531. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}],提现成功\n";
  532. RedisService::clear("caches:wallet:balanceLog:{$txid}");
  533. $success++;
  534. } catch (\Exception $exception) {
  535. DB::rollBack();
  536. RedisService::clear("caches:wallet:balanceLog:{$txid}");
  537. $this->error = "处理错误-" . $exception->getMessage();
  538. echo "处理错误-" . $exception->getMessage();
  539. continue;
  540. }
  541. }
  542. }
  543. }
  544. $this->error = 1010;
  545. return ['success' => $success, 'total' => count($datas)];
  546. } catch (\Exception $exception) {
  547. $message = $exception->getMessage();
  548. $this->error = $message;
  549. RedisService::set("caches:wallet:listen_error_{$address}", ['error' => $exception->getMessage(), 'trace' => $exception->getTrace()], 600);
  550. return false;
  551. }
  552. }
  553. }