QianBaoWithdrawService.php 15 KB

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