Skip to content
Source
Sign inSign up

Connect Polar for payments

Site administration

Connect Polar for payments

People pay for plans and credits through Polar, the merchant of record. The web Worker needs a Polar token, and Polar needs the products of Source. Until then, Subscribe and the credit packs fail with "Payments are not set up on this site yet." Connect the Polar sandbox first, to test. When you take real payments, connect Polar production.

6 min read

You need:

  • An organization in Polar: sandbox.polar.sh for tests, or polar.sh for real payments.
  • The Source repository with its packages (pnpm install), and wrangler signed in to the Cloudflare account of the deploy (pnpm -C apps/web exec wrangler login).
  • src, logged in, with write access to the repository, for its vault.
  • To be a site admin, for the test in step 4.

Run the commands from the root of the Source repository.

1. Put the token in the env file

  1. In Polar, make an organization access token for your organization. Give it the scopes for the organization, meters, products, webhooks, customers, customer sessions, checkouts, subscriptions, orders and events.

  2. Put the token in .wrangler/polar-sandbox.env, with the server:

    POLAR_ACCESS_TOKEN=<your token>
    POLAR_SERVER=sandbox

The vault of the repository keeps this file (step 3). To get it on another computer, run src vault pull.

2. Set up Polar

  1. Do a dry run. It only reads from Polar, and lists what a real run makes or changes:

    node --experimental-strip-types apps/web/scripts/polar-setup.ts --dry-run
  2. Run the setup:

    node --experimental-strip-types apps/web/scripts/polar-setup.ts

The setup makes or fixes 12 things in Polar:

  • The meter Usage over the plan. Polar bills the overage with it.
  • 9 products: Pro, Team and Enterprise, each monthly and yearly, and the credit packs of $10, $25 and $100.
  • The customer portal settings: plan changes off, seat changes off, usage view on. The Billing page of Source changes the plans, and the seats follow each organization.
  • The webhook endpoint https://source.tanstack.com/hooks/polar. For another site, add --webhook-url https://<your site>/hooks/polar.

Then the setup writes the secret of the webhook endpoint into the env file, as POLAR_WEBHOOK_SECRET. It never prints the token or the secret.

3. Give the Worker the secrets

Do the two next steps that the setup prints:

  1. Keep the env file in the vault, for each person with write access:

    src vault put .wrangler/polar-sandbox.env --scope role:write
  2. Put the token and the webhook secret on the web Worker in one change:

    grep -E '^POLAR_(ACCESS_TOKEN|WEBHOOK_SECRET)=' .wrangler/polar-sandbox.env | pnpm -C apps/web exec wrangler secret bulk

POLAR_SERVER is not a secret. It is a var in apps/web/wrangler.jsonc, and its value is sandbox.

4. Pay with a test account

  1. Open Limits. In the Billing card, under Billing on early, type the name of a test account. Then click Turn on early. See Turn billing on early for an account.
  2. Sign in as the test account, and open Billing. The test account needs a verified primary email address.
  3. Under Upgrade to Pro, click Subscribe.
  4. In the sandbox checkout of Polar, pay with the test card 4242 4242 4242 4242, a date in the future and any CVC.

If Subscribe says "Payments are not set up on this site yet", wait 10 minutes. The Worker reads the products of Polar again every 10 minutes.

Done

Polar sends the test account back to its Billing page, with "Your payment went through." After a minute, its Plan card says "Pro". While billing is off for the site, nobody else sees a change.

You can run the setup again at any time. It makes or changes only what is missing or wrong. So a second run ends with "12 found, 0 made, 0 changed".

If the setup stops

The setup stops at the first request that Polar refuses, and prints the answer of Polar.

  • "Polar answered 401" or "Polar answered 403": the token is for the other server, or it does not have a scope. Make a new token, put it in the env file, and run the setup again.
  • The token sees no Polar organization, or more than one: use an organization access token, not a personal token.

What the setup made before it stopped stays in Polar. The next run finds it.

If Polar turns the webhook endpoint off

Polar sends a webhook again after each failure, about 10 times. When the 10 newest events of the endpoint all failed, Polar turns the endpoint off.

  1. In the logs of the web Worker, search for Billing: a Polar webhook. Each line has the webhook id, and says what failed. Fix that cause.
  2. Run the setup again. It turns the endpoint on.
  3. If step 1 found no line, put the secrets on the Worker again, as in Give the Worker the secrets. Source refuses a webhook with no log line when the webhook secret of the Worker is wrong or missing.

A webhook that can never succeed does not count as a failure. For example, its data does not fit, or Polar does not have its subscription. Source answers 200 and logs the error with the webhook id, so Polar does not send it again.

The Polar pass of the cron also repairs two things that a lost webhook misses. Every 15 minutes, it reads from Polar each account with a subscription, and each account with a checkout in the last 2 days:

  • Subscriptions: it saves what Polar has now, also when the webhook of a change never came.
  • Credit packs: it adds the credits of each pack that the account paid for in the last 2 days, when no webhook added them.

Move to production

When you are ready to take real payments, do these steps:

  1. In Polar production, make an organization and an organization access token.

  2. Put the token in a second env file, for example .wrangler/polar-production.env, with POLAR_SERVER=production.

  3. Run the setup with this file:

    node --experimental-strip-types apps/web/scripts/polar-setup.ts --env-file .wrangler/polar-production.env
  4. Change POLAR_SERVER in apps/web/wrangler.jsonc to production. Merge the change, and wait for ci/deploy.

  5. Right after the deploy, do the next steps that the setup printed for this file.

While the var and the secrets are for different servers, each call to Polar fails. So keep the time between steps 4 and 5 short.

Note

For each account, Source keeps the Polar server of its customer. Within 15 minutes after the deploy, an account with a plan from the sandbox goes back to Free. The next checkout of an account that bought in the sandbox makes a new customer in production. Credits that an account bought in the sandbox stay in its balance.