{"object":"doc","slug":"accept-payments-with-checkout","title":"Accept payments with hosted checkout","summary":"Create a checkout session on your server, redirect the buyer to its url, fulfil once per session on the checkout.session.completed webhook, and check the session before showing paid.","tags":["guide","checkout","payments","webhooks"],"body":"# Accept payments with hosted checkout\n\nThis guide sells a $15 one-time 7-day pass. Your server creates a session, the buyer pays on ZeroPoint's page, and your webhook handler grants the pass. Read [Hosted checkout](checkout.md) first for the states and rules.\n\n## 1. Listen for the event\n\nRegister your receiver for `checkout.session.completed` and `checkout.session.expired` (see [Webhooks](webhooks.md) for `POST /v1/webhooks` and signature checks). Do this before the first sale: fulfilment happens there.\n\n## 2. Create a session on your server\n\nWhen the buyer presses Buy, your server (never the browser) creates the session with your secret key:\n\n```\nPOST /v1/checkout/sessions\nAuthorization: Bearer zp_test_...\nIdempotency-Key: 8f1c2a4e-buy-user_42-1\nContent-Type: application/json\n\n{\n  \"merchant_id\": \"mch_01j...\",\n  \"line_items\": [\n    {\"price_data\": {\"currency\": \"usd\", \"unit_amount\": 1500,\n                    \"product_data\": {\"name\": \"7-day pass\"}},\n     \"quantity\": 1}\n  ],\n  \"success_url\": \"https://example.com/thanks?session_id={CHECKOUT_SESSION_ID}\",\n  \"cancel_url\": \"https://example.com/pricing\",\n  \"client_reference_id\": \"user_42\",\n  \"metadata\": {\"access_days\": \"7\"}\n}\n```\n\nResponse `200`:\n\n```json\n{\"object\":\"checkout.session\",\"id\":\"cs_01j...\",\"livemode\":false,\"status\":\"open\",\"payment_status\":\"unpaid\",\"url\":\"https://HOST/pay/...\",\"amount_total\":1500,\"currency\":\"usd\",\"client_reference_id\":\"user_42\",\"payment\":null,\"expires_at\":1763600000}\n```\n\n- Amounts are integer cents. The total is computed from `line_items` on our server.\n- `Idempotency-Key` is optional, as on Stripe, and recommended: use one key per purchase attempt; a retry of the same request uses the same key and gets the same session back. Without a key, each request creates a session.\n- Send `client_reference_id` on every create, so you can find your user or order from the session and the event.\n- `{CHECKOUT_SESSION_ID}` in `success_url` is replaced with the session id.\n\nThen redirect the buyer with a `303` to `url`. Do not store `url` in logs; it opens the payment page.\n\nFrom an agent, the `create_checkout_session` tool does the same. It takes `client_reference_id` and a `request_key`: use a new `request_key` for each purchase and the same one only to retry the same call.\n\n## 3. In the sandbox, pay\n\nWith no browser (scripts, agents, CI): `POST /v1/test_helpers/checkout/sessions/{id}/confirm` with `{\"card\": \"tok_visa\", \"email\": \"buyer@example.com\"}` and an `Idempotency-Key`, or the MCP tool `confirm_checkout_session`. It runs the page's own confirm and answers the session, `status: complete`; the webhooks are the real ones. `tok_chargeDeclined` answers `402 card_declined` and leaves the session open. Test mode only; see `sandbox`, \"Test card tokens\".\n\nIn a browser: open `url` in a regular browser, type test card `4761739001010010` (any future expiry, any CVV), and pay. A browser driven by an automation tool is refused by the processor's reCAPTCHA.\n\nTo pay a second time, wait a few minutes or use another amount. The processor refuses the same card for the same amount moments after a payment as a repeat: the page shows \"This looks like a repeat of a payment made moments ago. Do not pay again. Contact {merchant} about this order.\" ({merchant} is the merchant's display name), and the session is held (below) until a complete read of the processor's records shows no sale for that attempt. The attempt is marked unknown about 15 minutes after the payment, and the first read that can settle it runs about an hour after that, so the session usually expires, and the `client_reference_id` is free again, about an hour and a half after the payment. If that read cannot finish (the processor is unreachable, or its records cannot be read completely), it is tried again about six hours and twenty-four hours after the hold; after that the session waits for ZeroPoint support.\n\n## 4. Fulfil on the webhook, once per session id\n\nThe success redirect proves nothing: anyone can open `success_url` with any id. Grant the pass only in your webhook handler:\n\n1. Verify the `ZeroPoint-Signature` header ([Webhooks](webhooks.md)).\n2. On `checkout.session.completed`, read `data.object`: the session.\n3. Record the session id as fulfilled in the same database transaction that grants the pass, and skip it if it is already there. Events can arrive twice; fulfil once per session id.\n4. Use `client_reference_id` (or your `metadata`) to find the user.\n\n## 5. Check the session on the success page\n\nYour success page retrieves the session with your server key:\n\n```\nGET /v1/checkout/sessions/{session_id}\n```\n\nShow \"paid\" only when `status` is `complete`, `payment_status` is `paid`, `merchant_id` is yours and `amount_total` is what you expected. While the webhook has not arrived yet, show \"Payment received, setting up your pass\" and poll your own database, not ZeroPoint, for the fulfilment.\n\n## When a payment's outcome is unknown\n\nIf the processor's answer for an attempt is unknown, the session is **held**: `status` stays `open` with `payment_status: processing`, and the page refuses another card. It completes or expires once the outcome is proven by the processor's records; a hold whose records never settle it waits for ZeroPoint support. While it is held, a create with the same `client_reference_id` for the same merchant answers:\n\n```json\n{\"error\":{\"type\":\"conflict\",\"code\":\"checkout_outcome_unknown\",\"param\":\"client_reference_id\",\"held_session_id\":\"cs_01j...\",\"message\":\"...\"}}\n```\n\nShow the buyer \"Your earlier payment is being confirmed.\" and do not create another session for them. Retrieve `held_session_id` later to see how it ended.\n\n## Cancel a session\n\n`POST /v1/checkout/sessions/{session_id}/expire` (no body; an `Idempotency-Key` is optional) closes an open session so its link stops working. It answers `409 checkout_session_busy` with `Retry-After` while an attempt is in flight, and `409 checkout_outcome_unknown` while the session is held.\n\n## Refusals and Idempotency-Key\n\nA refusal is kept under its `Idempotency-Key` for 24 hours, like any other answer. After you fix the cause (an `https` redirect, a live platform being enabled, a held session closing), send the request again under a new `Idempotency-Key`; the old key replays the old refusal. The MCP tools keep only successes, so after a fix the same `request_key` works.\n\n| Code | Meaning |\n|---|---|\n| `parameter_invalid` | A value breaks a rule: amount, quantity, currency, text length, expiry. |\n| `parameter_unknown` | A Stripe field this API never supports, named in `param`. |\n| `parameter_unavailable` | A field not available yet (see below). |\n| `url_invalid` | `success_url` or `cancel_url` is not an allowed URL. |\n| `checkout_unavailable` | Live mode is not enabled for your platform yet. |\n| `checkout_outcome_unknown` | A held session exists for this `client_reference_id`. |\n| `resource_limit_exceeded`, `sandbox_daily_limit_exceeded` | Sandbox ceilings on open and daily sessions. |\n\n## Also available\n\n- Catalog prices: a line `{price: price_..., quantity: 1}` instead of `price_data`.\n- Promotion codes: `allow_promotion_codes: true` (or one code in `discounts`) on a session with exactly one catalog price line. A code that brings the total to $0 lets the buyer complete the order with no card; the session completes with `payment_status` `no_payment_required`.\n- Subscription mode: send `mode: subscription` with one recurring catalog `price` to sell a plan; `subscription_data.metadata` is copied onto the subscription. A code that would make the first invoice $0 is refused. See [Hosted checkout](checkout.md) for its rules.\n\n## Not available yet\n\n- Google Pay and Apple Pay on the page: card entry only.\n- Tax: no tax is computed and no tax line is shown.\n\nSee also [Migrate from Stripe Checkout](migrate-from-stripe-checkout.md).","updated":1795275945}