QianBaoWithdrawService.php 17 KB

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