QianBaoWithdrawService.php 14 KB

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