Blog / Fintech and payments

Handling Paystack webhooks without crediting users twice

A user pays ₦25,000 into their wallet. Paystack sends your server a charge.success event. Your server is slow for a moment, the request times out on Paystack’s side, and Paystack sends the same event again. If your handler credits the wallet each time it hears about the payment, that user now has ₦50,000.

This is not an edge case. Gateways retry by design, because they cannot know whether your server received an event unless you answer with a success response in time. Every webhook handler has to assume it will see the same event more than once, and sometimes two copies at the same moment.

A webhook is attempted, not delivered. Write the handler as if every event arrives twice.

Verify before anything else

Anyone can send a request to your webhook address. Before you trust the body, check the signature header Paystack sends against a hash of the raw request body made with your secret key. If it does not match, stop there and log it.

Then confirm the transaction with Paystack’s verify endpoint, using the reference from the event. The amount you credit should come from Paystack’s answer, not from the request body. It costs one extra call and closes off a whole class of forged or tampered events.

Make the reference unique in the database

The fix for duplicates belongs in the database, not in application code. Store every processed event with its transaction reference, and put a unique constraint on that column. Two copies of the same event arriving at the same moment will both pass an if already_processed check written in code, because neither has finished yet. They cannot both insert the same unique row.

BEGIN;
INSERT INTO processed_events (reference, amount_kobo)
VALUES ('evt_8f2a', 2500000);
-- a second delivery fails here on the unique reference
INSERT INTO ledger_entries (wallet_id, amount_kobo, reference)
VALUES (4417, 2500000, 'evt_8f2a');
COMMIT;

Both inserts sit in one transaction. If the event was already processed, the first insert fails, the transaction rolls back, and nothing is credited. Your handler catches that specific failure and still answers with a success response, so Paystack stops retrying. Any other error should return a failure, so Paystack tries again later.

Store amounts in kobo as whole numbers. Floating point and money do not mix, and the bug they cause looks like a rounding error until it is a reconciliation break.

Never store the balance

If the wallet balance is a number you update, a duplicate that slips through changes it silently, and nobody can say which request did it. If the balance is the sum of ledger entries, every credit is a row you can see, trace to a reference and reverse with another row. That is the difference between “the balance looks wrong” and “here is the exact entry that made it wrong”.

Summing every entry on every page load gets slow eventually. When it does, keep a cached balance that is rebuilt from the entries and checked against them, never edited on its own.

Answer fast, work later

Do the verification and the insert, answer with a success response, then put slower work such as emails, SMS and notifications on a queue. A handler that sends an email before it responds is a handler that times out and invites another retry.

Test it like it will be attacked

Most teams test the webhook once, see the credit appear and move on. Test the cases that actually happen:

  • Send the same event twice in a row. One credit.
  • Send the same event twice at the same moment, from two processes. Still one credit.
  • Send an event with a bad signature. Nothing, and a log entry.
  • Kill the process between the two inserts. Nothing, and the retry later succeeds.
  • Change the amount in the body. The verified amount wins.

The short version

Verify the signature. Confirm the amount with the gateway. Let a unique reference in the database decide whether the event is new. Keep the balance as a sum of entries. Respond quickly. Do those five things and the tenth delivery of an event changes exactly as much as the first: nothing.

This post explains a general approach. Check Paystack’s current documentation for the exact header names and endpoints before you build. The same pattern works for Flutterwave and Stripe.

Where this comes from. We do this work for clients. See payment gateway integration, or the case study on Gamestakers.

Questions about this post? Email contact@naxfront.com. Nancy reads every one.

WhatsApp