agentleFS
Hive-AcademyHive-Academy/ptah-extension/.claude/skills/saas-platform-patterns/SKILL.md

saas-platform-patterns

Skill29 starsChanged 2 months agoMITView on GitHub

SaaS monetization patterns for NestJS. Use when designing free, trial and paid tiers, license keys, subscription state machines, checkout and customer portal flows, or pricing configuration. Not for webhook plumbing — use webhook-architecture.

The file

Files
SKILL.md

SaaS Platform Patterns

Patterns for turning a NestJS backend into a monetizable SaaS platform. These patterns cover the business layer that sits above payment provider integration (Stripe, Paddle, LemonSqueezy, etc.) and below the API surface. Provider-agnostic by design -- swap payment providers without rewriting business logic.

Activation scope

This skill activates per-module during Stage B of the SaaS bootstrap, against Billing & Monetization roadmap items (billing-*, licensing-*, subscription-*, checkout-*). saas-workspace-initializer deliberately keeps billing out of the Stage A foundation — pull this skill in only when working a specific monetization roadmap item in its own session.

Monetization Models

ModelDescriptionComplexityBest For
FreemiumFree tier + paid tiers with feature gatingMediumDeveloper tools, productivity apps
Trial-to-PaidTime-limited full access, then paywallLowB2B, enterprise software
Usage-BasedPay per API call, storage, or computeHighAPIs, infrastructure, cloud tools
Seat-BasedPer-user pricing within an organizationMediumTeam collaboration, project management

Most SaaS products combine models (e.g., freemium + seat-based). Start with the simplest model that validates your pricing, then layer complexity.

Architecture Overview

+-------------------------------------------------------+
|  Plan Configuration                                    |
|  Static tier definitions, feature flags, pricing       |
|  plans.config.ts                                       |
+---------------------------+---------------------------+
                            |
+---------------------------v---------------------------+
|  License System                                       |
|  Key generation, verification, status tracking        |
|  license.service.ts + License model                   |
+---------------------------+---------------------------+
                            |
+---------------------------v---------------------------+
|  Subscription Engine                                  |
|  State machine driven by payment webhooks             |
|  subscription.service.ts + Subscription model         |
+---------------------------+---------------------------+
                            |
+---------------------------v---------------------------+
|  Checkout Flow                                        |
|  Pre-checkout validation, portal session, price map   |
|  checkout.service.ts                                  |
+---------------------------+---------------------------+
                            |
+---------------------------v---------------------------+
|  Trial Manager                                        |
|  Period tracking, reminder emails, auto-downgrade     |
|  trial.service.ts (or within subscription.service)    |
+-------------------------------------------------------+

Data flows downward through webhooks: Payment provider emits events, the webhook layer (see webhook-architecture skill) routes them into the subscription engine, which updates licenses and triggers side-effects (emails, SSE pushes, audit logs).

Quick Start: Freemium Model

1. Define Plans

// src/config/plans.config.ts
export interface PlanDefinition {
  readonly name: string;
  readonly slug: string;
  readonly monthlyPrice: number;
  readonly yearlyPrice: number;
  readonly features: readonly string[];
  readonly limits: Readonly<Record<string, number>>;
}

export const PLANS = {
  community: {
    name: 'Community',
    slug: 'community',
    monthlyPrice: 0,
    yearlyPrice: 0,
    features: ['basic_chat', 'community_support'],
    limits: { messages_per_day: 50, storage_mb: 100 },
  },
  pro: {
    name: 'Pro',
    slug: 'pro',
    monthlyPrice: 9,
    yearlyPrice: 90,
    features: ['basic_chat', 'advanced_chat', 'priority_support', 'api_access'],
    limits: { messages_per_day: 1000, storage_mb: 10_000 },
  },
  enterprise: {
    name: 'Enterprise',
    slug: 'enterprise',
    monthlyPrice: 49,
    yearlyPrice: 490,
    features: ['basic_chat', 'advanced_chat', 'priority_support', 'api_access', 'sso', 'audit_log', 'custom_branding'],
    limits: { messages_per_day: -1, storage_mb: -1 }, // -1 = unlimited
  },
} as const satisfies Record<string, PlanDefinition>;

export type PlanSlug = keyof typeof PLANS;

2. Generate License Keys

// src/services/license.service.ts
import { Injectable, Logger } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { randomBytes } from 'crypto';

@Injectable()
export class LicenseService {
  private readonly logger = new Logger(LicenseService.name);

  constructor(private readonly prisma: PrismaService) {}

