# polar

Polar (polar.sh), a merchant of record for software with benefits, over
its HTTP API: no package. The whole of it is
[docs/polar.md](../../docs/polar.md).

```toml
[packages]
polar = "../mote/extensions/polar"
```

```text
use "pkg:polar/polar.mote"        // the API
use "pkg:polar/webhooks.mote"     // POST /polar/webhook, PolarSubscriptionKept, PolarGrant, the hooks

PolarButton product="…" label="Go Pro"
fn polarPaid(o: PolarOrder): bool
polarActive(user, "")  ·  polarCan(user, benefit)
```

Settings: `POLAR_TOKEN` (a secret), `POLAR_LIVE=1` for live (unset: the
sandbox), `POLAR_WEBHOOK_SECRET` (a secret) with webhooks.mote,
`POLAR_PRODUCTS`. Optional: `POLAR_API`.

| File | |
|---|---|
| `polar.mote` | Products (every page), customers by `external_id`, checkouts and checkout links, subscriptions (change with a proration, cancel at the end and take it back, revoke), orders, invoices, refunds, the portal, granted benefits; `polarBuy`, `polarManage`, `PolarButton`. |
| `webhooks.mote` | Standard Webhooks checked by `webhook.check(…, "polar", …)`, each event kept once, a job that reads each subscription and each account's grants again, and calls the hooks. |
| `tests.mote` | Its tests: `use "pkg:polar/tests.mote"`, then `mote test`. |

**Covered**: what a small software product sells through Polar: a checkout
or a lasting checkout link, the account as the customer's `external_id`,
subscriptions kept by events, plan changes, cancel and undo, revoke,
benefits as what an account may do (`polarCan`), orders, invoices,
refunds, the portal.

**Left out on purpose**: making products, prices and benefits from code
(Polar's dashboard); usage meters and meter credits; license key
validation (Polar's customer-portal API, called from the program's own
client); discounts made from code (a `discount` id is taken); embedded
checkout (a script of Polar's); payouts and the organization's balance.

**Not yet run against the real Polar**: only against a stand-in
(`tests/all.sh real_svc_polar`).
