Every SaaS billing integration eventually hits the same milestone: a customer's renewal fails, and you decide to listen to Stripe's invoice.payment_failed webhook to send a recovery email.
The webhook handler takes 20 minutes to write. The edge cases take months to discover.
1. Extract the actual decline_code
When invoice.payment_failed fires, don't treat all failures the same way. Inspect the underlying payment_intent.last_payment_error.decline_code :
const event = stripe . webhooks . constructEvent ( rawBody , sig , webhookSecret ) ;
if ( event . type === "invoice.payment_failed" ) { const invoice = event . data . object ; if ( ! invoice . subscription ) return ;
// Branch on soft vs.…