Skip to Content
KingsPay Docs
Events

Handling Events

When KingsPay delivers an event to your endpoint, your server must:

  1. Read the raw request body and the signature headers.
  2. Verify the signature to confirm the request came from KingsPay.
  3. Parse the event payload and handle it according to the event_type.
  4. Return a 2xx response.

Verifying the signature

Please read this section carefully. Skipping signature verification is a critical security risk. Without it, anyone on the internet can send a forged request to your webhook endpoint pretending to be KingsPay. This can be exploited to trigger fraudulent transactions, fake payment confirmations, or other manipulations that result in direct financial loss. Always verify the signature on every incoming webhook request.

KingsPay signs every webhook request using HMAC-SHA256. The signature is computed over the string {timestamp}.{raw_body} using your webhook secret, and sent in the mataqa-signature header. The timestamp is sent separately in the mataqa-timestamp header.

To verify:

  1. Read mataqa-timestamp and mataqa-signature from the request headers.
  2. Construct the string to sign: {timestamp}.{raw_body}.
  3. Compute HMAC-SHA256 of that string using your webhook secret.
  4. Compare the result to the value in mataqa-signature using a constant-time comparison.
  5. Reject the request if they do not match.

You can find your webhook secret in the Dashboard under Integrations > Webhook Destinations.

Do not parse or re-serialize the request body before verifying the signature. The signature is computed against the exact bytes sent by KingsPay. If your framework automatically deserializes the JSON body and you then re-serialize it to a string, the output may differ in whitespace, key ordering, or encoding — even if the data is logically identical — and the signature check will fail.

Many frameworks do this transparently. For example, ASP.NET Core's [FromBody], Express.js's express.json(), Django REST Framework's request.data, and Laravel's $request->json() all consume and decode the raw stream before you can access it. Make sure to read the body as a raw string before any automatic parsing or model binding kicks in, as shown in the examples below.

Implementation examples

import { Hono } from 'hono'
import { createHmac, timingSafeEqual } from 'crypto'

const app = new Hono()

const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET

app.post('/webhook', async (c) => {
  const rawBody = await c.req.text()
  const timestamp = c.req.header('mataqa-timestamp')
  const signature = c.req.header('mataqa-signature')

  if (!timestamp || !signature) {
    return c.text('Missing required headers', 400)
  }

  const stringToSign = timestamp + '.' + rawBody
  const expected = createHmac('sha256', WEBHOOK_SECRET)
    .update(stringToSign)
    .digest('hex')

  const isValid = timingSafeEqual(
    Buffer.from(expected, 'hex'),
    Buffer.from(signature, 'hex')
  )

  if (!isValid) {
    return c.text('Invalid signature', 401)
  }

  const event = JSON.parse(rawBody)
  console.log('Received event:', event.event_type)

  return c.text('OK')
})

export default app

Responding to events

Always return a 2xx status code once you have received and queued the event for processing. If your endpoint returns a non-2xx status or does not respond within 30 seconds, KingsPay will treat the delivery as failed and retry automatically.

Retry Mechanism

KingsPay queues the initial delivery immediately and automatically retries up to four times if delivery fails. Each retry delay starts when the previous attempt fails. Queue processing can add time.

AttemptScheduled delay
Initial delivery0 minutes
Retry 12 minutes
Retry 24 minutes
Retry 38 minutes
Retry 416 minutes

Failure Conditions

A delivery is considered failed when:

  • The endpoint returns a non-2xx status code
  • The request times out (30 seconds)
  • The endpoint is unreachable

After all retry attempts are exhausted, the webhook delivery is marked as failed. You can view failed deliveries and resend them from the Dashboard, in the details of the payment charge or payout they belong to.

Replay Attack Protection

To prevent replay attacks, verify that the timestamp in the mataqa-timestamp header is within an acceptable window of your current server time. A tolerance of 5 minutes (300 seconds) is recommended. If the timestamp difference exceeds this window, reject the request with a 403 status code.

On this page