Webhooks - Afriex Business API
Setup
Pass your webhook public key when initializing the SDK:
const afriex = new AfriexSDK({
apiKey: "your-api-key",
webhookPublicKey: "your-afriex-public-key",
});
Verify and Parse
Validate the signature and parse the payload in one call:
app.post("/webhook", (req, res) => {
const signature = req.headers["x-webhook-signature"];
const payload = req.body; // Raw string body
try {
const event = afriex.webhooks.verifyAndParse(payload, signature);
switch (event.event) {
case "CUSTOMER.CREATED":
case "CUSTOMER.UPDATED":
case "CUSTOMER.DELETED":
console.log("Customer event:", event.data);
break;
case "TRANSACTION.CREATED":
case "TRANSACTION.UPDATED":
console.log("Transaction event:", event.data);
break;
case "PAYMENT_METHOD.CREATED":
case "PAYMENT_METHOD.UPDATED":
case "PAYMENT_METHOD.DELETED":
console.log("Payment method event:", event.data);
break;
case "CHECKOUT_SESSION.CREATED":
console.log("Checkout session event:", event.data);
break;
}
res.status(200).send("OK");
} catch (error) {
console.error("Invalid signature");
res.status(400).send("Invalid signature");
}
});
Verify Only
Validate the signature without parsing:
const isValid = afriex.webhooks.verify(payload, signature);
if (isValid) {
const event = JSON.parse(payload);
// Handle event
}
Trigger Test Webhook (Sandbox)
Send a test webhook to your endpoint in sandbox:
const result = await afriex.webhooks.triggerTestWebhook({
event: "TRANSACTION.UPDATED",
resourceId: "transaction-id",
});
console.log(result); // { success: true, message: "..." }
For CHECKOUT_SESSION.CREATED, use the checkout session UUID as resourceId.
Test webhook triggers only work in the sandbox environment.