> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stash.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# Subscription Flow

> Understand the subscription lifecycle, state transitions, and how subscriptions move between trialing, active, past_due, canceled, and expired states. Learn about billing periods, grace periods, and renewal processing.

This guide explains how subscriptions move through their lifecycle, from creation to expiration.

## Subscription states

A subscription is always in one of these states:

| State | Description | Access |
| - | - | - |
| `trialing` | Subscription is in free trial period | Yes |
| `active` | Subscription is current and paid | Yes |
| `past_due` | Payment failed, in grace period | Yes |
| `canceled` | Cancellation scheduled for period end | Yes |
| `expired` | Subscription ended | No |

## State machine

```mermaid theme={null}
stateDiagram-v2
[*] --> trialing: Subscribe (with trial)
[*] --> active: Subscribe (no trial)
trialing --> active: Trial ends
trialing --> canceled: User cancels
active --> past_due: Payment failed
active --> canceled: User cancels
past_due --> active: Payment succeeds
past_due --> expired: Grace period ends
canceled --> active: Reactivate
canceled --> expired: Period ends
expired --> [*]
```

## Lifecycle events

### New subscription

When a user subscribes through a checkout link:

1. Subscription created with `active` status
2. `current_period_end` set based on billing period
3. `next_billing_date` scheduled for renewal
4. Webhook `subscription.created` sent

```mermaid theme={null}
sequenceDiagram
actor User
participant App
participant Stash

User->>App: Subscribe to plan
App->>Stash: Create checkout
Stash-->>User: Show payment
User->>Stash: Complete payment
Stash->>App: subscription.created webhook
App->>User: Grant access
```

### Successful renewal

When a subscription renews successfully:

1. Payment processed on `next_billing_date`
2. `current_period_end` extended by billing period
3. `next_billing_date` updated
4. Webhook `subscription.payment_succeeded` sent

### Failed payment

When a renewal payment fails:

1. Status changes to `past_due`
2. User retains access during grace period
3. Stash retries payment automatically
4. Webhook `subscription.payment_failed` sent

> [!NOTE]
>
> The default grace period is 48 hours with payment retries at 24h and 48h.
> During this time, the user keeps access while Stash attempts to recover the
> payment. Grace period duration and retry schedule can be configured based on
> your needs.

If payment succeeds during grace period:

* Status returns to `active`
* Webhook `subscription.payment_succeeded` sent

If all retries fail:

* Status changes to `expired`
* Webhook `subscription.expired` sent
* User loses access

### Cancellation

When a user cancels:

1. Status changes to `canceled`
2. `cancel_at_period_end` set to `true`
3. `canceled_at` timestamp recorded
4. Webhook `subscription.canceled` sent
5. User keeps access until `current_period_end`

When the period ends:

* Status changes to `expired`
* Webhook `subscription.expired` sent

```mermaid theme={null}
sequenceDiagram
actor User
participant App
participant Stash

User->>App: Request cancellation
App->>Stash: Cancel subscription
Stash-->>App: Subscription canceled
Stash->>App: subscription.canceled webhook
App->>User: Confirm cancellation
Note over User,Stash: User retains access until period end
Stash->>App: subscription.expired webhook
App->>User: Revoke access
```

### Plan change / upgrade checkout

Players can upgrade a subscription via a **subscription change checkout link** created with [Create a subscription change checkout link](https://docs.stash.gg/api/ingress/subscription-checkout-links/createsubscriptionchangecheckoutlink.md) (`POST /sdk/subscriptions/{subscriptionId}/change-checkout-links`). While `active`, `trialing`, or **canceled but still before `current_period_end`**, they can open that link and complete checkout; creating the link does not by itself reactivate a canceled subscription.

If they **complete** checkout successfully, Stash applies the change (and reactivates if they were canceled-in-period). If they **abandon** checkout, subscription state is unchanged.

### Reactivation

A canceled subscription can be reactivated before it expires:

1. Call [Reactivate Subscription](https://docs.stash.gg/api/ingress/subscriptions/reactivatesubscription.md), **or** complete a subscription change checkout successfully (upgrade flow).
2. Status returns to `active`
3. `cancel_at_period_end` set to `false`
4. Webhook `subscription.reactivated` sent (when applicable)

```mermaid theme={null}
sequenceDiagram
actor User
participant App
participant Stash

User->>App: Request reactivation
App->>Stash: Reactivate subscription
Stash-->>App: Subscription reactivated
Stash->>App: subscription.reactivated webhook
App->>User: Confirm reactivation
Note over User,Stash: Subscription resumes, will renew at period end
```

> [!WARNING]
>
> Reactivation is only possible while the subscription is in `canceled` status.
> Once it reaches `expired`, the user must create a new subscription.

## Billing period

The `period` object defines how often the subscription renews:

```json theme={null}
{
  "period": {
    "value": 1,
    "unit": "month"
  }
}
```

Supported units: `day`, `week`, `month`, `year`

Examples:

* Weekly: `{ "value": 1, "unit": "week" }`
* Monthly: `{ "value": 1, "unit": "month" }`
* Quarterly: `{ "value": 3, "unit": "month" }`
* Yearly: `{ "value": 1, "unit": "year" }`

## Key dates

| Field | Description |
| - | - |
| `current_period_end` | When the current billing period ends |
| `next_billing_date` | When the next payment will be attempted |
| `access_end_date` | When user access expires |
| `trial_end` | When the trial period ends (if applicable) |
| `canceled_at` | When the subscription was canceled |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.