@volter/twin-stripe 0.1.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/README.md +64 -27
  2. package/client/dashboard-api.ts +286 -0
  3. package/client/stripe-mirror.css +272 -159
  4. package/client/stripe-mirror.tsx +1384 -541
  5. package/dist/client/dashboard-api.d.ts +107 -0
  6. package/dist/client/dashboard-api.js +238 -0
  7. package/dist/client/dashboard-api.ts +286 -0
  8. package/dist/client/stripe-mirror.bundle.js +236 -0
  9. package/dist/client/stripe-mirror.css +275 -0
  10. package/dist/client/stripe-mirror.d.ts +134 -0
  11. package/dist/client/stripe-mirror.js +823 -0
  12. package/dist/client/stripe-mirror.tsx +1534 -0
  13. package/dist/src/cli.d.ts +2 -0
  14. package/dist/src/cli.js +39 -0
  15. package/dist/src/generated/events.gen.json +1 -0
  16. package/dist/src/generated/surface.gen.json +1 -0
  17. package/dist/src/generated/ui.gen.json +1 -0
  18. package/dist/src/index.d.ts +14 -0
  19. package/dist/src/index.js +73 -0
  20. package/dist/src/manifest.d.ts +2 -0
  21. package/dist/src/manifest.js +1065 -0
  22. package/dist/src/screens/checkout.d.ts +31 -0
  23. package/dist/src/screens/checkout.js +241 -0
  24. package/dist/src/screens/consent-skin.d.ts +4 -0
  25. package/dist/src/screens/consent-skin.js +18 -0
  26. package/dist/src/screens/financial-connections.d.ts +5 -0
  27. package/dist/src/screens/financial-connections.js +90 -0
  28. package/dist/src/screens/identity.d.ts +5 -0
  29. package/dist/src/screens/identity.js +86 -0
  30. package/dist/src/screens/industries.d.ts +1 -0
  31. package/dist/src/screens/industries.js +267 -0
  32. package/dist/src/screens/onboarding.d.ts +13 -0
  33. package/dist/src/screens/onboarding.js +225 -0
  34. package/dist/src/screens/portal.d.ts +5 -0
  35. package/dist/src/screens/portal.js +214 -0
  36. package/dist/src/screens/public-details.d.ts +5 -0
  37. package/dist/src/screens/public-details.js +90 -0
  38. package/dist/src/semantics/after-payment.d.ts +22 -0
  39. package/dist/src/semantics/after-payment.js +93 -0
  40. package/dist/src/semantics/apps-secrets.d.ts +2 -0
  41. package/dist/src/semantics/apps-secrets.js +54 -0
  42. package/dist/src/semantics/balance.d.ts +11 -0
  43. package/dist/src/semantics/balance.js +195 -0
  44. package/dist/src/semantics/billing.d.ts +2 -0
  45. package/dist/src/semantics/billing.js +220 -0
  46. package/dist/src/semantics/charges.d.ts +28 -0
  47. package/dist/src/semantics/charges.js +201 -0
  48. package/dist/src/semantics/checkout.d.ts +15 -0
  49. package/dist/src/semantics/checkout.js +303 -0
  50. package/dist/src/semantics/connect.d.ts +5 -0
  51. package/dist/src/semantics/connect.js +476 -0
  52. package/dist/src/semantics/coupons.d.ts +6 -0
  53. package/dist/src/semantics/coupons.js +92 -0
  54. package/dist/src/semantics/credit-notes.d.ts +2 -0
  55. package/dist/src/semantics/credit-notes.js +172 -0
  56. package/dist/src/semantics/customers.d.ts +6 -0
  57. package/dist/src/semantics/customers.js +429 -0
  58. package/dist/src/semantics/disputes.d.ts +2 -0
  59. package/dist/src/semantics/disputes.js +51 -0
  60. package/dist/src/semantics/entitlements.d.ts +2 -0
  61. package/dist/src/semantics/entitlements.js +95 -0
  62. package/dist/src/semantics/ephemeral-keys.d.ts +2 -0
  63. package/dist/src/semantics/ephemeral-keys.js +34 -0
  64. package/dist/src/semantics/files.d.ts +2 -0
  65. package/dist/src/semantics/files.js +125 -0
  66. package/dist/src/semantics/invoices.d.ts +18 -0
  67. package/dist/src/semantics/invoices.js +541 -0
  68. package/dist/src/semantics/issuing.d.ts +13 -0
  69. package/dist/src/semantics/issuing.js +570 -0
  70. package/dist/src/semantics/ledger.d.ts +54 -0
  71. package/dist/src/semantics/ledger.js +181 -0
  72. package/dist/src/semantics/payment-intents.d.ts +18 -0
  73. package/dist/src/semantics/payment-intents.js +404 -0
  74. package/dist/src/semantics/payment-links.d.ts +2 -0
  75. package/dist/src/semantics/payment-links.js +133 -0
  76. package/dist/src/semantics/payment-methods.d.ts +20 -0
  77. package/dist/src/semantics/payment-methods.js +138 -0
  78. package/dist/src/semantics/plans.d.ts +5 -0
  79. package/dist/src/semantics/plans.js +121 -0
  80. package/dist/src/semantics/platform.d.ts +9 -0
  81. package/dist/src/semantics/platform.js +206 -0
  82. package/dist/src/semantics/products.d.ts +2 -0
  83. package/dist/src/semantics/products.js +140 -0
  84. package/dist/src/semantics/radar.d.ts +2 -0
  85. package/dist/src/semantics/radar.js +83 -0
  86. package/dist/src/semantics/refunds.d.ts +9 -0
  87. package/dist/src/semantics/refunds.js +195 -0
  88. package/dist/src/semantics/renewals.d.ts +47 -0
  89. package/dist/src/semantics/renewals.js +251 -0
  90. package/dist/src/semantics/setup-intents.d.ts +2 -0
  91. package/dist/src/semantics/setup-intents.js +84 -0
  92. package/dist/src/semantics/shared.d.ts +78 -0
  93. package/dist/src/semantics/shared.js +192 -0
  94. package/dist/src/semantics/subscription-schedules.d.ts +2 -0
  95. package/dist/src/semantics/subscription-schedules.js +119 -0
  96. package/dist/src/semantics/subscriptions.d.ts +11 -0
  97. package/dist/src/semantics/subscriptions.js +605 -0
  98. package/dist/src/semantics/tax.d.ts +2 -0
  99. package/dist/src/semantics/tax.js +197 -0
  100. package/dist/src/semantics/terminal.d.ts +5 -0
  101. package/dist/src/semantics/terminal.js +182 -0
  102. package/dist/src/semantics/test-clocks.d.ts +6 -0
  103. package/dist/src/semantics/test-clocks.js +73 -0
  104. package/dist/src/semantics/tokens.d.ts +4 -0
  105. package/dist/src/semantics/tokens.js +44 -0
  106. package/dist/src/semantics/transfers.d.ts +2 -0
  107. package/dist/src/semantics/transfers.js +154 -0
  108. package/dist/src/semantics/treasury.d.ts +2 -0
  109. package/dist/src/semantics/treasury.js +377 -0
  110. package/dist/src/semantics/webhook-endpoints.d.ts +3 -0
  111. package/dist/src/semantics/webhook-endpoints.js +85 -0
  112. package/dist/src/stripe-budget.d.ts +55 -0
  113. package/dist/src/stripe-budget.js +155 -0
  114. package/dist/src/stripe-capabilities.d.ts +3 -0
  115. package/dist/src/stripe-capabilities.js +5052 -0
  116. package/dist/src/stripe-conformance.d.ts +41 -0
  117. package/dist/src/stripe-conformance.js +96 -0
  118. package/dist/src/stripe-connector.d.ts +161 -0
  119. package/dist/src/stripe-connector.js +414 -0
  120. package/dist/src/stripe-emit.d.ts +2 -0
  121. package/dist/src/stripe-emit.js +145 -0
  122. package/dist/src/stripe-events.d.ts +93 -0
  123. package/dist/src/stripe-events.js +388 -0
  124. package/dist/src/stripe-js.d.ts +4 -0
  125. package/dist/src/stripe-js.js +70 -0
  126. package/dist/src/stripe-mirror-ui.d.ts +15 -0
  127. package/dist/src/stripe-mirror-ui.js +87 -0
  128. package/dist/src/stripe-params.d.ts +3 -0
  129. package/dist/src/stripe-params.js +43 -0
  130. package/dist/src/stripe-perform-harness.d.ts +9 -0
  131. package/dist/src/stripe-perform-harness.js +26 -0
  132. package/dist/src/stripe-server.d.ts +33 -0
  133. package/dist/src/stripe-server.js +326 -0
  134. package/dist/src/stripe-shared.d.ts +106 -0
  135. package/dist/src/stripe-shared.js +273 -0
  136. package/dist/src/stripe-twin.d.ts +155 -0
  137. package/dist/src/stripe-twin.js +1226 -0
  138. package/dist/src/stripe-ui-conformance.d.ts +5 -0
  139. package/dist/src/stripe-ui-conformance.js +79 -0
  140. package/dist/src/stripe-ui-structure.d.ts +3 -0
  141. package/dist/src/stripe-ui-structure.js +168 -0
  142. package/dist/src/stripe-version.d.ts +10 -0
  143. package/dist/src/stripe-version.js +285 -0
  144. package/dist/test-fixtures/stripe-known-deviations.json +105 -0
  145. package/dist/test-fixtures/stripe-openapi-operations.SOURCE.md +14 -0
  146. package/dist/test-fixtures/stripe-openapi-operations.json +4717 -0
  147. package/dist/test-fixtures/stripe-schemas.SOURCE.md +35 -0
  148. package/dist/test-fixtures/stripe-schemas.json +3740 -0
  149. package/package.json +18 -10
  150. package/src/cli.ts +7 -7
  151. package/src/generated/events.gen.json +1 -0
  152. package/src/generated/surface.gen.json +1 -0
  153. package/src/generated/ui.gen.json +1 -0
  154. package/src/index.ts +31 -9
  155. package/src/manifest.ts +1097 -0
  156. package/src/screens/checkout.tsx +252 -0
  157. package/src/screens/consent-skin.ts +20 -0
  158. package/src/screens/financial-connections.tsx +101 -0
  159. package/src/screens/identity.tsx +96 -0
  160. package/src/screens/industries.ts +267 -0
  161. package/src/screens/onboarding.tsx +243 -0
  162. package/src/screens/portal.tsx +218 -0
  163. package/src/screens/public-details.tsx +105 -0
  164. package/src/semantics/after-payment.ts +113 -0
  165. package/src/semantics/apps-secrets.ts +58 -0
  166. package/src/semantics/balance.ts +209 -0
  167. package/src/semantics/billing.ts +216 -0
  168. package/src/semantics/charges.ts +211 -0
  169. package/src/semantics/checkout.ts +297 -0
  170. package/src/semantics/connect.ts +471 -0
  171. package/src/semantics/coupons.ts +97 -0
  172. package/src/semantics/credit-notes.ts +168 -0
  173. package/src/semantics/customers.ts +432 -0
  174. package/src/semantics/disputes.ts +62 -0
  175. package/src/semantics/entitlements.ts +94 -0
  176. package/src/semantics/ephemeral-keys.ts +34 -0
  177. package/src/semantics/files.ts +143 -0
  178. package/src/semantics/invoices.ts +541 -0
  179. package/src/semantics/issuing.ts +585 -0
  180. package/src/semantics/ledger.ts +216 -0
  181. package/src/semantics/payment-intents.ts +420 -0
  182. package/src/semantics/payment-links.ts +148 -0
  183. package/src/semantics/payment-methods.ts +143 -0
  184. package/src/semantics/plans.ts +131 -0
  185. package/src/semantics/platform.ts +220 -0
  186. package/src/semantics/products.ts +154 -0
  187. package/src/semantics/radar.ts +85 -0
  188. package/src/semantics/refunds.ts +218 -0
  189. package/src/semantics/renewals.ts +274 -0
  190. package/src/semantics/setup-intents.ts +87 -0
  191. package/src/semantics/shared.ts +215 -0
  192. package/src/semantics/subscription-schedules.ts +129 -0
  193. package/src/semantics/subscriptions.ts +610 -0
  194. package/src/semantics/tax.ts +220 -0
  195. package/src/semantics/terminal.ts +195 -0
  196. package/src/semantics/test-clocks.ts +77 -0
  197. package/src/semantics/tokens.ts +52 -0
  198. package/src/semantics/transfers.ts +174 -0
  199. package/src/semantics/treasury.ts +383 -0
  200. package/src/semantics/webhook-endpoints.ts +87 -0
  201. package/src/stripe-budget.ts +4 -4
  202. package/src/stripe-capabilities.ts +1456 -222
  203. package/src/stripe-conformance.ts +6 -5
  204. package/src/stripe-connector.ts +68 -40
  205. package/src/stripe-emit.ts +14 -7
  206. package/src/stripe-events.ts +94 -36
  207. package/src/stripe-js.ts +70 -0
  208. package/src/stripe-mirror-ui.ts +28 -298
  209. package/src/stripe-params.ts +44 -0
  210. package/src/stripe-perform-harness.ts +29 -0
  211. package/src/stripe-server.ts +263 -38
  212. package/src/stripe-shared.ts +294 -0
  213. package/src/stripe-twin.ts +429 -5325
  214. package/src/stripe-ui-conformance.ts +70 -107
  215. package/src/stripe-ui-structure.ts +124 -348
  216. package/src/stripe-version.ts +278 -0
  217. package/test-fixtures/stripe-known-deviations.json +2 -7
  218. package/test-fixtures/stripe-openapi-operations.json +1188 -2855
  219. package/src/stripe-form.ts +0 -35
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # @volter/twin-stripe
2
2
 
