← Tous les articles

SaaS: handling plan limits without scattering if statements everywhere

Project caps, seat limits, API access: subscription rules tend to leak into every corner of the codebase. A single entitlements class keeps them in one place and makes pricing changes painless.

SaaS: handling plan limits without scattering if statements everywhere

Key points

  • Plan limits must be enforced on the server, not just hidden in the UI.
  • A single entitlements class replaces plan-name checks scattered across the codebase.
  • A customer who downgrades should never lose data: block new items, keep read access.

Most SaaS applications we take over share the same pattern: if ($user->plan === 'pro') in controllers, templates and scheduled jobs. Then sales launch a mid-tier plan, or agree a special deal with a large customer, and someone has to hunt down every one of those conditions. One always slips through.

Ask about permissions, not plan names

Business code should never ask “which plan is this customer on?”. It should ask “is this account allowed to do this?”. That way, the whole mapping between plans and limits lives in one class:

final class Entitlements
{
    private const PLANS = [
        'starter' => ['projects' => 3,    'members' => 2,  'api' => false],
        'pro'     => ['projects' => 25,   'members' => 10, 'api' => true],
        'agency'  => ['projects' => null, 'members' => 50, 'api' => true], // null = unlimited
    ];

    public function __construct(private Account $account) {}

    public function allows(string $feature): bool
    {
        return (bool) ($this->limits()[$feature] ?? false);
    }

    public function canAdd(string $resource, int $current): bool
    {
        $limits = $this->limits();
        if (!array_key_exists($resource, $limits)) {
            return false;
        }
        return $limits[$resource] === null || $current < $limits[$resource];
    }

    private function limits(): array
    {
        $base = self::PLANS[$this->account->plan] ?? self::PLANS['starter'];
        return array_replace($base, $this->account->overrides ?? []);
    }
}

The overrides array, stored on the account, lets you give one Pro customer 30 projects without inventing a bespoke plan or touching the code.

Enforce it where it counts

Disabling a button is good UX, but it protects nothing: the API, a CSV import or a replayed request will go straight past it. The check belongs in the action that creates the resource:

public function store(Request $request, Entitlements $can)
{
    $count = Project::where('account_id', $request->account()->id)->count();

    if (!$can->canAdd('projects', $count)) {
        return response()->json(['error' => 'plan_limit', 'resource' => 'projects'], 402);
    }
    // ... create the project
}

Returning a specific error code (plan_limit) lets the front end show an upgrade prompt rather than a generic failure. In our experience, that screen is where a fair share of upgrades actually happen.

Where it breaks in production

  • Concurrent requests. Two requests that both count 2 projects can each create a third. For limits that really matter, count and insert inside one transaction that locks the account row (SELECT … FOR UPDATE).
  • Downgrades. A customer with 12 projects who moves to Starter keeps reading all of them; they just cannot add more while over the cap. Deleting or hiding data turns into support tickets and disputes.
  • Source of truth. Keep the plan in your own database, updated by your payment provider’s webhooks. Calling Stripe or Paddle on every request slows the app down and ties your uptime to theirs.
  • Background jobs. Exports, syncs and email sends must go through the same class, or a downgraded account keeps enjoying them.

Testing becomes easy

With the logic in one place, the tests are short: one set of cases per plan plus a few accounts with overrides. On our projects, those tests are what let us change a pricing grid on a Friday without holding our breath.

FAQ

Should plans live in the database instead of the code?

If plans rarely change, a constant versioned with the code is easier to read and test. A table makes sense once your sales team needs to create offers without a developer.

How should free trials fit in?

Treat the trial as a plan of its own (say “trial”, with Pro entitlements) plus an end date. Nothing else in the code needs to know it is a trial.

Planning new pricing, or inheriting a SaaS app that has become hard to change? Our team designs and evolves custom SaaS platforms.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

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

Parler de votre projet →