QianBaoWithdrawService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. case "DF005":
  297. break;
  298. }
  299. $keyboard = [
  300. [
  301. ['text' => "确认", 'callback_data' => 'withdraw@@qb_confirm']
  302. ],
  303. [
  304. ['text' => '❌取消', 'callback_data' => "message@@close"]
  305. ]];
  306. Cache::put("{$chatId}_WITHDRAW_QB_MONEY", $amount);
  307. return [
  308. 'chat_id' => $chatId,
  309. 'text' => $text,
  310. 'reply_to_message_id' => $messageId,
  311. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  312. ];
  313. }
  314. private static function confirm($chatId, $messageId)
  315. {
  316. $id = Cache::get("{$chatId}_QB_BANK_ID");
  317. $bank = Bank::where('id', $id)->first();
  318. $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY");
  319. $res = PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
  320. $res['message_id'] = $messageId;
  321. return $res;
  322. }
  323. //银行卡管理
  324. private static function banks($chatId, $messageId)
  325. {
  326. $text = "💳️ 银行卡管理\n";
  327. $text .= "--------------------------\n";
  328. $list = Bank::where('member_id', $chatId)
  329. ->get();
  330. $keyboard = [];
  331. foreach ($list as $item) {
  332. $keyboard[] = [['text' => "{$item->bank_name}({$item->card_no})", 'callback_data' => "withdrawAddress@@bank_detail{$item->id}"]];
  333. }
  334. if (count($list) < 5) {
  335. $keyboard[] = [['text' => "➕ 添加银行卡", 'callback_data' => "withdrawAddress@@bank_add"]];
  336. }
  337. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  338. return [
  339. 'chat_id' => $chatId,
  340. 'text' => $text,
  341. 'message_id' => $messageId,
  342. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  343. ];
  344. }
  345. //银行卡详情
  346. private static function bankDetails($chatId, $messageId, $id)
  347. {
  348. $text = "*银行卡管理*\n\n";
  349. $bank = Bank::where('id', $id)
  350. ->where('member_id', $chatId)->first();
  351. switch ($bank->channel) {
  352. case "DF001":
  353. $text .= "姓名:{$bank->account}\n";
  354. $text .= "银行:{$bank->bank_name}\n";
  355. $text .= "卡号:{$bank->card_no}\n";
  356. break;
  357. case "DF002":
  358. $text .= "姓名:{$bank->account}\n";
  359. $text .= "银行:{$bank->bank_name}\n";
  360. $text .= "账号:{$bank->card_no}\n";
  361. break;
  362. default:
  363. $text .= "姓名:{$bank->account}\n";
  364. $text .= "银行:{$bank->bank_name}\n";
  365. $text .= "卡号:{$bank->card_no}\n";
  366. break;
  367. }
  368. $keyboard = [
  369. [['text' => '❌删除该地址', 'callback_data' => "withdraw@@bank_del_{$id}"]],
  370. [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@banks']]
  371. ];
  372. return [
  373. 'chat_id' => $chatId,
  374. 'parse_mode' => 'MarkdownV2',
  375. 'text' => $text,
  376. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  377. 'message_id' => $messageId
  378. ];
  379. }
  380. //删除银行卡
  381. private static function bankDelete($chatId, $messageId, $id)
  382. {
  383. Bank::where('id', $id)
  384. ->where('member_id', $chatId)->delete();
  385. return static::banks($chatId, $messageId);
  386. }
  387. //添加银行卡
  388. private static function addBank($chatId, $messageId)
  389. {
  390. $text = "请选择 提现通道\n";
  391. $keyboard = [
  392. [
  393. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  394. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  395. ],
  396. [
  397. ['text' => '❌取消', 'callback_data' => "message@@close"]
  398. ]
  399. ];
  400. return [
  401. 'chat_id' => $chatId,
  402. 'text' => $text,
  403. 'message_id' => $messageId,
  404. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  405. ];
  406. }
  407. //选择通道
  408. private static function chooseChannel($chatId, $messageId, $channel)
  409. {
  410. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  411. switch ($channel) {
  412. case "DF002"://支付宝
  413. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  414. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  415. return [
  416. 'chat_id' => $chatId,
  417. 'text' => "请输入支付宝账号",
  418. 'message_id' => $messageId,
  419. ];
  420. break;
  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. break;
  429. default:
  430. return [
  431. 'chat_id' => $chatId,
  432. 'text' => "选择通道错误",
  433. 'message_id' => $messageId,
  434. ];
  435. break;
  436. }
  437. }
  438. //输入银行名称
  439. private static function inputBankName($chatId, $bankName, $messageId)
  440. {
  441. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  442. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  443. return [
  444. 'chat_id' => $chatId,
  445. 'text' => "请输入银行卡号",
  446. 'message_id' => $messageId,
  447. ];
  448. }
  449. //输入卡号
  450. private static function inputCardNo($chatId, $cardNo, $messageId)
  451. {
  452. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  453. if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
  454. return [
  455. 'chat_id' => $chatId,
  456. 'text' => "输入的银行卡号有误,请重新输入",
  457. 'reply_to_message_id' => $messageId,
  458. ];
  459. }
  460. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  461. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  462. return [
  463. 'chat_id' => $chatId,
  464. 'text' => "请输入姓名",
  465. 'message_id' => $messageId,
  466. ];
  467. }
  468. //输入姓名,并保存到数据库
  469. private static function inputAccount($chatId, $account, $messageId)
  470. {
  471. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  472. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  473. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  474. Bank::create([
  475. 'member_id' => $chatId,
  476. 'account' => $account,
  477. 'channel' => $channel,
  478. 'card_no' => $cardNo,
  479. 'bank_name' => $bankName
  480. ]);
  481. return static::banks($chatId, $messageId);
  482. }
  483. }