QianBaoWithdrawService.php 17 KB

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