TopUpService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. ->orderBy('created_at', 'desc')
  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. * @throws TelegramSDKException
  89. */
  90. public static function photo($chatId, $photo)
  91. {
  92. $telegram = $telegram = new Api(config('services.telegram.token'));
  93. $file = $telegram->getFile(['file_id' => $photo['file_id']]);
  94. $filePath = $file->getFilePath();
  95. $token = config('services.telegram.token');
  96. $file_url = "https://api.telegram.org/file/bot{$token}/{$filePath}";
  97. $file_info = pathinfo($file_url);
  98. $save_path = storage_path("app/public/images/{$chatId}/");
  99. if (!is_dir($save_path)) {
  100. mkdir($save_path, 0777, true);
  101. }
  102. $fileName = uniqid();
  103. $fileName = "{$fileName}.{$file_info['extension']}";
  104. $save_path .= $fileName;
  105. file_put_contents($save_path, file_get_contents($file_url));
  106. $path = Storage::url("images/{$chatId}/" . $fileName);
  107. $amount = Cache::get("{$chatId}_TOP_UP_AMOUNT");
  108. $toAddress = Cache::get("{$chatId}_TOP_UP_ADDRESS");
  109. $net = Cache::get("{$chatId}_TOP_UP_TYPE");
  110. $recharge = new Recharge();
  111. $recharge->member_id = $chatId;
  112. $recharge->net = $net;
  113. $recharge->coin = "USDT";
  114. $recharge->amount = $amount;
  115. $recharge->to_address = $toAddress;
  116. $recharge->status = 0;
  117. $recharge->type = 2;
  118. $recharge->image = $path;
  119. $recharge->save();
  120. Cache::delete(get_step_key($chatId));
  121. Cache::delete("{$chatId}_TOP_UP_AMOUNT");
  122. Cache::delete("{$chatId}_TOP_UP_ADDRESS");
  123. Cache::delete("{$chatId}_TOP_UP_TYPE");
  124. return [
  125. 'chat_id' => $chatId,
  126. 'text' => lang('已提交充值凭证,请等待系统审核完成')
  127. ];
  128. }
  129. //输入充值金额
  130. public static function inputAmount($chatId, $amount, $messageId)
  131. {
  132. if (!preg_match('/^\d+(\.\d+)?$/', $amount)) {
  133. return [
  134. 'chat_id' => $chatId,
  135. 'text' => lang("金额输入不正确,请发送充值数字"),
  136. 'reply_to_message_id' => $messageId
  137. ];
  138. }
  139. Cache::put("{$chatId}_TOP_UP_AMOUNT", $amount);
  140. Cache::put(get_step_key($chatId), StepStatus::INPUT_TOP_UP_IMAGE);
  141. return [
  142. 'chat_id' => $chatId,
  143. 'text' => lang("请发送您的充值凭证截图,方便系统验证"),
  144. ];
  145. }
  146. //用户点击我已付款 (手动)
  147. public static function pay2($chatId)
  148. {
  149. Cache::put(get_step_key($chatId), StepStatus::INPUT_TOP_UP_MONEY);
  150. return [
  151. 'chat_id' => $chatId,
  152. 'text' => lang("请输入充值金额")
  153. ];
  154. }
  155. //用户点击我已付款
  156. public function done($chatId)
  157. {
  158. RechargeService::syncUsdtRechargeRecords($chatId);
  159. $keyboard = [
  160. [['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"]]
  161. ];
  162. return [
  163. 'chat_id' => $chatId,
  164. 'text' => lang("请耐心等待,充值成功后 Bot 会通知您!"),
  165. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  166. ];
  167. }
  168. public static function chooseAddress($chatId, $messageId)
  169. {
  170. $keyboard = [
  171. [
  172. ['text' => 'TRC20', 'callback_data' => "topUp@@TRC20"],
  173. ['text' => 'ERC20', 'callback_data' => 'topUp@@ERC20']
  174. ],
  175. [
  176. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home"],
  177. ]
  178. ];
  179. $text = lang("请选择网络类型");
  180. return [
  181. 'chat_id' => $chatId,
  182. 'text' => $text,
  183. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  184. 'message_id' => $messageId,
  185. ];
  186. }
  187. //获取充值二维码
  188. public function scan($chatId, $messageId, $type)
  189. {
  190. // $receivingType = Config::where('field', 'receiving_type')->first()->val;
  191. $receivingType = 2;
  192. //自动
  193. if ($receivingType == 1) {
  194. $res = WalletService::getRechargeImageAddress($chatId);
  195. $address = $res['address'];
  196. $qrCode = $res['full_path'];
  197. } //
  198. //手动
  199. else {
  200. if ($type === "TRC20") {
  201. $address = Config::where('field', 'receiving_address')->first()->val;
  202. } else if ($type === "ERC20") {
  203. $address = Config::where('field', 'receiving_address_erc20')->first()->val;
  204. }
  205. $res = WalletService::getPlatformImageAddress($address);
  206. $res['net'] = $type;
  207. $qrCode = $res['full_path'];
  208. Cache::put("{$chatId}_TOP_UP_ADDRESS", $address);
  209. Cache::put("{$chatId}_TOP_UP_TYPE", $type);
  210. }
  211. $replyInfo = KeyboardService::findOne(['button' => '充值提示文字']);
  212. $caption = "\n";
  213. if ($replyInfo) {
  214. $caption .= "{$replyInfo->reply}\n\n";
  215. } else {
  216. $caption .= lang("支持货币") . "({$res['net']}):{$res['coin']}\n";
  217. $caption .= lang('专属收款地址') . ":\n";
  218. $caption .= "{$address}\n";
  219. $caption .= "\n";
  220. $caption .= lang("⚠️提示") . ":\n";
  221. $caption .= lang("- 如果上面地址和二维码不一致,请不要付款!") . "\n";
  222. $caption .= lang("- 对上述地址充值后, 请点击我已付款!") . "\n";
  223. $caption .= lang("- 请耐心等待, 充值成功后 Bot 会通知您!") . "\n";
  224. }
  225. $keyboard = [
  226. [
  227. ['text' => lang('📋 复制地址'), 'copy_text' => ['text' => $address]],
  228. ['text' => lang('✅ 我已付款'), 'callback_data' => 'topUp@@pay']
  229. ],
  230. [
  231. ['text' => lang("↩️返回"), 'callback_data' => "topUp@@home1"],
  232. ]
  233. ];
  234. //手动
  235. if ($receivingType != 1) {
  236. $keyboard[0][1] = ['text' => lang('✅ 我已付款'), 'callback_data' => 'topUp@@pay2'];
  237. }
  238. return [
  239. 'chat_id' => $chatId,
  240. 'photo' => InputFile::create($qrCode),
  241. 'caption' => $caption,
  242. 'protect_content' => true,
  243. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  244. 'message_id' => $messageId
  245. ];
  246. }
  247. public function index($chatId, $firstName, $messageId = null): array
  248. {
  249. $wallet = Wallet::where('member_id', $chatId)->first();
  250. $text = "👤 {$firstName}($chatId)\n";
  251. $text .= lang("💰钱包余额") . "\n";
  252. $temp = floatval($wallet->available_balance);
  253. $text .= "USDT:{$temp}\n";
  254. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  255. $keyboard = [
  256. [
  257. ['text' => lang('➕充值'), 'callback_data' => "topup@@topup"],
  258. ['text' => lang('🧾账单'), 'callback_data' => "topup@@bill"],
  259. ],
  260. [
  261. ['text' => lang('👩 客服帮助'), 'url' => "https://t.me/{$serviceAccount}"],
  262. ['text' => lang('❌取消'), 'callback_data' => "message@@close"],
  263. ]
  264. ];
  265. return [
  266. 'chat_id' => $chatId,
  267. 'text' => $text,
  268. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard]),
  269. 'message_id' => $messageId
  270. ];
  271. }
  272. }