# QuataPay in Node.js / TypeScript Plain HTTP, no QuataPay package; works with test and live keys. Secrets come from `QUATAPAY_API_KEY` and `QUATAPAY_WEBHOOK_SECRET`. ## Create a payment ```ts import { randomUUID } from "node:crypto"; const BASE = "https://api.quatapay.com/api/v1"; // Server-side only. idempotencyKey: make ONE per checkout attempt, save it with the order, // and reuse it when you retry the same attempt. A new attempt gets a new key. export async function createPayment(order: { id: string; amountXaf: number }, idempotencyKey = randomUUID()) { const key = process.env.QUATAPAY_API_KEY!; const path = key.startsWith("qpay_test_") ? "/gateway/sandbox/payments" : "/gateway/payments"; const res = await fetch(BASE + path, { method: "POST", headers: { Authorization: "Bearer " + key, "Content-Type": "application/json", "Idempotency-Key": idempotencyKey, }, body: JSON.stringify({ amount: order.amountXaf, // integer XAF, never a float currency: "XAF", customer_reference: order.id, description: "Order " + order.id, return_url: "https://your-site.example/pay/return?order=" + order.id, cancel_url: "https://your-site.example/pay/cancel?order=" + order.id, }), signal: AbortSignal.timeout(30_000), }); const json = await res.json(); if (!res.ok) throw new Error("QuataPay " + res.status + " " + json.code + ": " + json.error); return { checkoutUrl: json.data.checkout_url as string, paymentId: json.data.payment.id as string }; } ``` ## Verify a webhook ```ts import { createHmac, timingSafeEqual } from "node:crypto"; export function validSignature(secret: string, rawBody: Buffer | string, timestamp?: string | null, signature?: string | null) { if (!timestamp || !/^\d+$/.test(timestamp) || !signature) return false; if (Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return false; const expected = Buffer.from( "sha256=" + createHmac("sha256", secret).update(timestamp + ".").update(rawBody).digest("hex"), ); const given = Buffer.from(signature); return expected.length === given.length && timingSafeEqual(expected, given); } // Express: app.post("/quatapay/webhook", express.raw({ type: "application/json" }), (req, res) => { // if (!validSignature(process.env.QUATAPAY_WEBHOOK_SECRET!, req.body, req.get("X-QuataPay-Timestamp"), req.get("X-QuataPay-Signature"))) // return res.status(400).send("invalid signature"); // const { event, data } = JSON.parse(req.body.toString("utf8")); // // 1. insert (data.id, event) into a table with a UNIQUE constraint; if it already exists, just answer 200 // // 2. answer 200 fast, then fulfil the order from your own database state // res.sendStatus(200); // }); ```