WalletService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. if ($data = RedisService::get($cacheKey) && $cache) {
  221. return $data;
  222. }
  223. if (RedisService::get($cacheKey . '_lock') && $cache) {
  224. return false;
  225. }
  226. try {
  227. $apiUrl = ConfigService::make()->getConfigByCode('solana_scan_api', '');
  228. if (empty($apiUrl)) {
  229. $this->error = 1040;
  230. return false;
  231. }
  232. $token = ConfigService::make()->getConfigByCode('solana_scan_api_key', '');
  233. if (empty($token)) {
  234. $this->error = 1039;
  235. return false;
  236. }
  237. $headers = [
  238. "token: {$token}",
  239. ];
  240. $url = $apiUrl . sprintf($this->apiUrls['tokenAccounts'],$address,'token');
  241. $result = curl_get($url, [], $headers, 5);
  242. $result = $result ? json_decode($result, true) : [];
  243. $accounts = isset($result['data']) ? $result['data'] : [];
  244. if(empty($accounts)){
  245. $this->error = 2208;
  246. return false;
  247. }
  248. $balance = 0;
  249. $tokenAccount = '';
  250. foreach ($accounts as $item){
  251. if($item['token_address'] = $this->usdtTokenAddress){
  252. $tokenAccount = $item['token_account'];
  253. $balance = moneyFormat($item['amount']/pow(10, $item['token_decimals']), 2);
  254. }
  255. }
  256. RedisService::set($cacheKey, ['amount'=> $balance,'tokenAccount'=>$tokenAccount], rand(3, 5));
  257. RedisService::set($cacheKey . '_lock', true, rand(3, 5));
  258. return ['amount'=> $balance,'tokenAccount'=>$tokenAccount];
  259. } catch (\Exception $exception) {
  260. $this->error = $exception->getMessage();
  261. return false;
  262. }
  263. }
  264. /**
  265. * solana usdt 转账
  266. * @param $to 进账账户
  267. * @param $amount 转账金额
  268. */
  269. public function usdtTransfer($to, $amount, $from = '', $fromPrivateKey = '')
  270. {
  271. if ($amount <= 0) {
  272. $this->error = '2205';
  273. return false;
  274. }
  275. }
  276. /**
  277. * 监听充值存币处理
  278. * @param $address 钱包地址
  279. * @param int $accountType 账户类型:1-USDT充值,2-USDT提现
  280. * @param int $page 分页:默认0-轮询分页20页,每页100条记录
  281. * @return array|false
  282. */
  283. public function listenWallet($address, $accountType = 1, $page = 0)
  284. {
  285. // 获取钱包参数
  286. try {
  287. $apiUrl = ConfigService::make()->getConfigByCode('solana_scan_api', '');
  288. if (empty($apiUrl)) {
  289. $this->error = 1040;
  290. return false;
  291. }
  292. $token = ConfigService::make()->getConfigByCode('solana_scan_api_key', '');
  293. if (empty($token)) {
  294. $this->error = 1039;
  295. return false;
  296. }
  297. $headers = [
  298. "token: {$token}",
  299. ];
  300. if ($page <= 0) {
  301. $page = RedisService::get('caches:wallet:listen_recharge_page_' . $address);
  302. $maxPage = ConfigService::make()->getConfigByCode('wallet_listen_max_page', 20);
  303. $page = $page > 0 && $page <= $maxPage ? $page : 1;
  304. }
  305. $limit = ConfigService::make()->getConfigByCode('wallet_listen_limit', 300);
  306. $limit = $limit > 10 && $limit <= 100 ? $limit : 100;
  307. $time = ConfigService::make()->getConfigByCode('wallet_listen_time', 48);
  308. $startTime = $time > 0 ? time() - $time * 3600 : time() - 7200;
  309. $url = $apiUrl . sprintf($this->apiUrls['accountTransfer'], $address, 'ACTIVITY_SPL_TRANSFER', $startTime, $page, $limit);
  310. $result = curl_get($url, [], $headers, 5);
  311. $result = $result ? json_decode($result, true) : [];
  312. $datas = isset($result['data']) && is_array($result['data']) ? $result['data'] : [];
  313. $status = isset($result['success']) ? $result['success'] : '';
  314. $error = isset($result['error_message']) ? $result['error_message'] : '';
  315. RedisService::set("caches:wallet:listen_{$address}", ['url' => $url, 'page' => $page, 'result' => $result], 7200);
  316. if ($status != true || empty($datas)) {
  317. $this->error = $error ? '第{$page}页接口错误-' . $error : "第{$page}页没有取到数据";
  318. return false;
  319. }
  320. RedisService::set('caches:wallet:listen_recharge_page', $page + 1, rand(3600, 7200));
  321. $coinInMin = ConfigService::make()->getConfigByCode('recharge_min_money');
  322. $coinInMin = $coinInMin > 0 ? $coinInMin : 0;
  323. $cacheKey = "caches:wallet:listen:{$address}_{$accountType}:";
  324. $dateTime = date('Y-m-d H:i:s');
  325. $success = 0;
  326. if ($datas) {
  327. foreach ($datas as $v) {
  328. $amount = isset($v['amount']) ? intval($v['amount']) : 0;
  329. $tokenDecimals = isset($v['token_decimals']) && $v['token_decimals'] ? intval($v['token_decimals']) : 6;
  330. $amount = moneyFormat($amount / pow(10, $tokenDecimals), 6);
  331. $time = isset($v['block_time']) ? intval($v['block_time']) : 0;
  332. $txid = isset($v['trans_id']) ? $v['trans_id'] : '';
  333. $ownerAddress = isset($v['from_address']) ? $v['from_address'] : '';
  334. $toAddress = isset($v['to_address']) ? $v['to_address'] : '';
  335. $tokenAddress = isset($v['token_address']) ? $v['token_address'] : '';
  336. $coinType = 0;
  337. $coinName = '';
  338. if ($tokenAddress == $this->tokenAddress) {
  339. $coinType = 2;
  340. $coinName = 'Sol';
  341. } else if ($tokenAddress == $this->usdtTokenAddress) {
  342. $coinType = 1;
  343. $coinName = 'USDT';
  344. }
  345. if($amount<=0){
  346. echo "【{$dateTime} wallet】账户[{$ownerAddress}]到钱包[{$toAddress}]记录[{$txid}]金额[{$amount}]{$coinName},金额较小不处理\n";
  347. RedisService::set($cacheKey . "{$txid}:error", ['error' => '金额较小不处理', 'data' => $v], 7200);
  348. continue;
  349. }
  350. // 处理交易记录
  351. if (!WalletLogModel::checkExists($txid)) {
  352. $log = [
  353. 'owner_address' => $ownerAddress,
  354. 'to_address' => $toAddress,
  355. 'token_address' => $tokenAddress,
  356. 'token_type' => $coinType,
  357. 'hash' => $txid,
  358. 'amount' => $amount,
  359. 'create_time' => $time ? $time : time(),
  360. 'update_time' => time(),
  361. 'status' => 1,
  362. 'mark' => 1,
  363. ];
  364. WalletLogModel::insert($log);
  365. }
  366. // 充值处理(solana-usdt)
  367. if ($toAddress == $address && $ownerAddress && $accountType != 2) {
  368. if ($amount < $coinInMin) {
  369. echo "【{$dateTime} wallet】账户[{$ownerAddress}]到钱包[{$toAddress}]记录[{$txid}]金额[{$amount}]{$coinName},金额较小不处理\n";
  370. RedisService::set($cacheKey . "{$txid}:error", ['error' => '金额较小不处理', 'data' => $v], 7200);
  371. continue;
  372. }
  373. if ($tokenAddress != $this->usdtTokenAddress) {
  374. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]{$coinName}币种不匹配\n";
  375. RedisService::set($cacheKey . "{$txid}:error", ['error' => '币种不匹配', 'data' => $v], 7200);
  376. continue;
  377. }
  378. if (BalanceLogModel::checkExists($txid)) {
  379. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]{$coinName}交易已处理过\n";
  380. RedisService::set($cacheKey . "{$txid}:error", ['error' => '交易已处理过', 'data' => $v], 7200);
  381. continue;
  382. }
  383. $memberInfo = MemberModel::where(['wallet_url' => $ownerAddress, 'mark' => 1])
  384. ->select(['id', 'nickname', 'wallet_url', 'usdt', 'sbt'])
  385. ->orderBy('id', 'asc')
  386. ->first();
  387. $trcUrl = isset($memberInfo['wallet_url']) ? trim($memberInfo['wallet_url']) : '';
  388. $usdt = isset($memberInfo['usdt']) ? floatval($memberInfo['usdt']) : 0;
  389. $userId = isset($memberInfo['id']) ? intval($memberInfo['id']) : 0;
  390. if (empty($memberInfo)) {
  391. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]{$coinName}未绑定用户\n";
  392. RedisService::set($cacheKey . "{$txid}:error", ['error' => '未绑定用户', 'data' => $v], 7200);
  393. continue;
  394. }
  395. // 入账
  396. try {
  397. DB::beginTransaction();
  398. $updateData = ['update_time' => time()];
  399. $total = $amount;
  400. $updateData['usdt'] = DB::raw("usdt + {$total}");
  401. if (!MemberModel::where(['id' => $userId])->update($updateData)) {
  402. DB::rollBack();
  403. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]入账处理失败\n";
  404. RedisService::set($cacheKey . "{$txid}:error", ['error' => '入账处理失败', 'update' => $updateData, 'member' => $memberInfo, 'data' => $v], 7200);
  405. continue;
  406. }
  407. // 充值明细
  408. $data = [
  409. 'user_id' => $userId,
  410. 'order_no' => get_order_num('SR'),
  411. 'type' => 1,
  412. 'coin_type' => 1,
  413. 'money' => $amount,
  414. 'actual_money' => $total,
  415. 'pay_type' => 20,
  416. 'pay_status' => 20,
  417. 'pay_at' => date('Y-m-d H:i:s', $time),
  418. 'hash' => $txid,
  419. 'wallet_url' => $trcUrl,
  420. 'pt_wallet_url' => $address,
  421. 'date' => date('Y-m-d'),
  422. 'create_time' => time(),
  423. 'update_time' => time(),
  424. 'status' => 2,
  425. 'mark' => 1,
  426. ];
  427. if (!BalanceLogModel::insert($data)) {
  428. DB::rollBack();
  429. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]充值明细处理失败\n";
  430. RedisService::set($cacheKey . "{$txid}:error", ['error' => '充值明细处理失败', 'log' => $data, 'member' => $memberInfo, 'data' => $v], 7200);
  431. continue;
  432. }
  433. // 用户账户明细
  434. $log = [
  435. 'user_id' => $userId,
  436. 'order_no' => $data['order_no'],
  437. 'user_type' => 1,
  438. 'type' => 3,
  439. 'coin_type' => 1,
  440. 'hash' => $txid,
  441. 'money' => $amount,
  442. 'before_money' => $usdt,
  443. 'create_time' => time(),
  444. 'update_time' => date('Y-m-d H:i:s'),
  445. 'remark' => 'USDT充值',
  446. 'status' => 1,
  447. 'mark' => 1,
  448. ];
  449. if (!AccountLogModel::insert($log)) {
  450. DB::rollBack();
  451. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]账户明细处理失败\n";
  452. RedisService::set($cacheKey . "{$txid}:error", ['error' => '账户明细处理失败', 'log' => $log, 'member' => $memberInfo, 'data' => $v], 7200);
  453. continue;
  454. }
  455. // 平台进账
  456. FinanceService::make()->saveLog(0, $amount, 1);
  457. DB::commit();
  458. $amount = moneyFormat($amount, 2);
  459. RedisService::set($cacheKey . "{$txid}:success", ['error' => '处理成功', 'log' => $data, 'member' => $memberInfo, 'data' => $v], 7200);
  460. echo "【{$dateTime} recharge】账户[{$ownerAddress}]充值到钱包[{$address}]记录[{$txid}]金额[{$amount}]充值成功\n";
  461. RedisService::clear("caches:wallet:balanceLog:{$txid}");
  462. RedisService::keyDel("caches:balanceLog:total*");
  463. $success++;
  464. } catch (\Exception $exception) {
  465. DB::rollBack();
  466. RedisService::clear("caches:wallet:balanceLog:{$txid}");
  467. $this->error = "处理错误-" . $exception->getMessage();
  468. echo "处理错误-" . $exception->getMessage();
  469. continue;
  470. }
  471. } // 提现处理
  472. else if ($ownerAddress == $address && $toAddress && $accountType == 2) {
  473. // 处理锁跳过
  474. if (RedisService::get($cacheKey . "{$txid}:lock")) {
  475. echo "【{$dateTime} withdraw】提现记录[{$txid}]金额[{$amount}]间隔时间不处理\n";
  476. RedisService::set($cacheKey . "{$txid}:error", ['error' => '间隔时间内不处理', 'data' => $v], 600);
  477. continue;
  478. }
  479. $info = BalanceLogModel::checkExists($txid);
  480. $logId = isset($info['id']) ? $info['id'] : 0;
  481. $userId = isset($info['user_id']) ? $info['user_id'] : 0;
  482. $money = isset($info['money']) ? $info['money'] : 0;
  483. $actualMoney = isset($info['actual_money']) ? $info['actual_money'] : 0;
  484. $payStatus = isset($info['pay_status']) ? $info['pay_status'] : 0;
  485. $status = isset($info['status']) ? $info['status'] : 0;
  486. $trcUrl = isset($info['wallet_url']) ? trim($info['wallet_url']) : '';
  487. $walletUrl = isset($info['pt_wallet_url']) ? trim($info['pt_wallet_url']) : '';
  488. if (empty($info) || $logId <= 0 || $userId <= 0) {
  489. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}]提现记录不存在\n";
  490. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现记录不存在', 'data' => $v], 7200);
  491. RedisService::set($cacheKey . "{$txid}:lock", ['error' => '提现记录不存在', 'data' => $v], rand(30, 60));
  492. continue;
  493. }
  494. if ($status != 2) {
  495. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}]提现记录未审核\n";
  496. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现记录未审核', 'data' => $v], 7200);
  497. RedisService::set($cacheKey . "{$txid}:lock", ['error' => '提现记录未审核', 'data' => $v], rand(30, 60));
  498. continue;
  499. }
  500. if ($payStatus == 20) {
  501. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}]提现记录已打款\n";
  502. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现记录已打款', 'data' => $v], 7200);
  503. RedisService::set($cacheKey . "{$txid}:lock", ['error' => '提现记录已打款', 'data' => $v], rand(30, 60));
  504. continue;
  505. }
  506. if ($trcUrl != $toAddress || $walletUrl != $ownerAddress) {
  507. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}],提现订单交易地址不匹配\n";
  508. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现订单交易地址不匹配', 'info' => $info, 'data' => $v], 7200);
  509. continue;
  510. }
  511. // 金额验证
  512. if (abs($actualMoney - $amount) > 1) {
  513. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}],提现金额与交易金额不匹配\n";
  514. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现金额与交易金额不匹配', 'info' => $info, 'data' => $v], 7200);
  515. RedisService::set($cacheKey . "{$txid}:lock", ['error' => '提现金额与交易金额不匹配', 'data' => $v], rand(30, 60));
  516. continue;
  517. }
  518. // 更新状态
  519. try {
  520. DB::beginTransaction();
  521. if (!BalanceLogModel::where(['id' => $logId])->update(['pay_status' => 20, 'pay_at' => $dateTime, 'update_time' => time()])) {
  522. DB::rollBack();
  523. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}],提现状态更新失败\n";
  524. RedisService::set($cacheKey . "{$txid}:error", ['error' => '提现状态更新失败', 'info' => $info, 'data' => $v], 7200);
  525. continue;
  526. }
  527. // 平台出账
  528. FinanceService::make()->saveLog(0, $amount, 2);
  529. DB::commit();
  530. $amount = moneyFormat($amount, 2);
  531. RedisService::set($cacheKey . "{$txid}:success", ['error' => '处理成功', 'log' => $log, 'info' => $info, 'data' => $v], 7200);
  532. echo "【{$dateTime} withdraw】账户[{$toAddress}]从[{$address}]提现记录[{$txid}]金额[{$amount}],提现成功\n";
  533. RedisService::clear("caches:wallet:balanceLog:{$txid}");
  534. $success++;
  535. } catch (\Exception $exception) {
  536. DB::rollBack();
  537. RedisService::clear("caches:wallet:balanceLog:{$txid}");
  538. $this->error = "处理错误-" . $exception->getMessage();
  539. echo "处理错误-" . $exception->getMessage();
  540. continue;
  541. }
  542. }
  543. }
  544. }
  545. $this->error = 1010;
  546. return ['success' => $success, 'total' => count($datas)];
  547. } catch (\Exception $exception) {
  548. $message = $exception->getMessage();
  549. $this->error = $message;
  550. RedisService::set("caches:wallet:listen_error_{$address}", ['error' => $exception->getMessage(), 'trace' => $exception->getTrace()], 600);
  551. return false;
  552. }
  553. }
  554. }