TopUpService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. namespace App\Services;
  3. //充值服务
  4. use App\Constants\StepStatus;
  5. use App\Models\BalanceLog;
  6. use App\Models\Config;
  7. use App\Models\Recharge;
  8. use App\Models\Wallet;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\Storage;
  11. use Telegram\Bot\Api;
  12. use Telegram\Bot\FileUpload\InputFile;
  13. use Telegram\Bot\Exceptions\TelegramSDKException;
  14. use App\Services\BalanceLogService;
  15. class TopUpService
  16. {
  17. /**
  18. * @param Api $telegram
  19. * @param $data
  20. * @param $chatId
  21. * @param $firstName
  22. * @param $messageId
  23. * @return void
  24. * @throws TelegramSDKException
  25. */
  26. public static function init(Api $telegram, $data, $chatId, $firstName, $messageId): void
  27. {
  28. switch ($data) {
  29. case "topUp@@pay2"://手动充值(后台审核后到账)
  30. $telegram->deleteMessage(['chat_id' => $chatId, 'message_id' => $messageId]);
  31. $res = TopUpService::pay2($chatId);
  32. $telegram->sendMessage($res);
  33. break;
  34. case "topUp@@pay"://自动充值
  35. $telegram->deleteMessage(['chat_id' => $chatId, 'message_id' => $messageId]);
  36. $res = TopUpService::done($chatId);
  37. $telegram->sendMessage($res);
  38. break;
  39. case "topup@@bill"://点击充值的账单按钮
  40. $res = TopUpService::bill($chatId, $firstName, $messageId);
  41. $telegram->editMessageText($res);
  42. break;
  43. case "topup@@topup"://点击充值按钮
  44. $res = TopUpService::chooseAddress($chatId, $messageId);
  45. $telegram->editMessageText($res);
  46. break;
  47. case "topUp@@TRC20"://选择充值地址
  48. case "topUp@@ERC20"://选择充值地址
  49. $type = preg_replace('/^topUp@@/', '', $data);
  50. $res = TopUpService::scan($chatId, $messageId, $type);
  51. $telegram->deleteMessage(['chat_id' => $chatId, 'message_id' => $messageId]);
  52. $telegram->sendPhoto($res);
  53. break;
  54. default:
  55. //充值账单,下一页
  56. $pattern = "/^topUpBillNextPage@@\d+$/";
  57. if (preg_match($pattern, $data)) {
  58. $page = preg_replace('/^topUpBillNextPage@@/', '', $data);
  59. $page = intval($page);
  60. $res = TopUpService::bill($chatId, $firstName, $messageId, $page);
  61. $telegram->editMessageText($res);
  62. }
  63. break;
  64. }
  65. }
  66. public static function onMessage($chatId, $text, $messageId, $stepStatus): ?array
  67. {
  68. switch ($stepStatus) {
  69. case StepStatus::INPUT_TOP_UP_MONEY:
  70. $res = TopUpService::inputAmount($chatId, $text, $messageId);
  71. break;
  72. default:
  73. $res = null;
  74. break;
  75. }
  76. return $res;
  77. }
  78. /**
  79. * 到账通知
  80. * @param $memberId string 要通知的TgID
  81. * @param $text string 通知内容
  82. * @return bool 是否发送成功
  83. */
  84. public static function notifyTransferSuccess($memberId, $text)
  85. {
  86. try {
  87. $telegram = new Api(config('services.telegram.token'));
  88. $telegram->sendMessage(['chat_id' => $memberId, 'text' => $text]);
  89. } catch (TelegramSDKException $e) {
  90. return false;
  91. }
  92. return true;
  93. }
  94. public static function bill($chatId, $firstName, $messageId = null, $page = 1, $limit = 5)
  95. {
  96. RechargeService::syncUsdtRechargeRecords($chatId);
  97. $list = BalanceLog::where('member_id', $chatId)
  98. // ->where('change_type', '充值')
  99. ->whereIn('change_type', BalanceLogService::$RW)
  100. ->orderByDesc('id')
  101. ->forPage($page, $limit)
  102. ->get();
  103. $count = BalanceLog::where('member_id', $chatId)
  104. // ->where('change_type', '充值')
  105. ->whereIn('change_type', BalanceLogService::$RW)
  106. ->count();
  107. $text = "👤 {$firstName}({$chatId}) " . lang('钱包流水记录') . "\n\n";
  108. foreach ($list as $item) {
  109. $amount = floatval($item->amount);
  110. $amount = $amount < 0 ? ("➖ " . ($amount * -1)) : "➕ $amount";
  111. $balance = floatval($item->after_balance);
  112. $text .= "-------------------------------------\n";
  113. $text .= "{$amount} \n" . lang('余额') . ":{$balance} \n";
  114. $text .= lang('类别') . ":" . lang($item->change_type) . "\n";
  115. if ($item->remark) {
  116. $text .= lang("说明") . ":{$item->remark}\n";
  117. }
  118. $text .= lang("日期") . ":{$item->created_at}\n";
  119. }
  120. if ($page > 1) {
  121. $keyboard[] = [
  122. ['text' => lang("👆上一页"), 'callback_data' => "topUpBillNextPage@@" . ($page - 1)]
  123. ];
  124. }
  125. $allPage = ceil($count / $limit);
  126. if ($allPage > $page) {
  127. if ($page > 1) {
  128. $keyboard[count($keyboard) - 1][] = ['text' => lang("👇下一页"), 'callback_data' => "topUpBillNextPage@@" . ($page + 1)];
  129. } else {
  130. $keyboard[] = [
  131. ['text' => lang("👇下一页"), 'callback_data' => "topUpBillNextPage@@" . ($page + 1)]
  132. ];
  133. }
  134. }
  135. $keyboard[] = [
  136. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"]
  137. ];
  138. return [
  139. 'chat_id' => $chatId,
  140. 'text' => $text,
  141. 'message_id' => $messageId,
  142. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  143. ];
  144. }
  145. /**
  146. * 充值图片
  147. * @param $chatId
  148. * @param $photo
  149. * @return array
  150. * @throws TelegramSDKException
  151. */
  152. public static function photo($chatId, $photo): array
  153. {
  154. try {
  155. $telegram = new Api(config('services.telegram.token'));
  156. } catch (TelegramSDKException $e) {
  157. return [];
  158. }
  159. $file = $telegram->getFile(['file_id' => $photo['file_id']]);
  160. $filePath = $file->getFilePath();
  161. $token = config('services.telegram.token');
  162. $file_url = "https://api.telegram.org/file/bot{$token}/{$filePath}";
  163. $file_info = pathinfo($file_url);
  164. $save_path = storage_path("app/public/images/{$chatId}/");
  165. if (!is_dir($save_path)) {
  166. mkdir($save_path, 0777, true);
  167. }
  168. $fileName = uniqid();
  169. $fileName = "{$fileName}.{$file_info['extension']}";
  170. $save_path .= $fileName;
  171. file_put_contents($save_path, file_get_contents($file_url));
  172. $path = Storage::url("images/{$chatId}/" . $fileName);
  173. $amount = Cache::get("{$chatId}_TOP_UP_AMOUNT");
  174. $toAddress = Cache::get("{$chatId}_TOP_UP_ADDRESS");
  175. $net = Cache::get("{$chatId}_TOP_UP_TYPE");
  176. $recharge = new Recharge();
  177. $recharge->member_id = $chatId;
  178. $recharge->net = $net;
  179. $recharge->coin = "USDT";
  180. $recharge->amount = $amount;
  181. $recharge->to_address = $toAddress;
  182. $recharge->status = 0;
  183. $recharge->type = 2;
  184. $recharge->image = $path;
  185. $recharge->save();
  186. Cache::delete(get_step_key($chatId));
  187. Cache::delete("{$chatId}_TOP_UP_AMOUNT");
  188. Cache::delete("{$chatId}_TOP_UP_ADDRESS");
  189. Cache::delete("{$chatId}_TOP_UP_TYPE");
  190. return [
  191. 'chat_id' => $chatId,
  192. 'text' => lang('已提交充值凭证,请等待系统审核完成')
  193. ];
  194. }
  195. //输入充值金额
  196. public static function inputAmount($chatId, $amount, $messageId)
  197. {
  198. if (!preg_match('/^\d+(\.\d+)?$/', $amount)) {
  199. return [
  200. 'chat_id' => $chatId,
  201. 'text' => lang("金额输入不正确,请发送充值数字"),
  202. 'reply_to_message_id' => $messageId
  203. ];
  204. }
  205. Cache::put("{$chatId}_TOP_UP_AMOUNT", $amount);
  206. Cache::put(get_step_key($chatId), StepStatus::INPUT_TOP_UP_IMAGE);
  207. return [
  208. 'chat_id' => $chatId,
  209. 'text' => lang("请发送您的充值凭证截图,方便系统验证"),
  210. ];
  211. }
  212. //用户点击我已付款 (手动)
  213. public static function pay2($chatId)
  214. {
  215. Cache::put(get_step_key($chatId), StepStatus::INPUT_TOP_UP_MONEY);
  216. return [
  217. 'chat_id' => $chatId,
  218. 'text' => lang("请输入充值金额")
  219. ];
  220. }
  221. //用户点击我已付款
  222. public static function done($chatId)
  223. {
  224. RechargeService::syncUsdtRechargeRecords($chatId);
  225. $keyboard = [
  226. [['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"]]
  227. ];
  228. return [
  229. 'chat_id' => $chatId,
  230. 'text' => lang("请耐心等待,充值成功后 Bot 会通知您!"),
  231. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  232. ];
  233. }
  234. public static function chooseAddress($chatId, $messageId)
  235. {
  236. $keyboard = [
  237. [
  238. ['text' => 'TRC20', 'callback_data' => "topUp@@TRC20"],
  239. ['text' => 'ERC20', 'callback_data' => 'topUp@@ERC20']
  240. ],
  241. [
  242. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"],
  243. ]
  244. ];
  245. $text = lang("请选择网络类型");
  246. return [
  247. 'chat_id' => $chatId,
  248. 'text' => $text,
  249. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  250. 'message_id' => $messageId,
  251. ];
  252. }
  253. //获取充值二维码
  254. public static function scan($chatId, $messageId, $type)
  255. {
  256. // $receivingType = Config::where('field', 'receiving_type')->first()->val;
  257. $receivingType = 2;
  258. //自动
  259. if ($receivingType == 1) {
  260. $res = WalletService::getRechargeImageAddress($chatId);
  261. $address = $res['address'];
  262. $qrCode = $res['full_path'];
  263. } //
  264. //手动
  265. else {
  266. if ($type === "TRC20") {
  267. $address = Config::where('field', 'receiving_address')->first()->val;
  268. } else if ($type === "ERC20") {
  269. $address = Config::where('field', 'receiving_address_erc20')->first()->val;
  270. }
  271. $res = WalletService::getPlatformImageAddress($address);
  272. $res['net'] = $type;
  273. $qrCode = $res['full_path'];
  274. Cache::put("{$chatId}_TOP_UP_ADDRESS", $address);
  275. Cache::put("{$chatId}_TOP_UP_TYPE", $type);
  276. }
  277. $replyInfo = KeyboardService::findOne(['button' => '充值提示文字']);
  278. $caption = "\n";
  279. if ($replyInfo) {
  280. $caption .= "{$replyInfo->reply}\n\n";
  281. } else {
  282. $caption .= lang("支持货币") . "({$res['net']}):{$res['coin']}\n";
  283. $caption .= lang('专属收款地址') . ":\n";
  284. $caption .= "{$address}\n";
  285. $caption .= "\n";
  286. $caption .= lang("⚠️提示") . ":\n";
  287. $caption .= lang("- 如果上面地址和二维码不一致,请不要付款!") . "\n";
  288. $caption .= lang("- 对上述地址充值后, 请点击我已付款!") . "\n";
  289. $caption .= lang("- 请耐心等待, 充值成功后 Bot 会通知您!") . "\n";
  290. }
  291. $keyboard = [
  292. [
  293. ['text' => lang('📋 复制地址'), 'copy_text' => ['text' => $address]],
  294. ['text' => lang('✅ 我已付款'), 'callback_data' => 'topUp@@pay']
  295. ],
  296. [
  297. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home1"],
  298. ]
  299. ];
  300. //手动
  301. if ($receivingType != 1) {
  302. $keyboard[0][1] = ['text' => lang('✅ 我已付款'), 'callback_data' => 'topUp@@pay2'];
  303. }
  304. return [
  305. 'chat_id' => $chatId,
  306. 'photo' => InputFile::create($qrCode),
  307. 'caption' => $caption,
  308. 'protect_content' => true,
  309. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  310. 'message_id' => $messageId
  311. ];
  312. }
  313. public function index($chatId, $firstName, $messageId = null): array
  314. {
  315. $wallet = Wallet::where('member_id', $chatId)->first();
  316. $text = "👤 {$firstName}($chatId)\n";
  317. $text .= lang("💰钱包余额") . "\n";
  318. $temp = floatval($wallet->available_balance);
  319. $text .= "USDT:{$temp}\n";
  320. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  321. $keyboard = [
  322. [
  323. ['text' => lang('➕充值'), 'callback_data' => "topup@@topup"],
  324. ['text' => lang('🧾账单'), 'callback_data' => "topup@@bill"],
  325. ],
  326. [
  327. ['text' => lang('👩 客服帮助'), 'url' => "https://t.me/{$serviceAccount}"],
  328. ['text' => lang('❌取消'), 'callback_data' => "message@@close"],
  329. ]
  330. ];
  331. return [
  332. 'chat_id' => $chatId,
  333. 'text' => $text,
  334. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  335. 'message_id' => $messageId
  336. ];
  337. }
  338. }