# QuataPay in Java Plain HTTP, no QuataPay package; works with test and live keys. Secrets come from `QUATAPAY_API_KEY` and `QUATAPAY_WEBHOOK_SECRET`. ## Create a payment ```java // Java 11+. Uses Jackson (com.fasterxml.jackson.databind) to build and read JSON. import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; import java.util.Map; public final class QuataPay { private static final String BASE = "https://api.quatapay.com/api/v1"; private static final ObjectMapper JSON = new ObjectMapper(); private static final HttpClient HTTP = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build(); /** Returns checkout_url. idempotencyKey: one per checkout attempt, reused on retries. */ public static String createPayment(String orderId, long amountXaf, String idempotencyKey) throws Exception { String key = System.getenv("QUATAPAY_API_KEY"); String path = key.startsWith("qpay_test_") ? "/gateway/sandbox/payments" : "/gateway/payments"; String body = JSON.writeValueAsString(Map.of( "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)); HttpRequest req = HttpRequest.newBuilder(URI.create(BASE + path)) .timeout(Duration.ofSeconds(30)) .header("Authorization", "Bearer " + key) .header("Content-Type", "application/json") .header("Idempotency-Key", idempotencyKey) .POST(HttpRequest.BodyPublishers.ofString(body)) .build(); HttpResponse res = HTTP.send(req, HttpResponse.BodyHandlers.ofString()); JsonNode json = JSON.readTree(res.body()); if (res.statusCode() < 200 || res.statusCode() > 299) { throw new IllegalStateException("QuataPay " + res.statusCode() + " " + json.path("code").asText() + ": " + json.path("error").asText()); } return json.path("data").path("checkout_url").asText(); } } ``` ## Verify a webhook ```java import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.util.HexFormat; public final class QuataPayWebhook { public static boolean validSignature(String secret, byte[] rawBody, String timestamp, String signature) throws Exception { if (timestamp == null || !timestamp.matches("\\d+") || signature == null) return false; if (Math.abs(System.currentTimeMillis() / 1000 - Long.parseLong(timestamp)) > 300) return false; Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256")); mac.update((timestamp + ".").getBytes(StandardCharsets.UTF_8)); mac.update(rawBody); // the bytes exactly as received String expected = "sha256=" + HexFormat.of().formatHex(mac.doFinal()); return MessageDigest.isEqual(expected.getBytes(StandardCharsets.UTF_8), signature.getBytes(StandardCharsets.UTF_8)); } } ```