QianBaoWithdrawService.php 18 KB

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