Skip to main content
SavedMRR

Technical guide · 5 min read

How to handle Stripe's invoice.payment_failed webhook

Every DIY Stripe recovery system starts with listening to invoice.payment_failed. Here is how to handle it cleanly—and the edge cases that usually trip teams up in production.

1. Verify signatures and filter subscription invoices

Always verify stripe-signature against the raw request body, store processed event.id values for idempotency, and branch on the underlying Stripe decline code:

// 1. Verify the Stripe signature with the raw body
const event = stripe.webhooks.constructEvent(rawBody, sig, webhookSecret);

if (event.type === "invoice.payment_failed") {
  const invoice = event.data.object;
  // 2. Only trigger on subscription renewals, not one-off drafts
  if (!invoice.subscription) return;

  // 3. Inspect attempt_count and the underlying decline_code
  //    (expanded from payment_intent.last_payment_error.decline_code)
  //    - Hard declines (expired_card, lost_card, authentication_required): email on day 0
  //    - Soft declines (insufficient_funds, do_not_honor): wait 48h for Smart Retries
}

2. Four edge cases that break DIY dunning webhooks

  • Duplicate emails on Smart Retries: Stripe fires invoice.payment_failed on every retry attempt. Without state tracking per invoice, customers get emailed 4 times for one temporary insufficient_funds decline.
  • Canceling follow-ups when paid: You also have to listen to invoice.paid and customer.subscription.deleted to cancel queued follow-up emails.
  • 3D Secure / SCA renewals: authentication_required needs a hosted confirmation link, not a generic card-update form.
  • No visibility into expiring cards: invoice.payment_failed only fires after a payment fails—it doesn't warn you about cards expiring next month.

Frequently asked questions

Where is the decline code on Stripe's invoice.payment_failed event?
In most Stripe API versions, you inspect the invoice's payment_intent (or charge) to read last_payment_error.decline_code—for example insufficient_funds, expired_card, or authentication_required.
Should I email the customer on every invoice.payment_failed webhook?
No. Stripe fires invoice.payment_failed on every retry attempt. If you email on every event without checking attempt_count and decline_code, you will spam customers during temporary soft declines.
What breaks when teams build dunning webhooks in-house?
Common edge cases include webhook retries creating duplicate emails, ignoring 3D Secure (authentication_required), failing to cancel scheduled emails when invoice.paid arrives, and missing expiring-card pre-dunning.

Skip building webhook queues—audit your Stripe account in 2 minutes

SavedMRR runs a free read-only 90-day scan with zero webhook setup, and automates decline-aware dunning when you're ready.