3
3
  The **Stripe twin** — a local, spec-correct replica of the Stripe REST API on the
4
- shared [`@volter/twin`](../control-plane) kernel. The real `stripe` SDK works against it
4
+ shared [`@volter/world-core`](../../world-core) kernel. The real `stripe` SDK works against it
5
5
  unmodified; it's the QA stack's authoritative local Stripe (it replaced the old
6
6
  hand-made in-process mock).
7
7
 
@@ -12,7 +12,18 @@ hand-made in-process mock).
12
12
  payment_intents, setup_intents, payment_methods, products, prices, invoices,
13
13
  invoiceitems, subscriptions, refunds, coupons, promotion_codes, identity
14
14
  verification sessions, ephemeral_keys, checkout sessions, billing/customer-portal
15
- sessions + configurations.
15
+ sessions + configurations, and the legacy **Plans** API (`semantics/plans.ts`: a plan is the recurring
16
+ price it is; plan writes send `plan.created`/`plan.updated`/`plan.deleted`; a deleted plan's price stays
17
+ readable, inactive, for its existing subscribers).
18
+ - **Hosted pages** (`src/screens/`): Stripe's own pages an application sends a person to, served at their
19
+ vendor urls — Checkout's payment page (`checkout.stripe.com/c/pay/{id}`: the customer pays, the session
20
+ completes and `checkout.session.completed` fires), the Customer Portal (`billing.stripe.com/p/session/{id}`),
21
+ Connect hosted onboarding (`connect.stripe.com/setup/…`), Identity (`verify.stripe.com/start/{id}`) and the
22
+ Financial Connections bank-linking flow Stripe.js opens.
23
+ - **Stripe.js** (`stripe-js.ts`): `js.stripe.com` serves the client library at `/v3`, `/v3/`, `/v3/stripe.js` and
24
+ `/<release train>/stripe.js` — the paths the `@stripe/stripe-js` loader loads or accepts — with
25
+ `redirectToCheckout({ sessionId })` sending the page to the hosted Checkout page; any other member throws,
26
+ naming itself (`stripe.js.loader`).
16
27
  - **Writes** are local transactions (action log); reads are the projection (R3/R5/R18).
