QianBaoWithdrawService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\StepStatus;
  4. use App\Models\Bank;
  5. use App\Models\Config;
  6. use App\Models\PaymentOrder;
  7. use App\Models\Wallet;
  8. use Illuminate\Support\Facades\Cache;
  9. use Telegram\Bot\Api;
  10. use Telegram\Bot\Exceptions\TelegramSDKException;
  11. class QianBaoWithdrawService
  12. {
  13. /**
  14. * @param Api $telegram
  15. * @param $data
  16. * @param $chatId
  17. * @param $firstName
  18. * @param $messageId
  19. * @throws TelegramSDKException
  20. */
  21. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId): void
  22. {
  23. //点击钱宝提现按钮
  24. if ($data === "withdraw@@qb_show_channel") {
  25. $res = QianBaoWithdrawService::chooseType($chatId, $messageId);
  26. $telegram->editMessageText($res);
  27. }
  28. $pattern = "/^withdraw@@qb_choose_.*$/";
  29. if (preg_match($pattern, $data)) {
  30. $type = preg_replace('/^withdraw@@qb_choose_/', '', $data);
  31. $res = QianBaoWithdrawService::showBanks($chatId, $messageId, $type);
  32. $telegram->editMessageText($res);
  33. }
  34. //选择银行卡号
  35. $pattern = "/^withdrawAddress@@choose_qb_\d+$/";
  36. if (preg_match($pattern, $data)) {
  37. $id = preg_replace('/^withdrawAddress@@choose_qb_/', '', $data);
  38. $res = QianBaoWithdrawService::chooseBank($chatId, $id);
  39. $telegram->deleteMessage([
  40. 'chat_id' => $chatId,
  41. 'message_id' => $messageId,
  42. ]);
  43. $telegram->sendMessage($res);
  44. }
  45. //确认提现信息
  46. if ($data === "withdraw@@qb_confirm") {
  47. $res = QianBaoWithdrawService::confirm($chatId, $messageId);
  48. $telegram->editMessageText($res);
  49. }
  50. if ($data === "withdraw@@qb_apply") {
  51. $res = QianBaoWithdrawService::qbApply($chatId, $messageId);
  52. $telegram->editMessageText($res);
  53. }
  54. //钱宝账单
  55. $pattern = "/^withdraw@@bank_bill_\d+$/";
  56. if (preg_match($pattern, $data)) {
  57. $page = preg_replace('/^withdraw@@bank_bill_/', '', $data);
  58. if (empty($page) || $page < 1) $page = 1;
  59. $page = intval($page);
  60. $res = QianBaoWithdrawService::bill($chatId, $firstName, $messageId, $page);
  61. $telegram->editMessageText($res);
  62. }
  63. //银行卡管理
  64. $pattern = "/^withdraw@@management_.*$/";
  65. if (preg_match($pattern, $data)) {
  66. $channel = preg_replace('/^withdraw@@management_/', '', $data);
  67. $res = QianBaoWithdrawService::banks($chatId, $messageId, $channel);
  68. $telegram->editMessageText($res);
  69. }
  70. //添加银行卡
  71. // $pattern = "/^withdraw@@bank_add.*$/";
  72. // if (preg_match($pattern, $data)) {
  73. // $channel = preg_replace('/^withdraw@@bank_add/', '', $data);
  74. // $res = QianBaoWithdrawService::chooseChannel($chatId, $messageId,$channel);
  75. // $telegram->editMessageText($res);
  76. // }
  77. //银行卡详情
  78. $pattern = "/^withdrawAddress@@bank_detail\d+$/";
  79. if (preg_match($pattern, $data)) {
  80. $id = preg_replace('/^withdrawAddress@@bank_detail/', '', $data);
  81. $res = static::bankDetails($chatId, $messageId, $id);
  82. $telegram->editMessageText($res);
  83. }
  84. $pattern = "/^withdraw@@bank_del_\d+$/";
  85. if (preg_match($pattern, $data)) {
  86. $id = preg_replace('/^withdraw@@bank_del_/', '', $data);
  87. $res = static::bankDelete($chatId, $messageId, $id);
  88. $telegram->editMessageText($res);
  89. }
  90. $pattern = "/^withdrawAddress@@bank_choose_channel_.*$/";
  91. if (preg_match($pattern, $data)) {
  92. $channel = preg_replace('/^withdrawAddress@@bank_choose_channel_/', '', $data);
  93. $res = static::chooseChannel($chatId, $messageId, $channel);
  94. $telegram->editMessageText($res);
  95. }
  96. }
  97. public static function onMessage($chatId, $text, $messageId, $stepStatus): null|array
  98. {
  99. return match ($stepStatus) {
  100. StepStatus::QB_INPUT_ALIAS => QianBaoWithdrawService::inputAliAs($chatId, $text, $messageId),
  101. StepStatus::INPUT_WITHDRAW_QB_MONEY => QianBaoWithdrawService::inputQbAmount($chatId, $text, $messageId),
  102. StepStatus::QB_INPUT_BANK_NAME => QianBaoWithdrawService::inputBankName($chatId, $text, $messageId),
  103. StepStatus::QB_INPUT_CARD_NO => QianBaoWithdrawService::inputCardNo($chatId, $text, $messageId),
  104. StepStatus::QB_INPUT_ACCOUNT => QianBaoWithdrawService::inputAccount($chatId, $text, $messageId),
  105. default => null,
  106. };
  107. }
  108. private static function chooseType($chatId, $messageId)
  109. {
  110. $keyboard = [
  111. [
  112. ['text' => lang("USDT"), 'callback_data' => "withdraw@@apply"],
  113. ],
  114. [
  115. ['text' => lang("银行卡"), 'callback_data' => "withdraw@@qb_choose_bank"],
  116. ],
  117. [
  118. ['text' => lang("支付宝"), 'callback_data' => "withdraw@@qb_choose_aliPay"],
  119. ],
  120. [
  121. ['text' => lang("数字人民币"), 'callback_data' => "withdraw@@qb_choose_digital_RMB"],
  122. ],
  123. [
  124. ['text' => lang("返回"), 'callback_data' => "topUp@@home"],
  125. ],
  126. ];
  127. // $keyboard[] = [
  128. // ['text' => "提现账户管理", 'callback_data' => "withdraw@@banks"],
  129. // ];
  130. return [
  131. 'chat_id' => $chatId,
  132. 'text' => lang("请选择提现方式"),
  133. 'message_id' => $messageId,
  134. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  135. ];
  136. }
  137. private static function showBanks($chatId, $messageId, $type)
  138. {
  139. $channel = '';
  140. $card = "";
  141. $text = "";
  142. switch ($type) {
  143. case "bank":
  144. $card = lang("银行卡");
  145. $text = lang("请选择提现的银行卡");
  146. $channel = 'DF001';
  147. break;
  148. case "aliPay":
  149. $card = lang("支付宝");
  150. $text = lang("请选择提现的支付宝");
  151. $channel = "DF002";
  152. break;
  153. case "digital_RMB":
  154. $card = lang("数字人民币");
  155. $text = lang("请选择提现的账户");
  156. $channel = "DF005";
  157. break;
  158. }
  159. $list = Bank::where('channel', $channel)->get();
  160. $keyboard = [];
  161. foreach ($list as $item) {
  162. $keyboard[] = [['text' => $item->getAlias(), 'callback_data' => "withdrawAddress@@choose_qb_{$item->getId()}"]];
  163. }
  164. $keyboard[] = [
  165. ['text' => "{$card}" . lang("管理"), 'callback_data' => "withdraw@@management_{$channel}"],
  166. ['text' => lang("返回"), 'callback_data' => "withdraw@@qb_show_channel"]];
  167. return [
  168. 'chat_id' => $chatId,
  169. 'text' => $text,
  170. 'message_id' => $messageId,
  171. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  172. ];
  173. }
  174. //钱宝账单
  175. private static function bill($chatId, $firstName, $messageId, $page = 1, $limit = 5)
  176. {
  177. $list = PaymentOrder::where('member_id', $chatId)
  178. ->where('type', 2)
  179. ->orderByDesc('created_at')
  180. ->forPage($page, $limit)
  181. ->get();
  182. $count = PaymentOrder::where('member_id', $chatId)
  183. ->where('type', 2)
  184. ->count();
  185. $text = "👤 {$firstName}({$chatId}) " . lang('钱宝提现记录') . "\n\n";
  186. foreach ($list as $item) {
  187. $amount = floatval($item->amount);
  188. $amount = $item->type == 2 ? "➖ {$amount}" : "➕ $amount";
  189. $text .= "-------------------------------------\n";
  190. $text .= "{$amount} \n";
  191. $text .= lang("订单号") . ":{$item->order_no}\n";
  192. $text .= lang('银行') . ":{$item->bank_name}\n";
  193. $text .= lang("姓名") . ":{$item->account}\n";
  194. $text .= lang("卡号") . ":{$item->card_no}\n";
  195. $status = [lang('待处理'), lang('处理中'), lang('成功'), lang('失败')];
  196. $text .= lang("状态") . ":{$status[$item->status]}\n";
  197. if ($item->remark) {
  198. $text .= lang("说明") . ":{$item->remark}\n";
  199. }
  200. $text .= lang('日期').":{$item->created_at}\n";
  201. }
  202. if ($page > 1) {
  203. $keyboard[] = [
  204. ['text' => lang("👆上一页"), 'callback_data' => "withdraw@@bank_bill_" . ($page - 1)]
  205. ];
  206. }
  207. $allPage = ceil($count / $limit);
  208. if ($allPage > $page) {
  209. if ($page > 1) {
  210. $keyboard[count($keyboard) - 1][] = ['text' => lang("👇下一页"), 'callback_data' => "withdraw@@bank_bill_" . ($page + 1)];
  211. } else {
  212. $keyboard[] = [
  213. ['text' => lang("👇下一页"), 'callback_data' => "withdraw@@bank_bill_" . ($page + 1)]
  214. ];
  215. }
  216. }
  217. $keyboard[] = [
  218. ['text' => lang("返回"), 'callback_data' => "topUp@@home"]
  219. ];
  220. return [
  221. 'chat_id' => $chatId,
  222. 'text' => $text,
  223. 'message_id' => $messageId,
  224. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  225. ];
  226. }
  227. //1.钱宝提现
  228. private static function qbApply($chatId, $messageId)
  229. {
  230. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  231. if ($three_payment_switch != 1) {
  232. $res = WalletService::getBalance($chatId);
  233. $res['message_id'] = $messageId;
  234. return $res;
  235. }
  236. $wallet = Wallet::where('member_id', $chatId)->first();
  237. $temp = floatval($wallet->available_balance);
  238. $text = "请发送提现金额\n";
  239. $text .= "💰 当前余额{$temp} RMB\n";
  240. // Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  241. return [
  242. 'chat_id' => $chatId,
  243. 'text' => $text,
  244. 'message_id' => $messageId,
  245. ];
  246. }
  247. //2.选择银行卡号
  248. private static function chooseBank($chatId, $id)
  249. {
  250. Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  251. Cache::put("{$chatId}_QB_BANK_ID", $id);
  252. return [
  253. 'chat_id' => $chatId,
  254. 'text' => "请输入提现的金额",
  255. ];
  256. }
  257. //3.输入钱宝提现金额
  258. private static function inputQbAmount($chatId, $amount, $messageId)
  259. {
  260. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  261. return [
  262. 'chat_id' => $chatId,
  263. 'text' => "金额输入不正确,请发送提现数字",
  264. 'reply_to_message_id' => $messageId
  265. ];
  266. }
  267. $amount = floatval($amount);
  268. $wallet = Wallet::where('member_id', $chatId)->first();
  269. $temp = floatval($wallet->available_balance);
  270. if ($amount > $temp) {
  271. return [
  272. 'chat_id' => $chatId,
  273. 'text' => lang("可用余额不足,请重试"),
  274. 'reply_to_message_id' => $messageId
  275. ];
  276. }
  277. if ($amount < 100) {
  278. return [
  279. 'chat_id' => $chatId,
  280. 'text' => lang("提现不能少于100 RMB,请重试"),
  281. 'reply_to_message_id' => $messageId
  282. ];
  283. }
  284. if ($amount > 49999) {
  285. return [
  286. 'chat_id' => $chatId,
  287. 'text' => lang("最多提现 49999 RMB,请重试"),
  288. 'reply_to_message_id' => $messageId
  289. ];
  290. }
  291. $bankId = Cache::get("{$chatId}_QB_BANK_ID");
  292. $bank = Bank::where('id', $bankId)->first();
  293. $text = "";
  294. switch ($bank->getChannel()) {
  295. case "DF001":
  296. $text = "银行卡提现确认\n";
  297. $text .= "开户行:{$bank->getBankName()}\n";
  298. $text .= "姓名:{$bank->getAccount()}\n";
  299. $text .= "提现账号:{$bank->getCardNo()}\n";
  300. $text .= "提现金额:{$amount}\n";
  301. break;
  302. case "DF002":
  303. $text = "支付宝提现确认\n";
  304. $text .= "姓名:{$bank->getAccount()}\n";
  305. $text .= "提现账号:{$bank->getCardNo()}\n";
  306. $text .= "提现金额:{$amount}\n";
  307. break;
  308. case "DF005":
  309. $text = "数字人民币提现确认\n";
  310. $text .= "姓名:{$bank->getAccount()}\n";
  311. $text .= "提现账号:{$bank->getCardNo()}\n";
  312. $text .= "提现金额:{$amount}\n";
  313. break;
  314. }
  315. $keyboard = [
  316. [
  317. ['text' => "确认", 'callback_data' => 'withdraw@@qb_confirm']
  318. ],
  319. [
  320. ['text' => '❌取消', 'callback_data' => "message@@close"]
  321. ]];
  322. Cache::put("{$chatId}_WITHDRAW_QB_MONEY", $amount);
  323. return [
  324. 'chat_id' => $chatId,
  325. 'text' => $text,
  326. // 'reply_to_message_id' => $messageId,
  327. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  328. ];
  329. }
  330. private static function confirm($chatId, $messageId)
  331. {
  332. $id = Cache::get("{$chatId}_QB_BANK_ID");
  333. $bank = Bank::where('id', $id)->first();
  334. $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY");
  335. $res = PaymentOrderService::createPayout($chatId, $amount, $bank->getChannel(), $bank->getBankName(), $bank->getAccount(), $bank->getCardNo());
  336. $res['message_id'] = $messageId;
  337. return $res;
  338. }
  339. //银行卡管理
  340. private static function banks($chatId, $messageId, $channel = ""): array
  341. {
  342. switch ($channel) {
  343. case "DF001":
  344. $text = "银行卡列表\n";
  345. break;
  346. case "DF002":
  347. $text = "支付宝账户列表\n";
  348. break;
  349. case "DF005":
  350. $text = "数字人民币账户列表\n";
  351. break;
  352. }
  353. $list = Bank::where('member_id', $chatId)
  354. ->where("channel", $channel)
  355. ->get();
  356. $keyboard = [];
  357. foreach ($list as $item) {
  358. $keyboard[] = [['text' => $item->getAlias(), 'callback_data' => "withdrawAddress@@bank_detail{$item->getId()}"]];
  359. }
  360. if (count($list) < 5) {
  361. $keyboard[] = [['text' => "➕ 添加", 'callback_data' => "withdrawAddress@@bank_choose_channel_{$channel}"], ['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  362. } else {
  363. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  364. }
  365. return [
  366. 'chat_id' => $chatId,
  367. 'text' => $text,
  368. 'message_id' => $messageId,
  369. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  370. ];
  371. }
  372. //银行卡详情
  373. private static function bankDetails($chatId, $messageId, $id)
  374. {
  375. $bank = Bank::where('id', $id)
  376. ->where('member_id', $chatId)->first();
  377. switch ($bank->getChannel()) {
  378. case "DF001":
  379. $text = "*银行卡管理*\n\n";
  380. $text .= "姓名:{$bank->getAccount()}\n";
  381. $text .= "银行:{$bank->getAccount()}\n";
  382. $text .= "卡号:{$bank->getCardNo()}\n";
  383. break;
  384. case "DF002":
  385. $text = "*支付宝管理*\n\n";
  386. $text .= "姓名:{$bank->getAccount()}\n";
  387. $text .= "银行:{$bank->getBankName()}\n";
  388. $text .= "账号:{$bank->getCardNo()}\n";
  389. break;
  390. default:
  391. $text = "*银行卡管理*\n\n";
  392. $text .= "姓名:{$bank->getAccount()}\n";
  393. $text .= "银行:{$bank->getBankName()}\n";
  394. $text .= "卡号:{$bank->getCardNo()}\n";
  395. break;
  396. }
  397. $keyboard = [
  398. [['text' => '❌删除', 'callback_data' => "withdraw@@bank_del_{$id}"], ['text' => '↩️返回列表', 'callback_data' => "withdraw@@management_{$bank->getChannel()}"]],
  399. ];
  400. return [
  401. 'chat_id' => $chatId,
  402. 'parse_mode' => 'MarkdownV2',
  403. 'text' => $text,
  404. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  405. 'message_id' => $messageId
  406. ];
  407. }
  408. //删除银行卡
  409. private static function bankDelete($chatId, $messageId, $id): array
  410. {
  411. $channel = Bank::where('id', $id)
  412. ->where('member_id', $chatId)->first()->getChannel();
  413. Bank::where('id', $id)->where('member_id', $chatId)->delete();
  414. return static::banks($chatId, $messageId, $channel);
  415. }
  416. //选择通道
  417. private static function chooseChannel($chatId, $messageId, $channel)
  418. {
  419. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  420. switch ($channel) {
  421. case "DF001"://银行卡
  422. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  423. return [
  424. 'chat_id' => $chatId,
  425. 'text' => "请输入开户银行卡开户名称",
  426. 'message_id' => $messageId,
  427. ];
  428. case "DF002"://支付宝
  429. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  430. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  431. return [
  432. 'chat_id' => $chatId,
  433. 'text' => "请输入支付宝账号",
  434. 'message_id' => $messageId,
  435. ];
  436. case "DF005"://数字人民币
  437. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '数字人民币');
  438. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  439. return [
  440. 'chat_id' => $chatId,
  441. 'text' => "请输入数字人民币账号",
  442. 'message_id' => $messageId,
  443. ];
  444. default:
  445. return [
  446. 'chat_id' => $chatId,
  447. 'text' => "选择通道错误",
  448. 'message_id' => $messageId,
  449. ];
  450. }
  451. }
  452. //输入银行名称
  453. private static function inputBankName($chatId, $bankName, $messageId): array
  454. {
  455. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  456. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  457. return [
  458. 'chat_id' => $chatId,
  459. 'text' => "请输入银行卡号",
  460. 'message_id' => $messageId,
  461. ];
  462. }
  463. //输入卡号
  464. private static function inputCardNo($chatId, $cardNo, $messageId): array
  465. {
  466. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  467. if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
  468. return [
  469. 'chat_id' => $chatId,
  470. 'text' => "输入的银行卡号有误,请重新输入",
  471. 'reply_to_message_id' => $messageId,
  472. ];
  473. }
  474. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  475. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  476. return [
  477. 'chat_id' => $chatId,
  478. 'text' => "请输入姓名",
  479. 'message_id' => $messageId,
  480. ];
  481. }
  482. //输入姓名
  483. private static function inputAccount($chatId, $account, $messageId): array
  484. {
  485. Cache::put("{$chatId}_QB_ACCOUNT", $account);
  486. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ALIAS);
  487. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  488. $text = "请输入别名";
  489. switch ($channel) {
  490. case "DF005":
  491. case "DF001":
  492. $text = "请输入账号别名";
  493. break;
  494. }
  495. return [
  496. 'chat_id' => $chatId,
  497. 'text' => $text,
  498. 'message_id' => $messageId,
  499. ];
  500. }
  501. //输入别名,并保存到数据库
  502. private static function inputAliAs($chatId, $alias, $messageId): array
  503. {
  504. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  505. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  506. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  507. $account = Cache::get("{$chatId}_QB_ACCOUNT");
  508. Bank::create([
  509. 'member_id' => $chatId,
  510. 'account' => $account,
  511. 'channel' => $channel,
  512. 'card_no' => $cardNo,
  513. 'bank_name' => $bankName,
  514. 'alias' => $alias
  515. ]);
  516. Cache::delete(get_step_key($chatId));
  517. return static::banks($chatId, $messageId, $channel);
  518. }
  519. }