|
| 1 | +## Local Stripe Setup for Platform Billing Testing |
| 2 | + |
| 3 | +This guide will help you set up Stripe locally to test platform billing subscriptions for different platform plans. |
| 4 | + |
| 5 | +### Prerequisites |
| 6 | + |
| 7 | +- A Stripe account (you can sign up at [stripe.com](https://stripe.com)) |
| 8 | +- Stripe CLI installed on your machine |
| 9 | +- Access to the API v2 `.env` file |
| 10 | + |
| 11 | +### Step 1: Create Stripe Account and Get API Keys |
| 12 | + |
| 13 | +1. Sign up for a Stripe account at [stripe.com](https://stripe.com). You don't need to complete the full account activation process (payment details, company information, etc.) for testing purposes. |
| 14 | + |
| 15 | +2. Access your Stripe Dashboard at [dashboard.stripe.com](https://dashboard.stripe.com) |
| 16 | + |
| 17 | +  |
| 18 | + |
| 19 | +3. Switch to **Test mode** by clicking the toggle in the top left corner of the dashboard. |
| 20 | + |
| 21 | +  |
| 22 | + |
| 23 | +4. Navigate to **Developers** → **API keys** from the left sidebar. |
| 24 | + |
| 25 | +  |
| 26 | + |
| 27 | +5. Copy your **Publishable key**. This will be used as `STRIPE_API_KEY` in your API v2 `.env` file. |
| 28 | + |
| 29 | +  |
| 30 | + |
| 31 | +### Step 2: Install and Configure Stripe CLI |
| 32 | + |
| 33 | +The Stripe CLI is required to listen for webhook events generated by Stripe. This is necessary because the checkout flow triggers events that need to be forwarded to your local API v2 webhook endpoint. |
| 34 | + |
| 35 | +1. Install the Stripe CLI using Homebrew: |
| 36 | + |
| 37 | + ```sh |
| 38 | + brew install stripe/stripe-cli/stripe |
| 39 | + ``` |
| 40 | + |
| 41 | +2. Authenticate the CLI with your Stripe account: |
| 42 | + |
| 43 | + ```sh |
| 44 | + stripe login |
| 45 | + ``` |
| 46 | + |
| 47 | +### Step 3: Configure Environment Variables |
| 48 | + |
| 49 | +Update the following environment variables in your API v2 `.env` file: |
| 50 | + |
| 51 | +#### 3.1. Set Stripe API Key |
| 52 | + |
| 53 | +Add the Publishable key you copied in Step 1: |
| 54 | + |
| 55 | +```env |
| 56 | +STRIPE_API_KEY=pk_test_your_publishable_key_here |
| 57 | +``` |
| 58 | + |
| 59 | +#### 3.2. Generate Webhook Secret |
| 60 | + |
| 61 | +1. Run the following command in your terminal to start listening for Stripe events and generate a webhook signing secret: |
| 62 | + |
| 63 | + ```sh |
| 64 | + stripe listen --forward-to http://localhost:5555/api/v2/billing/webhook |
| 65 | + ``` |
| 66 | + |
| 67 | +2. Copy the webhook signing secret from the command output (it will look like `whsec_...`). |
| 68 | + |
| 69 | +3. Add it to your `.env` file: |
| 70 | + |
| 71 | + ```env |
| 72 | + STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret_here |
| 73 | + ``` |
| 74 | + |
| 75 | + > **Note:** Keep this terminal session running while testing, as it forwards Stripe events to your local webhook endpoint. |
| 76 | +
|
| 77 | +#### 3.3. Configure Product Price IDs |
| 78 | + |
| 79 | +You need to create products in Stripe and configure their price IDs in your `.env` file. The following environment variables are available: |
| 80 | + |
| 81 | +```env |
| 82 | +STRIPE_PRICE_ID_STARTER= |
| 83 | +STRIPE_PRICE_ID_STARTER_OVERAGE= |
| 84 | +STRIPE_PRICE_ID_ESSENTIALS= |
| 85 | +STRIPE_PRICE_ID_ESSENTIALS_OVERAGE= |
| 86 | +STRIPE_PRICE_ID_ENTERPRISE= |
| 87 | +STRIPE_PRICE_ID_ENTERPRISE_OVERAGE= |
| 88 | +``` |
| 89 | + |
| 90 | +**To set up products:** |
| 91 | + |
| 92 | +1. In your Stripe Dashboard, navigate to **Products** → **Product catalogue** from the left sidebar. |
| 93 | + |
| 94 | +  |
| 95 | + |
| 96 | +2. Create two products for each plan you want to test: |
| 97 | + |
| 98 | + - The main plan product (e.g., "Essentials") |
| 99 | + - The overage product for that plan (e.g., "Essentials Overage") |
| 100 | + |
| 101 | +3. For each product: |
| 102 | + |
| 103 | + - Click on the product in your catalogue |
| 104 | + - Scroll down to find the **Price ID** (it starts with `price_`) |
| 105 | + - Copy the Price ID |
| 106 | + |
| 107 | +4. Add the Price IDs to your `.env` file. For example, if testing the Essentials plan: |
| 108 | + |
| 109 | + ```env |
| 110 | + STRIPE_PRICE_ID_ESSENTIALS="price_1SXkelSliJQGPobQxR5L8nb5" |
| 111 | + STRIPE_PRICE_ID_ESSENTIALS_OVERAGE="price_1SXl1ZSliJQGPobQR55aQ0j9" |
| 112 | + ``` |
| 113 | + |
| 114 | + > **Note:** You only need to configure the price IDs for the plans you want to test. You can use the same price IDs for multiple products if desired. |
| 115 | +
|
| 116 | +### Step 4: Local Development Workaround |
| 117 | + |
| 118 | +> **Important:** This step is only required for local development. |
| 119 | +
|
| 120 | +In `apps/api/v2/src/modules/billing/services/billing.service.ts`, ensure that `quantity: 1` is set on line 88. This is a known workaround for local development (this works correctly in production without modification). |
| 121 | + |
| 122 | +### Step 5: Testing the Integration |
| 123 | + |
| 124 | +1. Start the API v2 server: |
| 125 | + |
| 126 | + ```sh |
| 127 | + yarn start |
| 128 | + ``` |
| 129 | + |
| 130 | + > **Note:** It's recommended to use `yarn start` instead of `yarn dev` for testing Stripe integrations. |
| 131 | +
|
| 132 | +2. Start the web application in a separate terminal. |
| 133 | + |
| 134 | +3. Navigate to `/settings/platform/new` in your browser. |
| 135 | + |
| 136 | +4. Create a new platform account. |
| 137 | + |
| 138 | +5. Click on one of the plan cards to initiate the Stripe checkout flow. |
| 139 | + |
| 140 | +6. Use the following test card details: |
| 141 | + |
| 142 | + - **Card number:** `4242 4242 4242 4242` |
| 143 | + - **Location:** United States (required for this test card) |
| 144 | + - **Expiry date:** Any future date |
| 145 | + - **CVC:** Any 3 digits |
| 146 | + - **ZIP code:** Any 5 digits |
| 147 | + |
| 148 | +7. Complete the checkout process. |
| 149 | + |
| 150 | +8. Verify the integration: |
| 151 | + |
| 152 | + - The platform dashboard should appear after successful checkout |
| 153 | + - Check the `PlatformBilling` table in your database for a new entry containing: |
| 154 | + - Customer ID |
| 155 | + - Plan details |
| 156 | + - Subscription ID |
| 157 | + |
| 158 | +9. Ensure the Stripe CLI terminal (from Step 3.2) is still running to receive webhook events. |
0 commit comments