Node.js
A minimal Express server that collects a payment and listens for the resulting webhook.
import express from 'express';
import axios from 'axios';
import crypto from 'crypto';
const app = express();
app.use(express.json());
const API_KEY = process.env.KAWRIE_API_KEY;
const WEBHOOK_SECRET = process.env.KAWRIE_WEBHOOK_SECRET;
app.post('/charge', async (req, res) => {
const { amount, phone, currency } = req.body;
const { data } = await axios.post(
'https://api.kawrie.com/v1/collect',
{ amount, phone, currency },
{
headers: {
Authorization: `Bearer ${API_KEY}`,
'Idempotency-Key': crypto.randomUUID(),
},
}
);
res.json(data);
});
app.post('/webhooks/kawrie', (req, res) => {
const signature = req.headers['kawrie-signature'];
if (!verifySignature(req.body, signature, WEBHOOK_SECRET)) {
return res.status(401).send('Invalid signature');
}
if (req.body.event === 'collection.completed') {
// Mark the matching order as paid
}
res.sendStatus(200);
});
function verifySignature(payload, signatureHeader, secret) {
const [t, v1] = signatureHeader.split(',');
const timestamp = t.split('=')[1];
const received = v1.split('=')[1];
const expected = crypto
.createHmac('sha256', secret)
.update(timestamp + payload.event + payload.data.kawrie_reference)
.digest('hex');
return received === expected;
}
app.listen(3000);
See the Webhooks Guide for the full signature verification walkthrough.