QianBaoWithdrawService.php 18 KB

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