QianBaoWithdrawService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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' => "USDT", 'callback_data' => "withdraw@@apply"],
  113. ],
  114. [
  115. ['text' => "银行卡", 'callback_data' => "withdraw@@qb_choose_bank"],
  116. ],
  117. [
  118. ['text' => "支付宝", 'callback_data' => "withdraw@@qb_choose_aliPay"],
  119. ],
  120. [
  121. ['text' => "数字人民币", 'callback_data' => "withdraw@@qb_choose_digital_RMB"],
  122. ],
  123. [
  124. ['text' => "返回", 'callback_data' => "topUp@@home"],
  125. ],
  126. ];
  127. // $keyboard[] = [
  128. // ['text' => "提现账户管理", 'callback_data' => "withdraw@@banks"],
  129. // ];
  130. return [
  131. 'chat_id' => $chatId,
  132. 'text' => "请选择提现方式",
  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. switch ($type) {
  142. case "bank":
  143. $card = "银行卡";
  144. $text = "请选择提现的银行卡";
  145. $channel = 'DF001';
  146. break;
  147. case "aliPay":
  148. $card = "支付宝";
  149. $text = "请选择提现的支付宝";
  150. $channel = "DF002";
  151. break;
  152. case "digital_RMB":
  153. $card = "数字人民币";
  154. $text = "请选择提现的账户";
  155. $channel = "DF005";
  156. break;
  157. }
  158. $list = Bank::where('channel', $channel)->get();
  159. $keyboard = [];
  160. foreach ($list as $item) {
  161. $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@choose_qb_{$item->id}"]];
  162. }
  163. $keyboard[] = [
  164. ['text' => "{$card}管理", 'callback_data' => "withdraw@@management_{$channel}"],
  165. ['text' => "返回", 'callback_data' => "withdraw@@qb_show_channel"]];
  166. return [
  167. 'chat_id' => $chatId,
  168. 'text' => $text,
  169. 'message_id' => $messageId,
  170. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  171. ];
  172. }
  173. //钱宝账单
  174. private static function bill($chatId, $firstName, $messageId, $page = 1, $limit = 5)
  175. {
  176. $list = PaymentOrder::where('member_id', $chatId)
  177. ->where('type', 2)
  178. ->orderByDesc('created_at')
  179. ->forPage($page, $limit)
  180. ->get();
  181. $count = PaymentOrder::where('member_id', $chatId)
  182. ->where('type', 2)
  183. ->count();
  184. $text = "👤 {$firstName}({$chatId}) 钱宝提现记录\n\n";
  185. foreach ($list as $item) {
  186. $amount = floatval($item->amount);
  187. $amount = $item->type == 2 ? "➖ {$amount}" : "➕ $amount";
  188. $text .= "-------------------------------------\n";
  189. $text .= "{$amount} \n";
  190. $text .= "订单号:{$item->order_no}\n";
  191. $text .= "银行:{$item->bank_name}\n";
  192. $text .= "姓名:{$item->account}\n";
  193. $text .= "卡号:{$item->card_no}\n";
  194. $status = ['待处理', '处理中', '成功', '失败'];
  195. $text .= "状态:{$status[$item->status]}\n";
  196. if ($item->remark) {
  197. $text .= "说明:{$item->remark}\n";
  198. }
  199. $text .= "日期:{$item->created_at}\n";
  200. }
  201. if ($page > 1) {
  202. $keyboard[] = [
  203. ['text' => "👆上一页", 'callback_data' => "withdraw@@bank_bill_" . ($page - 1)]
  204. ];
  205. }
  206. $allPage = ceil($count / $limit);
  207. if ($allPage > $page) {
  208. if ($page > 1) {
  209. $keyboard[count($keyboard) - 1][] = ['text' => "👇下一页", 'callback_data' => "withdraw@@bank_bill_" . ($page + 1)];
  210. } else {
  211. $keyboard[] = [
  212. ['text' => "👇下一页", 'callback_data' => "withdraw@@bank_bill_" . ($page + 1)]
  213. ];
  214. }
  215. }
  216. $keyboard[] = [
  217. ['text' => "返回", 'callback_data' => "topUp@@home"]
  218. ];
  219. return [
  220. 'chat_id' => $chatId,
  221. 'text' => $text,
  222. 'message_id' => $messageId,
  223. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  224. ];
  225. }
  226. //1.钱宝提现
  227. private static function qbApply($chatId, $messageId)
  228. {
  229. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  230. if ($three_payment_switch != 1) {
  231. $res = WalletService::getBalance($chatId);
  232. $res['message_id'] = $messageId;
  233. return $res;
  234. }
  235. $wallet = Wallet::where('member_id', $chatId)->first();
  236. $temp = floatval($wallet->available_balance);
  237. $text = "请发送提现金额\n";
  238. $text .= "💰 当前余额{$temp} RMB\n";
  239. // Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  240. return [
  241. 'chat_id' => $chatId,
  242. 'text' => $text,
  243. 'message_id' => $messageId,
  244. ];
  245. }
  246. //2.选择银行卡号
  247. private static function chooseBank($chatId, $id)
  248. {
  249. Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  250. Cache::put("{$chatId}_QB_BANK_ID", $id);
  251. return [
  252. 'chat_id' => $chatId,
  253. 'text' => "请输入提现的金额",
  254. ];
  255. }
  256. //3.输入钱宝提现金额
  257. private static function inputQbAmount($chatId, $amount, $messageId)
  258. {
  259. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  260. return [
  261. 'chat_id' => $chatId,
  262. 'text' => "金额输入不正确,请发送提现数字",
  263. 'reply_to_message_id' => $messageId
  264. ];
  265. }
  266. $amount = floatval($amount);
  267. $wallet = Wallet::where('member_id', $chatId)->first();
  268. $temp = floatval($wallet->available_balance);
  269. if ($amount > $temp) {
  270. return [
  271. 'chat_id' => $chatId,
  272. 'text' => "⚠️可用余额不足,请重试",
  273. 'reply_to_message_id' => $messageId
  274. ];
  275. }
  276. if ($amount < 100) {
  277. return [
  278. 'chat_id' => $chatId,
  279. 'text' => "⚠️提现不能少于100 RMB,请重试",
  280. 'reply_to_message_id' => $messageId
  281. ];
  282. }
  283. if ($amount > 49999) {
  284. return [
  285. 'chat_id' => $chatId,
  286. 'text' => "⚠️最多提现 49999 RMB,请重试",
  287. 'reply_to_message_id' => $messageId
  288. ];
  289. }
  290. $bankId = Cache::get("{$chatId}_QB_BANK_ID");
  291. $bank = Bank::where('id', $bankId)->first();
  292. $text = "";
  293. switch ($bank->channel) {
  294. case "DF001":
  295. $text = "银行卡提现确认\n";
  296. $text .= "开户行:{$bank->bank_name}\n";
  297. $text .= "姓名:{$bank->account}\n";
  298. $text .= "提现账号:{$bank->card_no}\n";
  299. $text .= "提现金额:{$amount}\n";
  300. break;
  301. case "DF002":
  302. $text = "支付宝提现确认\n";
  303. $text .= "姓名:{$bank->account}\n";
  304. $text .= "提现账号:{$bank->card_no}\n";
  305. $text .= "提现金额:{$amount}\n";
  306. break;
  307. case "DF005":
  308. $text = "数字人民币提现确认\n";
  309. $text .= "姓名:{$bank->account}\n";
  310. $text .= "提现账号:{$bank->card_no}\n";
  311. $text .= "提现金额:{$amount}\n";
  312. break;
  313. }
  314. $keyboard = [
  315. [
  316. ['text' => "确认", 'callback_data' => 'withdraw@@qb_confirm']
  317. ],
  318. [
  319. ['text' => '❌取消', 'callback_data' => "message@@close"]
  320. ]];
  321. Cache::put("{$chatId}_WITHDRAW_QB_MONEY", $amount);
  322. return [
  323. 'chat_id' => $chatId,
  324. 'text' => $text,
  325. // 'reply_to_message_id' => $messageId,
  326. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  327. ];
  328. }
  329. private static function confirm($chatId, $messageId)
  330. {
  331. $id = Cache::get("{$chatId}_QB_BANK_ID");
  332. $bank = Bank::where('id', $id)->first();
  333. $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY");
  334. $res = PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
  335. $res['message_id'] = $messageId;
  336. return $res;
  337. }
  338. //银行卡管理
  339. private static function banks($chatId, $messageId, $channel = ""): array
  340. {
  341. switch ($channel) {
  342. case "DF001":
  343. $text = "银行卡列表\n";
  344. break;
  345. case "DF002":
  346. $text = "支付宝账户列表\n";
  347. break;
  348. case "DF005":
  349. $text = "数字人民币账户列表\n";
  350. break;
  351. }
  352. $list = Bank::where('member_id', $chatId)
  353. ->where("channel", $channel)
  354. ->get();
  355. $keyboard = [];
  356. foreach ($list as $item) {
  357. $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@bank_detail{$item->id}"]];
  358. }
  359. if (count($list) < 5) {
  360. $keyboard[] = [['text' => "➕ 添加", 'callback_data' => "withdrawAddress@@bank_choose_channel_{$channel}"], ['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  361. } else {
  362. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  363. }
  364. return [
  365. 'chat_id' => $chatId,
  366. 'text' => $text,
  367. 'message_id' => $messageId,
  368. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  369. ];
  370. }
  371. //银行卡详情
  372. private static function bankDetails($chatId, $messageId, $id)
  373. {
  374. $bank = Bank::where('id', $id)
  375. ->where('member_id', $chatId)->first();
  376. switch ($bank->channel) {
  377. case "DF001":
  378. $text = "*银行卡管理*\n\n";
  379. $text .= "姓名:{$bank->account}\n";
  380. $text .= "银行:{$bank->bank_name}\n";
  381. $text .= "卡号:{$bank->card_no}\n";
  382. break;
  383. case "DF002":
  384. $text = "*支付宝管理*\n\n";
  385. $text .= "姓名:{$bank->account}\n";
  386. $text .= "银行:{$bank->bank_name}\n";
  387. $text .= "账号:{$bank->card_no}\n";
  388. break;
  389. default:
  390. $text = "*银行卡管理*\n\n";
  391. $text .= "姓名:{$bank->account}\n";
  392. $text .= "银行:{$bank->bank_name}\n";
  393. $text .= "卡号:{$bank->card_no}\n";
  394. break;
  395. }
  396. $keyboard = [
  397. [['text' => '❌删除', 'callback_data' => "withdraw@@bank_del_{$id}"], ['text' => '↩️返回列表', 'callback_data' => "withdraw@@management_{$bank->channel}"]],
  398. ];
  399. return [
  400. 'chat_id' => $chatId,
  401. 'parse_mode' => 'MarkdownV2',
  402. 'text' => $text,
  403. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  404. 'message_id' => $messageId
  405. ];
  406. }
  407. //删除银行卡
  408. private static function bankDelete($chatId, $messageId, $id)
  409. {
  410. Bank::where('id', $id)
  411. ->where('member_id', $chatId)->delete();
  412. return static::banks($chatId, $messageId);
  413. }
  414. //添加银行卡
  415. private static function addBank($chatId, $messageId): array
  416. {
  417. $text = "请选择 提现通道\n";
  418. $keyboard = [
  419. [
  420. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  421. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  422. ],
  423. [
  424. ['text' => '❌取消', 'callback_data' => "message@@close"]
  425. ]
  426. ];
  427. return [
  428. 'chat_id' => $chatId,
  429. 'text' => $text,
  430. 'message_id' => $messageId,
  431. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  432. ];
  433. }
  434. //选择通道
  435. private static function chooseChannel($chatId, $messageId, $channel)
  436. {
  437. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  438. switch ($channel) {
  439. case "DF001"://银行卡
  440. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  441. return [
  442. 'chat_id' => $chatId,
  443. 'text' => "请输入开户银行卡开户名称",
  444. 'message_id' => $messageId,
  445. ];
  446. case "DF002"://支付宝
  447. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  448. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  449. return [
  450. 'chat_id' => $chatId,
  451. 'text' => "请输入支付宝账号",
  452. 'message_id' => $messageId,
  453. ];
  454. case "DF005"://数字人民币
  455. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '数字人民币');
  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. default:
  463. return [
  464. 'chat_id' => $chatId,
  465. 'text' => "选择通道错误",
  466. 'message_id' => $messageId,
  467. ];
  468. }
  469. }
  470. //输入银行名称
  471. private static function inputBankName($chatId, $bankName, $messageId): array
  472. {
  473. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  474. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  475. return [
  476. 'chat_id' => $chatId,
  477. 'text' => "请输入银行卡号",
  478. 'message_id' => $messageId,
  479. ];
  480. }
  481. //输入卡号
  482. private static function inputCardNo($chatId, $cardNo, $messageId): array
  483. {
  484. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  485. if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
  486. return [
  487. 'chat_id' => $chatId,
  488. 'text' => "输入的银行卡号有误,请重新输入",
  489. 'reply_to_message_id' => $messageId,
  490. ];
  491. }
  492. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  493. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  494. return [
  495. 'chat_id' => $chatId,
  496. 'text' => "请输入姓名",
  497. 'message_id' => $messageId,
  498. ];
  499. }
  500. //输入姓名,并保存到数据库
  501. private static function inputAccount($chatId, $account, $messageId)
  502. {
  503. Cache::put("{$chatId}_QB_ACCOUNT", $account);
  504. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ALIAS);
  505. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  506. $text = "请输入别名";
  507. switch ($channel) {
  508. case "DF001":
  509. $text = "请输入账号别名";
  510. break;
  511. case "DF002":
  512. $text = "请输入别名";
  513. break;
  514. case "DF005":
  515. $text = "请输入账号别名";
  516. break;
  517. }
  518. return [
  519. 'chat_id' => $chatId,
  520. 'text' => $text,
  521. 'message_id' => $messageId,
  522. ];
  523. }
  524. private static function inputAliAs($chatId, $alias, $messageId)
  525. {
  526. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  527. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  528. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  529. $account = Cache::get("{$chatId}_QB_ACCOUNT");
  530. Bank::create([
  531. 'member_id' => $chatId,
  532. 'account' => $account,
  533. 'channel' => $channel,
  534. 'card_no' => $cardNo,
  535. 'bank_name' => $bankName,
  536. 'alias' => $alias
  537. ]);
  538. Cache::delete(get_step_key($chatId));
  539. return static::banks($chatId, $messageId, $channel);
  540. }
  541. }