| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643 |
- <?php
- namespace App\Services;
- use App\Constants\StepStatus;
- use App\Models\Bank;
- use App\Models\Config;
- use App\Models\PaymentOrder;
- use App\Models\Wallet;
- use Illuminate\Support\Facades\Cache;
- use Telegram\Bot\Api;
- class QianBaoWithdrawService
- {
- /**
- * @param Api $telegram
- * @param $data
- * @param $chatId
- * @param $firstName
- * @param $messageId
- * @throws \Telegram\Bot\Exceptions\TelegramSDKException
- */
- public static function init(Api $telegram, $data, $chatId, $firstName, $messageId)
- {
- //点击钱宝提现按钮
- if ($data === "withdraw@@qb_show_channel") {
- $res = QianBaoWithdrawService::chooseType($chatId, $messageId);
- $telegram->editMessageText($res);
- }
- $pattern = "/^withdraw@@qb_choose_.*$/";
- if (preg_match($pattern, $data)) {
- $type = preg_replace('/^withdraw@@qb_choose_/', '', $data);
- $res = QianBaoWithdrawService::showBanks($chatId, $messageId, $type);
- $telegram->editMessageText($res);
- }
- //选择银行卡号
- $pattern = "/^withdrawAddress@@choose_qb_\d+$/";
- if (preg_match($pattern, $data)) {
- $id = preg_replace('/^withdrawAddress@@choose_qb_/', '', $data);
- $res = QianBaoWithdrawService::chooseBank($chatId, $id);
- $telegram->deleteMessage([
- 'chat_id' => $chatId,
- 'message_id' => $messageId,
- ]);
- $telegram->sendMessage($res);
- }
- //确认提现信息
- if ($data === "withdraw@@qb_confirm") {
- $res = QianBaoWithdrawService::confirm($chatId, $messageId);
- $telegram->editMessageText($res);
- }
- if ($data === "withdraw@@qb_apply") {
- $res = QianBaoWithdrawService::qbApply($chatId, $messageId);
- $telegram->editMessageText($res);
- }
- //钱宝账单
- $pattern = "/^withdraw@@bank_bill_\d+$/";
- if (preg_match($pattern, $data)) {
- $page = preg_replace('/^withdraw@@bank_bill_/', '', $data);
- if (empty($page) || $page < 1) $page = 1;
- $page = intval($page);
- $res = QianBaoWithdrawService::bill($chatId, $firstName, $messageId, $page);
- $telegram->editMessageText($res);
- }
- //银行卡管理
- $pattern = "/^withdraw@@management_.*$/";
- if (preg_match($pattern, $data)) {
- $channel = preg_replace('/^withdraw@@management_/', '', $data);
- $res = QianBaoWithdrawService::banks($chatId, $messageId, $channel);
- $telegram->editMessageText($res);
- }
- //添加银行卡
- // $pattern = "/^withdraw@@bank_add.*$/";
- // if (preg_match($pattern, $data)) {
- // $channel = preg_replace('/^withdraw@@bank_add/', '', $data);
- // $res = QianBaoWithdrawService::chooseChannel($chatId, $messageId,$channel);
- // $telegram->editMessageText($res);
- // }
- //银行卡详情
- $pattern = "/^withdrawAddress@@bank_detail\d+$/";
- if (preg_match($pattern, $data)) {
- $id = preg_replace('/^withdrawAddress@@bank_detail/', '', $data);
- $res = static::bankDetails($chatId, $messageId, $id);
- $telegram->editMessageText($res);
- }
- $pattern = "/^withdraw@@bank_del_\d+$/";
- if (preg_match($pattern, $data)) {
- $id = preg_replace('/^withdraw@@bank_del_/', '', $data);
- $res = static::bankDelete($chatId, $messageId, $id);
- $telegram->editMessageText($res);
- }
- $pattern = "/^withdrawAddress@@bank_choose_channel_.*$/";
- if (preg_match($pattern, $data)) {
- $channel = preg_replace('/^withdrawAddress@@bank_choose_channel_/', '', $data);
- $res = static::chooseChannel($chatId, $messageId, $channel);
- $telegram->editMessageText($res);
- }
- }
- public static function onMessage($chatId, $text, $messageId, $stepStatus)
- {
- switch ($stepStatus) {
- case StepStatus::QB_INPUT_ALIAS:
- $res = QianBaoWithdrawService::inputAliAs($chatId, $text, $messageId);
- return $res;
- break;
- case StepStatus::INPUT_WITHDRAW_QB_MONEY://输入提现金额
- $res = QianBaoWithdrawService::inputQbAmount($chatId, $text, $messageId);
- return $res;
- break;
- case StepStatus::QB_INPUT_BANK_NAME://输入银行名称
- $res = QianBaoWithdrawService::inputBankName($chatId, $text, $messageId);
- return $res;
- case StepStatus::QB_INPUT_CARD_NO://输入银行卡号/支付宝账号
- $res = QianBaoWithdrawService::inputCardNo($chatId, $text, $messageId);
- return $res;
- break;
- case StepStatus::QB_INPUT_ACCOUNT://输入姓名
- $res = QianBaoWithdrawService::inputAccount($chatId, $text, $messageId);
- return $res;
- break;
- }
- return null;
- }
- private static function chooseType($chatId, $messageId)
- {
- $keyboard = [
- [
- ['text' => "USDT", 'callback_data' => "withdraw@@apply"],
- ],
- [
- ['text' => "银行卡", 'callback_data' => "withdraw@@qb_choose_bank"],
- ],
- [
- ['text' => "支付宝", 'callback_data' => "withdraw@@qb_choose_aliPay"],
- ],
- [
- ['text' => "数字人民币", 'callback_data' => "withdraw@@qb_choose_digital_RMB"],
- ],
- [
- ['text' => "返回", 'callback_data' => "topUp@@home"],
- ],
- ];
- // $keyboard[] = [
- // ['text' => "提现账户管理", 'callback_data' => "withdraw@@banks"],
- // ];
- return [
- 'chat_id' => $chatId,
- 'text' => "请选择提现方式",
- 'message_id' => $messageId,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
- ];
- }
- private static function showBanks($chatId, $messageId, $type)
- {
- $channel = '';
- $card = "";
- switch ($type) {
- case "bank":
- $card = "银行卡";
- $text = "请选择提现的银行卡";
- $channel = 'DF001';
- break;
- case "aliPay":
- $card = "支付宝";
- $text = "请选择提现的支付宝";
- $channel = "DF002";
- break;
- case "digital_RMB":
- $card = "数字人民币";
- $text = "请选择提现的账户";
- $channel = "DF005";
- break;
- }
- $list = Bank::where('channel', $channel)->get();
- $keyboard = [];
- foreach ($list as $item) {
- $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@choose_qb_{$item->id}"]];
- }
- $keyboard[] = [
- ['text' => "{$card}管理", 'callback_data' => "withdraw@@management_{$channel}"],
- ['text' => "取消", 'callback_data' => "topUp@@home"]];
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
- ];
- }
- //钱宝账单
- private static function bill($chatId, $firstName, $messageId, $page = 1, $limit = 5)
- {
- $list = PaymentOrder::where('member_id', $chatId)
- ->where('type', 2)
- ->orderByDesc('created_at')
- ->forPage($page, $limit)
- ->get();
- $count = PaymentOrder::where('member_id', $chatId)
- ->where('type', 2)
- ->count();
- $text = "👤 {$firstName}({$chatId}) 钱宝提现记录\n\n";
- foreach ($list as $item) {
- $amount = floatval($item->amount);
- $amount = $item->type == 2 ? "➖ {$amount}" : "➕ $amount";
- $text .= "-------------------------------------\n";
- $text .= "{$amount} \n";
- $text .= "订单号:{$item->order_no}\n";
- $text .= "银行:{$item->bank_name}\n";
- $text .= "姓名:{$item->account}\n";
- $text .= "卡号:{$item->card_no}\n";
- $status = ['待处理', '处理中', '成功', '失败'];
- $text .= "状态:{$status[$item->status]}\n";
- if ($item->remark) {
- $text .= "说明:{$item->remark}\n";
- }
- $text .= "日期:{$item->created_at}\n";
- }
- if ($page > 1) {
- $keyboard[] = [
- ['text' => "👆上一页", 'callback_data' => "withdraw@@bank_bill_" . ($page - 1)]
- ];
- }
- $allPage = ceil($count / $limit);
- if ($allPage > $page) {
- if ($page > 1) {
- $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "withdraw@@bank_bill_" . ($page + 1)];
- } else {
- $keyboard[] = [
- ['text' => "👇下一页", 'callback_data' => "withdraw@@bank_bill_" . ($page + 1)]
- ];
- }
- }
- $keyboard[] = [
- ['text' => "返回", 'callback_data' => "topUp@@home"]
- ];
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
- ];
- }
- //1.钱宝提现
- private static function qbApply($chatId, $messageId)
- {
- $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
- if ($three_payment_switch != 1) {
- $res = WalletService::getBalance($chatId);
- $res['message_id'] = $messageId;
- return $res;
- }
- $wallet = Wallet::where('member_id', $chatId)->first();
- $temp = floatval($wallet->available_balance);
- $text = "请发送提现金额\n";
- $text .= "💰 当前余额{$temp} RMB\n";
- // Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- ];
- }
- //2.选择银行卡号
- private static function chooseBank($chatId, $id)
- {
- Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
- Cache::put("{$chatId}_QB_BANK_ID", $id);
- return [
- 'chat_id' => $chatId,
- 'text' => "请输入提现的金额",
- ];
- }
- //3.输入钱宝提现金额
- private static function inputQbAmount($chatId, $amount, $messageId)
- {
- if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
- return [
- 'chat_id' => $chatId,
- 'text' => "金额输入不正确,请发送提现数字",
- 'reply_to_message_id' => $messageId
- ];
- }
- $amount = floatval($amount);
- $wallet = Wallet::where('member_id', $chatId)->first();
- $temp = floatval($wallet->available_balance);
- if ($amount > $temp) {
- return [
- 'chat_id' => $chatId,
- 'text' => "⚠️可用余额不足,请重试",
- 'reply_to_message_id' => $messageId
- ];
- }
- if ($amount < 100) {
- return [
- 'chat_id' => $chatId,
- 'text' => "⚠️提现不能少于100 RMB,请重试",
- 'reply_to_message_id' => $messageId
- ];
- }
- if ($amount > 49999) {
- return [
- 'chat_id' => $chatId,
- 'text' => "⚠️最多提现 49999 RMB,请重试",
- 'reply_to_message_id' => $messageId
- ];
- }
- $bankId = Cache::get("{$chatId}_QB_BANK_ID");
- $bank = Bank::where('id', $bankId)->first();
- $text = "";
- switch ($bank->channel) {
- case "DF001":
- $text = "银行卡提现确认\n";
- $text .= "开户行:{$bank->bank_name}\n";
- $text .= "姓名:{$bank->account}\n";
- $text .= "提现账号:{$bank->card_no}\n";
- $text .= "提现金额:{$amount}\n";
- break;
- case "DF002":
- $text = "支付宝提现确认\n";
- $text .= "姓名:{$bank->account}\n";
- $text .= "提现账号:{$bank->card_no}\n";
- $text .= "提现金额:{$amount}\n";
- break;
- case "DF005":
- $text = "数字人民币提现确认\n";
- $text .= "姓名:{$bank->account}\n";
- $text .= "提现账号:{$bank->card_no}\n";
- $text .= "提现金额:{$amount}\n";
- break;
- }
- $keyboard = [
- [
- ['text' => "确认", 'callback_data' => 'withdraw@@qb_confirm']
- ],
- [
- ['text' => '❌取消', 'callback_data' => "message@@close"]
- ]];
- Cache::put("{$chatId}_WITHDRAW_QB_MONEY", $amount);
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- // 'reply_to_message_id' => $messageId,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
- ];
- }
- private static function confirm($chatId, $messageId)
- {
- $id = Cache::get("{$chatId}_QB_BANK_ID");
- $bank = Bank::where('id', $id)->first();
- $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY");
- $res = PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
- $res['message_id'] = $messageId;
- return $res;
- }
- //银行卡管理
- private static function banks($chatId, $messageId, $channel)
- {
- switch ($channel) {
- case "DF001":
- $text = "银行卡列表\n";
- break;
- case "DF002":
- $text = "支付宝账户列表\n";
- break;
- case "DF005":
- $text = "数字人民币账户列表\n";
- break;
- }
- $list = Bank::where('member_id', $chatId)
- ->where("channel", $channel)
- ->get();
- $keyboard = [];
- foreach ($list as $item) {
- $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@bank_detail{$item->id}"]];
- }
- if (count($list) < 5) {
- $keyboard[] = [['text' => "➕ 添加", 'callback_data' => "withdrawAddress@@bank_choose_channel_{$channel}"], ['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
- } else {
- $keyboard[] = [['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
- }
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
- ];
- }
- //银行卡详情
- private static function bankDetails($chatId, $messageId, $id)
- {
- $bank = Bank::where('id', $id)
- ->where('member_id', $chatId)->first();
- switch ($bank->channel) {
- case "DF001":
- $text = "*银行卡管理*\n\n";
- $text .= "姓名:{$bank->account}\n";
- $text .= "银行:{$bank->bank_name}\n";
- $text .= "卡号:{$bank->card_no}\n";
- break;
- case "DF002":
- $text = "*支付宝管理*\n\n";
- $text .= "姓名:{$bank->account}\n";
- $text .= "银行:{$bank->bank_name}\n";
- $text .= "账号:{$bank->card_no}\n";
- break;
- default:
- $text = "*银行卡管理*\n\n";
- $text .= "姓名:{$bank->account}\n";
- $text .= "银行:{$bank->bank_name}\n";
- $text .= "卡号:{$bank->card_no}\n";
- break;
- }
- $keyboard = [
- [['text' => '❌删除', 'callback_data' => "withdraw@@bank_del_{$id}"], ['text' => '↩️返回列表', 'callback_data' => "withdraw@@management_{$bank->channel}"]],
- ];
- return [
- 'chat_id' => $chatId,
- 'parse_mode' => 'MarkdownV2',
- 'text' => $text,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
- 'message_id' => $messageId
- ];
- }
- //删除银行卡
- private static function bankDelete($chatId, $messageId, $id)
- {
- Bank::where('id', $id)
- ->where('member_id', $chatId)->delete();
- return static::banks($chatId, $messageId);
- }
- //添加银行卡
- private static function addBank($chatId, $messageId)
- {
- $text = "请选择 提现通道\n";
- $keyboard = [
- [
- ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
- ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
- ],
- [
- ['text' => '❌取消', 'callback_data' => "message@@close"]
- ]
- ];
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
- ];
- }
- //选择通道
- private static function chooseChannel($chatId, $messageId, $channel)
- {
- Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
- switch ($channel) {
- case "DF001"://银行卡
- Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
- return [
- 'chat_id' => $chatId,
- 'text' => "请输入开户银行卡开户名称",
- 'message_id' => $messageId,
- ];
- break;
- case "DF002"://支付宝
- Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
- Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
- return [
- 'chat_id' => $chatId,
- 'text' => "请输入支付宝账号",
- 'message_id' => $messageId,
- ];
- break;
- case "DF005"://数字人民币
- Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '数字人民币');
- Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
- return [
- 'chat_id' => $chatId,
- 'text' => "请输入数字人民币账号",
- 'message_id' => $messageId,
- ];
- break;
- default:
- return [
- 'chat_id' => $chatId,
- 'text' => "选择通道错误",
- 'message_id' => $messageId,
- ];
- break;
- }
- }
- //输入银行名称
- private static function inputBankName($chatId, $bankName, $messageId)
- {
- Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
- Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
- return [
- 'chat_id' => $chatId,
- 'text' => "请输入银行卡号",
- 'message_id' => $messageId,
- ];
- }
- //输入卡号
- private static function inputCardNo($chatId, $cardNo, $messageId)
- {
- $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
- if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
- return [
- 'chat_id' => $chatId,
- 'text' => "输入的银行卡号有误,请重新输入",
- 'reply_to_message_id' => $messageId,
- ];
- }
- Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
- Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
- return [
- 'chat_id' => $chatId,
- 'text' => "请输入姓名",
- 'message_id' => $messageId,
- ];
- }
- //输入姓名,并保存到数据库
- private static function inputAccount($chatId, $account, $messageId)
- {
- Cache::put("{$chatId}_QB_ACCOUNT", $account);
- Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ALIAS);
- $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
- $text = "请输入别名";
- switch ($channel) {
- case "DF001":
- $text = "请输入账号别名";
- break;
- case "DF002":
- $text = "请输入别名";
- break;
- case "DF005":
- $text = "请输入账号别名";
- break;
- }
- return [
- 'chat_id' => $chatId,
- 'text' => $text,
- 'message_id' => $messageId,
- ];
- }
- private static function inputAliAs($chatId, $alias, $messageId)
- {
- $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
- $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
- $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
- $account = Cache::get("{$chatId}_QB_ACCOUNT");
- Bank::create([
- 'member_id' => $chatId,
- 'account' => $account,
- 'channel' => $channel,
- 'card_no' => $cardNo,
- 'bank_name' => $bankName,
- 'alias' => $alias
- ]);
- Cache::delete(get_step_key($chatId));
- return static::banks($chatId, $messageId, $channel);
- }
- }
|