QianBaoWithdrawService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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' => "withdraw@@banks"]];
  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 bankDelete($chatId, $messageId, $id)
  264. {
  265. Bank::where('id', $id)
  266. ->where('member_id', $chatId)->delete();
  267. return static::banks($chatId, $messageId);
  268. }
  269. //银行卡详情
  270. private static function bankDetails($chatId, $messageId, $id)
  271. {
  272. $text = "*银行卡管理*\n\n";
  273. $bank = Bank::where('id', $id)
  274. ->where('member_id', $chatId)->first();
  275. switch ($bank->channel) {
  276. case "DF001":
  277. $text .= "姓名:{$bank->account}\n";
  278. $text .= "银行:{$bank->bank_name}\n";
  279. $text .= "卡号:{$bank->card_no}\n";
  280. break;
  281. case "DF002":
  282. $text .= "姓名:{$bank->account}\n";
  283. $text .= "银行:{$bank->bank_name}\n";
  284. $text .= "账号:{$bank->card_no}\n";
  285. break;
  286. default:
  287. $text .= "姓名:{$bank->account}\n";
  288. $text .= "银行:{$bank->bank_name}\n";
  289. $text .= "卡号:{$bank->card_no}\n";
  290. break;
  291. }
  292. $keyboard = [
  293. [['text' => '❌删除该地址', 'callback_data' => "withdraw@@bank_del_{$id}"]],
  294. [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@banks']]
  295. ];
  296. return [
  297. 'chat_id' => $chatId,
  298. 'parse_mode' => 'MarkdownV2',
  299. 'text' => $text,
  300. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  301. 'message_id' => $messageId
  302. ];
  303. }
  304. private static function addBank($chatId, $messageId)
  305. {
  306. $text = "请选择 提现通道\n";
  307. $keyboard = [
  308. [
  309. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  310. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  311. ],
  312. [
  313. ['text' => '❌取消', 'callback_data' => "message@@close"]
  314. ]
  315. ];
  316. return [
  317. 'chat_id' => $chatId,
  318. 'text' => $text,
  319. 'message_id' => $messageId,
  320. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  321. ];
  322. }
  323. private static function chooseChannel($chatId, $messageId, $channel)
  324. {
  325. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  326. switch ($channel) {
  327. case "DF002"://支付宝
  328. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  329. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  330. return [
  331. 'chat_id' => $chatId,
  332. 'text' => "请输入支付宝账号",
  333. 'message_id' => $messageId,
  334. ];
  335. break;
  336. case "DF001"://银行卡
  337. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  338. return [
  339. 'chat_id' => $chatId,
  340. 'text' => "请输入银行名称",
  341. 'message_id' => $messageId,
  342. ];
  343. break;
  344. default:
  345. return [
  346. 'chat_id' => $chatId,
  347. 'text' => "选择通道错误",
  348. 'message_id' => $messageId,
  349. ];
  350. break;
  351. }
  352. }
  353. private static function inputBankName($chatId, $bankName, $messageId)
  354. {
  355. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  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. }
  363. private static function inputCardNo($chatId, $cardNo, $messageId)
  364. {
  365. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  366. if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
  367. return [
  368. 'chat_id' => $chatId,
  369. 'text' => "输入的银行卡号有误,请重新输入",
  370. 'reply_to_message_id' => $messageId,
  371. ];
  372. }
  373. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  374. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  375. return [
  376. 'chat_id' => $chatId,
  377. 'text' => "请输入姓名",
  378. 'message_id' => $messageId,
  379. ];
  380. }
  381. private static function inputAccount($chatId, $account, $messageId)
  382. {
  383. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  384. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  385. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  386. Bank::create([
  387. 'member_id' => $chatId,
  388. 'account' => $account,
  389. 'channel' => $channel,
  390. 'card_no' => $cardNo,
  391. 'bank_name' => $bankName
  392. ]);
  393. return static::banks($chatId, $messageId);
  394. }
  395. }