TopUpService.php 13 KB

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