← Tous les articles

SaaS plan changes with Stripe: handling upgrades, downgrades and proration without billing mistakes

A customer upgrades mid-month; another wants to downgrade while exceeding the target plan's limits. Proration, amount previews, failed payments, end-of-period downgrades and webhook syncing: the approach we use on our SaaS projects, with PHP code.

SaaS plan changes with Stripe: handling upgrades, downgrades and proration without billing mistakes

Key points

  • Upgrade immediately and charge the prorated difference; downgrade at the end of the paid period. It’s the easiest rule to explain to customers.
  • Show the exact amount before confirming, and only switch the plan once the payment has gone through.
  • Your database doesn’t decide the plan: it receives it from Stripe webhooks, and it checks that the account fits the target plan’s limits before a downgrade.

A “Change plan” button looks harmless in a mockup. In production, it’s one of the most common sources of support tickets on the SaaS products we build: a charge the customer doesn’t understand, a premium plan unlocked even though the payment failed, a customer moved to the basic plan with fifteen users when it allows five. Stripe handles proration mechanics very well, but the business decisions are still yours. Here’s how we structure them, with the matching PHP code (the logic is the same with Stripe’s other libraries).

Settle the rules before writing code

The first question isn’t technical: what happens, from the customer’s point of view, when they change plan? We almost always propose the same rule because it’s easy to understand and explain:

  • Upgrades take effect immediately. The customer pays the difference for the time left in the period straight away and gets the new features at once.
  • Downgrades take effect at the next renewal. The customer has paid for the period and keeps it to the end; no credit notes or partial refunds to manage.
  • Monthly to annual is treated as an upgrade. Note that when the billing interval changes, Stripe resets the subscription’s billing anchor to the day of the change.

Other rules are possible (credit carried to the next invoice, immediate downgrade with a credit), but each variant complicates support and accounting. Write the chosen rule into your terms and onto the plan-change page: customers who were told don’t dispute their invoice.

In Stripe, these choices map to the proration_behavior parameter: create_prorations (the default, which carries the difference to the next invoice), always_invoice (invoice and collect now) or none (no proration). For an upgrade paid immediately, use always_invoice.

Show the exact amount before confirming

Nobody can work out a proration in their head: it depends on the exact second of the change, taxes and active discounts. Rather than recomputing it server-side and risking a mismatch, ask Stripe to preview the invoice the change would produce:

<?php
// How much will the customer pay if they upgrade right now?
$prorationDate = time(); // reuse exactly the same value for the real update

$preview = $stripe->invoices->createPreview([
    'customer'     => $sub->customer,
    'subscription' => $sub->id,
    'subscription_details' => [
        'items' => [[ 'id' => $sub->items->data[0]->id, 'price' => $newPriceId ]],
        'proration_behavior' => 'always_invoice',
        'proration_date'     => $prorationDate,
    ],
]);

// $preview->amount_due : amount charged now (in cents), proration lines included

Show that amount in the confirmation dialog along with the new renewal date and the next period’s price. Keep $prorationDate (in the session or a signed hidden field) and pass exactly the same value to the real update: the customer will then pay precisely what they were shown, even if they took two minutes to click.

Depending on how old your integration is, you may find the older preview method (invoices->upcoming()); recent API versions use createPreview. Check the API version set on your account before copying an example from the web.

Upgrades: unlock only once payment succeeds

The classic trap: update the subscription, switch the account to the higher plan in your database, then discover the card was declined. The customer enjoys a plan they haven’t paid for, and chasing it is painful. Stripe has a mode that solves this at the source:

<?php
// Upgrade: takes effect now, prorated amount invoiced now
$stripe->subscriptions->update($sub->id, [
    'items' => [[ 'id' => $sub->items->data[0]->id, 'price' => $newPriceId ]],
    'proration_behavior' => 'always_invoice',
    'proration_date'     => $prorationDate,
    // If payment fails, Stripe does NOT apply the change
    'payment_behavior'   => 'pending_if_incomplete',
], [
    'idempotency_key' => "upgrade-{$sub->id}-{$newPriceId}-{$prorationDate}",
]);

