QianBaoWithdrawService.php 15 KB

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