QianBaoWithdrawService.php 18 KB

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