QianBaoWithdrawService.php 19 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. 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 = "/^withdraw@@bank_add.*$/";
  71. // if (preg_match($pattern, $data)) {
  72. // $channel = preg_replace('/^withdraw@@bank_add/', '', $data);
  73. // $res = QianBaoWithdrawService::chooseChannel($chatId, $messageId,$channel);
  74. // $telegram->editMessageText($res);
  75. // }
  76. //银行卡详情
  77. $pattern = "/^withdrawAddress@@bank_detail\d+$/";
  78. if (preg_match($pattern, $data)) {
  79. $id = preg_replace('/^withdrawAddress@@bank_detail/', '', $data);
  80. $res = static::bankDetails($chatId, $messageId, $id);
  81. $telegram->editMessageText($res);
  82. }
  83. $pattern = "/^withdraw@@bank_del_\d+$/";
  84. if (preg_match($pattern, $data)) {
  85. $id = preg_replace('/^withdraw@@bank_del_/', '', $data);
  86. $res = static::bankDelete($chatId, $messageId, $id);
  87. $telegram->editMessageText($res);
  88. }
  89. $pattern = "/^withdrawAddress@@bank_choose_channel_.*$/";
  90. if (preg_match($pattern, $data)) {
  91. $channel = preg_replace('/^withdrawAddress@@bank_choose_channel_/', '', $data);
  92. $res = static::chooseChannel($chatId, $messageId, $channel);
  93. $telegram->editMessageText($res);
  94. }
  95. }
  96. public static function onMessage($chatId, $text, $messageId, $stepStatus)
  97. {
  98. switch ($stepStatus) {
  99. case StepStatus::INPUT_WITHDRAW_QB_MONEY://输入提现金额
  100. $res = QianBaoWithdrawService::inputQbAmount($chatId, $text, $messageId);
  101. return $res;
  102. break;
  103. case StepStatus::QB_INPUT_BANK_NAME://输入银行名称
  104. $res = QianBaoWithdrawService::inputBankName($chatId, $text, $messageId);
  105. return $res;
  106. case StepStatus::QB_INPUT_CARD_NO://输入银行卡号/支付宝账号
  107. $res = QianBaoWithdrawService::inputCardNo($chatId, $text, $messageId);
  108. return $res;
  109. break;
  110. case StepStatus::QB_INPUT_ACCOUNT://输入姓名
  111. $res = QianBaoWithdrawService::inputAccount($chatId, $text, $messageId);
  112. return $res;
  113. break;
  114. }
  115. return null;
  116. }
  117. private static function chooseType($chatId, $messageId)
  118. {
  119. $keyboard = [];
  120. $keyboard[] = [
  121. ['text' => "银行卡", 'callback_data' => "withdraw@@qb_choose_bank"],
  122. ['text' => "支付宝", 'callback_data' => "withdraw@@qb_choose_aliPay"],
  123. ];
  124. $keyboard[] = [
  125. ['text' => "数字人民币", 'callback_data' => "withdraw@@qb_choose_digital_RMB"],
  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' => "topUp@@home"]];
  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. switch ($bank->channel) {
  293. case "DF001":
  294. $text = "银行卡提现确认\n";
  295. break;
  296. case "DF002":
  297. $text = "支付宝提现确认\n";
  298. $text .= "姓名:{$bank->account}\n";
  299. $text .= "提现账号:{$bank->card_no}\n";
  300. $text .= "提现金额:{$amount}\n";
  301. break;
  302. case "DF005":
  303. break;
  304. }
  305. $keyboard = [
  306. [
  307. ['text' => "确认", 'callback_data' => 'withdraw@@qb_confirm']
  308. ],
  309. [
  310. ['text' => '❌取消', 'callback_data' => "message@@close"]
  311. ]];
  312. Cache::put("{$chatId}_WITHDRAW_QB_MONEY", $amount);
  313. return [
  314. 'chat_id' => $chatId,
  315. 'text' => $text,
  316. 'reply_to_message_id' => $messageId,
  317. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  318. ];
  319. }
  320. private static function confirm($chatId, $messageId)
  321. {
  322. $id = Cache::get("{$chatId}_QB_BANK_ID");
  323. $bank = Bank::where('id', $id)->first();
  324. $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY");
  325. $res = PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
  326. $res['message_id'] = $messageId;
  327. return $res;
  328. }
  329. //银行卡管理
  330. private static function banks($chatId, $messageId, $channel)
  331. {
  332. switch ($channel) {
  333. case "DF001":
  334. $text = "银行卡列表\n";
  335. break;
  336. case "DF002":
  337. $text = "支付宝账户列表\n";
  338. break;
  339. case "DF005":
  340. $text = "数字人民币账户列表\n";
  341. break;
  342. }
  343. $list = Bank::where('member_id', $chatId)
  344. ->where("channel", $channel)
  345. ->get();
  346. $keyboard = [];
  347. foreach ($list as $item) {
  348. $keyboard[] = [['text' => $item->alias, 'callback_data' => "withdrawAddress@@bank_detail{$item->id}"]];
  349. }
  350. if (count($list) < 5) {
  351. $keyboard[] = [['text' => "➕ 添加", 'callback_data' => "withdrawAddress@@bank_choose_channel_{$channel}"], ['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  352. } else {
  353. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  354. }
  355. return [
  356. 'chat_id' => $chatId,
  357. 'text' => $text,
  358. 'message_id' => $messageId,
  359. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  360. ];
  361. }
  362. //银行卡详情
  363. private static function bankDetails($chatId, $messageId, $id)
  364. {
  365. $bank = Bank::where('id', $id)
  366. ->where('member_id', $chatId)->first();
  367. switch ($bank->channel) {
  368. case "DF001":
  369. $text = "*银行卡管理*\n\n";
  370. $text .= "姓名:{$bank->account}\n";
  371. $text .= "银行:{$bank->bank_name}\n";
  372. $text .= "卡号:{$bank->card_no}\n";
  373. break;
  374. case "DF002":
  375. $text = "*支付宝管理*\n\n";
  376. $text .= "姓名:{$bank->account}\n";
  377. $text .= "银行:{$bank->bank_name}\n";
  378. $text .= "账号:{$bank->card_no}\n";
  379. break;
  380. default:
  381. $text = "*银行卡管理*\n\n";
  382. $text .= "姓名:{$bank->account}\n";
  383. $text .= "银行:{$bank->bank_name}\n";
  384. $text .= "卡号:{$bank->card_no}\n";
  385. break;
  386. }
  387. $keyboard = [
  388. [['text' => '❌删除', 'callback_data' => "withdraw@@bank_del_{$id}"], ['text' => '↩️返回列表', 'callback_data' => "withdraw@@management_{$bank->channel}"]],
  389. ];
  390. return [
  391. 'chat_id' => $chatId,
  392. 'parse_mode' => 'MarkdownV2',
  393. 'text' => $text,
  394. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  395. 'message_id' => $messageId
  396. ];
  397. }
  398. //删除银行卡
  399. private static function bankDelete($chatId, $messageId, $id)
  400. {
  401. Bank::where('id', $id)
  402. ->where('member_id', $chatId)->delete();
  403. return static::banks($chatId, $messageId);
  404. }
  405. //添加银行卡
  406. private static function addBank($chatId, $messageId)
  407. {
  408. $text = "请选择 提现通道\n";
  409. $keyboard = [
  410. [
  411. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  412. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  413. ],
  414. [
  415. ['text' => '❌取消', 'callback_data' => "message@@close"]
  416. ]
  417. ];
  418. return [
  419. 'chat_id' => $chatId,
  420. 'text' => $text,
  421. 'message_id' => $messageId,
  422. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  423. ];
  424. }
  425. //选择通道
  426. private static function chooseChannel($chatId, $messageId, $channel)
  427. {
  428. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  429. switch ($channel) {
  430. case "DF002"://支付宝
  431. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  432. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  433. return [
  434. 'chat_id' => $chatId,
  435. 'text' => "请输入支付宝账号",
  436. 'message_id' => $messageId,
  437. ];
  438. break;
  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. break;
  447. default:
  448. return [
  449. 'chat_id' => $chatId,
  450. 'text' => "选择通道错误",
  451. 'message_id' => $messageId,
  452. ];
  453. break;
  454. }
  455. }
  456. //输入银行名称
  457. private static function inputBankName($chatId, $bankName, $messageId)
  458. {
  459. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  460. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  461. return [
  462. 'chat_id' => $chatId,
  463. 'text' => "请输入银行卡号",
  464. 'message_id' => $messageId,
  465. ];
  466. }
  467. //输入卡号
  468. private static function inputCardNo($chatId, $cardNo, $messageId)
  469. {
  470. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  471. if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
  472. return [
  473. 'chat_id' => $chatId,
  474. 'text' => "输入的银行卡号有误,请重新输入",
  475. 'reply_to_message_id' => $messageId,
  476. ];
  477. }
  478. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  479. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  480. return [
  481. 'chat_id' => $chatId,
  482. 'text' => "请输入姓名",
  483. 'message_id' => $messageId,
  484. ];
  485. }
  486. //输入姓名,并保存到数据库
  487. private static function inputAccount($chatId, $account, $messageId)
  488. {
  489. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  490. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  491. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  492. Bank::create([
  493. 'member_id' => $chatId,
  494. 'account' => $account,
  495. 'channel' => $channel,
  496. 'card_no' => $cardNo,
  497. 'bank_name' => $bankName
  498. ]);
  499. return static::banks($chatId, $messageId);
  500. }
  501. }