QianBaoWithdrawService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\StepStatus;
  4. use App\Models\Address;
  5. use App\Models\Bank;
  6. use App\Models\Config;
  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 = "/^withdrawAddress@@choose_qb_\d+$/";
  29. if (preg_match($pattern, $data)) {
  30. $id = preg_replace('/^withdrawAddress@@choose_qb_/', '', $data);
  31. $res = QianBaoWithdrawService::chooseBank($chatId, $id);
  32. $telegram->deleteMessage([
  33. 'chat_id' => $chatId,
  34. 'message_id' => $messageId,
  35. ]);
  36. $telegram->sendMessage($res);
  37. }
  38. //银行卡管理
  39. if ($data === 'withdraw@@banks') {
  40. $res = QianBaoWithdrawService::banks($chatId, $messageId);
  41. $telegram->editMessageText($res);
  42. }
  43. //银行卡详情
  44. $pattern = "/^withdrawAddress@@bank_detail\d+$/";
  45. if (preg_match($pattern, $data)) {
  46. $id = preg_replace('/^withdrawAddress@@bank_detail/', '', $data);
  47. $res = static::bankDetails($chatId, $messageId, $id);
  48. $telegram->editMessageText($res);
  49. }
  50. $pattern = "/^withdraw@@bank_del_\d+$/";
  51. if (preg_match($pattern, $data)) {
  52. $id = preg_replace('/^withdraw@@bank_del_/', '', $data);
  53. $res = static::bankDelete($chatId, $messageId, $id);
  54. $telegram->editMessageText($res);
  55. }
  56. //添加银行卡
  57. if ($data === "withdrawAddress@@bank_add") {
  58. $res = QianBaoWithdrawService::addBank($chatId, $messageId);
  59. $telegram->editMessageText($res);
  60. }
  61. $pattern = "/^withdrawAddress@@bank_choose_channel_.*$/";
  62. if (preg_match($pattern, $data)) {
  63. $channel = preg_replace('/^withdrawAddress@@bank_choose_channel_/', '', $data);
  64. $res = static::chooseChannel($chatId, $messageId, $channel);
  65. $telegram->editMessageText($res);
  66. }
  67. }
  68. //1.钱宝提现
  69. static function qbApply($chatId, $messageId)
  70. {
  71. $three_payment_switch = Config::where('field', 'three_payment_switch')->first()->val;
  72. if ($three_payment_switch != 1) {
  73. $res = WalletService::getBalance($chatId);
  74. $res['message_id'] = $messageId;
  75. return $res;
  76. }
  77. $wallet = Wallet::where('member_id', $chatId)->first();
  78. $temp = floatval($wallet->available_balance);
  79. $text = "请发送提现金额\n";
  80. $text .= "💰 当前余额{$temp} RMB\n";
  81. Cache::put(get_step_key($chatId), StepStatus::INPUT_WITHDRAW_QB_MONEY);
  82. return [
  83. 'chat_id' => $chatId,
  84. 'text' => $text,
  85. 'message_id' => $messageId,
  86. ];
  87. }
  88. //2.输入钱宝提现金额
  89. static function inputQbAmount($chatId, $amount, $messageId)
  90. {
  91. if (!preg_match('/^\d+(\.\d{1,2})?$/', $amount)) {
  92. return [
  93. 'chat_id' => $chatId,
  94. 'text' => "金额输入不正确,请发送提现数字",
  95. 'reply_to_message_id' => $messageId
  96. ];
  97. }
  98. $amount = floatval($amount);
  99. $wallet = Wallet::where('member_id', $chatId)->first();
  100. $temp = floatval($wallet->available_balance);
  101. if ($amount > $temp) {
  102. return [
  103. 'chat_id' => $chatId,
  104. 'text' => "⚠️可用余额不足,请重试",
  105. 'reply_to_message_id' => $messageId
  106. ];
  107. }
  108. if ($amount < 100) {
  109. return [
  110. 'chat_id' => $chatId,
  111. 'text' => "⚠️提现不能少于100 RMB,请重试",
  112. 'reply_to_message_id' => $messageId
  113. ];
  114. }
  115. if ($amount > 49999) {
  116. return [
  117. 'chat_id' => $chatId,
  118. 'text' => "⚠️最多提现 49999 RMB,请重试",
  119. 'reply_to_message_id' => $messageId
  120. ];
  121. }
  122. Cache::put("{$chatId}_WITHDRAW_MONEY", $amount);
  123. $list = Bank::where('member_id', $chatId)->get();
  124. $keyboard = [];
  125. foreach ($list as $item) {
  126. $keyboard[] = [['text' => "$item->card_no($item->bank_name)", 'callback_data' => "withdrawAddress@@choose_qb_{$item->id}"]];
  127. }
  128. $keyboard[] = [
  129. ['text' => '🏠 银行卡管理', 'callback_data' => "withdraw@@banks"],
  130. ['text' => '❌取消', 'callback_data' => "message@@close"]
  131. ];
  132. $text = "请直接选择下面的地址\n";
  133. $text .= "⚠️提示:请务必确认提现地址正确无误,\n否则资金丢失将无法找回请自负!";
  134. Cache::put("{$chatId}_WITHDRAW_QB_MONEY", $amount);
  135. Cache::put(get_step_key($chatId), StepStatus::CHOOSE_WITHDRAW_QB_ADDRESS);
  136. return [
  137. 'chat_id' => $chatId,
  138. 'text' => $text,
  139. 'reply_to_message_id' => $messageId,
  140. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  141. ];
  142. }
  143. //3.选择银行卡号
  144. static function chooseBank($chatId, $id)
  145. {
  146. $amount = Cache::get("{$chatId}_WITHDRAW_QB_MONEY", '');
  147. if (!$amount) return WalletService::getBalance($chatId);
  148. $bank = Bank::where('id', $id)->first();
  149. // PaymentOrderService::createPayout($chatId, $amount, $bank->channel, $bank->bank_name, $bank->account, $bank->card_no);
  150. $text = "提交成功\n";
  151. $text .= "结果将在稍后通知您,请留意通知!!!";
  152. return [
  153. 'chat_id' => $chatId,
  154. 'text' => $text,
  155. ];
  156. }
  157. //银行卡管理
  158. static function banks($chatId, $messageId)
  159. {
  160. $text = "🏠 银行卡管理\n";
  161. $text .= "--------------------------\n";
  162. $list = Bank::where('member_id', $chatId)
  163. ->get();
  164. $keyboard = [];
  165. foreach ($list as $item) {
  166. $keyboard[] = [['text' => "{$item->bank_name}({$item->card_no})", 'callback_data' => "withdrawAddress@@bank_detail{$item->id}"]];
  167. }
  168. if (count($list) < 5) {
  169. $keyboard[] = [['text' => "➕ 添加银行卡", 'callback_data' => "withdrawAddress@@bank_add"]];
  170. }
  171. $keyboard[] = [['text' => "↩️返回", 'callback_data' => "withdraw@@home"]];
  172. return [
  173. 'chat_id' => $chatId,
  174. 'text' => $text,
  175. 'message_id' => $messageId,
  176. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  177. ];
  178. }
  179. //删除银行卡
  180. static function bankDelete($chatId, $messageId, $id)
  181. {
  182. Bank::where('id', $id)
  183. ->where('member_id', $chatId)->delete();
  184. return static::banks($chatId, $messageId);
  185. }
  186. //银行卡详情
  187. static function bankDetails($chatId, $messageId, $id)
  188. {
  189. $text = "*银行卡管理*\n\n";
  190. $bank = Bank::where('id', $id)
  191. ->where('member_id', $chatId)->first();
  192. switch ($bank->channel) {
  193. case "DF001":
  194. $text .= "姓名:{$bank->account}\n";
  195. $text .= "银行:{$bank->bank_name}\n";
  196. $text .= "卡号:{$bank->card_no}\n";
  197. break;
  198. case "DF002":
  199. $text .= "姓名:{$bank->account}\n";
  200. $text .= "支付宝账号:{$bank->card_no}\n";
  201. break;
  202. default:
  203. $text .= "姓名:{$bank->account}\n";
  204. $text .= "银行:{$bank->bank_name}\n";
  205. $text .= "卡号:{$bank->card_no}\n";
  206. break;
  207. }
  208. $keyboard = [
  209. [['text' => '❌删除该地址', 'callback_data' => "withdraw@@bank_del_{$id}"]],
  210. [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@banks']]
  211. ];
  212. return [
  213. 'chat_id' => $chatId,
  214. 'parse_mode' => 'MarkdownV2',
  215. 'text' => $text,
  216. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  217. 'message_id' => $messageId
  218. ];
  219. }
  220. static function addBank($chatId, $messageId)
  221. {
  222. $text = "请选择 提现通道\n";
  223. $keyboard = [
  224. [
  225. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  226. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  227. ],
  228. [
  229. ['text' => '❌取消', 'callback_data' => "message@@close"]
  230. ]
  231. ];
  232. return [
  233. 'chat_id' => $chatId,
  234. 'text' => $text,
  235. 'message_id' => $messageId,
  236. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  237. ];
  238. }
  239. static function chooseChannel($chatId, $messageId, $channel)
  240. {
  241. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  242. switch ($channel) {
  243. case "DF002"://支付宝
  244. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  245. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  246. return [
  247. 'chat_id' => $chatId,
  248. 'text' => "请输入支付宝账号",
  249. 'message_id' => $messageId,
  250. ];
  251. break;
  252. case "DF001"://银行卡
  253. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  254. return [
  255. 'chat_id' => $chatId,
  256. 'text' => "请输入银行名称",
  257. 'message_id' => $messageId,
  258. ];
  259. break;
  260. default:
  261. return [
  262. 'chat_id' => $chatId,
  263. 'text' => "选择通道错误",
  264. 'message_id' => $messageId,
  265. ];
  266. break;
  267. }
  268. }
  269. static function inputBankName($chatId, $bankName, $messageId)
  270. {
  271. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  272. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  273. return [
  274. 'chat_id' => $chatId,
  275. 'text' => "请输入银行卡号",
  276. 'message_id' => $messageId,
  277. ];
  278. }
  279. static function inputCardNo($chatId, $cardNo, $messageId)
  280. {
  281. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  282. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  283. return [
  284. 'chat_id' => $chatId,
  285. 'text' => "请输入姓名",
  286. 'message_id' => $messageId,
  287. ];
  288. }
  289. static function inputAccount($chatId, $account, $messageId)
  290. {
  291. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  292. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  293. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  294. Bank::create([
  295. 'member_id' => $chatId,
  296. 'account' => $account,
  297. 'channel' => $channel,
  298. 'card_no' => $cardNo,
  299. 'bank_name' => $bankName
  300. ]);
  301. return static::banks($chatId, $messageId);
  302. }
  303. }