Key Engineering Takeaways
- Always verify cryptographic webhook signatures before parsing payload parameters.
- Implement unique idempotency keys on payment authorization requests to prevent duplicate charges.
- Record raw webhook event IDs in an audit table before triggering asynchronous business logic.
- Design idempotent event consumers so duplicate webhook deliveries produce identical system state.
1. The Multi-Failure Problem: Why Network Timeouts Cause Ghost Charges
In distributed fintech systems, network requests fail in two distinct ways: either the request never reached the payment gateway, or the payment gateway processed the charge successfully but the response dropped before reaching your server.
If your application blindly retries the request without an `Idempotency-Key` header, the customer’s credit card will be charged a second time. This damages brand trust and triggers costly chargebacks.
2. Cryptographic Signature Verification & Replay Protection
Never trust an incoming webhook based on IP addresses alone. We enforce strict HMAC-SHA256 signature validation against the raw HTTP request body and check the timestamp tolerance window (rejecting events older than 300 seconds) to defend against replay attacks.
3. Designing Idempotent Webhook Consumer Queues
Payment providers guarantee "at least once" webhook delivery, meaning your webhook endpoint may receive the exact same `invoice.payment_succeeded` event twice within a 5-minute window.
Our architecture persists the gateway’s unique event ID (`evt_...`) with a database unique constraint before processing. If an event is encountered a second time, the worker acknowledges it immediately with HTTP 200 without executing duplicate balance increases.
Building a product with Stripe?
Discuss your system architecture, sprint requirements, and fixed milestone budget with our engineering team.
