QianBaoWithdrawService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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->card_no($item->bank_name)", '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. static function bankDelete($chatId, $messageId, $id)
  180. {
  181. Bank::where('id', $id)
  182. ->where('member_id', $chatId)->delete();
  183. return static::banks($chatId, $messageId);
  184. }
  185. static function bankDetails($chatId, $messageId, $id)
  186. {
  187. $text = "*银行卡管理*\n\n";
  188. $bank = Bank::where('id', $id)
  189. ->where('member_id', $chatId)->first();
  190. switch ($bank->channel) {
  191. case "DF001":
  192. $text .= "姓名:{$bank->account}\n";
  193. $text .= "银行:{$bank->bank_name}\n";
  194. $text .= "卡号:{$bank->card_no}\n";
  195. break;
  196. case "DF002":
  197. $text .= "姓名:{$bank->account}\n";
  198. $text .= "支付宝账号:{$bank->card_no}\n";
  199. break;
  200. default:
  201. $text .= "姓名:{$bank->account}\n";
  202. $text .= "银行:{$bank->bank_name}\n";
  203. $text .= "卡号:{$bank->card_no}\n";
  204. break;
  205. }
  206. $keyboard = [
  207. [['text' => '❌删除该地址', 'callback_data' => "withdraw@@bank_del_{$id}"]],
  208. [['text' => '↩️返回列表', 'callback_data' => 'withdraw@@banks']]
  209. ];
  210. return [
  211. 'chat_id' => $chatId,
  212. 'parse_mode' => 'MarkdownV2',
  213. 'text' => $text,
  214. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  215. 'message_id' => $messageId
  216. ];
  217. }
  218. static function addBank($chatId, $messageId)
  219. {
  220. $text = "请选择 提现通道\n";
  221. $keyboard = [
  222. [
  223. ['text' => '银行卡', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF001"],
  224. ['text' => '支付宝', 'callback_data' => "withdrawAddress@@bank_choose_channel_DF002"]
  225. ],
  226. [
  227. ['text' => '❌取消', 'callback_data' => "message@@close"]
  228. ]
  229. ];
  230. return [
  231. 'chat_id' => $chatId,
  232. 'text' => $text,
  233. 'message_id' => $messageId,
  234. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  235. ];
  236. }
  237. static function chooseChannel($chatId, $messageId, $channel)
  238. {
  239. Cache::put("{$chatId}_QB_WITHDRAW_CHANNEL", $channel);
  240. switch ($channel) {
  241. case "DF002"://支付宝
  242. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", '支付宝');
  243. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  244. return [
  245. 'chat_id' => $chatId,
  246. 'text' => "请输入支付宝账号",
  247. 'message_id' => $messageId,
  248. ];
  249. break;
  250. case "DF001"://银行卡
  251. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_BANK_NAME);
  252. return [
  253. 'chat_id' => $chatId,
  254. 'text' => "请输入银行名称",
  255. 'message_id' => $messageId,
  256. ];
  257. break;
  258. default:
  259. return [
  260. 'chat_id' => $chatId,
  261. 'text' => "选择通道错误",
  262. 'message_id' => $messageId,
  263. ];
  264. break;
  265. }
  266. }
  267. static function inputBankName($chatId, $bankName, $messageId)
  268. {
  269. Cache::put("{$chatId}_QB_WITHDRAW_BANK_NAME", $bankName);
  270. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_CARD_NO);
  271. return [
  272. 'chat_id' => $chatId,
  273. 'text' => "请输入银行卡号",
  274. 'message_id' => $messageId,
  275. ];
  276. }
  277. static function inputCardNo($chatId, $cardNo, $messageId)
  278. {
  279. Cache::put("{$chatId}_QB_WITHDRAW_CARD_NO", $cardNo);
  280. Cache::put(get_step_key($chatId), StepStatus::QB_INPUT_ACCOUNT);
  281. return [
  282. 'chat_id' => $chatId,
  283. 'text' => "请输入姓名",
  284. 'message_id' => $messageId,
  285. ];
  286. }
  287. static function inputAccount($chatId, $account, $messageId)
  288. {
  289. $channel = Cache::get("{$chatId}_QB_WITHDRAW_CHANNEL");
  290. $cardNo = Cache::get("{$chatId}_QB_WITHDRAW_CARD_NO");
  291. $bankName = Cache::get("{$chatId}_QB_WITHDRAW_BANK_NAME");
  292. Bank::create([
  293. 'member_id' => $chatId,
  294. 'account' => $account,
  295. 'channel' => $channel,
  296. 'card_no' => $cardNo,
  297. 'bank_name' => $bankName
  298. ]);
  299. return static::banks($chatId, $messageId);
  300. }
  301. }