TopUpService.php 13 KB

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