TopUpService.php 11 KB

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