QianBaoWithdrawService.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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\Bank;
  7. use App\Models\Config;
  8. use App\Models\PaymentOrder;
  9. use App\Models\RechargeChannel;
  10. use App\Models\User;
  11. use App\Models\Wallet;
  12. use App\Services\Payment\JdPayService;
  13. use App\Services\Payment\NoPayService;
  14. use App\Services\Payment\QianBaoService;
  15. use App\Services\Payment\ZimuPayService;
  16. use Exception;
  17. use Illuminate\Support\Facades\Cache;
  18. use Illuminate\Support\Facades\DB;
  19. use Telegram\Bot\Api;
  20. use Telegram\Bot\Exceptions\TelegramSDKException;
  21. class QianBaoWithdrawService
  22. {
  23. /**
  24. * @param Api $telegram
  25. * @param $data
  26. * @param $chatId
  27. * @param $firstName
  28. * @param $messageId
  29. * @throws TelegramSDKException
  30. */
  31. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId): void
  32. {
  33. //点击钱宝提现按钮
  34. if ($data === "withdraw@@qb_show_channel") {
  35. $res = QianBaoWithdrawService::chooseType($chatId, $messageId);
  36. $telegram->editMessageText($res);
  37. }
  38. $pattern = "/^withdraw@@qb_choose_.*$/";
  39. if (preg_match($pattern, $data)) {
  40. $type = preg_replace('/^withdraw@@qb_choose_/', '', $data);
  41. $res = QianBaoWithdrawService::showBanks($chatId, $messageId, $type);
  42. $telegram->editMessageText($res);
  43. }
  44. //选择银行卡号
  45. $pattern = "/^withdrawAddress@@choose_qb_\d+$/";
  46. if (preg_match($pattern, $data)) {
  47. $id = preg_replace('/^withdrawAddress@@choose_qb_/', '', $data);
  48. $res = QianBaoWithdrawService::chooseBank($chatId, $id);
  49. $telegram->deleteMessage([
  50. 'chat_id' => $chatId,
  51. 'message_id' => $messageId,
  52. ]);
  53. $telegram->sendMessage($res);
  54. }
  55. //确认提现信息
  56. if ($data === "withdraw@@qb_confirm") {
  57. $res = QianBaoWithdrawService::confirm($chatId, $messageId);
  58. $telegram->editMessageText($res);
  59. }
  60. if ($data === "withdraw@@qb_apply") {
  61. $res = QianBaoWithdrawService::qbApply($chatId, $messageId);
  62. $telegram->editMessageText($res);
  63. }
  64. //钱宝账单
  65. $pattern = "/^withdraw@@bank_bill_\d+$/";
  66. if (preg_match($pattern, $data)) {
  67. $page = preg_replace('/^withdraw@@bank_bill_/', '', $data);
  68. if (empty($page) || $page < 1) $page = 1;
  69. $page = intval($page);
  70. $res = QianBaoWithdrawService::bill($chatId, $firstName, $messageId, $page);
  71. $telegram->editMessageText($res);
  72. }
  73. //银行卡管理
  74. $pattern = "/^withdraw@@management_.*$/";
  75. if (preg_match($pattern, $data)) {
  76. $channel = preg_replace('/^withdraw@@management_/', '', $data);
  77. $res = QianBaoWithdrawService::banks($chatId, $messageId, $channel);
  78. $telegram->editMessageText($res);
  79. }
  80. //添加银行卡
  81. // $pattern = "/^withdraw@@bank_add.*$/";
  82. // if (preg_match($pattern, $data)) {
  83. // $channel = preg_replace('/^withdraw@@bank_add/', '', $data);
  84. // $res = QianBaoWithdrawService::chooseChannel($chatId, $messageId,$channel);
  85. // $telegram->editMessageText($res);
  86. // }
  87. //银行卡详情
  88. $pattern = "/^withdrawAddress@@bank_detail\d+$/";
  89. if (preg_match($pattern, $data)) {
  90. $id = preg_replace('/^withdrawAddress@@bank_detail/', '', $data);
  91. $res = static::bankDetails($chatId, $messageId, $id);
  92. $telegram->editMessageText($res);
  93. }
  94. $pattern = "/^withdraw@@bank_del_\d+$/";
  95. if (preg_match($pattern, $data)) {
  96. $id = preg_replace('/^withdraw@@bank_del_/', '', $data);
  97. $res = static::bankDelete($chatId, $messageId, $id);
  98. $telegram->editMessageText($res);
  99. }
  100. $pattern = "/^withdrawAddress@@bank_choose_channel_.*$/";
  101. if (preg_match($pattern, $data)) {
  102. $channel = preg_replace('/^withdrawAddress@@bank_choose_channel_/', '', $data);
  103. $res = static::chooseChannel($chatId, $messageId, $channel);
  104. $telegram->editMessageText($res);
  105. }
  106. }
  107. public static function onMessage($chatId, $text, $messageId, $stepStatus): null|array
  108. {
  109. return match ($stepStatus) {
  110. StepStatus::QB_INPUT_ALIAS => QianBaoWithdrawService::inputAliAs($chatId, $text, $messageId),
  111. StepStatus::INPUT_WITHDRAW_QB_MONEY => QianBaoWithdrawService::inputQbAmount($chatId, $text, $messageId),
  112. StepStatus::QB_INPUT_BANK_NAME => QianBaoWithdrawService::inputBankName($chatId, $text, $messageId),
  113. StepStatus::QB_INPUT_CARD_NO => QianBaoWithdrawService::inputCardNo($chatId, $text, $messageId),
  114. StepStatus::QB_INPUT_ACCOUNT => QianBaoWithdrawService::inputAccount($chatId, $text, $messageId),
  115. default => null,
  116. };
  117. }
  118. private static function chooseType($chatId, $messageId)
  119. {
  120. $serviceAccount = Config::where('field', 'service_customer')->first()->val;
  121. //是否封号
  122. $isBanned = UserService::getIsBanned($chatId);
  123. if ($isBanned) {
  124. $text = lang("账号异常") . "\n";
  125. $text .= lang("任何疑问都可以联系唯一客服") . ":@{$serviceAccount}";
  126. return [
  127. 'chat_id' => $chatId,
  128. 'text' => $text,
  129. 'message_id' => $messageId,
  130. ];
  131. }
  132. $activityUser = ActivityUserService::findOne(['member_id' => $chatId, 'status' => ActivityUser::STATUS_IN_PROGRESS]);
  133. if ($activityUser) {
  134. $text = lang("您有未完成的活动") . "\n";
  135. $text .= lang("任何疑问都可以联系唯一客服") . ":@{$serviceAccount}";
  136. return [
  137. 'chat_id' => $chatId,
  138. 'text' => $text,
  139. 'message_id' => $messageId,
  140. ];
  141. }
  142. $keyboard = [
  143. [
  144. ['text' => lang("USDT"), 'callback_data' => "withdraw@@apply"],
  145. ],
  146. [
  147. ['text' => lang("银行卡"), 'callback_data' => "withdraw@@qb_choose_bank"],
  148. ],
  149. [
  150. ['text' => lang("支付宝"), 'callback_data' => "withdraw@@qb_choose_aliPay"],
  151. ],
  152. [
  153. ['text' => lang("数字人民币"), 'callback_data' => "withdraw@@qb_choose_digital_RMB"],
  154. ],
  155. [
  156. ['text' => lang("返回"), 'callback_data' => "topUp@@home"],
  157. ],
  158. ];
  159. // $keyboard[] = [
  160. // ['text' => "提现账户管理", 'callback_data' => "withdraw@@banks"],
  161. // ];
  162. return [
  163. 'chat_id' => $chatId,
  164. 'text' => lang("请选择提现方式"),
  165. 'message_id' => $messageId,
  166. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  167. ];
  168. }
  169. private static function showBanks($chatId, $messageId, $type)
  170. {
  171. $channel = '';
  172. $card = "";
  173. $text = "";
  174. switch ($type) {
  175. case "bank":
  176. $card = lang("银行卡");
  177. $text = lang("请选择提现的银行卡");
  178. $channel = 'DF001';
  179. break;
  180. case "aliPay":
  181. $card = lang("支付宝");
  182. $text = lang("请选择提现的支付宝");
  183. $channel = "DF002";
  184. break;
  185. case "digital_RMB":
  186. $card = lang("数字人民币");
  187. $text = lang("请选择提现的账户");
  188. $channel = "DF005";
  189. break;
  190. }
  191. $list = Bank::where('member_id', $chatId)->where('channel', $channel)->get();
  192. $keyboard = [];
  193. foreach ($list as $item) {
  194. $keyboard[] = [['text' => $item->getAlias(), 'callback_data' => "withdrawAddress@@choose_qb_{$item->getId()}"]];
  195. }
  196. $keyboard[] = [
  197. ['text' => "{$card}" . lang("管理"), 'callback_data' => "withdraw@@management_{$channel}"],
  198. ['text' => lang("返回"), 'callback_data' => "withdraw@@qb_show_channel"]];
  199. return [
  200. 'chat_id' => $chatId,
  201. 'text' => $text,
  202. 'message_id' => $messageId,
  203. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  204. ];
  205. }
  206. //钱宝账单
  207. private static function bill($chatId, $firstName, $messageId, $page = 1, $limit = 5)
  208. {
  209. $list = PaymentOrder::where('member_id', $chatId)
  210. ->where('type', 2)
  211. ->orderByDesc('created_at')
  212. ->forPage($page, $limit)
  213. ->get();
  214. $count = PaymentOrder::where('member_id', $chatId)
  215. ->where('type', 2)
  216. ->count();
  217. $text = "👤 {$firstName}({$chatId}) " . lang('钱宝提现记录') . "\n\n";
  218. foreach ($list as $item) {
  219. $amount = floatval($item->amount);
  220. $amount = $item->type == 2 ? "➖ {$amount}" : "➕ $amount";
  221. $text .= "-------------------------------------\n";
  222. $text .= "{$amount} \n";
  223. $text .= lang("订单号") . ":{$item->order_no}\n";
  224. $text .= lang('银行') . ":{$item->bank_name}\n";
  225. $text .= lang("姓名") . ":{$item->account}\n";
  226. $text .= lang("卡号") . ":{$item->card_no}\n";
  227. $status = [lang('待处理'), lang('处理中'), lang('成功'), lang('失败')];
  228. $text .= lang("状态") . ":{$status[$item->status]}\n";
  229. if ($item->remark) {
  230. $text .= lang("说明") . ":{$item->remark}\n";
  231. }
  232. $text .= lang('日期') . ":{$item->created_at}\n";
  233. }
  234. if ($page > 1) {
  235. $keyboard[] = [
  236. ['text' => lang("👆上一页"), 'callback_data' => "withdraw@@bank_bill_" . ($page - 1)]
  237. ];
  238. }
  239. $allPage = ceil($count / $limit);
  240. if ($allPage > $page) {
  241. if ($page > 1) {
  242. $keyboard[count($keyboard) - 1][] = ['text' => lang("👇下一页"), 'callback_data' => "withdraw@@bank_bill_" . ($page + 1)];
  243. } else {
  244. $keyboard[] = [
  245. ['text' => lang("👇下一页"), 'callback_data' => "withdraw@@bank_bill_" . ($page + 1)]
  246. ];
  247. }
  248. }
  249. $keyboard[] = [
  250. ['text' => lang("返回"), 'callback_data' => "topUp@@home"]
  251. ];
  252. return [
  253. 'chat_id' => $chatId,
  254. 'text' => $text,
  255. 'message_id' => $messageId,
  256. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  257. ];
  258. }
  259. //1.钱宝提现
  260. private static function qbApply($chatId, $messageId)
  261. {
  262. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  263. if ($three_payment_switch != 1) {
  264. $res = WalletService::getBalance($chatId);
  265. $res['message_id'] = $messageId;
  266. return $res;
  267. }
  268. $wallet = Wallet::where('member_id', $chatId)->first();
  269. $temp = floatval($wallet->available_balance);
  270. $text = lang("请发送提现金额") . "\n";
  271. $text .= "💰 " . lang("当前余额") . "{$temp} USDT\n";
  272. // Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  273. return [
  274. 'chat_id' => $chatId,
  275. 'text' => $text,
  276. 'message_id' => $messageId,
  277. ];
  278. }
  279. //2.选择银行卡号
  280. private static function chooseBank($chatId, $id)
  281. {
  282. Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  283. Cache::put("{$chatId}_QB_BANK_ID", $id);
  284. return [
  285. 'chat_id' => $chatId,
  286. 'text' => lang("请输入提现的金额"),
  287. ];
  288. }
  289. //3.输入钱宝提现金额
  290. private static function inputQbAmount($chatId, $amount, $messageId)
  291. {
  292. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  293. return [
  294. 'chat_id' => $chatId,
  295. 'text' => lang('金额输入不正确,请发送提现数字'),
  296. 'reply_to_message_id' => $messageId
  297. ];
  298. }
  299. $amount = floatval($amount);
  300. $wallet = Wallet::where('member_id', $chatId)->first();
  301. $temp = floatval($wallet->available_balance);
  302. if ($amount > $temp) {
  303. return [
  304. 'chat_id' => $chatId,
  305. 'text' => lang("可用余额不足,请重试"),
  306. 'reply_to_message_id' => $messageId
  307. ];
  308. }
  309. if ($amount < 100) {
  310. return [
  311. 'chat_id' => $chatId,
  312. 'text' => lang("提现不能少于100 RMB,请重试"),
  313. 'reply_to_message_id' => $messageId
  314. ];
  315. }
  316. if ($amount > 49999) {
  317. return [
  318. 'chat_id' => $chatId,
  319. 'text' => lang("最多提现 49999 RMB,请重试"),
  320. 'reply_to_message_id' => $messageId
  321. ];
  322. }
  323. $bankId = Cache::get("{$chatId}_QB_BANK_ID");
  324. $bank = Bank::where('id', $bankId)->first();
  325. $text = "";
  326. switch ($bank->getChannel()) {
  327. case "DF001":
  328. $text = lang('银行卡提现确认') . "\n";
  329. $text .= lang('开户行') . ":{$bank->getBankName()}\n";
  330. $text .= lang('姓名') . ":{$bank->getAccount()}\n";
  331. $text .= lang('提现账号') . ":{$bank->getCardNo()}\n";
  332. $text .= lang('提现金额') . ":{$amount}\n";
  333. break;
  334. case "DF002":
  335. $text = lang("支付宝提现确认") . "\n";
  336. $text .= lang('姓名') . ":{$bank->getAccount()}\n";
  337. $text .= lang('提现账号') . ":{$bank->getCardNo()}\n";
  338. $text .= lang('提现金额') . ":{$amount}\n";
  339. break;
  340. case "DF005":
  341. $text = lang('数字人民币提现确认') . "\n";
  342. $text .= lang('姓名') . ":{$bank->getAccount()}\n";
  343. $text .= lang('提现账号') . ":{$bank->getCardNo()}\n";
  344. $text .= lang('提现金额') . ":{$amount}\n";
  345. break;
  346. }
  347. $keyboard = [
  348. [
  349. ['text' => lang("确认"), 'callback_data' => 'withdraw@@qb_confirm']
  350. ],
  351. [
  352. ['text' => '❌取消', 'callback_data' => "message@@close"]
  353. ]];
  354. Cache::put("{$chatId}_WITHDRAW_QB_MONEY", $amount);
  355. return [
  356. 'chat_id' => $chatId,
  357. 'text' => $text,
  358. // 'reply_to_message_id' => $messageId,
  359. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  360. ];
  361. }
  362. private static function confirm($chatId, $messageId)
  363. {
  364. $serviceAccount = Config::where('field', 'service_customer')->first()->val;
  365. $activityUser = ActivityUserService::findOne(['member_id' => $chatId, 'status' => ActivityUser::STATUS_IN_PROGRESS]);
  366. if ($activityUser) {
  367. $text = lang("您有未完成的活动") . "\n";
  368. $text .= lang("任何疑问都可以联系唯一客服") . ":@{$serviceAccount}";
  369. return [
  370. 'chat_id' => $chatId,
  371. 'text' => $text,
  372. 'message_id' => $messageId,
  373. ];
  374. }
  375. $id = Cache::get("{$chatId}_QB_BANK_ID");
  376. $bank = Bank::where('id', $id)->first();
  377. $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY");
  378. //提现是否自动转到三方到账,否的话,需要后台审核,审核通过后才会进行第三方转账
  379. //目前客户要求手动到账,后续也可以做到config 表,从后台进行控制
  380. $isAutoDeposit = false;
  381. if ($isAutoDeposit) {
  382. $res = PaymentOrderService::autoCreatePayout($chatId, $amount, $bank->getChannel(), $bank->getBankName(), $bank->getAccount(), $bank->getCardNo());
  383. } else {
  384. $res = static::createOrder($chatId, $amount, $bank->getChannel(), $bank->getBankName(), $bank->getAccount(), $bank->getCardNo());
  385. }
  386. $res['message_id'] = $messageId;
  387. return $res;
  388. }
  389. //创建提现订单
  390. public static function createOrder($memberId, $amount, $channel, $bank_name, $account, $card_no)
  391. {
  392. $result['chat_id'] = $memberId;
  393. $result['code'] = 0;
  394. $default_amount = $amount;
  395. $user = User::query()->where('member_id', $memberId)->first();
  396. $groupId = (int)($user->recharge_channel_group_id ?? 1);
  397. $channelConfig = RechargeChannel::checkWithdrawChannel($channel, $groupId);
  398. if ($channelConfig === false) {
  399. return ['chat_id' => $memberId, 'code' => HttpStatus::CUSTOM_ERROR, 'text' => '不支持此提现方式'];
  400. }
  401. $minAmount = (float)($channelConfig->min ?? 0);
  402. $maxAmount = (float)($channelConfig->max ?? 0);
  403. if ($amount < $minAmount || ($maxAmount > 0 && $amount > $maxAmount)) {
  404. return ['chat_id' => $memberId, 'code' => HttpStatus::CUSTOM_ERROR, 'text' => "提现金额范围为{$minAmount}-{$maxAmount}"];
  405. }
  406. $channelRate = (float)($channelConfig->rate ?? 0);
  407. DB::beginTransaction();
  408. try {
  409. $wallet = WalletService::findOne(['member_id' => $memberId]);
  410. if (!$wallet) throw new Exception('钱包不存在', HttpStatus::CUSTOM_ERROR);
  411. $balance = $wallet->available_balance;
  412. if (bccomp($balance, $amount, 2) < 0) {
  413. throw new Exception("您的钱包余额不足!", HttpStatus::CUSTOM_ERROR);
  414. }
  415. $available_balance = bcsub($balance, $amount, 10);
  416. // 先预扣款(锁定资金)
  417. $wallet->available_balance = $available_balance;
  418. if (!$wallet->save()) throw new Exception('钱包更新失败!', HttpStatus::CUSTOM_ERROR);
  419. $data = [];
  420. $data['type'] = PaymentOrderService::TYPE_PAYOUT;
  421. $data['order_no'] = PaymentOrderService::createOrderNo('sj' . $data['type'] . '_', $memberId);
  422. $data['member_id'] = $memberId;
  423. $data['fee'] = $amount * $channelRate + 2;
  424. $data['amount'] = number_format($amount, 2, '.', '');
  425. $data['channel'] = $channel;
  426. $data['bank_name'] = $bank_name;
  427. $data['account'] = $account;
  428. $data['card_no'] = $card_no;
  429. if (NoPayService::isWithdrawChannel($channel)) {
  430. $data['callback_url'] = NoPayService::getWithdrawNotifyUrl();
  431. } elseif (JdPayService::isChannel($channel)) {
  432. $data['callback_url'] = JdPayService::getRemitNotifyUrl();
  433. } elseif (ZimuPayService::isPayoutChannel($channel)) {
  434. $data['callback_url'] = ZimuPayService::getPayoutNotifyUrl();
  435. } else {
  436. $data['callback_url'] = QianBaoService::getNotifyUrl();
  437. }
  438. $data['status'] = PaymentOrderService::STATUS_STAY;
  439. $data['remark'] = '提现费率:' . ($channelRate * 100) . '%+2';
  440. // 创建待处理状态的提现记录
  441. $info = PaymentOrder::create($data);
  442. // 记录余额变动日志
  443. BalanceLogService::addLog($memberId, $default_amount * -1, $balance, $available_balance, '三方提现', $info->id, '钱宝提现费率:' . ($channelRate * 100) . '%+2');
  444. $balance = bcadd($available_balance, 0, 2);
  445. $text = "✅ 提现申请已提交!\n\n";
  446. $text .= "钱包余额:{$balance} RMB\n";
  447. $text .= "提现金额:{$default_amount} RMB\n";
  448. $text .= "⌛️请等待系统处理, 到账时间可能需要几分钟!\n";
  449. $result['text'] = $text;
  450. DB::commit();
  451. } //
  452. catch (Exception $e) {
  453. DB::rollBack();
  454. LogService::error($e);
  455. $result['text'] = "系统发生了错误,请联系管理员";
  456. $result['code'] = -1;
  457. if ($e->getCode() === HttpStatus::CUSTOM_ERROR) {
  458. $result['text'] = $e->getMessage();
  459. }
  460. }
  461. return $result;
  462. }
  463. //银行卡管理
  464. private static function banks($chatId, $messageId, $channel = ""): array
  465. {
  466. $text = '';
  467. switch ($channel) {
  468. case "DF001":
  469. $text = lang("银行卡列表") . "\n";
  470. break;
  471. case "DF002":
  472. $text = lang('支付宝账户列表') . "\n";
  473. break;
  474. case "DF005":
  475. $text = lang('数字人民币账户列表') . "\n";
  476. break;
  477. }
  478. $list = Bank::where('member_id', $chatId)
  479. ->where("channel", $channel)
  480. ->get();
  481. $keyboard = [];
  482. foreach ($list as $item) {
  483. $keyboard[] = [['text' => $item->getAlias(), 'callback_data' => "withdrawAddress@@bank_detail{$item->getId()}"]];
  484. }
  485. if (count($list) < 5) {
  486. $keyboard[] = [
  487. ['text' => lang("➕ 添加"), 'callback_data' => "withdrawAddress@@bank_choose_channel_{$channel}"],
  488. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"]
  489. ];
  490. } else {
  491. $keyboard[] = [['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"]];
  492. }
  493. return [
  494. 'chat_id' => $chatId,
  495. 'text' => $text,
  496. 'message_id' => $messageId,
  497. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  498. ];
  499. }
  500. //银行卡详情
  501. private static function bankDetails($chatId, $messageId, $id)
  502. {
  503. $bank = Bank::where('id', $id)
  504. ->where('member_id', $chatId)->first();
  505. switch ($bank->getChannel()) {
  506. case "DF001":
  507. $text = "*" . lang('银行卡管理') . "*\n\n";
  508. $text .= lang('姓名') . ":{$bank->getAccount()}\n";
  509. $text .= lang('银行') . ":{$bank->getAccount()}\n";
  510. $text .= lang('卡号') . ":{$bank->getCardNo()}\n";
  511. break;
  512. case "DF002":
  513. $text = "*" . lang('支付宝管理') . "*\n\n";
  514. $text .= lang('姓名') . ":{$bank->getAccount()}\n";
  515. $text .= lang('银行') . ":{$bank->getBankName()}\n";
  516. $text .= lang('账号') . ":{$bank->getCardNo()}\n";
  517. break;
  518. default:
  519. $text = "*" . lang('银行卡管理') . "*\n\n";
  520. $text .= lang('姓名') . ":{$bank->getAccount()}\n";
  521. $text .= lang('银行') . ":{$bank->getBankName()}\n";
  522. $text .= lang('卡号') . ":{$bank->getCardNo()}\n";
  523. break;
  524. }
  525. $keyboard = [
  526. [
  527. ['text' => lang('❌删除'), 'callback_data' => "withdraw@@bank_del_{$id}"],
  528. ['text' => lang('↩️返回列表'), 'callback_data' => "withdraw@@management_{$bank->getChannel()}"]
  529. ],
  530. ];
  531. return [
  532. 'chat_id' => $chatId,
  533. 'parse_mode' => 'MarkdownV2',
  534. 'text' => $text,
  535. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  536. 'message_id' => $messageId
  537. ];
  538. }
  539. //删除银行卡
  540. private static function bankDelete($chatId, $messageId, $id): array
  541. {
  542. $channel = Bank::where('id', $id)
  543. ->where('member_id', $chatId)->first()->getChannel();
  544. Bank::where('id', $id)->where('member_id', $chatId)->delete();
  545. return static::banks($chatId, $messageId, $channel);
  546. }
  547. //选择通道
  548. private static function chooseChannel($chatId, $messageId, $channel)
  549. {
  550. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  551. switch ($channel) {
  552. case "DF001"://银行卡
  553. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  554. return [
  555. 'chat_id' => $chatId,
  556. 'text' => lang("请输入开户银行卡开户名称"),
  557. 'message_id' => $messageId,
  558. ];
  559. case "DF002"://支付宝
  560. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  561. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  562. return [
  563. 'chat_id' => $chatId,
  564. 'text' => lang("请输入支付宝账号"),
  565. 'message_id' => $messageId,
  566. ];
  567. case "DF005"://数字人民币
  568. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '数字人民币');
  569. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  570. return [
  571. 'chat_id' => $chatId,
  572. 'text' => lang("请输入数字人民币账号"),
  573. 'message_id' => $messageId,
  574. ];
  575. default:
  576. return [
  577. 'chat_id' => $chatId,
  578. 'text' => lang("选择通道错误"),
  579. 'message_id' => $messageId,
  580. ];
  581. }
  582. }
  583. //输入银行名称
  584. private static function inputBankName($chatId, $bankName, $messageId): array
  585. {
  586. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  587. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  588. return [
  589. 'chat_id' => $chatId,
  590. 'text' => lang("请输入银行卡号"),
  591. 'message_id' => $messageId,
  592. ];
  593. }
  594. //输入卡号
  595. private static function inputCardNo($chatId, $cardNo, $messageId): array
  596. {
  597. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  598. if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
  599. return [
  600. 'chat_id' => $chatId,
  601. 'text' => lang("输入的银行卡号有误,请重新输入"),
  602. 'reply_to_message_id' => $messageId,
  603. ];
  604. }
  605. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  606. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  607. return [
  608. 'chat_id' => $chatId,
  609. 'text' => lang("请输入姓名"),
  610. 'message_id' => $messageId,
  611. ];
  612. }
  613. //输入姓名
  614. private static function inputAccount($chatId, $account, $messageId): array
  615. {
  616. Cache::put("{$chatId}_QB_ACCOUNT", $account);
  617. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ALIAS);
  618. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  619. $text = lang("请输入别名");
  620. switch ($channel) {
  621. case "DF005":
  622. case "DF001":
  623. $text = lang('请输入账号别名');
  624. break;
  625. }
  626. return [
  627. 'chat_id' => $chatId,
  628. 'text' => $text,
  629. 'message_id' => $messageId,
  630. ];
  631. }
  632. //输入别名,并保存到数据库
  633. private static function inputAliAs($chatId, $alias, $messageId): array
  634. {
  635. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  636. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  637. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  638. $account = Cache::get("{$chatId}_QB_ACCOUNT");
  639. Bank::create([
  640. 'member_id' => $chatId,
  641. 'account' => $account,
  642. 'channel' => $channel,
  643. 'card_no' => $cardNo,
  644. 'bank_name' => $bankName,
  645. 'alias' => $alias
  646. ]);
  647. Cache::delete(get_step_key($chatId));
  648. return static::banks($chatId, $messageId, $channel);
  649. }
  650. }