{"object":"doc","slug":"build-subscriptions","title":"Sell a subscription in five calls","summary":"A monthly plan end to end in the sandbox. Create a product and a recurring price, open a checkout session in subscription mode, provision on the webhook, and give the customer the portal. With the MCP tool for each call.","tags":["guide","billing","subscriptions","checkout","webhooks"],"body":"# Sell a subscription in five calls\n\nThis guide sells Pro at $29.00 a month. The buyer pays on ZeroPoint's hosted page, the card is saved and charged, and the subscription renews on it. Your app grants access from a webhook and sends the customer to the hosted portal to manage the plan. Every amount is integer cents. Read [Subscriptions and invoices](subscriptions.md) first for the statuses and rules.\n\nBilling runs in test mode in this release: the first charge happens at checkout, and renewals run when you advance a test clock ([Test your billing with test clocks and failure injection](test-billing-with-test-clocks.md)). Use a `zp_test_` key.\n\nScopes: `catalog:write`, `checkout_sessions:write`, `payment_methods:write`, `subscriptions:write`, `webhooks:write`. New sandbox keys carry them all.\n\n## 0. Set the merchant's billing settings\n\n### Set up billing in one step\n\nThree things get a merchant moving from Stripe ready for its first subscription: its time zone, its subscription guard and a webhook endpoint that receives the billing events. Do them once per merchant, before the first subscription.\n\nZeroPoint's operator does it in one command, for one merchant:\n\n```\npoyntmcp billing setup --merchant mch_123 --timezone America/Chicago \\\n  --subscription-guard customer --webhook-url https://example.com/zeropoint/webhook \\\n  --site-host example.com --confirm <operator> --note \"pilot setup\" --apply\n```\n\nIt sets `timezone` (required, an IANA name), `upcoming_days` (default 3) and `subscription_guard` (required: `off`, `price`, `product` or `customer`), audited. For the webhook:\n\n- A webhook endpoint belongs to your platform, not to one merchant: it receives the events of every merchant of the platform in that mode. The output says so.\n- With no endpoint for the URL, it registers one for every billing event type and the payment and refund types, with `stripe_signature_header` and `stripe_compatible_payload` on (`--stripe-signature-header off` if the URL still receives real Stripe deliveries), and prints its signing secret once.\n- An existing endpoint for the URL (the exact string) is never changed without an explicit flag. Missing types are listed and added only with `--add-event-types`. Each Stripe flag changes only with `--stripe-signature-header on|off` or `--stripe-compatible-payload on|off`, and the output says what that changes for every delivery to the URL. The secret is kept unless `--rotate-secret`.\n\nRun again, it changes only what differs. It ends with a checklist of what is still closed and why: live billing, the scheduler's own stops (on, halted, paused, the merchant's renewals paused, dry run), checkout's live switch, the live redirect hosts (checked only with `--site-host`), the endpoint's event types and its signed test delivery. It never opens any of them. Without `--apply` nothing is written; a live merchant also needs `--live`. A live merchant's settings are set this way, by the operator, before go-live: the API answers `billing_unavailable` for a live merchant until billing is switched on for your platform.\n\nYour own code does the same with three calls:\n\n```\nPOST /v1/billing_settings\nZeroPoint-Account: mch_123\n{\"timezone\": \"America/Chicago\", \"upcoming_days\": 3, \"subscription_guard\": \"customer\"}\n\nPOST /v1/webhooks\n{\"url\": \"https://example.com/zeropoint/webhook\",\n \"event_types\": [\"checkout.session.completed\", \"checkout.session.expired\", \"customer.created\", \"customer.deleted\", \"customer.subscription.created\", \"customer.subscription.deleted\", \"customer.subscription.updated\", \"customer.updated\", \"invoice.marked_uncollectible\", \"invoice.paid\", \"invoice.payment_failed\", \"invoice.payment_succeeded\", \"invoice.upcoming\", \"invoice.voided\", \"payment.canceled\", \"payment.expired\", \"payment.failed\", \"payment.needs_attention\", \"payment.processing\", \"payment.reversed_late\", \"payment.succeeded\", \"payment_method.attached\", \"payment_method.detached\", \"refund.failed\", \"refund.succeeded\"],\n \"stripe_signature_header\": true, \"stripe_compatible_payload\": true}\n\nPOST /v1/webhooks/{id}/test\n```\n\nSend each with your `zp_test_` key and its own `Idempotency-Key`. Store the `secret` from the second answer: it is shown only there. The third sends a signed `webhook.test` to your endpoint; verify its signature in your receiver. List your endpoints first (`GET /v1/webhooks`) and change the one for your URL with `PATCH /v1/webhooks/{id}` rather than registering it twice; remember it serves every merchant of your platform in this mode, and that turning on `stripe_compatible_payload` changes the payload of every delivery to it. No MCP tool does all three, and `register_webhook` does not set the two Stripe flags: send the first two over REST (`verify_webhook` can send the third).\n\n### The settings\n\nTwo settings matter here:\n\n- `timezone` (an IANA name, default `UTC`): renewal dates, and the dates the checkout page and the portal show the customer, are days in this zone. With the default, a purchase at 10:30 PM in Chicago reads as the next day. A subscription keeps the zone it was created with, so set it first.\n- `subscription_guard` (default `product`): stops a customer you identify buying the same thing twice. While they hold a live subscription (`active`, `past_due` or `unpaid`), a new one is refused: `price` to the same price; `product` to any price of the same product (Pro monthly blocks Pro yearly); `customer` to anything at this merchant (one subscription per customer: they change plans instead); `off` never. A plan change is never blocked by the subscription being changed, so upgrades still work.\n  The case most likely to surprise a merchant: every subscription is quantity 1, so under the default one customer record cannot hold the same product twice. A customer who buys it a second time on purpose (a parent paying for two children under one `customer`, or two locker rentals) is refused with `409 subscription_exists`. Use one customer per person (records that share the parent's email are still two buyers), and do not send the parent's user id as `client_reference_id` on both (a shared reference says \"the same buyer\" and the second is refused), or set `subscription_guard` to `price` or `off` for this merchant.\n\n  The default also refuses a subscription checkout session that names no buyer: `400 customer_identity_required`, nothing created. Name the buyer one of three ways: `customer`, `client_reference_id` or `customer_email` (below). An integration written the plain Stripe way, with none of them, fails on its first session until it sends one or the merchant sets the guard to `off`.\n\n  `product` compares products as your catalog has them: a monthly and a yearly price are \"the same product\" only when both sit under one product. `client_reference_id` is compared exactly (case and spaces count). A change of the guard applies to sessions created afterwards; a session opened before is not checked again for a buyer.\n\n```\nPOST /v1/billing_settings\nAuthorization: Bearer zp_test_...\nZeroPoint-Account: mch_123\nIdempotency-Key: settings-mch_123-1\nContent-Type: application/json\n\n{\"timezone\": \"America/Chicago\", \"subscription_guard\": \"customer\"}\n```\n\nThe merchant can be named in the `ZeroPoint-Account` header (`Stripe-Account` works too) or as `\"merchant_id\": \"mch_123\"` in the body, the same as every other route; with one merchant on the platform you may leave it out. The answer is the whole `billing_settings` object (every field is in [Failed renewals and retries](failed-renewals-and-retries.md), \"Billing settings\"). Fields you leave out stay as they are. `GET /v1/billing_settings` with the same header (or `?merchant_id=mch_123`) reads them; MCP: `get_billing_settings`. There is no MCP tool that changes them: send this REST call.\n\nWith `subscription_guard` at anything but `off` (so by default), every subscription-mode checkout session must name the buyer. The guard matches the customer record, not the email: by `customer` id, by `client_reference_id`, or by the `customer_email` you pass on a session that names no customer. Reuse one customer per user (never a new `cus_` per checkout). Name the buyer with one of:\n\n- `customer` (`cus_...`), the ZeroPoint customer you created for this user;\n- `client_reference_id`, your own id for the user, the same value every time for one buyer (never per location, campaign or household: every later buyer under a shared value is refused);\n- `customer_email`, only an address your app has verified.\n\nA session that names nobody is refused at create, and nothing is created:\n\n```\n400 {\"error\": {\"type\": \"invalid_request\", \"code\": \"customer_identity_required\", \"param\": \"customer\",\n     \"message\": \"This merchant's subscription guard is on (it stops a customer buying the same subscription twice), so each subscription checkout must name the customer: send customer, client_reference_id or customer_email.\"}}\n```\n\nA buyer who already has a live subscription the guard matches is refused at create too, naming the subscription they have; the message says which match (price, product or customer). Nothing is charged; send them to the portal (step 5) instead:\n\n```\n409 {\"error\": {\"type\": \"conflict\", \"code\": \"subscription_exists\", \"subscription\": \"sub_...\",\n     \"message\": \"This customer already has a subscription to this product. To move them to another price of it, change that subscription's plan. Nothing was charged here. If the subscription is past due, update the card; if it is set to cancel, undo the cancel.\"}}\n```\n\nThe full rules, including what the buyer sees on the page, are in [Hosted checkout](checkout.md).\n\n## 1. Create the product\n\n```\nPOST /v1/products\nAuthorization: Bearer zp_test_...\nIdempotency-Key: product-pro-1\nContent-Type: application/json\n\n{\"merchant_id\": \"mch_123\", \"name\": \"Pro\"}\n```\n\nMCP: `create_product` with `merchant_id` and `name`. Answer: the product, `id` `prod_...`. Call `list_products` first and reuse one that exists.\n\n## 2. Create the monthly price\n\n```\nPOST /v1/prices\nIdempotency-Key: price-pro-monthly-1\n\n{\"merchant_id\": \"mch_123\", \"product\": \"prod_123\", \"unit_amount\": 2900, \"currency\": \"usd\",\n \"recurring\": {\"interval\": \"month\"}, \"lookup_key\": \"pro_monthly\"}\n```\n\nMCP: `create_price`. A price never changes; to change the amount, create a new price and archive the old one. In your app, find the price by its lookup key and its merchant (`GET /v1/prices` with `lookup_keys=pro_monthly&merchant_id=mch_...`, or send the merchant in the `ZeroPoint-Account` header; without one, a key that exists on more than one of your merchants answers `400 parameter_invalid`), never by a hard-coded id. Only `interval_count` 1 is supported: every 1 month or every 1 year.\n\n## 3. Open a checkout session in subscription mode\n\nWhen the signed-in user presses Subscribe, your server creates the session and redirects the browser to `url` with a 303:\n\n```\nPOST /v1/checkout/sessions\nIdempotency-Key: subscribe-user_42-1\n\n{\n  \"merchant_id\": \"mch_123\",\n  \"mode\": \"subscription\",\n  \"line_items\": [{\"price\": \"price_123\", \"quantity\": 1}],\n  \"subscription_data\": {\"metadata\": {\"user_id\": \"user_42\"}},\n  \"client_reference_id\": \"user_42\",\n  \"allow_promotion_codes\": true,\n  \"success_url\": \"https://example.com/account?session_id={CHECKOUT_SESSION_ID}\",\n  \"cancel_url\": \"https://example.com/pricing\"\n}\n```\n\nMCP: `create_checkout_session` with the same fields and a new `request_key` for this purchase. Answer: the session, `id` `cs_...`, `status: open`, and `url`. The page shows \"Renews at $29.00 a month\" and a Subscribe button. In the sandbox, open `url` in a regular browser and pay with test card `4761739001010010` (any future expiry, any CVV), or with no browser: `POST /v1/test_helpers/checkout/sessions/{id}/confirm` with `{\"card\": \"tok_visa\", \"email\": \"buyer@example.com\"}` and an `Idempotency-Key` (MCP `confirm_checkout_session`), which runs the page's own confirm and makes the same subscription, invoice, events and webhooks (`sandbox`, \"Test card tokens\"). `email` is the address the buyer would type on the page: it is needed when the session names no email, as this one does (only `client_reference_id`), and ignored when the session has `customer_email` or names a `customer` that has an email; without it the answer is `400 parameter_missing` (`param: email`).\n\nRules:\n\n- Exactly one line, a recurring catalog price, `quantity: 1`. A first invoice of $0 (a 100 percent off code) is refused: in `discounts` at create it answers `400 parameter_unavailable`, and on the page the code is turned down with nothing changed. Use a code that leaves at least 1 cent to pay.\n- Use the same key only to retry the same call; a new purchase gets a new key.\n- A declined card leaves the session open for another card. A promotion code the buyer applied stays reserved for that session for 15 minutes after the last decline, then is released for others; the next attempt reserves it again.\n- A payment whose outcome is unknown is never treated as declined: the page says the payment is being confirmed, and `409 checkout_outcome_unknown` on a new session for the same `client_reference_id` means \"wait\". Do not open another session meanwhile.\n\n## 4. Provision on the webhook\n\nRegister a receiver for the billing events (they are opt-in: an endpoint with empty `event_types` gets payment events only):\n\n```\nPOST /v1/webhooks\n\n{\"url\": \"https://example.com/webhooks/zeropoint\",\n \"event_types\": [\"checkout.session.completed\", \"customer.subscription.created\",\n                 \"customer.subscription.updated\", \"customer.subscription.deleted\",\n                 \"invoice.paid\", \"invoice.payment_failed\", \"invoice.upcoming\"]}\n```\n\nMCP: `register_webhook`, then `verify_webhook`. Keep the `whsec_` secret it returns once.\n\nDeveloping on your own machine? ZeroPoint cannot deliver to `localhost`. Register the endpoint at a public URL (or the hosted `/sandbox/webhook-sink`) and run the `listen.mjs` helper from the `zeropoint.node.subscriptions` snippet: it replays that endpoint's events to `http://localhost:...` signed with your secret exactly as a delivery is, so your verification code runs unchanged. Running the ZeroPoint server itself on your machine? It refuses every local and `http` endpoint, its own `/sandbox/webhook-sink` included, so pass `listen` the event `types` instead of a `webhookId`: it then needs no endpoint (the repository README, \"Local development\", has the command). A tunnel to your machine, or polling `list_events`, works too ([Webhooks](webhooks.md), \"Developing locally\").\n\nYour handler, after verifying the `ZeroPoint-Signature` header over the raw body:\n\n1. Store `event.id`; if you have seen it, answer 200 and stop. Deliveries repeat.\n2. `checkout.session.completed`: read `client_reference_id` (your user) and `subscription` (`sub_...`); store the subscription id on the user. Do not grant on the success redirect alone: the redirect can be faked or lost.\n3. `customer.subscription.created`, `.updated` and `.deleted`: store `status` and grant or revoke by it. A paid checkout sends `customer.subscription.created` already `active` and no `customer.subscription.updated`; the first `.updated` comes at the next renewal, so a handler without `.created` never sees the purchase. Grant or revoke by `status` (`active` grants; `canceled`, `unpaid` and `incomplete_expired` revoke; `past_due` is your grace period). Overwrite your stored copy only when `data.object_version` is greater than or equal to the one you hold. If only `payment_hold` changed, do nothing.\n4. `invoice.payment_failed`: read `billing_reason` first. `subscription_cycle` (a renewal): email the customer to update their card, with a link to your account page (step 5). `subscription_update` (a plan change that did not go through): the plan is unchanged and the invoice is voided next; tell the customer the change failed. `subscription_create` (a first payment through `POST /v1/subscriptions`): ask for another card and pay `latest_invoice`. [Failed renewals and retries](failed-renewals-and-retries.md) has the rest. `invoice.upcoming`: send your renewal reminder. ZeroPoint sends no email to your customers.\n\nReady-made pieces: `get_snippet` with `zeropoint.node.webhook-receiver` verifies and stores events durably, and `zeropoint.node.subscriptions` creates the step 3 session under your key (with your `customer` and a `promotion_code` when you have them; `startPurchaseCheckout` for a one-time price) and applies the stored events once each, a paid one-time purchase included (access by `status`, `object_version` overwrite rule, a durable outbox for your reminder, receipt and failed-payment emails). See [Billing events](billing-events.md) for every event your handler can get.\n\nCheck the result with `GET /v1/subscriptions/{subscription_id}` (MCP `get_subscription`): `status: active`, `items.data[0].price.id` is your price, `current_period_end` is a month after `current_period_start`.\n\n## 5. Give the customer the portal\n\nOn your account page, a Manage billing button posts to your server, which creates a portal session for the user's customer and redirects at once:\n\n```\nPOST /v1/billing_portal/sessions\n\n{\"customer\": \"cus_123\", \"return_url\": \"https://example.com/account\"}\n```\n\nMCP: `create_portal_session`. The answer's `url` works once and expires after 5 minutes. Never email it, store it or show it. On the portal the customer cancels at period end (and undoes it), replaces the card and pays an open invoice; you receive `customer.subscription.updated` for each change.\n\n## What next\n\n- Renew it: [Test your billing with test clocks and failure injection](test-billing-with-test-clocks.md). `integration_checklist` marks `renew_on_test_clock` done after a paid renewal on a clock.\n- Plans of different prices: [Upgrades, downgrades and cancel](upgrades-downgrades-and-cancel.md).\n- Declines: [Failed renewals and retries](failed-renewals-and-retries.md).\n- Porting Stripe code: [Coming from Stripe](coming-from-stripe.md).","updated":1795275945}