Webhooks
Receive real-time payment events from ZSI Pay and securely verify webhook requests before processing them in your application.
How webhooks work
Webhooks allow ZSI Pay to notify your server when the state of a payment changes. Instead of continuously polling the payment API, your application can receive events directly from ZSI Pay.
Customer completes checkout.
ZSI Pay creates an event.
Your endpoint receives it.
Your server handles it.
Configure your webhook endpoint
Create a webhook endpoint in your ZSI Pay dashboard and configure the URL that ZSI Pay should send events to.
https://your-domain.com/api/webhooks/zsi-payKeep your webhook endpoint publicly reachable over HTTPS so ZSI Pay can deliver events to your application.
Add your webhook secret
Your webhook secret is used to verify that incoming webhook requests were signed by ZSI Pay.
ZSI_PAY_WEBHOOK_SECRET=your_webhook_secretKeep the webhook secret private and store it as a server-side environment variable.
Preserve the raw request body
Webhook signature verification requires the original request body. Do not parse or modify the body before passing it to the SDK's verification method.
Important
If your framework automatically parses JSON request bodies, make sure your webhook route receives the raw request body required for signature verification.
Verify a webhook
Use zsiPay.webhooks.constructEvent() to verify the webhook signature and construct the event.
const event = zsiPay.webhooks.constructEvent({
rawBody,
headers: {
"zsi-pay-webhook-signature": signature,
"zsi-pay-webhook-timestamp": timestamp,
},
});Always verify the webhook before processing its event data.
Required webhook headers
ZSI Pay sends signature information in the webhook request headers.
| Header | Purpose |
|---|---|
zsi-pay-webhook-signature | Signature generated for the webhook request. |
zsi-pay-webhook-timestamp | Timestamp associated with the webhook signature. |
Express integration
With Express, make sure the webhook route receives the raw request body before JSON parsing is applied to that route.
import express from "express";
import { ZSIPay } from "zsi-pay";
const app = express();
const zsiPay = new ZSIPay({
apiKey: process.env.ZSI_PAY_API_KEY!,
apiSecret: process.env.ZSI_PAY_API_SECRET!,
webhookSecret: process.env.ZSI_PAY_WEBHOOK_SECRET!,
});
app.post(
"/api/webhooks/zsi-pay",
express.raw({ type: "application/json" }),
(req, res) => {
try {
const signature =
req.headers["zsi-pay-webhook-signature"];
const timestamp =
req.headers["zsi-pay-webhook-timestamp"];
const event = zsiPay.webhooks.constructEvent({
rawBody: req.body,
headers: {
"zsi-pay-webhook-signature":
String(signature),
"zsi-pay-webhook-timestamp":
String(timestamp),
},
});
// Handle the verified event here
res.status(200).json({ received: true });
} catch (error) {
res.status(400).json({
message: "Invalid webhook signature",
});
}
}
);Event types
Your webhook handler should inspect the event type and execute the appropriate application logic.
payment.successTriggered when a payment is successfully completed.
payment.failedTriggered when a payment attempt fails.
payment.cancelledTriggered when a payment is cancelled.
payment.expiredTriggered when a payment session expires.
Handle webhook events
After the event has been verified, handle the event according to its type.
switch (event.type) {
case "payment.success":
// Mark the order as paid
break;
case "payment.failed":
// Handle the failed payment
break;
case "payment.cancelled":
// Handle the cancelled payment
break;
case "payment.expired":
// Handle the expired payment
break;
default:
// Ignore events your application does not handle
break;
}Make webhook processing idempotent
Your webhook handler should be safe to execute more than once. Network failures or delivery retries can result in your endpoint receiving the same event again.
const event = zsiPay.webhooks.constructEvent({
rawBody,
headers: {
"zsi-pay-webhook-signature": signature,
"zsi-pay-webhook-timestamp": timestamp,
},
});
// Check your database before processing the event.
// If the event has already been processed,
// return a successful response.
switch (event.type) {
case "payment.success":
// Process payment success
break;
case "payment.failed":
// Process payment failure
break;
}Store an appropriate unique event identifier from the webhook payload when your API provides one, and use it to prevent duplicate business operations.
Return a successful response
Once your server has successfully received and accepted the webhook, return an appropriate successful HTTP response.
res.status(200).json({
received: true,
});Keep webhook processing reliable and avoid unnecessarily long-running work inside the request handler.
Webhook security
- • Always verify the webhook signature.
- • Preserve the raw request body during verification.
- • Keep your webhook secret on the server.
- • Never trust an unverified webhook payload.
- • Make webhook processing idempotent.
- • Use HTTPS for your production webhook endpoint.
