# Test Stripe Subscription Billing (/en/docs/billing/stripe/subscription/test)

Run this test after changing recurring products, Checkout, webhooks, the customer
portal or subscription access.

**Give your agent this prompt:**

```text
Test Stripe subscription billing in the test environment.
Follow https://coremvp.com/en/docs/billing/stripe/subscription/test.md. Complete https://coremvp.com/en/docs/billing/stripe/subscription/configure.md first. Ask me before changing provider settings or secrets; do not use live payments or print secrets.
Remove any conflicting BILLING_DEMO_PROVIDER override from the runner. Finish when ./coremvp e2e billing:subscription passes and both monthly and yearly cases complete in Stripe test Checkout with Stripe subscription state saved.
Report what changed, the check result, and anything you could not run.
```

## What the test does

The local test runs monthly and yearly cases; the hosted test runs monthly only. Each case:

1. signs up a new buyer;
2. subscribes through Stripe-hosted Checkout with Stripe's test card;
3. waits for the signed webhook and checks the stored Price through `/api/user/status` and `/api/user/subscription`, then checks the Active label and Manage Billing button on `/dashboard/billing`;
4. opens a real Stripe test-mode customer portal session;
5. opens paid content, then checks access is gone after signing out.

## Run it locally

Complete [Connect Stripe](/en/docs/billing/stripe/connect), including the customer
portal step. Keep the app and the filtered listener running, with the listener's
signing secret in ignored `.env.local`, and map both Pro Prices from the same test
account:

```dotenv title=".env.local" interactive=true
PAYMENT_PROVIDER=stripe
NEXT_PUBLIC_STRIPE_MODE=test
NEXT_PUBLIC_STRIPE_PRO_PRICE_ID_MONTHLY=price_replace_me
NEXT_PUBLIC_STRIPE_PRO_PRICE_ID_YEARLY=price_replace_me
```

Remove any conflicting `BILLING_DEMO_PROVIDER` from the runner environment.
It takes precedence over `PAYMENT_PROVIDER` in the checkout tests.

From the repository root, run:

```bash
./coremvp e2e billing:subscription
```

A passing run exits with code `0` after both Stripe monthly and yearly cases.
Check that both used Stripe test Checkout and saved a Stripe subscription. To sell
only one billing period, leave the other Price unmapped; that offer stays
unavailable. Local signup skips the confirmation email; see
[Auth testing](/en/docs/authentication/test-authentication) for that.

## Run it against your deployment

The deployed app stays in **Stripe test mode**; no real charge is made.

1. [Activate the customer portal in test mode](https://dashboard.stripe.com/test/settings/billing/portal).
2. In your ignored `.env`, add the deployment's `NEXT_PUBLIC_SUPABASE_URL`, its
   matching `SUPABASE_SECRET_KEY` and its monthly test Price as
   the following variable. Never print them, and stop if they
   belong to another Supabase project.

   ```text
   NEXT_PUBLIC_STRIPE_PRO_PRICE_ID_MONTHLY
   ```

   [Prepare the local runner](/en/docs/deployment/deploy-app#prepare-the-local-runner)
   lists every value.
3. Load `.env` and run the test against the exact deployed URL:

```bash interactive=true
set -a; source .env; set +a
export TEST_BASE_URL=https://<app-domain>
export PROD_E2E_AUTH_MODE=signup
./coremvp prod e2e billing:subscription
```

_Video: The subscription billing check in a browser, sped up: it signs up, subscribes with Stripe's test card, opens Billing and the customer portal, then the paid content (/docs/deployment/test-subscription.mp4)._

The clip is one passing hosted run on Vercel, at 2.5× speed, with the test
account's email hidden.

A passing run prints `result production E2E passed` and your target URL. For the
required deployment state, see
[Deploy and Test Your App](/en/docs/deployment/deploy-app#test-production).

## If the test fails

| Symptom                                    | Check                                                                                                                                                         |
| ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Checkout cannot start                      | Confirm `NEXT_PUBLIC_STRIPE_MODE=test` and the recurring Price inputs, then restart the app.                                                                  |
| Checkout asks for sign-in                  | Sign in first; recurring Checkout is not a guest checkout path.                                                                                               |
| Webhook requests return 400                | Copy the active listener signing secret into ignored `.env.local`, then restart `bun run dev`.                                                                |
| Local catalog is empty                     | Confirm the [filtered listener](/en/docs/billing/stripe/connect#forward-events-to-your-local-app) was running before the fixture created products and prices. |
| Portal cannot open                         | Confirm authentication and an owned Stripe management target in account status.                                                                               |
| Success page appears but access is missing | Check [webhook delivery](/en/docs/billing/stripe/webhooks) for the actual purchase first.                                                                     |

For Stripe products or catalog sync, return to
[Configure Stripe Subscription Billing](/en/docs/billing/stripe/subscription/configure).

## Check later subscription states

The test covers the first active subscription. Check renewals, failed payments
and cancellations by hand in Stripe test mode, with the app and listener running.
Find the test buyer's customer and subscription first:

```bash interactive=true
stripe customers list --email <buyer-email> --limit 1
stripe subscriptions list --customer <customer-id> --limit 1
```

Run the commands for one state, then reload `/dashboard/billing`.

| State                           | Expect on `/dashboard/billing`                                                                                  |
| ------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| Renewal succeeds                | The period moves forward and access stays.                                                                      |
| Payment enters retries          | Check the status after the webhook arrives; access follows the stored status.                                   |
| Customer schedules cancellation | The period end shows, and access stays until then.                                                              |
| Subscription ends               | The status is final and access is removed if the buyer has no other eligible subscription or lifetime purchase. |
| Plan changes                    | The new Price and period show after the webhook arrives.                                                        |

**Renewal succeeds:**

```bash interactive=true
stripe subscriptions update <subscription-id> -d billing_cycle_anchor=now -d proration_behavior=none
```

**Payment enters retries:** attach Stripe's failing test card, set the returned
payment method on the subscription, then run the renewal command again.

```bash interactive=true
stripe payment_methods attach pm_card_chargeCustomerFail --customer <customer-id>
stripe subscriptions update <subscription-id> -d default_payment_method=<payment-method-id>
```

**Customer schedules cancellation:** choose **Cancel** in the customer portal, or run:

```bash interactive=true
stripe subscriptions update <subscription-id> -d cancel_at_period_end=true
```

**Subscription ends:**

```bash interactive=true
stripe subscriptions cancel <subscription-id>
```

**Plan changes:** switch plans in the customer portal, if you turned on
[plan switching](/en/docs/billing/stripe/connect#configure-the-customer-portal).

For how `invoice.paid` and `invoice.payment_failed` affect access, see
[Stripe Subscription Lifecycle and Access](/en/docs/billing/stripe/subscription/lifecycle#payment-failure-follows-stripe-policy).

## What's next

- [Stripe Subscription Lifecycle and Access](/en/docs/billing/stripe/subscription/lifecycle): See how renewals, failed payments and cancellations affect access.

- [Configure Stripe Webhooks](/en/docs/billing/stripe/webhooks): Check event selection, signing secrets, and delivery failures.

- [Email](/en/docs/email): Verify and customize the messages triggered by subscription checkout.
