QianBaoWithdrawService.php 18 KB

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