DEV Community

Julia for Light Cloud

Posted on Originally published at blog.light-cloud.com on

SaaS Part 3: Take Payments With Stripe (Test Mode)

SaaS Part 3: Take Payments With Stripe (Test Mode)

To take payments in a SaaS on Light Cloud, create your subscription prices in Stripe's test mode, add a webhook that points at /api/auth/stripe/webhook on your app's URL, and set four environment variables: STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET, STRIPE_PRICE_PRO and STRIPE_PRICE_BUSINESS. After the redeploy, workspace owners can upgrade through Stripe Checkout, and the webhook keeps each workspace's plan in sync with Stripe.

What you will build

The app from part 2, with working billing:

  • a pricing page with Free, Pro ($19 a month) and Business ($49 a month);
  • Upgrade to Pro and Upgrade to Business on the workspace's billing page, through Stripe Checkout;
  • Manage billing in Stripe , which opens Stripe's customer portal to change plan, update the card or cancel;
  • plan limits that follow the subscription: Free allows 3 projects, Pro 50, Business unlimited.

    Before you start

  • The app from part 2, running. Part 2 isn't strictly needed for billing, but receipts and invitations work better with email on.

  • A Stripe account. You don't need to activate payments for test mode.

Make sure the toggle at the top of the Stripe Dashboard says Test mode (Stripe calls it a sandbox in newer accounts) for every step below. <!-- VERIFY: current Stripe label: "Test mode" or "Sandbox" -->

Step 1: Create the products and prices

The app's plans live in src/lib/plans.ts. The prices shown there are for display only. Stripe charges what the price in Stripe says, so create matching prices:

  1. In Stripe, open Product catalog and click Add product.
  2. Name it Pro, choose Recurring , Monthly , and enter 19.00. Click Add product.
  3. Do the same for Business at 49.00.
  4. Open each product and copy its price ID. It starts with price_. <!-- IMAGE: s3-01-stripe-price.webp, a product page with the price ID highlighted (light + dark) --> ## Step 2: Add the webhook

Stripe tells the app about payments through a webhook. Without it the payment goes through, but the workspace never changes plan.

  1. In Stripe, open Developers , Webhooks , and click Add endpoint. <!-- VERIFY: Stripe's current menu path ("Workbench" in newer dashboards) -->
  2. For Endpoint URL , enter your app's URL followed by /api/auth/stripe/webhook:
https://main-tutorial-saas-starter-yourworkspace.light-cloud.io/api/auth/stripe/webhook

Enter fullscreen mode Exit fullscreen mode
  1. Under Select events , add these four:
    • checkout.session.completed
    • customer.subscription.created
    • customer.subscription.updated
    • customer.subscription.deleted
  2. Click Add endpoint , then Reveal under Signing secret , and copy it. It starts with whsec_. <!-- IMAGE: s3-02-webhook.webp, the webhook endpoint with its URL and the four events (light + dark) -->

Part 4 moves the app to its own domain. The webhook URL changes then too, and part 4 shows where.

Step 3: Copy the secret key

In Stripe, open Developers , API keys , and copy the Secret key. In test mode it starts with sk_test_.

Step 4: Add the four variables

  1. In the Light Cloud console, open the app, then Production , Settings , and click Edit next to Environment Variables.
  2. Add:
Name Value
STRIPE_SECRET_KEY the sk_test_ key from step 3
STRIPE_WEBHOOK_SECRET the whsec_ secret from step 2
STRIPE_PRICE_PRO the Pro price ID
STRIPE_PRICE_BUSINESS the Business price ID
  1. Click Save.

The Production editor with four new rows, STRIPE_SECRET_KEY and STRIPE_WEBHOOK_SECRET hidden, STRIPE_PRICE_PRO and STRIPE_PRICE_BUSINESS, Save highlighted

The values in my screenshots are fake samples. Yours come from your own Stripe sandbox.

Billing switches on only when all four are set. Saving deploys the app again. <!-- VERIFY: "In my run it was live N seconds later." -->

Step 5: Upgrade with a test card

  1. In the app, open Settings , Billing , and click Upgrade to Pro.
  2. Stripe Checkout opens. Enter 4242 4242 4242 4242, any future expiry date, any three-digit CVC and any name.
  3. Click Subscribe. <!-- IMAGE: s3-04-checkout.webp, Stripe Checkout with the test card filled in (light only) -->

Stripe sends you back to the billing page, which now shows Pro. <!-- VERIFY: exact confirmation text on the billing page after ?upgraded -->

In Stripe, open the webhook endpoint. Each event shows a 200 response from your app. If one shows an error, the troubleshooting section below lists the usual causes.

Step 6: Open the customer portal

Click Manage billing in Stripe. Stripe's customer portal opens, where the workspace owner can switch to Business, update the card, see invoices or cancel. Cancel the test subscription now. After the webhook arrives, the billing page shows Free again. <!-- VERIFY: whether cancel is immediate or at period end in the portal's default settings -->

Who can pay

Subscriptions belong to the workspace, not to the person who pays. Only the workspace's owners can upgrade or open Manage billing in Stripe. Admins and members see the plan and its usage, but can't change it.

Troubleshooting

Open the webhook endpoint in Stripe first. It lists every delivery with your app's response.

The webhook shows 400 with a signature error

STRIPE_WEBHOOK_SECRET doesn't belong to this endpoint. Each endpoint has its own signing secret. Copy the one from this endpoint and save the variable again. <!-- VERIFY: exact error text in the app logs -->

The payment worked, but the plan didn't change

The webhook isn't reaching the app. Check that the endpoint URL ends with /api/auth/stripe/webhook and that the four events are selected.

The billing page says billing isn't available yet

One of the four Stripe variables is missing or empty. Check all four are on the Production environment, then let the redeploy finish.

No such price

A price ID was copied from live mode, or the other way round. Test mode and live mode have separate prices. Copy the IDs while the dashboard shows test mode.

Top comments (0)