QianBaoWithdrawService.php 11 KB

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