# Stripe checkout and CUSTOS lease provisioning

The Express backend serves `POST /api/stripe/webhook` before any JSON parser. It uses Stripe’s signature verifier on the exact raw request bytes with `STRIPE_WEBHOOK_SECRET` and a 300-second tolerance. An unsigned or altered body is rejected with HTTP 400. Authenticated unrelated events are acknowledged without provisioning.

For `checkout.session.completed`, the handler retrieves the session from Stripe and verifies that it is complete, paid, one-time, for the configured ARKHEIA product and price, with quantity one. Unpaid sessions are not granted leases. The server-created checkout is linked to a stored buyer reference, email, actual Ed25519 holder public key, and hashed browser-access token. The issuer never generates a buyer private key or derives a holder from an email address.

The signer implements the supplied v1.0.1 CUSTOS lease schema. Its Ed25519 preimage is `ARKHEIA/custos/lease/v1` followed by a zero byte and integers-only RFC 8785 JCS of the lease without `sig`. Object keys are sorted in UTF-16 code-unit order. The supplied public lease and mandate fixture signatures were both verified offline. The fixture was not replayed against the live transfer endpoint.

## Required runtime configuration

`CUSTOS_ISSUER_SEED` contains the server-only 32-byte Ed25519 issuer seed as 64 hex characters. It is read from the environment, never from an embedded fallback. `STRIPE_WEBHOOK_SECRET` and `STRIPE_SECRET_KEY` are also server-only environment values. On Manus, Stripe’s integration owns its credentials; live keys are configured in Settings → Integrations.

Approved public defaults are centralized in `server/tier-config.ts`. The sandbox Product is `prod_Vi7NZ0gfNDM9ky`; its active one-time Price is `price_1Ugm590tW4sX0txtl7LRIf3e`, USD 1.00 (100 minor units), with `livemode: false`. This tier refuses live Stripe credentials and live webhook events. Enabling live payments requires a separately authorized configuration change, not simply entering live keys.

`CUSTOS_TRUSTED_ISSUER_PUBLIC_KEY` optionally overrides the default issuer pin `157d41e3024408a115fb8dded8acfc87411668d24df3cba143a199a12de08fda`. The signer checks that its derived public key matches this pin. The user states that the default is enrolled in the engine’s `custos_issuers` set. Enrollment is controlled by the engine operator’s `ARKHEIA_CLEARANCE_TRUST_FILE`; a storefront configuration alone cannot add trust to the engine. The clearance engine’s own signing key must not be used as an issuer.

`CUSTOS_LEASE_POLICY` optionally overrides the approved public policy: `swarm_id: "arkheia-7-prod"`, `scopes: ["aegis.evaluate", "mandate.verify"]`, `max_amount: "1000000000"`, and `ttl: 50000`. The JSON object has exactly `swarm_id`, `scopes`, `max_amount`, and `ttl`. Scopes must be strictly ascending, unique, and at most 64. `max_amount` is a canonical unsigned 128-bit decimal string. `ttl` is between 1 and 100000 **Lamport ticks**, not seconds. Do not substitute the high-value transfer fixture policy.

`STRIPE_PRICE_ID` optionally overrides the approved one-time test Price in the same Stripe account/environment. `APP_ORIGIN` is the actual deployed public browser origin, such as the connected ericscott.ai site; it is used for checkout origin validation and canonical metadata. Do not use an internal proxy hostname. The actual browser-visible Preview origin is already allowed for sandbox checkout. The checkout page refuses purchases if required signer, webhook, or Price configuration is missing.

## Issuance and retries

The webhook reads the public clearance head and checks its public key and key ID against the clearance pins before using its Lamport clock. It constructs a lease with a fresh 16-byte ID, the issuer public key, the buyer holder public key, the approved policy, current `lamport_issued`, and signature.

A MySQL transaction inserts the unique Stripe event ID and locks the checkout record. It signs and stores a lease only when no fulfillment has been committed. The event and entitlement commit together. Repeated delivery of an event, or distinct events for an already fulfilled session, cannot issue a second stored lease. Failed processing rolls back and returns 503 so Stripe can retry. No receipt, lease, email, request body, or private material is logged.

Run the committed additive migrations with `pnpm db:migrate` before deployment. Managed development and production share the project database; tests use generated in-memory test keys and avoid writing real purchase records.

## Buyer delivery

`GET /api/orders` requires the secure, HttpOnly, SameSite=None browser token established when checkout is started. Only its hash is stored. A public checkout session ID is not an access credential. The success page renders processing until stored fulfillment is available; the buyer can download the signed lease once committed. The token expires after 30 days. Cross-device or expired-token recovery is not implemented and requires a separate authenticated account or verified email-link flow.

This build does not renew or revoke leases on refund, dispute, or subscription changes; checkout sells a one-time bounded lease and only `checkout.session.completed` is provisioned. Do not offer recurring plans or delayed payment methods without adding the corresponding lifecycle handling.

## ericscott.ai handoff

This is a separate prepared website project, not an in-place edit of the existing ericscott.ai hosting. The current project has no custom domain attached. Publish a saved checkpoint, then connect ericscott.ai in Settings → Domains and complete the records shown there. Coordinate with the existing site’s operator before changing DNS. No DNS or existing-site changes were performed.

Claim the Stripe sandbox promptly through Settings → Integrations. Test checkout with Stripe’s `4242 4242 4242 4242` card, any future expiry, and any three-digit CVC; do not use real card details. Live payment activation requires separate authorization, removal of the sandbox-only restriction, approved live issuer/policy/price/origin configuration, live keys in Settings → Integrations, and republishing. No real checkout payment was performed during development.
