QianBaoWithdrawService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. $result = PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
  233. return $result;
  234. // $text = "提交成功\n";
  235. // $text .= "结果将在稍后通知您,请留意通知!!!";
  236. // return [
  237. // 'chat_id' => $chatId,
  238. // 'text' => $text,
  239. // ];
  240. }
  241. //银行卡管理
  242. private static function banks($chatId, $messageId)
  243. {
  244. $text = "💳️ 银行卡管理\n";
  245. $text .= "--------------------------\n";
  246. $list = Bank::where('member_id', $chatId)
  247. ->get();
  248. $keyboard = [];
  249. foreach ($list as $item) {
  250. $keyboard[] = [['text' => "{$item->bank_name}({$item->card_no})", 'callback_data' => "withdrawAddress@@bank_detail{$item->id}"]];
  251. }
  252. if (count($list) < 5) {
  253. $keyboard[] = [['text' => "➕ 添加银行卡", 'callback_data' => "withdrawAddress@@bank_add"]];
  254. }
  255. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "topUp@@home"]];
  256. return [
  257. 'chat_id' => $chatId,
  258. 'text' => $text,
  259. 'message_id' => $messageId,
  260. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  261. ];
  262. }
  263. //银行卡详情
  264. private static function bankDetails($chatId, $messageId, $id)
  265. {
  266. $text = "*银行卡管理*\n\n";
  267. $bank = Bank::where('id', $id)
  268. ->where('member_id', $chatId)->first();
  269. switch ($bank->channel) {
  270. case "DF001":
  271. $text .= "姓名:{$bank->account}\n";
  272. $text .= "银行:{$bank->bank_name}\n";
  273. $text .= "卡号:{$bank->card_no}\n";
  274. break;
  275. case "DF002":
  276. $text .= "姓名:{$bank->account}\n";
  277. $text .= "银行:{$bank->bank_name}\n";
  278. $text .= "账号:{$bank->card_no}\n";
  279. break;
  280. default:
  281. $text .= "姓名:{$bank->account}\n";
  282. $text .= "银行:{$bank->bank_name}\n";
  283. $text .= "卡号:{$bank->card_no}\n";
  284. break;
  285. }
  286. $keyboard = [
  287. [['text' => '❌删除该地址', 'callback_data' => "withdraw@@bank_del_{$id}"]],
  288. [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@banks']]
  289. ];
  290. return [
  291. 'chat_id' => $chatId,
  292. 'parse_mode' => 'MarkdownV2',
  293. 'text' => $text,
  294. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  295. 'message_id' => $messageId
  296. ];
  297. }
  298. //删除银行卡
  299. private static function bankDelete($chatId, $messageId, $id)
  300. {
  301. Bank::where('id', $id)
  302. ->where('member_id', $chatId)->delete();
  303. return static::banks($chatId, $messageId);
  304. }
  305. //添加银行卡
  306. private static function addBank($chatId, $messageId)
  307. {
  308. $text = "请选择 提现通道\n";
  309. $keyboard = [
  310. [
  311. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  312. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  313. ],
  314. [
  315. ['text' => '❌取消', 'callback_data' => "message@@close"]
  316. ]
  317. ];
  318. return [
  319. 'chat_id' => $chatId,
  320. 'text' => $text,
  321. 'message_id' => $messageId,
  322. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  323. ];
  324. }
  325. //选择通道
  326. private static function chooseChannel($chatId, $messageId, $channel)
  327. {
  328. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  329. switch ($channel) {
  330. case "DF002"://支付宝
  331. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  332. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  333. return [
  334. 'chat_id' => $chatId,
  335. 'text' => "请输入支付宝账号",
  336. 'message_id' => $messageId,
  337. ];
  338. break;
  339. case "DF001"://银行卡
  340. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  341. return [
  342. 'chat_id' => $chatId,
  343. 'text' => "请输入银行名称",
  344. 'message_id' => $messageId,
  345. ];
  346. break;
  347. default:
  348. return [
  349. 'chat_id' => $chatId,
  350. 'text' => "选择通道错误",
  351. 'message_id' => $messageId,
  352. ];
  353. break;
  354. }
  355. }
  356. //输入银行名称
  357. private static function inputBankName($chatId, $bankName, $messageId)
  358. {
  359. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  360. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  361. return [
  362. 'chat_id' => $chatId,
  363. 'text' => "请输入银行卡号",
  364. 'message_id' => $messageId,
  365. ];
  366. }
  367. //输入卡号
  368. private static function inputCardNo($chatId, $cardNo, $messageId)
  369. {
  370. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  371. if ($channel === 'DF001' && !preg_match('/^\d+$/', $cardNo)) {
  372. return [
  373. 'chat_id' => $chatId,
  374. 'text' => "输入的银行卡号有误,请重新输入",
  375. 'reply_to_message_id' => $messageId,
  376. ];
  377. }
  378. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  379. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  380. return [
  381. 'chat_id' => $chatId,
  382. 'text' => "请输入姓名",
  383. 'message_id' => $messageId,
  384. ];
  385. }
  386. //输入姓名,并保存到数据库
  387. private static function inputAccount($chatId, $account, $messageId)
  388. {
  389. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  390. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  391. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  392. Bank::create([
  393. 'member_id' => $chatId,
  394. 'account' => $account,
  395. 'channel' => $channel,
  396. 'card_no' => $cardNo,
  397. 'bank_name' => $bankName
  398. ]);
  399. return static::banks($chatId, $messageId);
  400. }
  401. }