DEV Community

Julia for Light Cloud

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

SaaS Part 7: Go Live With Real Payments

SaaS Part 7: Go Live With Real Payments

To go live with real payments, activate your Stripe account, recreate the two prices and the webhook in live mode, and replace the four Stripe variables on the Production environment with the live values. Staging and previews keep the test keys. Before you announce the launch, take a database backup, set usage alerts, decide whether to keep an instance always on, and practise a rollback once while nothing is at stake.

What you will build

The app from part 6, ready for paying customers:

  • live Stripe keys on Production only;
  • a database backup you know how to restore;
  • usage alerts, so a traffic spike can't surprise you;
  • a decision on cold starts;
  • a rollback you've tried once.

Before you start

  • The app from part 6, with test payments working on the custom domain.
  • A Stripe account activated for live payments. Stripe asks for your business details and a bank account before it lets you leave test mode.

Step 1: Create the live prices and webhook

Live mode has its own products, prices, webhooks and keys. Switch the dashboard out of test mode, then repeat part 3:

  1. In Product catalog , create Pro at 19.00 and Business at 49.00, monthly, and copy both price IDs.
  2. In Developers , Webhooks , add an endpoint for https://app.example.com/api/auth/stripe/webhook with the same four events, and copy its signing secret.
  3. In Developers , API keys , copy the live Secret key. It starts with sk_live_.

Treat the live secret key like a bank password. Anyone with it can refund payments and read your customers' details.

Step 2: Replace the variables on Production

  1. In the Light Cloud console, open the app, then Production , Settings , and click Edit next to Environment Variables.
  2. Replace the four values:
Name Value
STRIPE_SECRET_KEY the sk_live_ key
STRIPE_WEBHOOK_SECRET the live endpoint's signing secret
STRIPE_PRICE_PRO the live Pro price ID
STRIPE_PRICE_BUSINESS the live Business price ID
  1. Click Save.

Set them on Production , never on the app's defaults. Defaults reach every new preview environment, and a preview should never be able to charge a card.

Workspaces that upgraded with test cards still show their plan, but their test subscriptions don't exist in live mode. Downgrade those test workspaces in /admin, or delete them, before you launch. <!-- VERIFY: how the app handles a test-mode subscription id after the switch, and the cleanest way to reset it -->

Step 3: Back up the database

Download a backup of the production database now, while it's small and you know what's in it:

  1. Open saas-db in the console, then the Backup tab.
  2. Under Manual backup , click Download backup. You get a full SQL dump as a gzip-compressed file.

The Backup tab of saas-db: Manual backup with Download backup highlighted, and Import a dump below

The same tab has Import dump , which restores that file into a database. Back up a database shows the whole flow. Try the restore once into a spare database. A backup you haven't restored is a guess.

Step 4: Set usage alerts

Open Billing. Under Usage alerts , the console already emails you at 80% of the usage your plan includes and again when it's used up. Add Extra alert when usage this cycle passes with an amount a little above what the app uses today, and click Save. A launch-day spike then sends you an email before it becomes a bill.

Alerts only send email, they never pause anything. If you want a hard stop, set Usage limit : it pauses server apps after the amount of extra usage you choose.

Step 5: Decide on cold starts

With Min instances at 0, the app costs nothing while nobody uses it, but the first visitor after a quiet spell waits while an instance starts. In part 1 that first request took 3.1 seconds, and the next one 0.6.

For a launch, I'd keep one instance running. On Production , Overview , raise min under Running now to 1:

The Production overview with Running now set to min 1 and max 3, highlighted

An always-on instance is billed for every hour it runs, so check the Billing page after a day to see what it adds. Set it back to 0 later if traffic is low and the wait doesn't matter.

Step 6: Practise a rollback

Roll back once now, while nothing is at stake:

  1. Open Production , Deployments.
  2. Click Rollback and pick the deployment before the current one.

The Deployments tab with the Rollback menu open: Roll back to a previous deployment, with cc74a2f marked current and c90faf7 highlighted

  1. The console says what will happen. Click Roll back.

The Roll back deployment dialog: The environment will serve the code from deployment c90faf7, no rebuild happens. Code only: current environment variables stay as they are, and database changes made since are not reverted. Roll back highlighted

In my run the old version was live 22 seconds after I clicked, and the history listed the rollback as a deployment that took 20 seconds. There's no rebuild, which is why it's fast.

As the dialog says, a rollback brings back the code only. Your variables stay as they are, and database migrations aren't undone. That's why it pays to make database changes in two steps: first add the new column, and only remove the old one in a later release.

To go forward again, click Redeploy , which builds the latest code from main.

Step 7: Watch the logs on launch day

Keep the Logs tab open. The lines worth searching for:

  • Email to shows a failed email;
  • Database connection lost shows the pool replacing a dropped connection;
  • anything with stripe shows a webhook problem.

And keep the health check handy:

$ curl https://app.example.com/api/health
{"status":"ok","database":"ok"}


PS> curl.exe https://app.example.com/api/health
{"status":"ok","database":"ok"}

Enter fullscreen mode Exit fullscreen mode

What the whole setup costs

I ran the series on the Starter plan, which includes $19 of usage a month. From the usage page after the runs:

Item What it used
A Dev database (saas-db or saas-staging-db) $2.08 for 380 hours, so about $4 for a full month
The app, Production and Staging, scaling to zero $0.01 each so far
A pull request preview $0.01, and it stops counting once it's removed
Build minutes counted with everything else the workspace builds

So two databases and an app that scales to zero fit inside what Starter includes, and you pay the plan price. An always-on instance from step 5 adds its hours on top. Stripe and Resend charge separately: see Stripe's pricing and Resend's pricing.

Troubleshooting

No such price after the switch

A price ID is still from test mode. Copy the IDs again with the dashboard in live mode.

Live payments go through, but plans don't change

The live webhook endpoint is missing, or STRIPE_WEBHOOK_SECRET still holds the test secret. Each mode has its own endpoint and its own secret.

A preview shows real prices or can take payments

Live Stripe values ended up in the app's defaults. Remove them there and keep them on Production only.

Top comments (0)