QianBaoWithdrawService.php 18 KB

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