Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Verify Webhook Signatures

Verify Ramp webhook signatures with HMAC-SHA512 over compact data JSON before processing payment events.

Ivorypay sends { "event": "...", "data": {...} } and signs only the compact JSON representation of data, using HMAC-SHA512 and the secret API key for the webhook's business and environment. The header x-ivorypay-signature is a 128-character hexadecimal digest. There is no separate webhook secret.

Do not hash the complete request body. Do not pretty-print, reorder keys or change number formatting before checking the signature. For Python, preserve the original data JSON bytes rather than re-serializing parsed numbers.

The signature does not cover the outer event label. Use verified data and an authenticated status read to decide whether to fulfil an order. Never grant value from the event name alone. Key rotation also changes the key you need for verification.

Node.js

This function accepts the raw request body as a Buffer, the signature header, and your environment's secret key. Node's JSON.stringify matches the signing representation.

const { createHmac, timingSafeEqual } = require('node:crypto');

function verifyWebhook(rawBody, signature, secretKey) {
  if (typeof signature !== 'string' || !/^[0-9a-f]{128}$/i.test(signature)) {
    throw new Error('Invalid webhook signature');
  }
  const payload = JSON.parse(rawBody.toString('utf8'));
  if (!Object.prototype.hasOwnProperty.call(payload, 'data')) {
    throw new Error('Missing webhook data');
  }
  const expected = createHmac('sha512', secretKey)
    .update(JSON.stringify(payload.data), 'utf8').digest();
  const received = Buffer.from(signature, 'hex');
  if (!timingSafeEqual(expected, received)) {
    throw new Error('Invalid webhook signature');
  }
  return payload.data;
}

For Express, register express.raw({ type: 'application/json', limit: '1mb' }) on the webhook route before a global JSON parser. Pass req.body, req.get('x-ivorypay-signature'), and your stored secret to this function. Reject invalid signatures with HTTP 401.

Python

This standalone function accepts raw UTF-8 request bytes. It locates the top-level data value with Python's JSON decoder and hashes its original bytes. It does not use a regular expression to parse JSON.

For Flask, obtain raw bytes with request.get_data() before any middleware transforms the body. Apply a request-size limit. Return HTTP 401 on a verification failure.

Acknowledge and process safely

  1. Verify the signature using the matching environment key.

  2. Persist the event or enqueue it durably. Do not acknowledge a message that exists only in memory.

  3. Return HTTP 200 promptly. Other status codes are treated as delivery failures.

  4. Process asynchronously and idempotently. A reference can receive several status events; do not discard later updates just because you saw that reference before. Deduplicate the same event/status while fulfilling each successful order once.

  5. Match the saved order's reference, currency and amount using an authenticated status read. Reconcile delayed or missing webhooks with the API.

Events can be duplicated, delayed or arrive out of order. A valid signature is not a replay-prevention mechanism. Store processed event identities and enforce one fulfilment per order in your database.

Use the on-ramp status or off-ramp status route for authenticated reconciliation.

Last updated