← Tous les articles

Handling failed subscription payments in a SaaS without losing the customer

An expired card shouldn't lock a customer out overnight. Here's how to handle past_due and unpaid statuses, payment reminders and a grace period in a SaaS billed through Stripe.

Handling failed subscription payments in a SaaS without losing the customer

Key points

  • A declined payment isn’t a cancellation: past_due gives the customer time to fix it.
  • Re-fetch the subscription from the API on every webhook, because events can arrive out of order.
  • Fall back to read-only access instead of a hard lockout, so the customer can still pay and export.

In most SaaS codebases we inherit, failed payments are handled in a single line: if the subscription isn’t “active”, access is gone. So a loyal customer whose card expired while they were on holiday gets locked out on a Monday morning, without a single email warning them. They call support, and sometimes they leave. A failed payment is a billing incident, not churn, and your application should treat it that way.

What Stripe does when a charge fails

When a subscription invoice fails, Stripe moves the subscription to past_due and retries according to your account’s revenue recovery settings. Once all retries are exhausted, the subscription becomes canceled or unpaid, or stays past_due, depending on what you picked in those settings. You don’t need to rebuild that schedule; you just need to listen to the right events.

$event = StripeWebhook::constructEvent(
    $payload,
    $_SERVER['HTTP_STRIPE_SIGNATURE'] ?? '',
    $webhookSecret
);

switch ($event->type) {
    case 'customer.subscription.updated':
    case 'customer.subscription.deleted':
        // Re-read the subscription: events can arrive out of order
        $sub = $stripe->subscriptions->retrieve($event->data->object->id);
        $accounts->syncSubscription($sub->customer, $sub->id, $sub->status);
        break;

    case 'invoice.payment_failed':
        $invoice = $event->data->object;
        $mailer->paymentFailed(
            $invoice->customer,
            $invoice->hosted_invoice_url,   // page where the customer can pay
            $invoice->next_payment_attempt  // null when Stripe will not retry
        );
        break;

    case 'invoice.payment_action_required':
        // 3-D Secure: the bank wants the customer to confirm
        $mailer->paymentNeedsConfirmation($event->data->object->customer, $event->data->object->hosted_invoice_url);
        break;

    case 'invoice.paid':
        $accounts->clearPaymentWarning($event->data->object->customer);
        break;
}

http_response_code(200);

The detail that cost us the most time on one project: webhooks don’t necessarily arrive in order. An older customer.subscription.updated can land after a newer one and flip a customer who has just paid back to past_due. Re-reading the subscription from the API, rather than trusting the event payload, makes that impossible.

Turn the status into an access level

Instead of checking Stripe statuses all over the codebase, we keep the rule in one function. The rest of the app only knows about access levels:

enum Access
{
    case Full;
    case Warning;   // full access + "update your card" banner
    case ReadOnly;  // can log in and export, cannot create
    case None;
}

function accessFor(string $status): Access
{
    return match ($status) {
        'active', 'trialing' => Access::Full,
        'past_due'           => Access::Warning,
        'unpaid', 'paused'   => Access::ReadOnly,
        default              => Access::None, // canceled, incomplete, incomplete_expired
    };
}

Read-only mode is the one that saves the most support tickets. The customer logs in, sees their data, can export it, and finds an “Update payment method” button. Nothing suggests their account has been deleted.

Reminders people actually act on

  • A direct link. The invoice’s hosted_invoice_url opens a page where the customer can pay in two clicks, without logging into your app.
  • The next retry date. next_payment_attempt lets you write “we’ll try again on the 4th” instead of a vague “your payment failed”.
  • An in-app banner. Plenty of people never read billing emails. The Warning level shows a banner to the account’s admins, not to every user on the team.
  • 3-D Secure. Some banks ask the customer to confirm the payment. That isn’t a decline, and the message should say so.

Test it before it happens in production

Stripe provides test cards that fail on renewal, and the Stripe CLI can fire events at your local environment with stripe trigger invoice.payment_failed. We always test at least three paths: failure then successful payment, final failure, and 3-D Secure confirmation. It’s also a good moment to check that the webhook replies quickly and hands email sending off to a queue.

FAQ

How long should a customer stay in past_due?

It depends on your billing cycle and your customers. For monthly plans, anything from a few days to two weeks is common. What matters is that the period is stated in your emails and is the same for everyone.

Should I delete data when a subscription is cancelled for non-payment?

Not straight away. Keep it for a period stated in your terms, read-only or archived, then delete it. A customer who comes back three weeks later will be glad to find everything where they left it.

Losing SaaS customers to badly handled failed payments? We design and rework billing for SaaS products, from Stripe integration to the user experience.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

Agence web : sites WordPress sur mesure, thèmes premium et logiciels SaaS.

Parler de votre projet →