Many transactions do not finish instantly. A recharge may sit as pending while the operator confirms it; a payout may take time to reach the bank. Instead of making you ask again and again, the provider sends a callback (also called a webhook): an HTTP request to a URL on your server, carrying the final status. Handled well, callbacks keep your users' wallets accurate. Handled badly, they are a door for fraud.
Why callbacks need protection
Your callback URL is public. Anyone who discovers it can send a fake "success" or "failed" message. A fake "failed" callback, for example, could trick your system into refunding a user for a recharge that actually succeeded. So your handler must answer three questions for every callback:
- Did this really come from the provider?
- Has it been modified on the way?
- Have I already processed it?
Signatures: proving the callback is genuine
The usual method is an HMAC signature. The provider and you share a secret (separate from your API key). The provider computes a hash of the raw request body using that secret and sends it in a header. You compute the same hash on your side; if they match, the body is genuine and unchanged.
The header name, algorithm and exact signed content vary between APIs, so check our API documentation for the specifics. The pattern looks like this.
PHP
$raw = file_get_contents('php://input');
$received = $_SERVER['HTTP_X_SIGNATURE'] ?? '';
$expected = hash_hmac('sha256', $raw, getenv('PEPPLO_WEBHOOK_SECRET'));
if (!hash_equals($expected, $received)) {
http_response_code(401);
exit('invalid signature');
}
$payload = json_decode($raw, true);
Node.js (Express)
const crypto = require('crypto');
app.post('/callbacks/pepplo', express.raw({ type: 'application/json' }), (req, res) => {
const expected = crypto.createHmac('sha256', process.env.PEPPLO_WEBHOOK_SECRET)
.update(req.body).digest('hex');
const received = req.get('X-Signature') || '';
const ok = received.length === expected.length &&
crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(received));
if (!ok) return res.status(401).send('invalid signature');
const payload = JSON.parse(req.body.toString('utf8'));
// queue payload for processing
res.status(200).send('OK');
});
Two details matter:
- Use the raw body. If your framework parses JSON first and you re-encode it, spacing or key order may change and the signature will not match.
- Use a constant-time comparison (
hash_equals,timingSafeEqual,hmac.compare_digest) rather than==.
Replay protection
A valid callback captured once could be sent again later. If the provider includes a timestamp in the signed data, reject callbacks older than a few minutes. In every case, record the provider's transaction reference and the status you applied, so a repeated callback changes nothing.
Process each transaction only once
Providers may send the same callback more than once, deliberately, if your server did not respond in time. Your handler must be idempotent: running it twice must have the same effect as running it once.
// Laravel-style pseudocode
DB::transaction(function () use ($payload) {
$txn = Transaction::where('reference', $payload['reference'])->lockForUpdate()->first();
if (!$txn || $txn->status !== 'pending') {
return; // unknown or already final: do nothing
}
if ($payload['status'] === 'failed') {
$txn->update(['status' => 'failed']);
Wallet::credit($txn->user_id, $txn->amount, 'Refund ' . $txn->reference);
} elseif ($payload['status'] === 'success') {
$txn->update(['status' => 'success', 'operator_ref' => $payload['operator_ref'] ?? null]);
}
});
Note the rule: only move a transaction out of pending. Never let a callback flip a success to failed (or the reverse) without manual review.
Respond fast, work later
Return a 200 as soon as you have verified and stored the callback. Do heavy work, such as sending SMS to the user or updating reports, in a background job. Slow handlers cause timeouts, and timeouts cause retries.
Extra layers
- Use HTTPS for your callback URL. If you need a certificate, DomainsRock provides SSL certificates and hosting.
- Use a hard-to-guess path, for example one containing a random token, but do not rely on that alone.
- If the provider publishes its callback source IPs, allow only those at the firewall.
- Log every callback (raw body, headers without secrets, result) for reconciliation.
Do not rely on callbacks alone
Callbacks can be delayed or lost through network issues. Run a scheduled job that checks the status API for transactions that have been pending longer than expected. Between callbacks and status checks, every transaction should reach a final state.
Testing checklist
- Valid signature: processed once.
- Wrong signature: rejected with 401, nothing changed.
- Same callback sent twice: second one ignored.
- Callback for unknown reference: logged, nothing changed.
- Slow database: handler still answers within the provider's timeout.
Our APIs support callbacks for asynchronous services. If you want help building a robust callback handler or reviewing an existing one, our API integration team can help.
0 comments