WithdrawService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\HttpStatus;
  4. use App\Constants\StepStatus;
  5. use App\Models\ActivityUser;
  6. use App\Models\Address;
  7. use App\Models\BalanceLog;
  8. use App\Models\Bank;
  9. use App\Models\Config;
  10. use App\Models\RoomUser;
  11. use App\Models\User;
  12. use App\Models\Wallet;
  13. use App\Models\Withdraw;
  14. use App\Services\Payment\ZimuPayService;
  15. use Exception;
  16. use Illuminate\Support\Facades\Cache;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Log;
  19. use LaravelLang\Publisher\Console\Add;
  20. use Telegram\Bot\Api;
  21. use Telegram\Bot\Exceptions\TelegramSDKException;
  22. class WithdrawService
  23. {
  24. public $serviceCharge;
  25. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId): void
  26. {
  27. switch ($data) {
  28. case "withdraw@@address"://地址管理
  29. $res = WithdrawService::getAddress($chatId, $messageId);
  30. $telegram->editMessageText($res);
  31. break;
  32. case "withdrawAddress@@done":
  33. $res = WithdrawService::done($chatId, $messageId, $firstName);
  34. $telegram->editMessageText($res);
  35. break;
  36. case "withdraw@@bill"://点击提现的账单按钮
  37. $res = WithdrawService::bill($chatId, $firstName, $messageId);
  38. $telegram->editMessageText($res);
  39. break;
  40. case "withdrawAddress@@add":
  41. $res = WithdrawService::addAddress($chatId, $messageId);
  42. $telegram->editMessageText($res);
  43. break;
  44. case "withdraw@@apply"://点击提现按钮
  45. $res = WithdrawService::apply($chatId, $messageId);
  46. $telegram->editMessageText($res);
  47. break;
  48. default:
  49. //选择提现地址
  50. $pattern = "/^withdrawAddress@@choose\d+$/";
  51. if (preg_match($pattern, $data)) {
  52. $id = preg_replace('/^withdrawAddress@@choose/', '', $data);
  53. $res = WithdrawService::chooseAddress($chatId, $firstName, $messageId, $id);
  54. $telegram->editMessageText($res);
  55. }
  56. //删除地址
  57. $pattern = "/^withdrawAddress@@del\d+$/";
  58. if (preg_match($pattern, $data)) {
  59. $id = preg_replace('/^withdrawAddress@@del/', '', $data);
  60. $res = WithdrawService::delAddress($chatId, $id, $messageId);
  61. $telegram->editMessageText($res);
  62. }
  63. //地址详情
  64. $pattern = "/^withdrawAddress@@detail\d+$/";
  65. if (preg_match($pattern, $data)) {
  66. $id = preg_replace('/^withdrawAddress@@detail/', '', $data);
  67. $res = WithdrawService::addressDetails($chatId, $messageId, $id);
  68. $telegram->editMessageText($res);
  69. }
  70. //提现账单,下一页
  71. $pattern = "/^withdrawBillNextPage@@\d+$/";
  72. if (preg_match($pattern, $data)) {
  73. $page = preg_replace('/^withdrawBillNextPage@@/', '', $data);
  74. $page = intval($page);
  75. $res = WithdrawService::bill($chatId, $firstName, $messageId, $page);
  76. $telegram->editMessageText($res);
  77. }
  78. break;
  79. }
  80. }
  81. public static function onMessage($chatId, $text, $messageId, $stepStatus)
  82. {
  83. switch ($stepStatus) {
  84. case StepStatus::INPUT_WITHDRAW_MONEY:
  85. $res = WithdrawService::inputAmount($chatId, $text, $messageId);
  86. $res = $res[0];
  87. break;
  88. case StepStatus::INPUT_ADDRESS_TRC20:
  89. $res = WithdrawService::inputAddress($chatId, $text, $messageId);
  90. break;
  91. case StepStatus::INPUT_ADDRESS_ALIAS:
  92. $res = WithdrawService::inputAlias($chatId, $text, $messageId);
  93. break;
  94. default:
  95. $res = null;
  96. break;
  97. }
  98. return $res;
  99. }
  100. public static function model(): string
  101. {
  102. return Withdraw::class;
  103. }
  104. public static function enum(): string
  105. {
  106. return '';
  107. }
  108. public static function getWhere(array $search = []): array
  109. {
  110. $where = [];
  111. if (isset($search['id']) && !empty($search['id'])) {
  112. $where[] = ['withdraws.id', '=', $search['id']];
  113. }
  114. if (isset($search['member_id']) && !empty($search['member_id'])) {
  115. $where[] = ['withdraws.member_id', '=', $search['member_id']];
  116. }
  117. if (isset($search['status']) && $search['status'] != '') {
  118. $where[] = ['withdraws.status', '=', $search['status']];
  119. }
  120. if (isset($search['first_name']) && !empty($search['first_name'])) {
  121. $where[] = ['users.first_name', 'like', "%" . $search['first_name'] . "%"];
  122. }
  123. return $where;
  124. }
  125. public static function findOne(array $search): ?Withdraw
  126. {
  127. return self::model()::where(self::getWhere($search))->first();
  128. }
  129. public static function findAll(array $search = [])
  130. {
  131. return self::model()::where(self::getWhere($search))->get();
  132. }
  133. public static function paginate(array $search = [])
  134. {
  135. $limit = isset($search['limit']) ? $search['limit'] : 10;
  136. $paginator = self::model()::where(self::getWhere($search))
  137. ->orderBy('created_at', 'desc')->paginate($limit);
  138. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  139. }
  140. public function __construct()
  141. {
  142. $serviceCharge = Config::where('field', 'service_charge')->first()->val;
  143. $serviceCharge = floatval($serviceCharge);
  144. $this->serviceCharge = $serviceCharge;
  145. }
  146. public static function getServiceCharge(): float
  147. {
  148. $serviceCharge = Config::where('field', 'service_charge')->first()->val;
  149. return floatval($serviceCharge);
  150. }
  151. public static function notify($data)
  152. {
  153. try {
  154. $telegram = new Api(config('services.telegram.token'));
  155. $telegram->sendMessage($data);
  156. } catch (TelegramSDKException $e) {
  157. return false;
  158. }
  159. return true;
  160. }
  161. //提现账单
  162. public static function bill($chatId, $firstName, $messageId = null, $page = 1, $limit = 5)
  163. {
  164. $list = Withdraw::where('member_id', $chatId)
  165. ->orderBy('id', 'desc')
  166. ->forPage($page, $limit)
  167. ->get();
  168. $count = Withdraw::where('member_id', $chatId)
  169. ->whereIn('status', [0, 1])
  170. ->count();
  171. $status = [lang('等待放行'), lang('已到账'), lang("失败")];
  172. $text = "👤 {$firstName}({$chatId}) " . lang("钱包提现记录") . "\n\n";
  173. foreach ($list as $item) {
  174. $amount = floatval($item->amount);
  175. $balance = floatval($item->after_balance);
  176. $text .= "-------------------------------------\n";
  177. $text .= "➖ {$amount} USDT\n" . lang("余额") . ":{$balance} \n";
  178. $text .= lang("类别") . "类别:" . lang("提现") . "\n";
  179. $text .= lang("状态") . ":{$status[$item->status]}\n";
  180. if ($item->remark) {
  181. $text .= lang("说明") . ":{$item->remark}\n";
  182. }
  183. $text .= lang("日期") . ":{$item->created_at}\n";
  184. }
  185. if ($page > 1) {
  186. $keyboard[] = [
  187. ['text' => lang("👆上一页"), 'callback_data' => "withdrawBillNextPage@@" . ($page - 1)]
  188. ];
  189. }
  190. $allPage = ceil($count / $limit);
  191. if ($allPage > $page) {
  192. if ($page > 1) {
  193. $keyboard[count($keyboard) - 1][] = ['text' => lang("👇下一页"), 'callback_data' => "withdrawBillNextPage@@" . ($page + 1)];
  194. } else {
  195. $keyboard[] = [
  196. ['text' => lang("👇下一页"), 'callback_data' => "withdrawBillNextPage@@" . ($page + 1)]
  197. ];
  198. }
  199. }
  200. $keyboard[] = [
  201. ['text' => lang("↩️返回"), 'callback_data' => "withdraw@@home"]
  202. ];
  203. return [
  204. 'chat_id' => $chatId,
  205. 'text' => $text,
  206. 'message_id' => $messageId,
  207. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  208. ];
  209. }
  210. //删除地址
  211. public static function delAddress($chatId, $id, $messageId)
  212. {
  213. Address::where('id', $id)
  214. ->where('member_id', $chatId)->delete();
  215. return WithdrawService::getAddress($chatId, $messageId);
  216. }
  217. //地址详情
  218. public static function addressDetails($chatId, $messageId, $id)
  219. {
  220. $text = lang("*TRC20 地址管理*") . "\n\n";
  221. $address = Address::where('id', $id)
  222. ->where('member_id', $chatId)->first();
  223. $text .= lang("地址") . ":{$address->address}\n";
  224. $text .= lang("别名") . ":{$address->alias}";
  225. $keyboard = [
  226. [['text' => lang('❌删除该地址'), 'callback_data' => "withdrawAddress@@del{$id}"]],
  227. [['text' => lang('↩️返回列表'), 'callback_data' => 'withdraw@@address']]
  228. ];
  229. return [
  230. 'chat_id' => $chatId,
  231. 'parse_mode' => 'MarkdownV2',
  232. 'text' => $text,
  233. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  234. 'message_id' => $messageId
  235. ];
  236. }
  237. //输入别名
  238. public static function inputAlias($chatId, $alias, $messageId)
  239. {
  240. if (mb_strlen($alias) > 20) {
  241. return [
  242. 'chat_id' => $chatId,
  243. 'text' => lang("别名不能超过20个字,请重新输入"),
  244. 'reply_to_message_id' => $messageId,
  245. ];
  246. }
  247. $address = Cache::get("{$chatId}_address");
  248. $a = new Address();
  249. $a->address = $address;
  250. $a->alias = $alias;
  251. $a->member_id = $chatId;
  252. $a->save();
  253. Cache::delete("{$chatId}_address");
  254. Cache::delete(get_step_key($chatId));
  255. return self::getAddress($chatId, $messageId);
  256. }
  257. //用户输入TRC20地址
  258. public static function inputAddress($chatId, $address, $messageId)
  259. {
  260. if (!preg_match('/^T[a-zA-Z1-9]{33}$/', $address)) {
  261. return [
  262. 'chat_id' => $chatId,
  263. 'text' => lang("地址输入不正确,请重新输入TRC20地址"),
  264. 'reply_to_message_id' => $messageId,
  265. ];
  266. }
  267. Cache::put("{$chatId}_address", $address);
  268. Cache::put(get_step_key($chatId), StepStatus::INPUT_ADDRESS_ALIAS);
  269. return [
  270. 'chat_id' => $chatId,
  271. 'text' => lang("请输入地址别名,便于区分多个地址"),
  272. ];
  273. }
  274. //添加地址
  275. public static function addAddress($chatId, $messageId)
  276. {
  277. $text = lang('请输入新的TRC20地址') . "\n";
  278. $keyboard = [[
  279. ['text' => lang('❌取消'), 'callback_data' => "message@@close"],
  280. ]];
  281. Cache::put(get_step_key($chatId), StepStatus::INPUT_ADDRESS_TRC20);
  282. return [
  283. 'chat_id' => $chatId,
  284. 'text' => $text,
  285. 'message_id' => $messageId,
  286. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  287. ];
  288. }
  289. //地址管理
  290. public static function getAddress($chatId, $messageId)
  291. {
  292. $text = lang("🏠 地址管理");
  293. $text .= "\n--------------------------\n";
  294. $list = Address::where('member_id', $chatId)
  295. ->get();
  296. $keyboard = [];
  297. foreach ($list as $item) {
  298. $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@detail{$item->id}"]];
  299. }
  300. if (count($list) < 5) {
  301. $keyboard[] = [['text' => lang("➕ 添加地址"), 'callback_data' => "withdrawAddress@@add"]];
  302. }
  303. $keyboard[] = [['text' => lang("↩️返回"), 'callback_data' => "withdraw@@home"]];
  304. return [
  305. 'chat_id' => $chatId,
  306. 'text' => $text,
  307. 'message_id' => $messageId,
  308. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  309. ];
  310. }
  311. public static function done($chatId, $messageId, $firstName)
  312. {
  313. $serviceAccount = Config::where('field', 'service_customer')->first()->val;
  314. $activityUser = ActivityUserService::findOne(['member_id' => $chatId, 'status' => ActivityUser::STATUS_IN_PROGRESS]);
  315. if ($activityUser) {
  316. $text = lang("您有未完成的活动") . "\n";
  317. $text .= lang("任何疑问都可以联系唯一客服") . ":@{$serviceAccount}";
  318. return [
  319. 'chat_id' => $chatId,
  320. 'text' => $text,
  321. 'message_id' => $messageId,
  322. ];
  323. }
  324. $serviceCharge = (new WithdrawService())->serviceCharge;
  325. $amount = Cache::get("{$chatId}_WITHDRAW_MONEY", '');
  326. $address = Cache::get("{$chatId}_WITHDRAW_ADDRESS", '');
  327. if (!$amount || !$address) return WithdrawService::index($chatId, $firstName, $messageId);
  328. $real = bcsub($amount, $serviceCharge, 10);
  329. $real = floatval($real);
  330. if ($amount <= $serviceCharge) {
  331. return [
  332. 'chat_id' => $chatId,
  333. 'text' => lang("⚠️提现不能少于") . "{$serviceCharge} USDT," . lang("请重试"),
  334. 'message_id' => $messageId
  335. ];
  336. }
  337. $wallet = Wallet::where('member_id', $chatId)->first();
  338. $temp = floatval($wallet->available_balance);
  339. // 汇率
  340. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  341. $exchange_rate_difference = Config::where('field', 'exchange_rate_difference')->first()->val ?? 0;
  342. $rate = bcadd($rate, $exchange_rate_difference, 2);
  343. $rate_usdt_amount = bcdiv($temp, $rate, 2); // 钱包可用余额 折合USDT
  344. $rate_rmb_amount = bcmul($amount, $rate, 2); // 提现金额 折合RMB
  345. if ($amount > $rate_usdt_amount) {
  346. return [
  347. 'chat_id' => $chatId,
  348. 'text' => lang("⚠️可用余额不足,请重试"),
  349. 'message_id' => $messageId
  350. ];
  351. }
  352. $ru = RoomUser::where('member_id', $chatId)
  353. ->whereIn('status', [0, 1, 2, 3])
  354. ->first();
  355. if ($ru) {
  356. $text = lang("游戏未结算") . "\n";
  357. $text .= "⚠️ " . lang("您还有进行中的游戏,所有游戏结算后方可提现") . "\n";
  358. return [
  359. 'chat_id' => $chatId,
  360. 'text' => $text,
  361. 'message_id' => $messageId,
  362. 'reply_markup' => json_encode(['inline_keyboard' => [[
  363. ['text' => lang('进入房间'), 'callback_data' => "games@@home{$ru->room_id}"],
  364. ]]])
  365. ];
  366. }
  367. $wallet = Wallet::where('member_id', $chatId)->first();
  368. $changeAmount = bcmul(($amount * -1), $rate, 2);
  369. $beforeBalance = $wallet->available_balance;
  370. $afterBalance = bcsub($wallet->available_balance, $rate_rmb_amount, 2);
  371. $wallet->available_balance = $afterBalance;
  372. $wallet->save();
  373. $withdraw = Withdraw::create([
  374. 'member_id' => $chatId,
  375. 'amount' => $amount,
  376. 'service_charge' => $serviceCharge,
  377. 'to_account' => $real,
  378. 'address' => $address,
  379. 'exchange_rate' => $rate,
  380. 'status' => 0,
  381. 'after_balance' => $afterBalance
  382. ]);
  383. BalanceLogService::addLog($chatId, $changeAmount, $beforeBalance, $afterBalance, '提现', $withdraw->id, '');
  384. $temp = floatval($afterBalance);
  385. $text = "✅ " . lang("提现申请已提交!") . "\n\n";
  386. $text .= lang("钱包余额") . ":{$temp} RMB\n";
  387. $text .= lang("汇率") . ":1 USDT = {$rate} RMB\n";
  388. $text .= lang("剩余可用USDT余额") . ":" . number_format(bcdiv($temp, $rate, 2), 2) . " USDT\n";
  389. $text .= lang("提现金额") . ":{$amount} USDT\n";
  390. $text .= lang("实际到账") . ":{$real} USDT\n";
  391. $text .= lang("手续费") . ":{$serviceCharge} USDT\n\n";
  392. $text .= lang("⌛️请等待系统处理, 到账时间可能需要几分钟!") . "\n";
  393. Cache::delete(get_step_key($chatId));
  394. return [
  395. 'chat_id' => $chatId,
  396. 'text' => $text,
  397. 'message_id' => $messageId,
  398. ];
  399. }
  400. public static function chooseAddress($chatId, $firstName, $messageId, $id)
  401. {
  402. $serviceCharge = (new WithdrawService())->serviceCharge;
  403. $amount = Cache::get("{$chatId}_WITHDRAW_MONEY", '');
  404. if (!$amount) return WithdrawService::index($chatId, $firstName, $messageId);
  405. $amount = floatval($amount);
  406. $real = bcsub($amount, $serviceCharge, 10);
  407. $real = floatval($real);
  408. $address = Address::where('id', $id)
  409. ->where('member_id', $chatId)->first();
  410. $text = lang("请确认") . "\n\n";
  411. $text .= lang('手续费') . ":{$serviceCharge} USDT\n";
  412. $text .= lang("提现金额") . ":{$amount} USDT\n";
  413. $text .= lang("实际到账") . ":{$real} USDT\n";
  414. $text .= lang("提现地址") . ":{$address->address}\n";
  415. $keyboard = [
  416. [
  417. ['text' => lang('✅ 确认'), 'callback_data' => "withdrawAddress@@done"],
  418. ],
  419. [
  420. ['text' => lang('❌取消'), 'callback_data' => "message@@close"]
  421. ]
  422. ];
  423. Cache::put("{$chatId}_WITHDRAW_ADDRESS", $address->address);
  424. return [
  425. 'chat_id' => $chatId,
  426. 'text' => $text,
  427. 'message_id' => $messageId,
  428. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  429. ];
  430. }
  431. //用户输入提现金额(USDT)
  432. public static function inputAmount($chatId, $amount, $messageId)
  433. {
  434. if (!preg_match('/^-?\d+(\.\d+)?$/', $amount)) {
  435. return [[
  436. 'chat_id' => $chatId,
  437. 'text' => lang("金额输入不正确,请发送提现数字"),
  438. 'reply_to_message_id' => $messageId
  439. ]];
  440. }
  441. if ($amount < 20) {
  442. return [[
  443. 'chat_id' => $chatId,
  444. 'text' => lang("提现不能少于20 USDT,请重试"),
  445. 'reply_to_message_id' => $messageId
  446. ]];
  447. }
  448. $wallet = Wallet::where('member_id', $chatId)->first();
  449. $temp = floatval($wallet->available_balance);
  450. // 汇率
  451. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  452. $rate_amount = bcdiv($temp, $rate, 2);
  453. $serviceCharge = Config::where('field', 'service_charge')->first()->val;
  454. $serviceCharge = floatval($serviceCharge);
  455. if ($amount <= $serviceCharge) {
  456. return [[
  457. 'chat_id' => $chatId,
  458. 'text' => lang("⚠️提现不能少于") . "{$serviceCharge} USDT," . lang('请重试'),
  459. 'reply_to_message_id' => $messageId
  460. ]];
  461. }
  462. if ($amount > $rate_amount) {
  463. return [[
  464. 'chat_id' => $chatId,
  465. 'text' => lang("可用余额不足,请重试"),
  466. 'reply_to_message_id' => $messageId
  467. ]];
  468. }
  469. $ru = RoomUser::where('member_id', $chatId)
  470. ->whereIn('status', [0, 1, 2, 3])
  471. ->first();
  472. if ($ru) {
  473. $text = lang('*游戏未结算*') . "\n";
  474. $text .= lang("⚠️ 您还有进行中的游戏,所有游戏结算后方可提现") . "\n";
  475. return [[
  476. 'chat_id' => $chatId,
  477. 'text' => $text,
  478. 'parse_mode' => 'MarkdownV2',
  479. 'reply_markup' => json_encode(['inline_keyboard' => [[
  480. ['text' => lang('进入房间'), 'callback_data' => "games@@home{$ru->room_id}"],
  481. ]]])
  482. ]];
  483. }
  484. $list = Address::where('member_id', $chatId)->get();
  485. $keyboard = [];
  486. foreach ($list as $item) {
  487. $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@choose{$item->id}"]];
  488. }
  489. $keyboard[] = [
  490. ['text' => lang("🏠 地址管理"), 'callback_data' => "withdraw@@address"],
  491. ['text' => lang('❌取消'), 'callback_data' => "message@@close"]
  492. ];
  493. $text = lang('请直接选择下面的地址') . "\n";
  494. $text .= lang("⚠️提示:请务必确认提现地址正确无误,\n否则资金丢失将无法找回请自负!");
  495. Cache::put("{$chatId}_WITHDRAW_MONEY", $amount);
  496. Cache::put(get_step_key($chatId), StepStatus::CHOOSE_WITHDRAW_ADDRESS);
  497. return [[
  498. 'chat_id' => $chatId,
  499. 'text' => $text,
  500. 'reply_to_message_id' => $messageId,
  501. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  502. ]];
  503. }
  504. //申请提现
  505. public static function apply($chatId, $messageId)
  506. {
  507. $wallet = Wallet::where('member_id', $chatId)->first();
  508. // 汇率
  509. $rate = Config::where('field', 'exchange_rate_rmb')->first()->val ?? 1;
  510. $exchange_rate_difference = Config::where('field', 'exchange_rate_difference')->first()->val ?? 0;
  511. $rate = bcadd($rate, $exchange_rate_difference, 2);
  512. $temp = floatval($wallet->available_balance);
  513. $amount = bcdiv($temp, $rate, 2);
  514. $serviceCharge = static::getServiceCharge();
  515. $text = lang("请发送提现金额");
  516. $text .= " (USDT)";
  517. $text .= "\n💰 " . lang('当前余额') . ":{$temp} RMB ({$amount} USDT)\n";
  518. $text .= "⚠️" . lang('汇率') . ":1 USDT = {$rate} RMB\n";
  519. $text .= "⚠️ " . lang('提现将收取') . "{$serviceCharge}" . lang('U作为手续费') . "\n";
  520. // $keyboard = [[
  521. // ['text' => "🔙返回", 'callback_data' => "withdraw@@home"],
  522. // ]];
  523. Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_MONEY);
  524. return [
  525. 'chat_id' => $chatId,
  526. 'text' => $text,
  527. 'message_id' => $messageId,
  528. // 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  529. ];
  530. }
  531. //提现管理
  532. public static function index($chatId, $firstName, $messageId = null)
  533. {
  534. $wallet = Wallet::where('member_id', $chatId)->first();
  535. $text = "👤 {$firstName}({$chatId})\n\n";
  536. $text .= "💰钱包余额\n";
  537. $temp = floatval($wallet->available_balance);
  538. $text .= "USDT:{$temp}\n";
  539. $text .= "--------------------------\n";
  540. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  541. $keyboard = [
  542. [
  543. ['text' => '➕ 提现', 'callback_data' => "withdraw@@apply"],
  544. ['text' => '🧾 账单', 'callback_data' => "withdraw@@bill"]
  545. ],
  546. [
  547. ['text' => lang("🏠 地址管理"), 'callback_data' => "withdraw@@address"],
  548. ['text' => lang('👩 客服帮助'), 'url' => "https://t.me/{$serviceAccount}"]
  549. ],
  550. [
  551. ['text' => lang('❌取消'), 'callback_data' => "message@@close"],
  552. ]
  553. ];
  554. return [
  555. 'chat_id' => $chatId,
  556. 'text' => $text,
  557. 'message_id' => $messageId,
  558. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  559. ];
  560. }
  561. public static function setStatus($id, $status, $remark)
  562. {
  563. $w = WithdrawService::findOne(['id' => $id, 'status' => 0]);
  564. if (!$w) return 0;
  565. // 汇率
  566. $rate = $w->exchange_rate ?? 1;
  567. if ($status == 1 && ($w->channel ?? '') === ZimuPayService::CHANNEL_WITHDRAW) {
  568. $orderNo = $w->order_no ?: PaymentOrderService::createOrderNo('zw_', $w->member_id);
  569. $ret = ZimuPayService::withdraw($w->to_account, $orderNo, (string)$w->member_id, (string)$w->address);
  570. Log::channel('payment')->info('ZIMU钱包地址提现接口调用', [
  571. 'withdraw_id' => $w->id,
  572. 'order_no' => $orderNo,
  573. 'member_id' => $w->member_id,
  574. 'amount' => $w->to_account,
  575. 'address' => $w->address,
  576. 'response' => $ret,
  577. ]);
  578. if (($ret['code'] ?? -1) != 0) {
  579. throw new Exception($ret['msg'] ?? 'ZIMU钱包地址提现失败', HttpStatus::CUSTOM_ERROR);
  580. }
  581. $w->order_no = $orderNo;
  582. $w->pay_no = $ret['data']['orderNo'] ?? '';
  583. $w->pay_data = json_encode($ret, JSON_UNESCAPED_UNICODE);
  584. $w->status = 1;
  585. $w->save();
  586. } else if ($status == 1) {
  587. $w->status = 1;
  588. $w->save();
  589. } else if ($status == 2) {
  590. $rate_rmb_amount = bcmul($w->amount, $rate, 2); // 提现金额 折合RMB
  591. $w->status = 2;
  592. $w->remark = $remark;
  593. $w->save();
  594. $wallet = WalletService::findOne(['member_id' => $w->member_id]);
  595. $afterBalance = bcadd($wallet->available_balance, $rate_rmb_amount, 10);
  596. BalanceLogService::addLog($w->member_id, $w->rate_rmb_amount, $wallet->available_balance, $afterBalance, '提现', $w->id, '');
  597. $wallet->available_balance = $afterBalance;
  598. $wallet->save();
  599. }
  600. $arr = ['⏳️申请中', '✅️成功', '❌️失败'];
  601. $text = "📢 提现结果通知\n\n";
  602. $temp = floatval($w->service_charge);
  603. $text .= "手续费:{$temp} USDT\n";
  604. $temp = floatval($w->amount);
  605. $text .= "提现金额:{$temp} USDT\n";
  606. $temp = floatval($w->to_account);
  607. $text .= "到账金额:{$temp} USDT\n";
  608. $text .= "提现地址:{$w->address}\n\n";
  609. $text .= "状态:{$arr[$w->status]}\n";
  610. if ($w->remark) $text .= "说明:{$w->remark}";
  611. WithdrawService::notify([
  612. 'chat_id' => $w->member_id,
  613. 'text' => $text,
  614. ]);
  615. return 1;
  616. }
  617. public static function receiveZimuWithdraw(array $params): bool
  618. {
  619. $withdraw = Withdraw::where('order_no', $params['mchOrderNo'] ?? '')->first();
  620. if (!$withdraw || ($withdraw->channel ?? '') !== ZimuPayService::CHANNEL_WITHDRAW) {
  621. return false;
  622. }
  623. if (!ZimuPayService::verifyNotify($params)) {
  624. Log::error('ZIMU钱包地址提现回调验签失败', [
  625. 'mch_order_no' => $params['mchOrderNo'] ?? '',
  626. 'signature' => ZimuPayService::notifySignatureDiagnostics($params),
  627. ]);
  628. return false;
  629. }
  630. if (bccomp(ZimuPayService::amount($withdraw->to_account), ZimuPayService::amount($params['amount'] ?? 0), 2) !== 0) {
  631. Log::error('ZIMU钱包地址提现回调金额不一致', [
  632. 'mch_order_no' => $params['mchOrderNo'] ?? '',
  633. 'order_amount' => $withdraw->to_account,
  634. 'callback_amount' => $params['amount'] ?? null,
  635. ]);
  636. return false;
  637. }
  638. DB::transaction(function () use ($params, $withdraw) {
  639. $order = Withdraw::where('id', $withdraw->id)->lockForUpdate()->first();
  640. if (!$order || $order->status == 2) {
  641. return;
  642. }
  643. $order->callback_data = json_encode($params, JSON_UNESCAPED_UNICODE);
  644. if ((string)$params['status'] === ZimuPayService::WITHDRAW_STATUS_SUCCESS) {
  645. $order->status = 1;
  646. $order->save();
  647. } elseif ((string)$params['status'] === ZimuPayService::WITHDRAW_STATUS_FAIL) {
  648. if ($order->status != 2) {
  649. $order->status = 2;
  650. $order->remark = $params['msg'] ?? 'ZIMU钱包地址提现失败';
  651. $order->save();
  652. $wallet = Wallet::where('member_id', $order->member_id)->lockForUpdate()->first();
  653. if ($wallet) {
  654. $refundAmount = bcmul($order->amount, $order->exchange_rate ?: 1, 2);
  655. $beforeBalance = $wallet->available_balance;
  656. $afterBalance = bcadd($beforeBalance, $refundAmount, 10);
  657. $wallet->available_balance = $afterBalance;
  658. $wallet->save();
  659. BalanceLogService::addLog($order->member_id, $refundAmount, $beforeBalance, $afterBalance, '提现', $order->id, 'ZIMU钱包地址提现失败退款');
  660. }
  661. }
  662. } else {
  663. $order->save();
  664. }
  665. }, 3);
  666. return true;
  667. }
  668. public static function batchReject()
  669. {
  670. $list = Withdraw::where('status', 0)->get();
  671. foreach ($list as $k => $v) {
  672. WithdrawService::setStatus($v->id, 2, '赠送彩金,提现需满足活动需求,提现只允许走rmb渠道');
  673. }
  674. }
  675. }