Skip to main content
Dev Kit for AI provides a complete subscription management system built on Stripe, handling the full lifecycle from checkout to cancellation. This guide covers all billing workflows, subscription states, and use cases for SaaS monetization.

Subscription Lifecycle Overview

The subscription system manages the complete payment lifecycle through Stripe integration:

Subscription States

StateDescriptionUser Access
activeSubscription is current and paidFull access
trialingUser is in a trial periodFull access
past_duePayment failed, retryingLimited access (configurable)
cancelledSubscription endedNo access
incompleteInitial payment pendingNo access
pausedSubscription temporarily pausedNo access
unpaidAll payment retries exhaustedNo access

Core Billing Workflows

Checkout Session Flow

When a user subscribes, the system creates a Stripe Checkout Session and redirects them to Stripe’s hosted payment page:

Webhook Event Processing

Cloud API processes Stripe webhook events to maintain subscription state:

Handled Webhook Events

EventAction Taken
checkout.session.completedCreate subscription, link Stripe customer to user
customer.subscription.createdRecord new subscription with status and period
customer.subscription.updatedUpdate status, plan, or cancellation flags
customer.subscription.deletedMark subscription as cancelled
invoice.paidRecord payment, extend subscription period
invoice.payment_failedMark subscription as past_due
charge.refundedRecord refund in payment history

Use Case Scenarios

Dev Kit for AI handles all common subscription scenarios out of the box:

New Subscription

A user without an existing subscription initiates checkout, completes payment, and receives an active subscription:
The system creates a Stripe customer record and stores stripe_customer_id on the user for future transactions.

Returning Customer

When a user who previously had a subscription (cancelled or expired) subscribes again, the system reuses their existing Stripe customer ID:
Returning customers see their saved payment methods in Stripe Checkout, enabling faster re-subscription.

Duplicate Prevention

If a user with an active subscription attempts to start another checkout, the system blocks it and directs them to manage their existing subscription:
// Cloud API validates subscription status before creating checkout
const existingSubscription = await getActiveSubscription(userId, projectId);
if (existingSubscription) {
  return { error: "Active subscription exists", redirectTo: "/billing" };
}

Upgrade/Downgrade

Users can change their subscription plan through the customer portal or update API:
Change TypeBilling Behavior
UpgradeProrated charge immediately
DowngradeCredit applied to next invoice

Cancellation

Users can cancel their subscription immediately or at the end of the billing period:
User retains access until the current billing period expires.

Uncancellation

When a user who scheduled cancellation at period end changes their mind, they can restore their subscription:
Users can manage their subscription through the Stripe Customer Portal, accessible via the /customer-portal endpoint.

Subscription Renewal

When recurring billing succeeds, the system records the payment and extends the subscription:

Trial Periods

Subscriptions can include trial periods where users access features before their first payment:

Payment Failure

When a recurring payment fails, Stripe automatically retries and the system tracks the status:
Configure your application to gracefully degrade or restrict access for users with past_due subscriptions.

Customer Portal Access

Users can manage their subscription through Stripe’s hosted customer portal:

Subscription Data Model

The Cloud API stores subscription data with these key fields:
FieldTypeDescription
idUUIDUnique subscription identifier
user_idUUIDAssociated end user
project_idUUIDProject scope
stripe_subscription_idstringStripe subscription ID
stripe_customer_idstringStripe customer ID
statusenumCurrent subscription state
price_idstringStripe price ID for the plan
current_period_starttimestampCurrent billing period start
current_period_endtimestampCurrent billing period end
cancel_at_period_endbooleanScheduled for cancellation
cancelled_attimestampWhen cancellation was requested

Payment History

All transactions are recorded for user access and developer reporting:

End-User Billing Endpoints

End users access their billing information through these Cloud API endpoints:

Developer Analytics

Developers can monitor subscription and payment metrics through Cloud Admin or the API:

Aggregated Statistics

The /api/v1/payments/stats endpoint provides:
  • Total active subscriptions
  • Total revenue (by period)
  • Subscription counts by status
  • Transaction counts and totals

Per-Project Analytics

Each project’s Payments tab in Cloud Admin displays: (((REPLACE_THIS_WITH_IMAGE: console-project-payments-analytics.png: Project payments tab showing subscription stats, transaction history, and revenue metrics)))
  • Active subscription count
  • Monthly recurring revenue
  • Recent transactions
  • Subscription/cancellation trends

Integration Checklist

1

Configure Stripe

Set up API keys and webhook endpoints in Cloud Admin
2

Create Stripe Products

Define your subscription plans in the Stripe Dashboard
3

Implement Checkout UI

Add subscription buttons that call the checkout session endpoint
4

Handle Subscription States

Check user subscription status to gate access to premium features
5

Add Billing Management

Implement billing UI components for end users
6

Test Thoroughly

Use Stripe test mode to verify all workflows

Next Steps