QianBaoWithdrawService.php 19 KB

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