Menu

1 / 2

Your payment webhook will arrive twice. Is your database ready?

DEV Community: database·Isaac Tonyloi - SWE·9 days ago
#Dc4v0xXU
#devtodatabase#devto#dev#delivery#event
Long article (1771 words) — no AI summary yet.
Reading 0:00
15s threshold

Every payment provider retries webhooks. If your endpoint is slow, times out, or returns anything other than a 2xx, the provider sends the same event again. Sometimes it sends it again even when you did everything right, because the response got lost on the way back.

So at some point your handler will receive the same payment twice. The usual defence looks like this.

if not db . exists ( "SELECT 1 FROM credits WHERE event_id = ?" , event . id ) : db . execute ( "INSERT INTO credits ..." )

Enter fullscreen mode

Exit fullscreen mode

It works in every test, and it works in production most of the time. It fails when the two deliveries arrive close together, and then the customer gets paid twice.

I wanted to see exactly how that happens, so I reproduced it on a double-entry ledger I've been building in SQL Server.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.