With payment_behavior => 'pending_if_incomplete', the change stays pending until the proration invoice is paid. If the payment fails, or strong customer authentication isn’t completed, the subscription stays on the old plan and the pending update expires. In the UI, if the payment needs customer action (3-D Secure), fetch the invoice and its payment to show the authentication step; only then does the plan change.

The idempotency key prevents double charges if the user double-clicks or your request is retried after a network glitch. Build it from stable data about the operation, not a random ID generated on each attempt.

Downgrades: defer the change and check the limits

To apply the new plan at renewal, Stripe needs to know a change is scheduled. Subscription schedules exist for exactly this: one phase for the current period, another for what follows.

<?php
// Downgrade: keep the current plan until the end of the paid period
$schedule = $stripe->subscriptionSchedules->create(['from_subscription' => $sub->id]);
$current  = $schedule->phases[0];

$stripe->subscriptionSchedules->update($schedule->id, [
    'end_behavior' => 'release', // the subscription goes back to normal after the last phase
    'phases' => [
        [   // current phase, unchanged
            'items'      => [[ 'price' => $current->items[0]->price, 'quantity' => 1 ]],
            'start_date' => $current->start_date,
            'end_date'   => $current->end_date,
        ],
        [   // new plan from the next renewal
            'items'      => [[ 'price' => $newPriceId, 'quantity' => 1 ]],
            'iterations' => 1,
        ],
    ],
]);

Depending on the API version, the new phase’s length is expressed with iterations or a duration parameter; what matters is that the current phase ends on the already-paid period’s end date. Also store the upcoming plan in your database (a pending_plan column, say) so you can show it: “Your plan will switch to Starter on 3 December”, with a button to cancel the change.

Then comes the trickiest business question: what if the account exceeds the target plan’s limits (more users, projects or storage than allowed)? We prefer blocking the downgrade with a clear message saying what to do (“deactivate 3 users to switch to this plan”) over deleting or hiding data automatically. Run this check when the request is made and at renewal, because the customer may have added users in between. If your per-plan limits live in one place in the code, the check is a single function.

Keep your database in sync with webhooks

The last principle is the most important: your application mustn’t decide a customer’s plan on its own. A change can come from your UI, but also from Stripe’s customer portal, from your team acting in the Stripe dashboard, or from a schedule running at renewal. The only way to stay correct is to listen for customer.subscription.updated and derive the plan from it:

<?php
// Webhook customer.subscription.updated : the single source of truth for the plan
function onSubscriptionUpdated(object $sub, PDO $db): void
{
    $priceId = $sub->items->data[0]->price->id;
    $plan    = Plan::fromStripePrice($priceId); // price -> plan lookup

    $db->prepare('UPDATE accounts SET plan = :plan, plan_period_end = :end,
                  pending_plan = NULL WHERE stripe_subscription_id = :id')
       ->execute([
           'plan' => $plan->value,
           'end'  => date('Y-m-d H:i:s', $sub->items->data[0]->current_period_end),
           'id'   => $sub->id,
       ]);
}

The handler must be idempotent (Stripe can deliver the same event more than once) and tolerate out-of-order delivery: re-fetch the subscription from the API if you’re unsure the payload is current. Access rights in your app are then computed from the plan column, never from whatever the browser claims.

Before going live, test every scenario with Stripe test clocks, which let you fast-forward to renewal: an upgrade with a failing card, a scheduled downgrade then cancelled, a downgrade on an account over the limits, a monthly-to-annual switch. Half a day of testing saves weeks of fixing invoices by hand.

FAQ

Should I refund the difference when a customer downgrades?

Not if your terms say downgrades take effect at renewal: the customer keeps the plan they paid for until the period ends. It’s the simplest rule for support and accounting.

What happens if the proration payment fails?

With payment_behavior set to pending_if_incomplete, Stripe doesn’t apply the change and the customer stays on their current plan. Show them the error and offer to update their payment method.

Can I handle plan changes without webhooks?

Not reliably: a change can come from the customer portal, your team or a schedule running at renewal. Without webhooks, your database will eventually show a plan that doesn’t match what’s being billed.

Is your SaaS mis-billing plan changes, or are you about to launch paid plans? We design and integrate subscription billing with Stripe: clear rules, secure payments and reliable syncing.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

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

Parler de votre projet →