QianBaoWithdrawService.php 16 KB

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