QianBaoWithdrawService.php 18 KB

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