WalletService.php 28 KB

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