WithdrawService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\StepStatus;
  4. use App\Models\Address;
  5. use App\Models\BalanceLog;
  6. use App\Models\Bank;
  7. use App\Models\Config;
  8. use App\Models\RoomUser;
  9. use App\Models\User;
  10. use App\Models\Wallet;
  11. use App\Models\Withdraw;
  12. use Illuminate\Support\Facades\Cache;
  13. use LaravelLang\Publisher\Console\Add;
  14. use Telegram\Bot\Api;
  15. use Telegram\Bot\Exceptions\TelegramSDKException;
  16. class WithdrawService
  17. {
  18. private $serviceCharge;
  19. public static function model(): string
  20. {
  21. return Withdraw::class;
  22. }
  23. public static function enum(): string
  24. {
  25. return '';
  26. }
  27. public static function getWhere(array $search = []): array
  28. {
  29. $where = [];
  30. if (isset($search['id']) && !empty($search['id'])) {
  31. $where[] = ['id', '=', $search['id']];
  32. }
  33. if (isset($search['member_id']) && !empty($search['member_id'])) {
  34. $where[] = ['member_id', '=', $search['member_id']];
  35. }
  36. if (isset($search['status']) && $search['status'] != '') {
  37. $where[] = ['status', '=', $search['status']];
  38. }
  39. return $where;
  40. }
  41. public static function findOne(array $search): ?Withdraw
  42. {
  43. return self::model()::where(self::getWhere($search))->first();
  44. }
  45. public static function findAll(array $search = [])
  46. {
  47. return self::model()::where(self::getWhere($search))->get();
  48. }
  49. public static function paginate(array $search = [])
  50. {
  51. $limit = isset($search['limit']) ? $search['limit'] : 10;
  52. $paginator = self::model()::where(self::getWhere($search))
  53. ->orderBy('created_at', 'desc')->paginate($limit);
  54. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  55. }
  56. public function __construct()
  57. {
  58. $serviceCharge = Config::where('field', 'service_charge')->first()->val;
  59. $serviceCharge = floatval($serviceCharge);
  60. $this->serviceCharge = $serviceCharge;
  61. }
  62. public static function notify($data)
  63. {
  64. try {
  65. $telegram = new Api(config('services.telegram.token'));
  66. $telegram->sendMessage($data);
  67. } catch (TelegramSDKException $e) {
  68. return false;
  69. }
  70. return true;
  71. }
  72. //提现账单
  73. public function bill($chatId, $firstName, $messageId = null, $page = 1, $limit = 5)
  74. {
  75. $list = Withdraw::where('member_id', $chatId)
  76. ->orderBy('id', 'desc')
  77. ->forPage($page, $limit)
  78. ->get();
  79. $count = Withdraw::where('member_id', $chatId)
  80. ->whereIn('status', [0, 1])
  81. ->count();
  82. $status = ['等待放行', '已到账', "失败"];
  83. $text = "👤 {$firstName}({$chatId}) 钱包提现记录\n\n";
  84. foreach ($list as $item) {
  85. $amount = floatval($item->amount);
  86. $balance = floatval($item->after_balance);
  87. $text .= "-------------------------------------\n";
  88. $text .= "➖ {$amount} USDT\n余额:{$balance} \n";
  89. $text .= "类别:提现\n";
  90. $text .= "状态:{$status[$item->status]}\n";
  91. if ($item->remark) {
  92. $text .= "说明:{$item->remark}\n";
  93. }
  94. $text .= "日期:{$item->created_at}\n";
  95. }
  96. if ($page > 1) {
  97. $keyboard[] = [
  98. ['text' => "👆上一页", 'callback_data' => "withdrawBillNextPage@@" . ($page - 1)]
  99. ];
  100. }
  101. $allPage = ceil($count / $limit);
  102. if ($allPage > $page) {
  103. if ($page > 1) {
  104. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "withdrawBillNextPage@@" . ($page + 1)];
  105. } else {
  106. $keyboard[] = [
  107. ['text' => "👇下一页", 'callback_data' => "withdrawBillNextPage@@" . ($page + 1)]
  108. ];
  109. }
  110. }
  111. $keyboard[] = [
  112. ['text' => "🔙返回", 'callback_data' => "withdraw@@home"]
  113. ];
  114. return [
  115. 'chat_id' => $chatId,
  116. 'text' => $text,
  117. 'message_id' => $messageId,
  118. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  119. ];
  120. }
  121. //删除地址
  122. public static function delAddress($chatId, $id, $messageId)
  123. {
  124. Address::where('id', $id)
  125. ->where('member_id', $chatId)->delete();
  126. return WithdrawService::getAddress($chatId, $messageId);
  127. }
  128. //地址详情
  129. public static function addressDetails($chatId, $messageId, $id)
  130. {
  131. $text = "*TRC20 地址管理*\n\n";
  132. $address = Address::where('id', $id)
  133. ->where('member_id', $chatId)->first();
  134. $text .= "地址:{$address->address}\n";
  135. $text .= "别名:{$address->alias}";
  136. $keyboard = [
  137. [['text' => '❌删除该地址', 'callback_data' => "withdrawAddress@@del{$id}"]],
  138. [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@address']]
  139. ];
  140. return [
  141. 'chat_id' => $chatId,
  142. 'parse_mode' => 'MarkdownV2',
  143. 'text' => $text,
  144. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  145. 'message_id' => $messageId
  146. ];
  147. }
  148. //输入别名
  149. public static function inputAlias($chatId, $alias, $messageId)
  150. {
  151. if (mb_strlen($alias) > 20) {
  152. return [
  153. 'chat_id' => $chatId,
  154. 'text' => "别名不能超过20个字,请重新输入",
  155. 'reply_to_message_id' => $messageId,
  156. ];
  157. }
  158. $address = Cache::get("{$chatId}_address");
  159. $a = new Address();
  160. $a->address = $address;
  161. $a->alias = $alias;
  162. $a->member_id = $chatId;
  163. $a->save();
  164. Cache::delete("{$chatId}_address");
  165. Cache::delete(get_step_key($chatId));
  166. return self::getAddress($chatId, $messageId);
  167. }
  168. //用户输入TRC20地址
  169. public static function inputAddress($chatId, $address, $messageId)
  170. {
  171. if (!preg_match('/^T[a-zA-Z1-9]{33}$/', $address)) {
  172. return [
  173. 'chat_id' => $chatId,
  174. 'text' => "地址输入不正确,请重新输入TRC20地址",
  175. 'reply_to_message_id' => $messageId,
  176. ];
  177. }
  178. Cache::put("{$chatId}_address", $address);
  179. Cache::put(get_step_key($chatId), StepStatus::INPUT_ADDRESS_ALIAS);
  180. return [
  181. 'chat_id' => $chatId,
  182. 'text' => "请输入地址别名,便于区分多个地址",
  183. ];
  184. }
  185. //添加地址
  186. public static function addAddress($chatId, $messageId)
  187. {
  188. $text = "请输入新的TRC20地址\n";
  189. $keyboard = [[
  190. ['text' => '❌取消', 'callback_data' => "message@@close"],
  191. ]];
  192. Cache::put(get_step_key($chatId), StepStatus::INPUT_ADDRESS_TRC20);
  193. return [
  194. 'chat_id' => $chatId,
  195. 'text' => $text,
  196. 'message_id' => $messageId,
  197. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  198. ];
  199. }
  200. //地址管理
  201. public static function getAddress($chatId, $messageId)
  202. {
  203. $text = "🏠 地址管理\n";
  204. $text .= "--------------------------\n";
  205. $list = Address::where('member_id', $chatId)
  206. ->get();
  207. $keyboard = [];
  208. foreach ($list as $item) {
  209. $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@detail{$item->id}"]];
  210. }
  211. if (count($list) < 5) {
  212. $keyboard[] = [['text' => "➕ 添加地址", 'callback_data' => "withdrawAddress@@add"]];
  213. }
  214. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "withdraw@@home"]];
  215. return [
  216. 'chat_id' => $chatId,
  217. 'text' => $text,
  218. 'message_id' => $messageId,
  219. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  220. ];
  221. }
  222. //设置提现地址
  223. public function setAddress($chatId, $address)
  224. {
  225. $address = trim($address);
  226. if (!$address) return [['chat_id' => $chatId, 'text' => "提现地址不能为空"]];
  227. $user = User::where('member_id', $chatId)->first();
  228. $user->usdt = $address;
  229. $user->save();
  230. $wallet = Wallet::where('member_id', $chatId)->first();
  231. $temp = floatval($wallet->available_balance);
  232. $text = "✅ 提现地址已设置\n\n";
  233. $text .= "请发送提现金额\n";
  234. $text .= "💰 当前可用USDT余额:{$temp}\n";
  235. $text .= "⚠️ 提现将收取{$this->serviceCharge}U作为手续费\n";
  236. $text .= "------------------------------------\n";
  237. $text .= "例如您要提现100 USDT\n";
  238. $text .= "那么请发送:【提现】100\n";
  239. return [['chat_id' => $chatId, 'text' => $text]];
  240. }
  241. public static function done($chatId, $messageId, $firstName)
  242. {
  243. $serviceCharge = (new WithdrawService())->serviceCharge;
  244. $amount = Cache::get("{$chatId}_WITHDRAW_MONEY", '');
  245. $address = Cache::get("{$chatId}_WITHDRAW_ADDRESS", '');
  246. if (!$amount || !$address) return WithdrawService::index($chatId, $firstName, $messageId);
  247. $real = bcsub($amount, $serviceCharge, 10);
  248. $real = floatval($real);
  249. if ($amount <= $serviceCharge) {
  250. return [
  251. 'chat_id' => $chatId,
  252. 'text' => "⚠️提现不能少于{$serviceCharge} USDT,请重试",
  253. 'message_id' => $messageId
  254. ];
  255. }
  256. $wallet = Wallet::where('member_id', $chatId)->first();
  257. $temp = floatval($wallet->available_balance);
  258. // 汇率
  259. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  260. $rate_usdt_amount = bcdiv($temp, $rate, 2); // 钱包可用余额 折合USDT
  261. $rate_rmb_amount = bcmul($amount, $rate, 2); // 提现金额 折合RMB
  262. if ($amount > $rate_usdt_amount) {
  263. return [
  264. 'chat_id' => $chatId,
  265. 'text' => "⚠️可用余额不足,请重试",
  266. 'message_id' => $messageId
  267. ];
  268. }
  269. $ru = RoomUser::where('member_id', $chatId)
  270. ->whereIn('status', [0, 1, 2, 3])
  271. ->first();
  272. if ($ru) {
  273. $text = "游戏未结算\n";
  274. $text .= "⚠️ 您还有进行中的游戏,所有游戏结算后方可提现\n";
  275. return [
  276. 'chat_id' => $chatId,
  277. 'text' => $text,
  278. 'message_id' => $messageId,
  279. 'reply_markup' => json_encode(['inline_keyboard' => [[
  280. ['text' => '进入房间', 'callback_data' => "games@@home{$ru->room_id}"],
  281. ]]])
  282. ];
  283. }
  284. $wallet = Wallet::where('member_id', $chatId)->first();
  285. $bl = new BalanceLog();
  286. $bl->member_id = $chatId;
  287. $bl->amount = bcmul(($amount * -1), $rate, 10); // 扣除的金额 RMB
  288. $bl->before_balance = $wallet->available_balance;
  289. $wallet->available_balance = bcsub($wallet->available_balance, $rate_rmb_amount, 10);
  290. $wallet->save();
  291. $bl->after_balance = $wallet->available_balance;
  292. $bl->change_type = '提现';
  293. $withdraw = new Withdraw();
  294. $withdraw->member_id = $chatId;
  295. $withdraw->amount = $amount;
  296. $withdraw->service_charge = $serviceCharge;
  297. $withdraw->to_account = $real;
  298. $withdraw->address = $address;
  299. $withdraw->exchange_rate = $rate;
  300. $withdraw->status = 0;
  301. $withdraw->after_balance = bcdiv($wallet->available_balance, $rate, 2);
  302. $withdraw->save();
  303. $bl->related_id = $withdraw->id;
  304. $bl->save();
  305. $temp = floatval($wallet->available_balance);
  306. $text = "✅ 提现申请已提交!\n\n";
  307. $text .= "钱包余额:{$temp} RMB\n";
  308. $text .= "汇率:1 USDT = {$rate} RMB\n";
  309. $text .= "剩余可用USDT余额:" . number_format(bcdiv($temp, $rate, 2), 2) . " USDT\n";
  310. $text .= "提现金额:{$amount} USDT\n";
  311. $text .= "实际到账:{$real} USDT\n";
  312. $text .= "手续费:{$serviceCharge} USDT\n\n";
  313. $text .= "⌛️请等待系统处理, 到账时间可能需要几分钟!\n";
  314. Cache::delete(get_step_key($chatId));
  315. return [
  316. 'chat_id' => $chatId,
  317. 'text' => $text,
  318. 'message_id' => $messageId,
  319. ];
  320. }
  321. public static function chooseAddress($chatId, $firstName, $messageId, $id)
  322. {
  323. $serviceCharge = (new WithdrawService())->serviceCharge;
  324. $amount = Cache::get("{$chatId}_WITHDRAW_MONEY", '');
  325. if (!$amount) return WithdrawService::index($chatId, $firstName, $messageId);
  326. $amount = floatval($amount);
  327. $real = bcsub($amount, $serviceCharge, 10);
  328. $real = floatval($real);
  329. $address = Address::where('id', $id)
  330. ->where('member_id', $chatId)->first();
  331. $text = "请确认\n\n";
  332. $text .= "手续费:{$serviceCharge} USDT\n";
  333. $text .= "提现金额:{$amount} USDT\n";
  334. $text .= "实际到账:{$real} USDT\n";
  335. $text .= "提现地址:{$address->address}\n";
  336. $keyboard = [
  337. [
  338. ['text' => '✅ 确认', 'callback_data' => "withdrawAddress@@done"],
  339. ],
  340. [
  341. ['text' => '❌取消', 'callback_data' => "message@@close"]
  342. ]
  343. ];
  344. Cache::put("{$chatId}_WITHDRAW_ADDRESS", $address->address);
  345. return [
  346. 'chat_id' => $chatId,
  347. 'text' => $text,
  348. 'message_id' => $messageId,
  349. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  350. ];
  351. }
  352. //用户输入提现金额
  353. public function inputAmount($chatId, $amount, $messageId)
  354. {
  355. if (!preg_match('/^-?\d+(\.\d+)?$/', $amount)) {
  356. return [[
  357. 'chat_id' => $chatId,
  358. 'text' => "金额输入不正确,请发送提现数字",
  359. 'reply_to_message_id' => $messageId
  360. ]];
  361. }
  362. $wallet = Wallet::where('member_id', $chatId)->first();
  363. $temp = floatval($wallet->available_balance);
  364. // 汇率
  365. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  366. $rate_amount = bcdiv($temp, $rate, 2);
  367. if ($amount <= $this->serviceCharge) {
  368. return [[
  369. 'chat_id' => $chatId,
  370. 'text' => "⚠️提现不能少于{$this->serviceCharge} USDT,请重试",
  371. 'reply_to_message_id' => $messageId
  372. ]];
  373. }
  374. if ($amount > $rate_amount) {
  375. return [[
  376. 'chat_id' => $chatId,
  377. 'text' => "⚠️可用余额不足,请重试",
  378. 'reply_to_message_id' => $messageId
  379. ]];
  380. }
  381. $ru = RoomUser::where('member_id', $chatId)
  382. ->whereIn('status', [0, 1, 2, 3])
  383. ->first();
  384. if ($ru) {
  385. $text = "*游戏未结算*\n";
  386. $text .= "⚠️ 您还有进行中的游戏,所有游戏结算后方可提现\n";
  387. return [[
  388. 'chat_id' => $chatId,
  389. 'text' => $text,
  390. 'parse_mode' => 'MarkdownV2',
  391. 'reply_markup' => json_encode(['inline_keyboard' => [[
  392. ['text' => '进入房间', 'callback_data' => "games@@home{$ru->room_id}"],
  393. ]]])
  394. ]];
  395. }
  396. $list = Address::where('member_id', $chatId)->get();
  397. $keyboard = [];
  398. foreach ($list as $item) {
  399. $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@choose{$item->id}"]];
  400. }
  401. $keyboard[] = [
  402. ['text' => '🏠 地址管理', 'callback_data' => "withdraw@@address"],
  403. ['text' => '❌取消', 'callback_data' => "message@@close"]
  404. ];
  405. $text = "请直接选择下面的地址\n";
  406. $text .= "⚠️提示:请务必确认提现地址正确无误,\n否则资金丢失将无法找回请自负!";
  407. Cache::put("{$chatId}_WITHDRAW_MONEY", $amount);
  408. Cache::put(get_step_key($chatId), StepStatus::CHOOSE_WITHDRAW_ADDRESS);
  409. return [[
  410. 'chat_id' => $chatId,
  411. 'text' => $text,
  412. 'reply_to_message_id' => $messageId,
  413. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  414. ]];
  415. }
  416. //申请提现
  417. public function apply($chatId, $messageId)
  418. {
  419. $wallet = Wallet::where('member_id', $chatId)->first();
  420. // 汇率
  421. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  422. $temp = floatval($wallet->available_balance);
  423. $amount = bcdiv($temp, $rate, 2);
  424. $text = "请发送提现金额\n";
  425. $text .= "💰 当前余额{$temp} RMB\n";
  426. $text .= "⚠️汇率:1 USDT = {$rate} RMB\n";
  427. $text .= "💰 当前可用USDT余额:{$amount}\n";
  428. $text .= "⚠️ 提现将收取{$this->serviceCharge}U作为手续费\n";
  429. // $keyboard = [[
  430. // ['text' => "🔙返回", 'callback_data' => "withdraw@@home"],
  431. // ]];
  432. Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_MONEY);
  433. return [
  434. 'chat_id' => $chatId,
  435. 'text' => $text,
  436. 'message_id' => $messageId,
  437. // 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  438. ];
  439. }
  440. //提现管理
  441. public static function index($chatId, $firstName, $messageId = null)
  442. {
  443. $wallet = Wallet::where('member_id', $chatId)->first();
  444. $text = "👤 {$firstName}({$chatId})\n\n";
  445. $text .= "💰钱包余额\n";
  446. $temp = floatval($wallet->available_balance);
  447. $text .= "USDT:{$temp}\n";
  448. $text .= "--------------------------\n";
  449. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  450. $keyboard = [
  451. [
  452. ['text' => '➕ 提现', 'callback_data' => "withdraw@@apply"],
  453. ['text' => '🧾 账单', 'callback_data' => "withdraw@@bill"]
  454. ],
  455. [
  456. ['text' => '🏠 地址管理', 'callback_data' => "withdraw@@address"],
  457. ['text' => '👩 客服帮助', 'url' => "https://t.me/{$serviceAccount}"]
  458. ],
  459. [
  460. ['text' => '❌取消', 'callback_data' => "message@@close"],
  461. ]
  462. ];
  463. return [
  464. 'chat_id' => $chatId,
  465. 'text' => $text,
  466. 'message_id' => $messageId,
  467. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  468. ];
  469. }
  470. }