17
28
  - **Card declines** (`stripe-twin.ts`): Stripe's documented test cards get vendor-faithful
18
29
  outcomes on charge create / PaymentIntent confirm — `4242…4242` (and `pm_card_visa`/`tok_visa`)
@@ -20,10 +31,35 @@ hand-made in-process mock).
20
31
  `pm_card_*`/`tok_*` tokens) return the real `card_error` envelope (HTTP 402) and leave a
21
32
  confirmed PaymentIntent at `requires_payment_method`. Unknown cards succeed. See
22
33
  `_card.declines-test-set-only` in `stripe-known-deviations.json`.
34
+ - **Manual capture** (`semantics/payment-intents.ts`, `semantics/charges.ts`): confirming a `capture_method=manual`
35
+ intent authorizes an uncaptured charge (`captured: false`, nothing on the balance); capture takes that charge
36
+ (`charge.captured`; a partial capture releases the rest with no Refund, Stripe's basil change), and an incremental
37
+ authorization grows its amount. An uncaptured charge is never refunded out of the balance: cancelling a
38
+ `requires_capture` intent releases the authorization (no Refund; `amount_captured` 0, `refunded` false) and its
39
+ charge's capture is then refused with `payment_intent_unexpected_state`, a refund of a PaymentIntent's uncaptured
40
+ charge is refused (cancel the intent, as Stripe's refunds guide says), and a `capture=false` charge's refund releases
41
+ it, after which its capture is refused with `charge_already_refunded`. An uncaptured charge is refused as a transfer's
42
+ `source_transaction` (the twin's rule; the docs are silent).
23
43
  - **Idempotency keys**: the `Idempotency-Key` header is honored on POST — a replay with the
24
44
  same key returns the stored response without re-applying the write (`_idempotency.stored-per-root`).
25
45
  - **Events** (`stripe-events.ts`): fires Stripe `event` objects on write
26
- (`payment_intent.succeeded`, `customer.subscription.created`, `invoice.paid`, …).
46
+ (`payment_intent.succeeded`, `customer.subscription.created`, `invoice.paid`, …). A charge fires the event
47
+ Stripe sends for it — `charge.succeeded` (made, or authorized under manual capture), `charge.failed` (a declined
48
+ attempt), `charge.captured` (an authorization captured) — never an invented `charge.created`.
49
+ - **Connect webhooks**: an event of a connected account (a write made with its `Stripe-Account` header, its own
50
+ `account.updated`, or a row kept on its books such as the automatic payout time makes for it) carries the
51
+ top-level `account` and reaches only endpoints created with `connect=true`; the platform's reach only the others
52
+ (`stripe.connect.webhook_scope`, `stripe.connect.event_scope`). `GET /v1/events` and `/v1/events/{id}` answer the
53
+ acting account's events: with the header that account's, without it the platform's. An event's id is minted once
54
+ per World (`evt_twin_<n>`) and is the delivered webhook's id and the stored event's alike (`stripe.events.ids`).
55
+ `world-stripe emit` re-fires a connected account's object with its `account`; it does not yet route it to Connect
56
+ endpoints only (the kernel's emit seam has no endpoint scope).
57
+ - **Signing secrets in a World**: Stripe mints an endpoint's `secret`; in a World the app's env is the World's, so an
58
+ endpoint is given the World's value, which the app already verifies with: `STRIPE_WEBHOOK_SECRET` (or
59
+ `STRIPE_WEBHOOK_SIGNING_SECRET`, `STRIPE_ENDPOINT_SECRET`, `STRIPE_WEBHOOK_SECRET_KEY`,
60
+ `NEXT_PRIVATE_STRIPE_WEBHOOK_SECRET`), and for a `connect=true` endpoint `STRIPE_CONNECT_WEBHOOK_SECRET` (or
61
+ `STRIPE_WEBHOOK_SECRET_CONNECT`, `STRIPE_CONNECT_WEBHOOK_SIGNING_SECRET`) first. With none set the twin mints
62
+ `whsec_twin_<id>`. Where Stripe stops and the twin decides: Stripe lets no one choose a secret.
27
63
  - **Conformance** (`stripe-conformance.ts`): field name + type checked vs Stripe's
28
64
  real OpenAPI (standing gate).
29
65
  - **UI mirror** (`stripe-mirror-ui.ts`): a Stripe-dashboard-style React app over the
@@ -42,10 +78,15 @@ Point the real `stripe` SDK at it with `{ host, port, protocol: 'http' }`.
42
78
 
43
79
  ## Interaction surfaces
44
80
 
45
- 1. **SDK/API** — *zero edits (preferred):* `STRIPE_TWIN_URL=http://127.0.0.1:PORT node --require @volter/twin/inject your-app` redirects the real `stripe` SDK from `api.stripe.com` to the twin (`cookbook/zero-edit-inject`). For the browser too: `volter-twin proxy --target <app> --map stripe=http://127.0.0.1:PORT` (browser + backend share one twin). *Or* override directly: `new Stripe(key, { host: '127.0.0.1', port: PORT, protocol: 'http' })`.
46
- 2. **API + CLI** — `world-stripe serve` (writable) + drive with `volter-twin status|plan|refs stripe`, then push.
81
+ 1. **SDK/API** — *zero edits (preferred):* `STRIPE_TWIN_URL=http://127.0.0.1:PORT node --require @volter/world-core/inject your-app` redirects the real `stripe` SDK from `api.stripe.com` to the twin (`cookbook/zero-edit-inject`). For browser routing, follow [run a full stack](../../../docs/guides/run-a-full-stack.md). *Or* override directly: `new Stripe(key, { host: '127.0.0.1', port: PORT, protocol: 'http' })`.
82
+ 2. **API + CLI** — run the twin in a World and inspect it with `volter world log` and `diff`. Use the [deployment guide](../../../docs/guides/deploy-from-a-shared-world.md) for changeset review and deployment.
47
83
  3. **Read-only** — `world-stripe serve --read-only`: unlimited local reads, no rate limits; writes refuse like Stripe (4xx).
48
84
  4. **UI mirror** — `world-stripe mirror` renders a Stripe-dashboard-style view of the twin's state.
85
+ 5. **Time's events** — `POST /_twin/drain` sends what World time produced since the last drain: `balance.available`
86
+ to each account whose settled funds added to its balance (never for negative transactions alone), `payout.paid` for
87
+ each payout that arrived, a connected account's with its `account` to its Connect endpoints
88
+ (`stripe.events.time_drain`). A read sees time's moves without it; a runner's drainer calls it so the events
89
+ arrive, as the qstash and vercel twins' drain doors do.
49
90
 
50
91
  (See Getting Started → "Twin interaction surfaces".)
51
92
 
@@ -53,9 +94,8 @@ Stable on the twin rubric: fidelity, read/write/fork, sync, observability, event
53
94
 
54
95
  ## Coverage
55
96
 
56
- Goal: **honest, explicitly tracked coverage of Stripe's core feature surface.** The only
57
- accepted carve-outs are the explicit **out-of-scope** items listed below. Anything not done
58
- or carved out is a gap to close.
97
+ Goal: **honest, explicitly tracked coverage of Stripe's core feature surface.** Every capability
98
+ is either done or a tracked todo; anything not done is a gap to close.
59
99
 
60
100
  **Done** — core resources: **customers**, **charges**, **payment_intents** (+ confirm),
61
101
  **setup_intents** (+ confirm), **payment_methods** (+ detach), **subscriptions** (create/
@@ -68,7 +108,10 @@ line_items), **Customer Portal** (`billing_portal` sessions + configurations cre
68
108
  list/update), **Connect** (connected **accounts** create/retrieve/list/update/delete +
69
109
  `login_links`, and **transfers** platform→connected-account create/retrieve/list filtered by
70
110
  destination; a new account starts un-onboarded with charges/payouts disabled + a `requirements`
71
- hash), plus **ephemeral_keys**, **identity verification_sessions**, **file_links**
111
+ hash; a transfer's `source_transaction` waives the balance check only while its charge is unsettled,
112
+ takes the charge's `transfer_group` or writes `group_<payment intent>` onto both, and counts reversals
113
+ back as room; connected-account balances, payouts (manual and automatic) and payout reversal, top-ups,
114
+ account sessions, persons, external accounts, application fees and hosted onboarding), plus **ephemeral_keys**, **identity verification_sessions**, **file_links**
72
115
  (synthesized) and **coupons**/**promotion_codes** (retrieve/list, seed-only). Cursor pagination
73
116
  (`limit`/`starting_after`/`ending_before` + `has_more`); per-resource list filters; `expand[]`
74
117
  on the modeled paths; vendor-faithful **test-card declines** (`4242…` succeeds; documented
@@ -107,21 +150,15 @@ helpers) are tracked todos in `stripe-capabilities.ts`. **Terminal**
107
150
  (locations/readers/connection tokens + process_payment_intent) is likewise modeled — the old
108
151
  "Planned" listing for both families was stale.
109
152
 
110
- **Planned** (known-missing, will do) — **Treasury**
111
- (financial accounts), **Climate**, **Financial Connections**, **Entitlements**, **Billing**
112
- credit grants + usage alerts, **Connect** remaining surfaces (account sessions, connected-account
113
- payouts, top-ups, payout reverse), **Reporting/Sigma**, the legacy **Sources** API, and
114
- additional list endpoints/filters as needed. (See `stripe-capabilities.ts` for the full honest
115
- todo list — every entry not explicitly out-of-scope is a tracked gap.)
116
-
117
- **Out of scope** (deliberately not modeled, with reason) —
118
- - Pixel-rendering Stripe-**hosted** Checkout / Customer Portal *pages*: the hosted HTML is
119
- Stripe's, not an API object — the twin models the **Session/API object + redirect `url`**
120
- instead (now **Done**; the page pixels stay out of scope). See
121
- `_checkout.hosted-page-pixels` in `stripe-known-deviations.json`.
122
- - Real **money settlement / bank movement** (proposed — pending owner approval): actually moving
123
- funds is real-world infra, not the API — the payout/balance_transaction/balance *objects* and
124
- their lifecycle are modeled.
153
+ Also modelled: **Treasury** financial accounts and money movement, **Climate** orders,
154
+ **Financial Connections** sessions/accounts/transactions, **Entitlements**, **Billing** meters,
155
+ credit grants and usage alerts, **Reporting** report runs, and the legacy **Sources** API.
156
+
157
+ **Planned** (known-missing, will do) — **Sigma scheduled query runs** (`GET /v1/sigma/scheduled_query_runs`
158
+ list + retrieve, with the run object's status/result-file lifecycle), Treasury financial addresses and
159
+ reversals, Financial Connections refresh, the Issuing gaps above, connector pulls of payment methods and
160
+ setup intents, and additional list endpoints/filters as needed. (See `stripe-capabilities.ts` for the full
161
+ honest todo list — every entry is a tracked gap.)
125
162
 
126
163
  ## Rate budget — the fail-closed backstop on live calls
127
164
 
@@ -136,9 +173,9 @@ validated by *method identity*, so a subclass or a `Proxy` that replaces `checkB
136
173
 
137
174
  The declared numbers: **120 weighted units / 60s** = 2 requests/second — 2% of Stripe's documented 100/s live mode and 8% of the 25/s a sandbox key or any single endpoint gets. `POST`/`DELETE` cost 2 (not a published ratio — a judgement call about blast radius: a write creates a charge, refund or receipt e-mail and cannot be taken back) and `POST /v1/payouts` costs 5 (documented at 15 creates/second). Stripe's own window is a **second** while this one is a minute, so an intra-second burst reaches Stripe's limiter first; the 429 cooldown is the backstop for that shape.
138
175
 
139
- The mechanism is **shared and vendor-agnostic** — it lives in the kernel (`@volter/twin` →
140
- `control-plane/src/rateBudget.ts`); what lives here in [`src/stripe-budget.ts`](src/stripe-budget.ts) is this vendor's
176
+ The mechanism is **shared and vendor-agnostic** — it lives in the kernel (`@volter/world-core` →
177
+ `packages/world-core/src/rateBudget.ts`); what lives here in [`src/stripe-budget.ts`](src/stripe-budget.ts) is this vendor's
141
178
  **declaration** (window, ceiling, per-endpoint weights, and a `reason` citing the limits above) plus
142
179
  the vendor-bound `StripeBudget`. The rule is ratified as
143
- [ARCHITECTURE.md](../../../ARCHITECTURE.md) **D8**, and the kernel module's header documents what the
180
+ [../../../docs/contributing/architecture.md](../../../docs/contributing/architecture.md) **D8**, and the kernel module's header documents what the
144
181
  guard does *not* guarantee — read that before trusting it.
@@ -0,0 +1,286 @@
1
+ // The Stripe Dashboard's reads and writes, through Stripe's own API on this origin (docs/contributing/
2
+ // architecture.md, "Screens": a workspace reads and writes only through the pack's operations). The calls
3
+ // the Dashboard makes for the pages it shows: the balance, payments (PaymentIntents and the Charges they
4
+ // made), refunds, customers and their saved payment methods, subscriptions, invoices, products and prices,
5
+ // and events for a detail page's timeline. The writes a person makes here: refund a payment
6
+ // (POST /v1/refunds), cancel a subscription (DELETE /v1/subscriptions/{id}, or schedule it with
7
+ // cancel_at_period_end), create a customer or a product. Bodies are form-encoded as Stripe's API takes them;
8
+ // an `error` envelope, never the HTTP status alone, is what refuses.
9
+
10
+ type Json = any;
11
+ export type StripeObject = Record<string, any>;
12
+
13
+ export function wireBaseFrom(hasBaseElement: boolean, baseURI: string): string {
14
+ return hasBaseElement ? new URL('.', baseURI).pathname.replace(/\/$/, '') : '';
15
+ }
16
+ // Served at the origin root the base is ''; served under a path prefix (an embedder's earlier <base>) every
17
+ // call resolves inside that prefix.
18
+ export const API_BASE = typeof document === 'undefined' ? '' : wireBaseFrom(document.querySelector('base[href]') !== null, document.baseURI);
19
+
20
+ export class StripeApiError extends Error {
21
+ constructor(message: string, readonly code?: string) { super(message); }
22
+ }
23
+
24
+ async function call(method: string, path: string, body?: Record<string, string | number | boolean | undefined>): Promise<Json> {
25
+ const init: RequestInit = { method, headers: { accept: 'application/json' } };
26
+ if (body) {
27
+ const form = Object.entries(body).filter(([, v]) => v !== undefined && v !== '').map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`).join('&');
28
+ init.body = form;
29
+ (init.headers as Record<string, string>)['content-type'] = 'application/x-www-form-urlencoded';
30
+ }
31
+ const res = await fetch(`${API_BASE}${path}`, init);
32
+ const json = await res.json().catch(() => ({ error: { message: `${res.status} ${res.statusText}` } }));
33
+ if (json && json.error) throw new StripeApiError(String(json.error.message ?? 'Request failed'), json.error.code);
34
+ return json;
35
+ }
36
+
37
+ export const retrieve = (path: string): Promise<StripeObject> => call('GET', path);
38
+ /** A list endpoint's `data`, newest first as Stripe returns it (one page of up to 100). */
39
+ export async function list(path: string): Promise<StripeObject[]> {
40
+ const sep = path.includes('?') ? '&' : '?';
41
+ const body = await call('GET', `${path}${sep}limit=100`);
42
+ return Array.isArray(body?.data) ? body.data : [];
43
+ }
44
+ const optional = <T>(p: Promise<T>, fallback: T): Promise<T> => p.catch(() => fallback);
45
+
46
+ // ── what a payment row is ────────────────────────────────────────────────────
47
+ /** One row of the Payments list: a PaymentIntent (or a Charge made without one) with what the Dashboard
48
+ * shows beside it — its charge, refunds, card and customer. */
49
+ export type Payment = {
50
+ id: string;
51
+ amount: number;
52
+ currency: string;
53
+ status: PaymentStatus;
54
+ description?: string;
55
+ created: number;
56
+ customer?: StripeObject;
57
+ charge?: StripeObject;
58
+ intent?: StripeObject;
59
+ paymentMethod?: StripeObject;
60
+ amountRefunded: number;
61
+ refundedAt?: number;
62
+ declineReason?: string;
63
+ declineMessage?: string;
64
+ };
65
+ export type PaymentStatus = 'succeeded' | 'refunded' | 'partially_refunded' | 'failed' | 'incomplete' | 'uncaptured' | 'canceled' | 'disputed' | 'processing';
66
+
67
+ export function paymentStatusOf(intent: StripeObject | undefined, charge: StripeObject | undefined): PaymentStatus {
68
+ if (charge?.disputed) return 'disputed';
69
+ if (charge?.refunded) return 'refunded';
70
+ if (charge && Number(charge.amount_refunded) > 0) return 'partially_refunded';
71
+ if (charge?.status === 'failed') return 'failed';
72
+ if (intent) {
73
+ if (intent.status === 'succeeded') return 'succeeded';
74
+ if (intent.status === 'canceled') return 'canceled';
75
+ if (intent.status === 'requires_capture') return 'uncaptured';
76
+ if (intent.status === 'processing') return 'processing';
77
+ if (intent.last_payment_error) return 'failed';
78
+ return 'incomplete';
79
+ }
80
+ return charge?.status === 'succeeded' ? 'succeeded' : charge?.status === 'pending' ? 'processing' : 'failed';
81
+ }
82
+
83
+ const DECLINE_REASONS: Record<string, string> = {
84
+ generic_decline: 'Generic decline', insufficient_funds: 'Insufficient funds', lost_card: 'Lost card', stolen_card: 'Stolen card',
85
+ expired_card: 'Expired card', incorrect_cvc: 'Incorrect CVC', processing_error: 'Processing error', card_declined: 'Card declined',
86
+ };
87
+ export const declineReasonText = (code: string | undefined): string | undefined => (code ? DECLINE_REASONS[code] ?? code.replace(/_/g, ' ') : undefined);
88
+
89
+ type Directory = { customers: Map<string, StripeObject>; paymentMethods: Map<string, StripeObject> };
90
+
91
+ async function paymentMethodsById(ids: Iterable<string>): Promise<Map<string, StripeObject>> {
92
+ const unique = [...new Set([...ids].filter((id) => typeof id === 'string' && id.startsWith('pm_')))];
93
+ const found = await Promise.all(unique.map((id) => optional(retrieve(`/v1/payment_methods/${id}`), undefined as StripeObject | undefined)));
94
+ return new Map(found.filter((pm): pm is StripeObject => Boolean(pm?.id)).map((pm) => [pm.id, pm]));
95
+ }
96
+
97
+ function paymentOf(intent: StripeObject | undefined, charge: StripeObject | undefined, refunds: StripeObject[], dir: Directory): Payment {
98
+ const base = intent ?? charge!;
99
+ const customerId = typeof base.customer === 'string' ? base.customer : base.customer?.id;
100
+ const pmId = typeof base.payment_method === 'string' ? base.payment_method : charge?.payment_method;
101
+ const error = intent?.last_payment_error;
102
+ const refundsOf = refunds.filter((r) => (intent && r.payment_intent === intent.id) || (charge && r.charge === charge.id));
103
+ return {
104
+ id: String(base.id),
105
+ amount: Number(base.amount) || 0,
106
+ currency: String(base.currency ?? 'usd'),
107
+ status: paymentStatusOf(intent, charge),
108
+ description: base.description ?? charge?.description ?? undefined,
109
+ created: Number(base.created) || 0,
110
+ customer: customerId ? dir.customers.get(customerId) ?? { id: customerId } : undefined,
111
+ charge, intent,
112
+ paymentMethod: pmId ? dir.paymentMethods.get(pmId) : undefined,
113
+ amountRefunded: Number(charge?.amount_refunded) || refundsOf.reduce((n, r) => n + (r.status === 'failed' ? 0 : Number(r.amount) || 0), 0),
114
+ refundedAt: refundsOf.length ? Math.max(...refundsOf.map((r) => Number(r.created) || 0)) : undefined,
115
+ declineReason: declineReasonText(error?.decline_code ?? error?.code ?? charge?.failure_code),
116
+ declineMessage: error?.message ?? charge?.failure_message ?? undefined,
117
+ };
118
+ }
119
+
120
+ /** The Payments list: every PaymentIntent, and every Charge made without one. */
121
+ export async function loadPayments(): Promise<Payment[]> {
122
+ const [intents, charges, refunds, customers] = await Promise.all([
123
+ list('/v1/payment_intents'), list('/v1/charges'), optional(list('/v1/refunds'), []), list('/v1/customers'),
124
+ ]);
125
+ const chargeById = new Map(charges.map((c) => [c.id, c]));
126
+ const pmIds = [...intents.map((i) => i.payment_method), ...charges.map((c) => c.payment_method)];
127
+ const dir: Directory = { customers: new Map(customers.map((c) => [c.id, c])), paymentMethods: await paymentMethodsById(pmIds) };
128
+ const rows = intents.map((pi) => {
129
+ const charge = (typeof pi.latest_charge === 'string' ? chargeById.get(pi.latest_charge) : pi.latest_charge) ?? charges.find((c) => c.payment_intent === pi.id);
130
+ return paymentOf(pi, charge, refunds, dir);
131
+ });
132
+ for (const ch of charges) if (!ch.payment_intent) rows.push(paymentOf(undefined, ch, refunds, dir));
133
+ return rows.sort((a, b) => b.created - a.created);
134
+ }
135
+
136
+ export type PaymentDetail = Payment & { refunds: StripeObject[]; events: StripeObject[]; invoice?: StripeObject };
137
+
138
+ /** One payment's page: the intent (or charge), its charge, refunds, card, customer and the events about it. */
139
+ export async function loadPayment(id: string): Promise<PaymentDetail> {
140
+ let intent: StripeObject | undefined;
141
+ let charge: StripeObject | undefined;
142
+ if (id.startsWith('ch_') || id.startsWith('py_')) {
143
+ charge = await retrieve(`/v1/charges/${id}`);
144
+ if (typeof charge.payment_intent === 'string') intent = await optional(retrieve(`/v1/payment_intents/${charge.payment_intent}`), undefined);
145
+ } else {
146
+ intent = await retrieve(`/v1/payment_intents/${id}`);
147
+ const chargeId = typeof intent.latest_charge === 'string' ? intent.latest_charge : intent.latest_charge?.id;
148
+ if (chargeId) charge = await optional(retrieve(`/v1/charges/${chargeId}`), undefined);
149
+ }
150
+ const base = intent ?? charge!;
151
+ const customerId = typeof base.customer === 'string' ? base.customer : undefined;
152
+ const pmId = typeof base.payment_method === 'string' ? base.payment_method : charge?.payment_method;
153
+ const refundPath = intent ? `/v1/refunds?payment_intent=${intent.id}` : `/v1/refunds?charge=${charge!.id}`;
154
+ const [customer, pms, refunds, events, invoice] = await Promise.all([
155
+ customerId ? optional(retrieve(`/v1/customers/${customerId}`), { id: customerId }) : Promise.resolve(undefined),
156
+ paymentMethodsById(pmId ? [pmId] : []),
157
+ optional(list(refundPath), []),
158
+ optional(list('/v1/events'), []),
159
+ typeof base.invoice === 'string' ? optional(retrieve(`/v1/invoices/${base.invoice}`), undefined) : Promise.resolve(undefined),
160
+ ]);
161
+ const dir: Directory = { customers: new Map(customer ? [[customer.id, customer]] : []), paymentMethods: pms };
162
+ const related = new Set<string>([base.id, charge?.id, intent?.id, ...refunds.map((r) => r.id)].filter(Boolean) as string[]);
163
+ return {
164
+ ...paymentOf(intent, charge, refunds, dir),
165
+ refunds,
166
+ events: events.filter((e) => related.has(e?.data?.object?.id)).sort((a, b) => b.created - a.created),
167
+ invoice,
168
+ };
169
+ }
170
+
171
+ // ── customers ────────────────────────────────────────────────────────────────
172
+ export type CustomerRow = StripeObject & { defaultCard?: StripeObject; totalSpend: number; payments: number; refunds: number; currency: string };
173
+
174
+ export async function loadCustomers(): Promise<CustomerRow[]> {
175
+ const [customers, charges, intents] = await Promise.all([list('/v1/customers'), list('/v1/charges'), list('/v1/payment_intents')]);
176
+ const pmIds = customers.map((c) => c.invoice_settings?.default_payment_method).filter((x) => typeof x === 'string');
177
+ const pms = await paymentMethodsById(pmIds);
178
+ return customers.map((c) => {
179
+ const mine = charges.filter((ch) => ch.customer === c.id && ch.status === 'succeeded');
180
+ const pmId = c.invoice_settings?.default_payment_method;
181
+ return {
182
+ ...c,
183
+ defaultCard: typeof pmId === 'string' ? pms.get(pmId) : undefined,
184
+ totalSpend: mine.reduce((n, ch) => n + (Number(ch.amount_captured ?? ch.amount) || 0) - (Number(ch.amount_refunded) || 0), 0),
185
+ payments: intents.filter((i) => i.customer === c.id && i.status === 'succeeded').length || mine.length,
186
+ refunds: mine.reduce((n, ch) => n + (Number(ch.amount_refunded) || 0), 0),
187
+ currency: String(mine[0]?.currency ?? c.currency ?? 'usd'),
188
+ };
189
+ });
190
+ }
191
+
192
+ export type CustomerDetail = {
193
+ customer: StripeObject;
194
+ paymentMethods: StripeObject[];
195
+ subscriptions: StripeObject[];
196
+ invoices: StripeObject[];
197
+ payments: Payment[];
198
+ products: Map<string, StripeObject>;
199
+ events: StripeObject[];
200
+ };
201
+
202
+ export async function loadCustomer(id: string): Promise<CustomerDetail> {
203
+ const customer = await retrieve(`/v1/customers/${id}`);
204
+ const [paymentMethods, subscriptions, invoices, payments, products, events] = await Promise.all([
205
+ optional(list(`/v1/customers/${id}/payment_methods`), []),
206
+ optional(list(`/v1/subscriptions?customer=${id}&status=all`), []),
207
+ optional(list(`/v1/invoices?customer=${id}`), []),
208
+ loadPayments().then((all) => all.filter((p) => p.customer?.id === id)),
209
+ optional(list('/v1/products'), []),
210
+ optional(list('/v1/events'), []),
211
+ ]);
212
+ const mine = new Set([id, ...subscriptions.map((s) => s.id), ...invoices.map((i) => i.id), ...payments.map((p) => p.id)]);
213
+ return {
214
+ customer, paymentMethods, subscriptions, invoices, payments,
215
+ products: new Map(products.map((p) => [p.id, p])),
216
+ events: events.filter((e) => mine.has(e?.data?.object?.id)).sort((a, b) => b.created - a.created),
217
+ };
218
+ }
219
+
220
+ // ── billing ──────────────────────────────────────────────────────────────────
221
+ export type Billing = { subscriptions: StripeObject[]; customers: Map<string, StripeObject>; products: Map<string, StripeObject> };
222
+
223
+ export async function loadSubscriptions(): Promise<Billing> {
224
+ const [subscriptions, customers, products] = await Promise.all([list('/v1/subscriptions?status=all'), list('/v1/customers'), list('/v1/products')]);
225
+ return { subscriptions, customers: new Map(customers.map((c) => [c.id, c])), products: new Map(products.map((p) => [p.id, p])) };
226
+ }
227
+
228
+ export type SubscriptionDetail = { subscription: StripeObject; customer?: StripeObject; invoices: StripeObject[]; products: Map<string, StripeObject>; events: StripeObject[]; paymentMethod?: StripeObject };
229
+
230
+ export async function loadSubscription(id: string): Promise<SubscriptionDetail> {
231
+ const subscription = await retrieve(`/v1/subscriptions/${id}`);
232
+ const customerId = typeof subscription.customer === 'string' ? subscription.customer : subscription.customer?.id;
233
+ const [customer, invoices, products, events] = await Promise.all([
234
+ customerId ? optional(retrieve(`/v1/customers/${customerId}`), undefined) : Promise.resolve(undefined),
235
+ optional(list(`/v1/invoices?subscription=${id}`), []),
236
+ optional(list('/v1/products'), []),
237
+ optional(list('/v1/events'), []),
238
+ ]);
239
+ const pmId = subscription.default_payment_method ?? customer?.invoice_settings?.default_payment_method;
240
+ const pms = await paymentMethodsById(typeof pmId === 'string' ? [pmId] : []);
241
+ return {
242
+ subscription, customer, invoices, products: new Map(products.map((p) => [p.id, p])),
243
+ events: events.filter((e) => e?.data?.object?.id === id).sort((a, b) => b.created - a.created),
244
+ paymentMethod: typeof pmId === 'string' ? pms.get(pmId) : undefined,
245
+ };
246
+ }
247
+
248
+ export async function loadInvoices(): Promise<{ invoices: StripeObject[]; customers: Map<string, StripeObject> }> {
249
+ const [invoices, customers] = await Promise.all([list('/v1/invoices'), list('/v1/customers')]);
250
+ return { invoices, customers: new Map(customers.map((c) => [c.id, c])) };
251
+ }
252
+
253
+ export async function loadProducts(): Promise<{ products: StripeObject[]; prices: StripeObject[] }> {
254
+ const [products, prices] = await Promise.all([list('/v1/products'), list('/v1/prices')]);
255
+ return { products, prices };
256
+ }
257
+
258
+ export type HomeData = { balance: StripeObject; payments: Payment[]; customers: StripeObject[]; subscriptions: StripeObject[]; invoices: StripeObject[] };
259
+ export async function loadHome(): Promise<HomeData> {
260
+ const [balance, payments, customers, subscriptions, invoices] = await Promise.all([
261
+ retrieve('/v1/balance'), loadPayments(), list('/v1/customers'), optional(list('/v1/subscriptions?status=all'), []), optional(list('/v1/invoices'), []),
262
+ ]);
263
+ return { balance, payments, customers, subscriptions, invoices };
264
+ }
265
+
266
+ export async function loadBalances(): Promise<{ balance: StripeObject; transactions: StripeObject[]; payouts: StripeObject[] }> {
267
+ const [balance, transactions, payouts] = await Promise.all([retrieve('/v1/balance'), optional(list('/v1/balance_transactions'), []), optional(list('/v1/payouts'), [])]);
268
+ return { balance, transactions, payouts };
269
+ }
270
+
271
+ export async function loadDevelopers(): Promise<{ events: StripeObject[]; webhooks: StripeObject[]; account?: StripeObject }> {
272
+ const [events, webhooks, account] = await Promise.all([optional(list('/v1/events'), []), optional(list('/v1/webhook_endpoints'), []), optional(retrieve('/v1/account'), undefined)]);
273
+ return { events, webhooks, account };
274
+ }
275
+
276
+ // ── writes ───────────────────────────────────────────────────────────────────
277
+ /** Refund a payment in full (no amount) or in part. */
278
+ export function createRefund(payment: { intent?: StripeObject; charge?: StripeObject }, amount: number | undefined, reason: string | undefined): Promise<StripeObject> {
279
+ const target = payment.intent ? { payment_intent: String(payment.intent.id) } : { charge: String(payment.charge?.id) };
280
+ return call('POST', '/v1/refunds', { ...target, amount, reason });
281
+ }
282
+ /** Cancel now (DELETE) or at the end of the current period (cancel_at_period_end). */
283
+ export function cancelSubscription(id: string, when: 'now' | 'period_end'): Promise<StripeObject> {
284
+ return when === 'now' ? call('DELETE', `/v1/subscriptions/${id}`) : call('POST', `/v1/subscriptions/${id}`, { cancel_at_period_end: true });
285
+ }
286
+ export const createObject = (collection: string, fields: Record<string, string>): Promise<StripeObject> => call('POST', `/v1/${collection}`, fields);