# Payments

Stripe, Polar, Pagar.me and RevenueCat feed one server-verified entitlement model.

## Enable providers deliberately [#enable-providers-deliberately]

All providers start off. Set `BILLING_PROVIDERS` to the required comma-separated names: `stripe`, `polar`, `pagarme`, `revenuecat`. Each listed provider must have its complete valid configuration. `BILLING_ENVIRONMENT` distinguishes SANDBOX from PRODUCTION; Stripe and Pagar.me key modes must agree. Payment settings are Worker secrets: `scripts/check-production-config.mjs` refuses them in `wrangler.jsonc` `vars`.

Stripe, Polar and Pagar.me sell on the web. RevenueCat connects store purchases on mobile. If several web providers are enabled, `BILLING_CHECKOUT_PROVIDER` chooses new checkouts (default: Stripe, then Polar, then Pagar.me); existing customers keep their provider. Visibility separately uses `billing.web` and `billing.mobile`.

## Pix, card and boleto with Pagar.me [#pix-card-and-boleto-with-pagarme]

Pagar.me is for Brazil: Pix, credit card with installments and boleto, in BRL. There is no hosted page, so `/billing` opens an in-app checkout: billing details once (CPF or CNPJ, phone and address, which Pagar.me requires; the server keeps only the Pagar.me customer id), then the method.

| Method | Renews   | Notes                                                                                                                                                                               |
| ------ | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Pix    | Never    | QR code and copy-and-paste code, valid `PAGARME_PIX_EXPIRES_IN_SECONDS` (default 30 min). Pays one plan period; the User can pay the next one ahead. Pagar.me has no recurring Pix. |
| Card   | Optional | Tokenized in the browser with `PAGARME_PUBLIC_KEY`, so the number never reaches the server. One period can be split into up to `PAGARME_MAX_INSTALLMENTS` (default 12).             |
| Boleto | Optional | Confirmed by the bank within a few business days; access starts then.                                                                                                               |

The checkout answer and a poll (`billing.payment`, re-reading Pagar.me at most every 10 seconds) apply the same way a webhook does. `/api/billing/pagarme` verifies `X-Webhook-Asymmetric-Signature` (RS256 over the raw body, Pagar.me's JWKS) when present and always re-reads the order or subscription by id before granting, so a refund or chargeback revokes access. Set `PAGARME_WEBHOOK_SIGNATURE=required` once a test delivery shows the header. Pagar.me cancels a subscription at once; the template keeps access through the paid cycle. Split and recipients are not implemented.

<Callout title="Monthly Pix">
Pix every month needs Pix Automático, which neither Pagar.me nor (for Brazilian accounts) Stripe
offers today. If monthly fees must be paid by Pix, compare a provider that has it before choosing
Pagar.me.
</Callout>

## Entitlements and webhooks [#entitlements-and-webhooks]

Paid Access belongs to the User, not an Organization. The server's entitlement read model projects enabled-provider rows in the current environment. Device-reported purchases alone grant nothing. RevenueCat reconciliation replaces only its provider rows; Stripe, Polar and Pagar.me apply verified, deduplicated events in transactions, and Stripe and Pagar.me re-read the subscription or order from their API first.

| Provider   | Webhook path              | Server secrets                                                                                 |
| ---------- | ------------------------- | ---------------------------------------------------------------------------------------------- |
| Stripe     | `/api/billing/stripe`     | STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET, STRIPE_PRICE_IDS                                     |
| Polar      | `/api/billing/polar`      | POLAR_ACCESS_TOKEN, POLAR_WEBHOOK_SECRET, POLAR_PRODUCT_IDS                                    |
| Pagar.me   | `/api/billing/pagarme`    | PAGARME_SECRET_KEY, PAGARME_PUBLIC_KEY, PAGARME_PLAN_IDS                                       |
| RevenueCat | `/api/billing/revenuecat` | REVENUECAT_SECRET_API_KEY, REVENUECAT_WEBHOOK_AUTHORIZATION, REVENUECAT_WEBHOOK_SIGNING_SECRET |

Configure RevenueCat app and entitlement IDs too. Public platform SDK keys belong in the mobile environment; server keys do not. Raw webhook verification precedes payload parsing. A replay is a no-op, and a failed write rolls back its dedupe record so the provider can retry.

## Extend and test [#extend-and-test]

Add plans to the leaf `billing-plans.ts`, map provider product IDs in env, and add name/description translations. Checkout return URLs come from the server, never a client-supplied redirect. Test purchases in sandbox and verify `billing.status`, cancellation and webhook replay before enabling production visibility.

Stripe, Polar and Pagar.me rely on webhooks plus the grace window (and, for a pending Pagar.me payment, the checkout's poll); no scheduled pull reconciles their subscriptions. Stripe Checkout offers the methods enabled in the Dashboard for the price's currency; `STRIPE_PAYMENT_METHOD_CONFIGURATION` pins a set, such as card and boleto for BRL prices on a Brazilian account. RevenueCat verification has a freshness bound. Native purchases require a development build with the SDK, not just a successful TypeScript check. See [ADR-0004](/docs/adr/0004-payments-providers).

## Key files [#key-files]

- [`packages/api/src/features/billing/billing.settings.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/packages/api/src/features/billing/billing.settings.ts)
- [`packages/api/src/features/billing/billing.pagarme.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/packages/api/src/features/billing/billing.pagarme.ts)
- [`packages/api/src/features/billing/billing.web-http.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/packages/api/src/features/billing/billing.web-http.ts)
- [`packages/api/src/features/billing/billing.webhook.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/packages/api/src/features/billing/billing.webhook.ts)
- [`packages/api/src/features/billing/billing.status.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/packages/api/src/features/billing/billing.status.ts)
- [`packages/api/src/features/billing/billing.plans.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/packages/api/src/features/billing/billing.plans.ts)
- [`apps/mobile/src/features/billing/billing.purchases-sdk.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/apps/mobile/src/features/billing/billing.purchases-sdk.ts)
- [`docs/billing.md`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/docs/billing.md)
