// .NET 8+. Coloque em um projeto Console e execute somente no servidor. using System.Net.Http.Headers; using System.Text; using System.Text.Json; using System.Text.RegularExpressions; var key = Environment.GetEnvironmentVariable("PIX_API_KEY"); var id = Environment.GetEnvironmentVariable("PIX_SALE_ID"); if (string.IsNullOrWhiteSpace(key) || id is null || !Regex.IsMatch(id, @"\A[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{21}\z")) throw new ArgumentException("Configure PIX_API_KEY e PIX_SALE_ID."); if (args.Length != 1 || (args[0] != "create" && args[0] != "get")) throw new ArgumentException("Use: dotnet run -- create|get"); // Persista ID e corpo antes do POST; preserve ambos em toda retentativa. var create = args[0] == "create"; var body = new { sale_id = id, amount_cents = 8990, reference = "Pedido 1042" }; var url = "https://pixlojista.com.br/v1/integration/sales" + (create ? "" : "/" + id); using var handler = new HttpClientHandler { AllowAutoRedirect = false }; using var client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(15) }; using var request = new HttpRequestMessage(create ? HttpMethod.Post : HttpMethod.Get, url); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", key); if (create) request.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json"); using var response = await client.SendAsync(request); var json = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) throw new HttpRequestException($"HTTP {(int)response.StatusCode}: {json}"); Console.WriteLine(json); // JSON: sale, brcode, qr_png_base64.