Confirming payments on the server
In any payment integration, decide first which system is the source of truth for payment state. In the marketplace API it’s the server:
- Checkout creates an order in a pending state and a Mercado Pago payment.
- For PIX, the buyer gets a QR code and copy-paste code and pays in their bank app.
- Mercado Pago sends a webhook notification to the API.
- The API verifies the notification, fetches the payment from Mercado Pago, and only then transitions the order.
- Handling is idempotent: a repeated notification for an already-paid order changes nothing.
Verifying notifications
Mercado Pago signs webhook notifications. Per its documentation, the x-signature header carries a timestamp and an HMAC-SHA256 signature computed over a manifest built from the notification’s data ID, the x-request-id header and that timestamp, using the secret configured for your application. Verify that signature and fetch the payment by ID before trusting the status.
// Illustrative verification on Workers (Web Crypto)
const [ts, v1] = parseSignatureHeader(c.req.header("x-signature")); // "ts=...,v1=..."
const manifest = `id:${dataId};request-id:${c.req.header("x-request-id")};ts:${ts};`;
const expected = await hmacSha256Hex(env.MP_WEBHOOK_SECRET, manifest);
if (!timingSafeEqual(expected, v1)) return c.json({ error: "invalid signature" }, 401);
const payment = await mp.payments.get(dataId); // trust the API, not the payload
For the general pattern (verify, enqueue, process idempotently, sweep), see the article on payment webhooks linked below.