When
A published App should gate a capability or meter usage with one-time products (feature unlock and/or credit packs).
Outcome
- Products + benefits configured at Space billing scope (
business = space) - App hardcodes
productKey/benefitKey - Closed loop works only inside a published Cohub App shell:
- feature:
load → gate → purchase → return → load - credits:
load → balance → consume → (insufficient? purchase) → return
- feature:
Preconditions
- App is published (not raw static URL / local preview).
- You develop commerce against the public App URL.
- Checkout state is owned by the outer host (sign-in, top-level navigation, return), not primarily by the iframe.
- Purchase is triggered by a user action, not during app initialization.
Benefit types
| Type | Role |
|---|---|
feature | Access gate via entitlement metadata (enabled, limits) |
credits | Consumable balance auto-granted on paid order (cohub_credit) |
Steps
- Design keys as versioned product ids (
pro_unlock_v2,credit_pack_050). Prices are immutable after create — change price by new product + bind same benefit + archive old. - In the App (published runtime): ```ts const cohub = createCohubClient(); await cohub.context(); // must be non-null in App shell
const { products } = await cohub.app.commerce.resolveProducts({ productKeys: ["pro_unlock"], }); const { entitlements, credits } = await cohub.app.commerce.getEntitlements(); ```
- Feature gate: if benefit not enabled → on user click →
purchase({ productKey })→ host transitions straight to checkout → return → re-read entitlements / order. - Credits: if
credits.availableenough →consumeCredits({ amount, operationId, reason })with uniqueoperationId(idempotent retry). Oninsufficient→ purchase pack. - After return URL (
cohub_checkout/cohub_order), callgetCheckoutState()and optionallygetOrder(orderId). Reuse the samepurchaseAttemptIdwhen retrying a timed-out purchase so Billing resolves the original order. - For expensive side effects: App only triggers; agent/script consumes credits, writes result file; App reads file (don't parse chat turns as ledger).
- Keep scopes minimal — commerce UI still does not justify blanket prompt/generation scopes.
Checkout behavior (v2.39-v2.41)
- In-app purchases go straight to checkout; the redundant host confirmation dialog is gone.
- Concurrent or repeated purchase requests for the same product are deduplicated and serialized by attempt id, so a double click cannot create two orders.
- Redirects carry a server-appended outcome and product key, and a checkout-confirmation endpoint reconciles each return against the provider's settlement state. Confirm success only when an order or subscription is actually paid or active — a forged URL cannot claim a purchase, and a stale redirect resolves to the correct product state.
- The billing catalog surfaces viewer-independent promotions (discount percent and end date) for pricing pages; priced offers are applied server-side only to eligible signed-in users.
CLI helper
cohub apps commerce credits consume --app-id <app-id> --amount 100
cohub apps commerce products resolve --app-id <app-id> --product-key pro_unlock
cohub apps commerce entitlements --app-id <app-id> --jsonDone when
- [ ] Feature and/or credit loop verified on public App URL
- [ ] Purchase return restores coherent UI state
- [ ] Purchase success is confirmed against the order/subscription state, not only the return URL
- [ ]
operationIdused for consumes - [ ] Purchase retries reuse
purchaseAttemptId - [ ] Side effects persisted as Space files when metered work is heavy
Avoid
- Testing commerce only on local static previews (
context()is null) - Letting the iframe own pending checkout as source of truth
- Triggering purchase on app load instead of a user action
- Mutating product price in place
- Double-charge retries without idempotency keys