QianBaoWithdrawService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. // $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY", '');
  244. // if (!$amount) return WalletService::getBalance($chatId);
  245. Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  246. Cache::put("{$chatId}_QB_BANK_ID", $id);
  247. return [
  248. 'chat_id' => $chatId,
  249. 'text' => "请输入提现的金额",
  250. ];
  251. // $bank = Bank::where('id', $id)->first();
  252. // $result = PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
  253. // return $result;
  254. // $text = "提交成功\n";
  255. // $text .= "结果将在稍后通知您,请留意通知!!!";
  256. }
  257. //3.输入钱宝提现金额
  258. private static function inputQbAmount($chatId, $amount, $messageId)
  259. {
  260. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  261. return [
  262. 'chat_id' => $chatId,
  263. 'text' => "金额输入不正确,请发送提现数字",
  264. 'reply_to_message_id' => $messageId
  265. ];
  266. }
  267. $amount = floatval($amount);
  268. $wallet = Wallet::where('member_id', $chatId)->first();
  269. $temp = floatval($wallet->available_balance);
  270. if ($amount > $temp) {
  271. return [
  272. 'chat_id' => $chatId,
  273. 'text' => "⚠️可用余额不足,请重试",
  274. 'reply_to_message_id' => $messageId
  275. ];
  276. }
  277. if ($amount < 100) {
  278. return [
  279. 'chat_id' => $chatId,
  280. 'text' => "⚠️提现不能少于100 RMB,请重试",
  281. 'reply_to_message_id' => $messageId
  282. ];
  283. }
  284. if ($amount > 49999) {
  285. return [
  286. 'chat_id' => $chatId,
  287. 'text' => "⚠️最多提现 49999 RMB,请重试",
  288. 'reply_to_message_id' => $messageId
  289. ];
  290. }
  291. $bankId = Cache::get("{$chatId}_QB_BANK_ID");
  292. $bank = Bank::where('id', $bankId)->first();
  293. switch ($bank->channel) {
  294. case "DF001":
  295. $text = "银行卡提现确认\n";
  296. break;
  297. case "DF002":
  298. $text = "支付宝提现确认\n";
  299. $text .= "姓名:{$bank->account}\n";
  300. $text .= "提现账号:{$bank->card_no}\n";
  301. $text .= "提现金额:{$amount}\n";
  302. break;
  303. }
  304. $keyboard = [
  305. [
  306. ['text' => "确认", 'callback_data' => 'withdraw@@qb_confirm']
  307. ],
  308. [
  309. ['text' => '❌取消', 'callback_data' => "message@@close"]
  310. ]];
  311. Cache::put("{$chatId}_WITHDRAW_QB_MONEY", $amount);
  312. return [
  313. 'chat_id' => $chatId,
  314. 'text' => $text,
  315. 'reply_to_message_id' => $messageId,
  316. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  317. ];
  318. }
  319. private static function confirm($chatId, $messageId)
  320. {
  321. $id = Cache::get("{$chatId}_QB_BANK_ID");
  322. $bank = Bank::where('id', $id)->first();
  323. $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY");
  324. return PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
  325. }
  326. //银行卡管理
  327. private static function banks($chatId, $messageId)
  328. {
  329. $text = "💳️ 银行卡管理\n";
  330. $text .= "--------------------------\n";
  331. $list = Bank::where('member_id', $chatId)
  332. ->get();
  333. $keyboard = [];
  334. foreach ($list as $item) {
  335. $keyboard[] = [['text' => "{$item->bank_name}({$item->card_no})", 'callback_data' => "withdrawAddress@@bank_detail{$item->id}"]];
  336. }
  337. if (count($list) < 5) {
  338. $keyboard[] = [['text' => "➕ 添加银行卡", 'callback_data' => "withdrawAddress@@bank_add"]];
  339. }
  340. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  341. return [
  342. 'chat_id' => $chatId,
  343. 'text' => $text,
  344. 'message_id' => $messageId,
  345. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  346. ];
  347. }
  348. //银行卡详情
  349. private static function bankDetails($chatId, $messageId, $id)
  350. {
  351. $text = "*银行卡管理*\n\n";
  352. $bank = Bank::where('id', $id)
  353. ->where('member_id', $chatId)->first();
  354. switch ($bank->channel) {
  355. case "DF001":
  356. $text .= "姓名:{$bank->account}\n";
  357. $text .= "银行:{$bank->bank_name}\n";
  358. $text .= "卡号:{$bank->card_no}\n";
  359. break;
  360. case "DF002":
  361. $text .= "姓名:{$bank->account}\n";
  362. $text .= "银行:{$bank->bank_name}\n";
  363. $text .= "账号:{$bank->card_no}\n";
  364. break;
  365. default:
  366. $text .= "姓名:{$bank->account}\n";
  367. $text .= "银行:{$bank->bank_name}\n";
  368. $text .= "卡号:{$bank->card_no}\n";
  369. break;
  370. }
  371. $keyboard = [
  372. [['text' => '❌删除该地址', 'callback_data' => "withdraw@@bank_del_{$id}"]],
  373. [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@banks']]
  374. ];
  375. return [
  376. 'chat_id' => $chatId,
  377. 'parse_mode' => 'MarkdownV2',
  378. 'text' => $text,
  379. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  380. 'message_id' => $messageId
  381. ];
  382. }
  383. //删除银行卡
  384. private static function bankDelete($chatId, $messageId, $id)
  385. {
  386. Bank::where('id', $id)
  387. ->where('member_id', $chatId)->delete();
  388. return static::banks($chatId, $messageId);
  389. }
  390. //添加银行卡
  391. private static function addBank($chatId, $messageId)
  392. {
  393. $text = "请选择 提现通道\n";
  394. $keyboard = [
  395. [
  396. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  397. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  398. ],
  399. [
  400. ['text' => '❌取消', 'callback_data' => "message@@close"]
  401. ]
  402. ];
  403. return [
  404. 'chat_id' => $chatId,
  405. 'text' => $text,
  406. 'message_id' => $messageId,
  407. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  408. ];
  409. }
  410. //选择通道
  411. private static function chooseChannel($chatId, $messageId, $channel)
  412. {
  413. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  414. switch ($channel) {
  415. case "DF002"://支付宝
  416. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  417. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  418. return [
  419. 'chat_id' => $chatId,
  420. 'text' => "请输入支付宝账号",
  421. 'message_id' => $messageId,
  422. ];
  423. break;
  424. case "DF001"://银行卡
  425. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  426. return [
  427. 'chat_id' => $chatId,
  428. 'text' => "请输入银行名称",
  429. 'message_id' => $messageId,
  430. ];
  431. break;
  432. default:
  433. return [
  434. 'chat_id' => $chatId,
  435. 'text' => "选择通道错误",
  436. 'message_id' => $messageId,
  437. ];
  438. break;
  439. }
  440. }
  441. //输入银行名称
  442. private static function inputBankName($chatId, $bankName, $messageId)
  443. {
  444. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  445. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  446. return [
  447. 'chat_id' => $chatId,
  448. 'text' => "请输入银行卡号",
  449. 'message_id' => $messageId,
  450. ];
  451. }
  452. //输入卡号
  453. private static function inputCardNo($chatId, $cardNo, $messageId)
  454. {
  455. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  456. if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
  457. return [
  458. 'chat_id' => $chatId,
  459. 'text' => "输入的银行卡号有误,请重新输入",
  460. 'reply_to_message_id' => $messageId,
  461. ];
  462. }
  463. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  464. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  465. return [
  466. 'chat_id' => $chatId,
  467. 'text' => "请输入姓名",
  468. 'message_id' => $messageId,
  469. ];
  470. }
  471. //输入姓名,并保存到数据库
  472. private static function inputAccount($chatId, $account, $messageId)
  473. {
  474. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  475. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  476. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  477. Bank::create([
  478. 'member_id' => $chatId,
  479. 'account' => $account,
  480. 'channel' => $channel,
  481. 'card_no' => $cardNo,
  482. 'bank_name' => $bankName
  483. ]);
  484. return static::banks($chatId, $messageId);
  485. }
  486. }