# QuataPay in C# / .NET Plain HTTP, no QuataPay package; works with test and live keys. Secrets come from `QUATAPAY_API_KEY` and `QUATAPAY_WEBHOOK_SECRET`. ## Create a payment ```csharp using System.Net.Http.Json; using System.Text.Json; public static class QuataPay { static readonly HttpClient Http = new() { BaseAddress = new Uri("https://api.quatapay.com/api/v1/"), Timeout = TimeSpan.FromSeconds(30), }; // idempotencyKey: one per checkout attempt, reused on retries of that attempt. public static async Task<(string CheckoutUrl, string PaymentId)> CreatePaymentAsync( string orderId, long amountXaf, string idempotencyKey) { var key = Environment.GetEnvironmentVariable("QUATAPAY_API_KEY")!; var path = key.StartsWith("qpay_test_") ? "gateway/sandbox/payments" : "gateway/payments"; using var req = new HttpRequestMessage(HttpMethod.Post, path) { Content = JsonContent.Create(new { amount = amountXaf, // integer XAF currency = "XAF", customer_reference = orderId, return_url = "https://your-site.example/pay/return?order=" + orderId, cancel_url = "https://your-site.example/pay/cancel?order=" + orderId, }), }; req.Headers.Add("Authorization", "Bearer " + key); req.Headers.Add("Idempotency-Key", idempotencyKey); using var res = await Http.SendAsync(req); using var doc = JsonDocument.Parse(await res.Content.ReadAsStringAsync()); if (!res.IsSuccessStatusCode) throw new InvalidOperationException("QuataPay " + (int)res.StatusCode + ": " + doc.RootElement.GetProperty("error").GetString()); var data = doc.RootElement.GetProperty("data"); return (data.GetProperty("checkout_url").GetString()!, data.GetProperty("payment").GetProperty("id").GetString()!); } } ``` ## Verify a webhook ```csharp using System.Security.Cryptography; using System.Text; public static class QuataPayWebhook { public static bool ValidSignature(string secret, byte[] rawBody, string? timestamp, string? signature) { if (string.IsNullOrEmpty(timestamp) || !timestamp.All(char.IsAsciiDigit) || signature is null) return false; if (Math.Abs(DateTimeOffset.UtcNow.ToUnixTimeSeconds() - long.Parse(timestamp)) > 300) return false; using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secret)); var signed = Encoding.UTF8.GetBytes(timestamp + ".").Concat(rawBody).ToArray(); // raw bytes, as received var expected = "sha256=" + Convert.ToHexString(hmac.ComputeHash(signed)).ToLowerInvariant(); return CryptographicOperations.FixedTimeEquals(Encoding.UTF8.GetBytes(expected), Encoding.UTF8.GetBytes(signature)); } } ```