QianBaoWithdrawService.php 18 KB

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