  async createLicense(userId: string, plan: string, createdBy: string): Promise<string> {
    // Revoke existing active licenses
    await this.prisma.license.updateMany({
      where: { userId, status: 'active' },
      data: { status: 'revoked' },
    });

    const licenseKey = `lic_${randomBytes(32).toString('hex')}`;

    await this.prisma.license.create({
      data: { userId, licenseKey, plan, status: 'active', createdBy },
    });

    this.logger.log(`License created for user ${userId}, plan: ${plan}`);
    return licenseKey;
  }

  async verify(licenseKey: string) {
    const license = await this.prisma.license.findUnique({
      where: { licenseKey },
    });

    if (!license || license.status !== 'active') {
      return { valid: false, plan: 'community' };
    }

    if (license.expiresAt && license.expiresAt < new Date()) {
      await this.prisma.license.update({
        where: { id: license.id },
        data: { status: 'expired' },
      });
      return { valid: false, plan: 'community' };
    }

    return { valid: true, plan: license.plan, expiresAt: license.expiresAt };
  }
}

3. Handle Subscription Webhooks

// src/services/subscription.service.ts (excerpt)
async handleSubscriptionCreated(data: SubscriptionEventData): Promise<void> {
  const plan = this.mapPriceIdToPlan(data.priceId);

  // Upsert user
  const user = await this.prisma.user.upsert({
    where: { email: data.customerEmail },
    update: { externalCustomerId: data.customerId },
    create: { email: data.customerEmail, externalCustomerId: data.customerId },
  });

  // Create subscription record
  await this.prisma.subscription.create({
    data: {
      userId: user.id,
      externalSubscriptionId: data.subscriptionId,
      externalCustomerId: data.customerId,
      status: data.trialEnd ? 'trialing' : 'active',
      priceId: data.priceId,
      currentPeriodEnd: new Date(data.currentPeriodEnd),
      trialEnd: data.trialEnd ? new Date(data.trialEnd) : null,
    },
  });

  // Provision license
  await this.licenseService.createLicense(user.id, plan, `webhook_${data.eventId}`);
}

Module Structure

libs/
├── shared/
│   ├── licensing/
│   │   ├── config/
│   │   │   └── plans.config.ts         # Tier definitions
│   │   ├── services/
│   │   │   ├── license.service.ts       # Key generation and verification
│   │   │   ├── subscription.service.ts  # State machine and webhook handlers
│   │   │   ├── checkout.service.ts      # Pre-checkout and portal
│   │   │   └── trial.service.ts         # Trial period management
│   │   ├── controllers/
│   │   │   ├── license.controller.ts    # Public verification endpoint
│   │   │   └── subscription.controller.ts # Checkout and portal endpoints
│   │   ├── guards/
│   │   │   └── plan.guard.ts            # @RequirePlan() decorator guard
│   │   └── licensing.module.ts
│   └── payments/                        # Payment provider abstraction
│       └── ...                          # (see nestjs-backend-patterns)

Decision Matrix

NeedPatternReference
Define tiers, features, limitsPlan configuration + feature gatingfreemium-model.md
Generate and verify license keysLicense lifecycle + status machinelicense-lifecycle.md
React to payment webhooksSubscription state machinesubscription-state-machine.md
Checkout, portal, price mappingCheckout flow + reconciliationcheckout-and-portal.md
Webhook processing infrastructure3-layer webhook architectureSee webhook-architecture skill
Payment provider abstractionProvider patternSee nestjs-backend-patterns skill

Integration with Other Skills

  • webhook-architecture: Provides the HTTP layer that receives payment events. The subscription state machine in this skill is the business service (Layer 3) that webhook routing invokes.
  • nestjs-backend-patterns: Provides the provider pattern for swappable payment SDKs. This skill consumes the abstract payment client.
  • nx-workspace-architect: Guides library placement. Licensing lives in shared/licensing, payment adapters in shared/payments.

Anti-Patterns

Anti-PatternWhy It FailsDo Instead
Storing plan details in DBSchema migrations for pricing changesStatic config file, deploy to change plans
Trusting client-side feature flagsUsers can modify frontend flagsAlways verify plan server-side via guard or service
Checking subscription status on every requestHammers payment API, adds latencyCache locally, sync on webhook events
Coupling business logic to payment providerCannot switch providersAbstract via interface, provider-specific adapters
Skipping idempotency on webhooksDuplicate charges, duplicate licensesDeduplicate by event ID (see webhook-architecture)

References

Load these for detailed implementation guidance:

  • freemium-model.md - Tier architecture, feature gating, trial management, upgrade/downgrade flows
  • license-lifecycle.md - Key generation, Prisma schema, status machine, verification endpoint, provisioning
  • subscription-state-machine.md - Subscription model, state transitions, webhook handlers, status resolution
  • checkout-and-portal.md - Pre-checkout validation, checkout flow, portal integration, price mapping, reconciliation

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.