totalum-sdk 1.11.0 → 1.12.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 (51) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/_types/{coerce.d-BKhuaDya.d.ts → coerce.d-Cp5NZQN-.d.ts} +1 -1
  3. package/dist/_types/{errors.d-RTRkidv5.d.ts → errors.d-ve4rY-Hx.d.ts} +37 -5
  4. package/dist/_types/{integrations.d-CzTa9T-L.d.ts → integrations.d-BRhNKT7K.d.ts} +2 -2
  5. package/dist/_types/{ops.d-CNsrT4Ma.d.ts → ops.d-DIC4u0go.d.ts} +2 -2
  6. package/dist/ai/index.d.ts +275 -23
  7. package/dist/ai/index.js +305 -24
  8. package/dist/analytics/index.d.ts +58 -12
  9. package/dist/analytics/index.js +46 -3
  10. package/dist/browser/index.d.ts +228 -20
  11. package/dist/browser/index.js +122 -13
  12. package/dist/connections/index.d.ts +305 -23
  13. package/dist/connections/index.js +302 -21
  14. package/dist/cron/index.d.ts +147 -20
  15. package/dist/cron/index.js +157 -15
  16. package/dist/d1/errors.js +15 -0
  17. package/dist/d1/index.d.ts +254 -10
  18. package/dist/d1/index.js +24 -4
  19. package/dist/d1/session.js +26 -2
  20. package/dist/email/index.d.ts +65 -10
  21. package/dist/email/index.js +60 -5
  22. package/dist/errors.js +17 -0
  23. package/dist/files/index.d.ts +243 -20
  24. package/dist/files/index.js +247 -14
  25. package/dist/index.d.ts +25 -10
  26. package/dist/index.js +19 -5
  27. package/dist/logs/index.d.ts +48 -10
  28. package/dist/logs/index.js +39 -4
  29. package/dist/payments/index.d.ts +221 -23
  30. package/dist/payments/index.js +216 -18
  31. package/dist/pdf/index.d.ts +59 -9
  32. package/dist/pdf/index.js +54 -4
  33. package/dist/react/index.d.ts +78 -6
  34. package/dist/react/index.js +51 -5
  35. package/dist/realtime/index.d.ts +55 -8
  36. package/dist/realtime/index.js +53 -6
  37. package/dist/scan/index.d.ts +78 -10
  38. package/dist/scan/index.js +63 -3
  39. package/dist/seo/index.d.ts +1118 -8
  40. package/dist/seo/index.js +325 -4
  41. package/dist/speech/index.d.ts +34 -10
  42. package/dist/speech/index.js +25 -5
  43. package/dist/vectors/index.d.ts +96 -9
  44. package/dist/vectors/index.js +93 -7
  45. package/dist/web/index.d.ts +168 -15
  46. package/dist/web/index.js +163 -10
  47. package/dist/webhooks/index.d.ts +122 -15
  48. package/dist/webhooks/index.js +119 -13
  49. package/package.json +2 -1
  50. package/totalum-sdk.md +10336 -754
  51. package/totalum-sdk.signatures.json +69 -1
@@ -1,19 +1,50 @@
1
- import { O as OnboardingLinkInput, C as CheckoutInput, h as CustomerPortalLinkInput, G as SubscriptionUpdateInput, R as RefundInput } from '../_types/integrations.d-CzTa9T-L.js';
2
- export { a as CheckoutOutput, b as CheckoutSession, i as CustomerPortalLinkOutput, p as OnboardingLinkOutput, P as PaymentsStatus, t as RefundOutput, D as Subscription } from '../_types/integrations.d-CzTa9T-L.js';
3
- import { T as TotalumClientOptions } from '../_types/errors.d-RTRkidv5.js';
4
- export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-RTRkidv5.js';
5
- import '../_types/coerce.d-BKhuaDya.js';
1
+ import { O as OnboardingLinkInput, C as CheckoutInput, h as CustomerPortalLinkInput, G as SubscriptionUpdateInput, R as RefundInput } from '../_types/integrations.d-BRhNKT7K.js';
2
+ export { a as CheckoutOutput, b as CheckoutSession, i as CustomerPortalLinkOutput, p as OnboardingLinkOutput, P as PaymentsStatus, t as RefundOutput, D as Subscription } from '../_types/integrations.d-BRhNKT7K.js';
3
+ import { T as TotalumClientOptions } from '../_types/errors.d-ve4rY-Hx.js';
4
+ export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-ve4rY-Hx.js';
5
+ import '../_types/coerce.d-Cp5NZQN-.js';
6
6
 
7
7
  /**
8
- * `totalum.payments` over the Totalum API `/v1/payments/*` with a project key (scope `payments`): Stripe
9
- * Connect direct charges on the project's own connected account. Card data never passes through here — send buyers to
10
- * the hosted Checkout `url` (SAQ-A); the Totalum API refuses card fields with `CARD_DATA_NOT_ACCEPTED`. Stripe test mode on the
11
- * preview and in the sandbox; live mode only on the published site, once the owner onboarded and switched live on.
12
- * Payment events (`payments.<Stripe type>`) reach the app's own `POST /api/webhooks/stripe` with no setup — test mode's
13
- * on the preview, live mode's on the published site — signed by Totalum: verify them with `totalum.webhooks.verify`.
8
+ * Creates the `payments` namespace (the same object as `totalum.payments`, scope `payments`): Stripe Connect charges
9
+ * on the project's own connected account. Card data never passes through here — send buyers to the hosted Checkout
10
+ * `url`; card fields are refused with `CARD_DATA_NOT_ACCEPTED`. Stripe test mode on the preview and in the sandbox;
11
+ * live mode only on the published site, once the owner onboarded and switched live on. Payment events
12
+ * (`payments.<Stripe type>`) reach the app's own `POST /api/webhooks/stripe` with no setup — test mode's on the
13
+ * preview, live mode's on the published site — signed by Totalum: verify them with `totalum.webhooks.verify`.
14
+ *
15
+ * @param options - The project key, API URL and environment; each falls back to its environment variable.
16
+ * @returns The `payments` namespace: the methods below.
17
+ * @example
18
+ * import { totalumPayments } from 'totalum-sdk/payments';
19
+ *
20
+ * const payments = totalumPayments();
21
+ * const status = await payments.getStatus();
14
22
  */
15
23
  declare function totalumPayments(options?: TotalumClientOptions): {
16
- /** Whether the project's Stripe account can take payments (charges, payouts, pending requirements). */
24
+ /**
25
+ * Whether the project's Stripe account can take payments right now: use it to show "Connect Stripe" to the owner or
26
+ * to hide a Buy button while payments are off. Free.
27
+ *
28
+ * @returns The state of the connected Stripe account in the current mode (test on the preview, live on the
29
+ * published site).
30
+ * @throws PAYMENTS_NOT_ENABLED (403) Payments are not configured for this environment.
31
+ * @example
32
+ * const status = await totalum.payments.getStatus();
33
+ * if (!status.chargesEnabled) return <p>Payments are not set up yet.</p>;
34
+ * //=>
35
+ * {
36
+ * "enabled": true,
37
+ * "mode": "connect",
38
+ * "accountId": "acct_1QZ3kPQm4n8Xa2Lb",
39
+ * "livemode": true,
40
+ * "chargesEnabled": true,
41
+ * "payoutsEnabled": true,
42
+ * "detailsSubmitted": true,
43
+ * "requirements": { "currentlyDue": [], "pastDue": [], "eventuallyDue": [] },
44
+ * "liveEnabled": true,
45
+ * "disconnectedAt": null
46
+ * }
47
+ */
17
48
  getStatus: () => Promise<{
18
49
  enabled: boolean;
19
50
  mode: "connect" | "own_key";
@@ -30,24 +61,99 @@ declare function totalumPayments(options?: TotalumClientOptions): {
30
61
  liveEnabled: boolean;
31
62
  disconnectedAt?: string | null | undefined;
32
63
  }>;
33
- /** Stripe's hosted onboarding for the project owner (single use; never for the agent's sandbox key). */
64
+ /**
65
+ * A single-use link to Stripe's hosted onboarding, where the project owner connects (or updates) the Stripe account
66
+ * the app's payments go to. Only for the owner: the coding agent's sandbox key is refused. The live account can
67
+ * only be connected once the project is published.
68
+ *
69
+ * @param input - Where Stripe sends the owner back, and the account's country.
70
+ * @returns The onboarding URL to redirect the owner to, and when it expires.
71
+ * @throws PAYMENTS_PUBLISH_FIRST (409) The project is not published yet: publish it, then connect Stripe.
72
+ * @throws MISSING_INPUT (400) `country` is needed to create the live Stripe account.
73
+ * @throws FORBIDDEN_SCOPE (403) Called with the agent's sandbox key: onboarding links are for the project owner.
74
+ * @throws STRIPE_ERROR (400) Stripe refused the request; `details.stripeCode` says why.
75
+ * @example
76
+ * const { url } = await totalum.payments.onboardingLink({
77
+ * returnUrl: 'https://acme.totalum.app/admin/payments?connected=1',
78
+ * refreshUrl: 'https://acme.totalum.app/admin/payments',
79
+ * country: 'ES',
80
+ * });
81
+ * //=>
82
+ * { "url": "https://connect.stripe.com/setup/e/acct_1QZ3kPQm4n8Xa2Lb/Xq3VbD9yPpLk", "expiresAt": "2026-10-11T18:05:00.000Z" }
83
+ */
34
84
  onboardingLink: (input: OnboardingLinkInput) => Promise<{
35
85
  url: string;
36
86
  expiresAt: string;
37
87
  }>;
38
88
  /**
39
- * A hosted Checkout Session: `payment`, `subscription`, or `setup` (save a card for later). Redirect to `url`. A
40
- * subscription needs a recurring price: an inline item with `recurring: { interval: 'month' }`, or a `priceId`;
41
- * its `metadata` is also set on the subscription.
89
+ * Creates a Stripe Checkout page for a one-off payment (`payment`), a subscription (`subscription`) or saving a card
90
+ * for later (`setup`), and returns its URL: redirect the buyer there. Put your own ids (order, user) in `metadata`
91
+ * to find them again on the return page or in the webhook. A subscription needs a recurring price: an inline item
92
+ * with `recurring: { interval: 'month' }`, or a `priceId`; its `metadata` is also set on the subscription.
93
+ *
94
+ * @param input - What is sold, the mode, and where Stripe sends the buyer afterwards.
95
+ * @returns The Checkout Session id and the URL to redirect the buyer to.
96
+ * @throws CARD_DATA_NOT_ACCEPTED (400) The request contained card fields: card data is only ever typed on Stripe's page.
97
+ * @throws PAYMENTS_LIVE_ONBOARDING_REQUIRED (403) Live mode, but the owner has not finished Stripe onboarding.
98
+ * @throws PAYMENTS_ACCOUNT_DISCONNECTED (403) The owner disconnected the Stripe account: they must connect it again.
99
+ * @throws PAYMENTS_NOT_ENABLED (403) Payments are not configured for this environment.
100
+ * @throws STRIPE_ERROR (400) Stripe refused the session (an unknown `priceId`, a bad currency); `details.stripeCode`.
101
+ * @throws STRIPE_RATE_LIMITED (429) Stripe's own rate limit; retry shortly.
102
+ * @example
103
+ * // src/app/api/checkout/route.ts
104
+ * const { url } = await totalum.payments.createCheckout({
105
+ * mode: 'payment',
106
+ * lineItems: [{ name: 'Concert ticket', unitAmount: 2500, currency: 'eur', quantity: 2 }],
107
+ * successUrl: 'https://acme.totalum.app/orders/ord_42?session_id={CHECKOUT_SESSION_ID}',
108
+ * cancelUrl: 'https://acme.totalum.app/cart',
109
+ * customerEmail: 'ana@example.com',
110
+ * metadata: { orderId: 'ord_42' },
111
+ * });
112
+ * return Response.redirect(url, 303);
113
+ * //=>
114
+ * { "id": "cs_test_a1B2c3D4e5F6g7H8i9J0", "url": "https://checkout.stripe.com/c/pay/cs_test_a1B2c3D4e5F6g7H8i9J0#fid…" }
115
+ * @example
116
+ * // a monthly subscription with a 14-day trial
117
+ * const { url } = await totalum.payments.createCheckout({
118
+ * mode: 'subscription',
119
+ * lineItems: [{ name: 'Pro', unitAmount: 1900, currency: 'eur', quantity: 1, recurring: { interval: 'month' } }],
120
+ * trialDays: 14,
121
+ * successUrl: 'https://acme.totalum.app/billing?session_id={CHECKOUT_SESSION_ID}',
122
+ * cancelUrl: 'https://acme.totalum.app/pricing',
123
+ * metadata: { userId: user.id },
124
+ * });
42
125
  */
43
126
  createCheckout: (input: CheckoutInput) => Promise<{
44
127
  id: string;
45
128
  url: string;
46
129
  }>;
47
130
  /**
48
- * A Checkout Session read back (`paymentStatus: 'paid'`, your `metadata`): the page Stripe returns the buyer to
49
- * (`successUrl` with `?session_id={CHECKOUT_SESSION_ID}`) confirms the payment with it — no webhook needed. A
131
+ * Reads a Checkout Session back, to confirm a payment on the page Stripe returns the buyer to (`successUrl` with
132
+ * `?session_id={CHECKOUT_SESSION_ID}`): when `paymentStatus` is `'paid'`, mark the order paid — no webhook needed. A
50
133
  * subscription checkout also gives `subscriptionId` and `customerId`: store both on the member.
134
+ *
135
+ * @param sessionId - The `session_id` Stripe added to `successUrl` (`cs_test_…` or `cs_live_…`).
136
+ * @returns The session's status, amount, your `metadata` and the ids it created.
137
+ * @throws VALIDATION_ERROR (400) Not a Checkout Session id.
138
+ * @throws STRIPE_ERROR (404) No such session on this project's Stripe account.
139
+ * @example
140
+ * // src/app/orders/[id]/page.tsx
141
+ * const session = await totalum.payments.getCheckout(searchParams.session_id);
142
+ * if (session.paymentStatus === 'paid') await markOrderPaid(session.metadata.orderId);
143
+ * //=>
144
+ * {
145
+ * "id": "cs_test_a1B2c3D4e5F6g7H8i9J0",
146
+ * "status": "complete",
147
+ * "paymentStatus": "paid",
148
+ * "amountTotal": 5000,
149
+ * "currency": "eur",
150
+ * "metadata": { "orderId": "ord_42" },
151
+ * "paymentIntentId": "pi_3QZ4mNPQm4n8Xa2L0x1y2z3a",
152
+ * "customerEmail": "ana@example.com",
153
+ * "clientReferenceId": null,
154
+ * "customerId": null,
155
+ * "subscriptionId": null
156
+ * }
51
157
  */
52
158
  getCheckout: (sessionId: string) => Promise<{
53
159
  id: string;
@@ -62,12 +168,48 @@ declare function totalumPayments(options?: TotalumClientOptions): {
62
168
  customerId: string | null;
63
169
  subscriptionId: string | null;
64
170
  }>;
65
- /** Stripe's hosted portal where a customer manages their subscriptions and saved cards. */
171
+ /**
172
+ * A link to Stripe's hosted customer portal, where a subscriber updates their card, downloads invoices or cancels
173
+ * their subscription. Show it as "Manage billing".
174
+ *
175
+ * @param input - The Stripe customer and where to send them back.
176
+ * @returns The portal URL to redirect the customer to.
177
+ * @throws STRIPE_ERROR (404) No such customer on this project's Stripe account.
178
+ * @throws PAYMENTS_LIVE_ONBOARDING_REQUIRED (403) Live mode, but the owner has not finished Stripe onboarding.
179
+ * @example
180
+ * const { url } = await totalum.payments.customerPortalLink({
181
+ * customerId: member.stripeCustomerId,
182
+ * returnUrl: 'https://acme.totalum.app/account',
183
+ * });
184
+ * //=>
185
+ * { "url": "https://billing.stripe.com/p/session/test_YWNjdF8xUVozaw…" }
186
+ */
66
187
  customerPortalLink: (input: CustomerPortalLinkInput) => Promise<{
67
188
  url: string;
68
189
  }>;
69
190
  subscriptions: {
70
- /** A subscription: access while `status` is `'active'` or `'trialing'`; `currentPeriodEnd` = next renewal. */
191
+ /**
192
+ * Reads a subscription: give access while `status` is `'active'` or `'trialing'`; `currentPeriodEnd` is the next
193
+ * renewal (or the end of access when `cancelAtPeriodEnd` is true).
194
+ *
195
+ * @param subscriptionId - The subscription id (`sub_…`) from `getCheckout()` or a payment webhook.
196
+ * @returns The subscription's status, renewal date and metadata.
197
+ * @throws VALIDATION_ERROR (400) Not a subscription id.
198
+ * @throws STRIPE_ERROR (404) No such subscription on this project's Stripe account.
199
+ * @example
200
+ * const sub = await totalum.payments.subscriptions.get(member.subscriptionId);
201
+ * const active = sub.status === 'active' || sub.status === 'trialing';
202
+ * //=>
203
+ * {
204
+ * "id": "sub_1QZ5aBPQm4n8Xa2LcD3eF4gH",
205
+ * "status": "active",
206
+ * "customerId": "cus_R2x9YpL0aBcDeF",
207
+ * "cancelAtPeriodEnd": false,
208
+ * "currentPeriodEnd": "2026-11-11T17:40:00.000Z",
209
+ * "canceledAt": null,
210
+ * "metadata": { "userId": "user_42" }
211
+ * }
212
+ */
71
213
  get: (subscriptionId: string) => Promise<{
72
214
  id: string;
73
215
  status: "active" | "paused" | "unpaid" | "incomplete" | "incomplete_expired" | "trialing" | "past_due" | "canceled";
@@ -77,7 +219,28 @@ declare function totalumPayments(options?: TotalumClientOptions): {
77
219
  canceledAt: string | null;
78
220
  metadata: Record<string, string>;
79
221
  }>;
80
- /** `cancelAtPeriodEnd: true` ends it when the paid period ends (access until then); `false` resumes it. */
222
+ /**
223
+ * Cancels a subscription at the end of the paid period (`cancelAtPeriodEnd: true`, the usual "Cancel" button: the
224
+ * member keeps access until then), or undoes that (`false`).
225
+ *
226
+ * @param subscriptionId - The subscription id (`sub_…`).
227
+ * @param input - Whether it ends at the end of the period.
228
+ * @returns The updated subscription.
229
+ * @throws VALIDATION_ERROR (400) Not a subscription id.
230
+ * @throws STRIPE_ERROR (404) No such subscription on this project's Stripe account.
231
+ * @example
232
+ * const sub = await totalum.payments.subscriptions.update(member.subscriptionId, { cancelAtPeriodEnd: true });
233
+ * //=>
234
+ * {
235
+ * "id": "sub_1QZ5aBPQm4n8Xa2LcD3eF4gH",
236
+ * "status": "active",
237
+ * "customerId": "cus_R2x9YpL0aBcDeF",
238
+ * "cancelAtPeriodEnd": true,
239
+ * "currentPeriodEnd": "2026-11-11T17:40:00.000Z",
240
+ * "canceledAt": null,
241
+ * "metadata": { "userId": "user_42" }
242
+ * }
243
+ */
81
244
  update: (subscriptionId: string, input: SubscriptionUpdateInput) => Promise<{
82
245
  id: string;
83
246
  status: "active" | "paused" | "unpaid" | "incomplete" | "incomplete_expired" | "trialing" | "past_due" | "canceled";
@@ -87,7 +250,27 @@ declare function totalumPayments(options?: TotalumClientOptions): {
87
250
  canceledAt: string | null;
88
251
  metadata: Record<string, string>;
89
252
  }>;
90
- /** Cancel now: no more charges, `status: 'canceled'` at once. */
253
+ /**
254
+ * Cancels a subscription immediately: no more charges, `status: 'canceled'` at once, access ends now. To let the
255
+ * member keep the period they paid for, use `update(id, { cancelAtPeriodEnd: true })` instead.
256
+ *
257
+ * @param subscriptionId - The subscription id (`sub_…`).
258
+ * @returns The canceled subscription.
259
+ * @throws VALIDATION_ERROR (400) Not a subscription id.
260
+ * @throws STRIPE_ERROR (404) No such subscription on this project's Stripe account.
261
+ * @example
262
+ * const sub = await totalum.payments.subscriptions.cancel('sub_1QZ5aBPQm4n8Xa2LcD3eF4gH');
263
+ * //=>
264
+ * {
265
+ * "id": "sub_1QZ5aBPQm4n8Xa2LcD3eF4gH",
266
+ * "status": "canceled",
267
+ * "customerId": "cus_R2x9YpL0aBcDeF",
268
+ * "cancelAtPeriodEnd": false,
269
+ * "currentPeriodEnd": "2026-11-11T17:40:00.000Z",
270
+ * "canceledAt": "2026-10-11T18:02:11.000Z",
271
+ * "metadata": { "userId": "user_42" }
272
+ * }
273
+ */
91
274
  cancel: (subscriptionId: string) => Promise<{
92
275
  id: string;
93
276
  status: "active" | "paused" | "unpaid" | "incomplete" | "incomplete_expired" | "trialing" | "past_due" | "canceled";
@@ -99,7 +282,22 @@ declare function totalumPayments(options?: TotalumClientOptions): {
99
282
  }>;
100
283
  };
101
284
  refunds: {
102
- /** Refund a payment (all of it, or `amount` in the smallest unit). */
285
+ /**
286
+ * Refunds a payment, in full or in part. The buyer gets the money back on their card in a few days.
287
+ *
288
+ * @param input - The payment to refund and, for a partial refund, how much.
289
+ * @returns The refund's id, status and amount.
290
+ * @throws STRIPE_ERROR (400) Stripe refused it (already refunded, an amount above what is left); `details.stripeCode`.
291
+ * @throws PAYMENTS_LIVE_ONBOARDING_REQUIRED (403) Live mode, but the owner has not finished Stripe onboarding.
292
+ * @example
293
+ * const refund = await totalum.payments.refunds.create({
294
+ * paymentIntentId: 'pi_3QZ4mNPQm4n8Xa2L0x1y2z3a',
295
+ * amount: 2500,
296
+ * reason: 'requested_by_customer',
297
+ * });
298
+ * //=>
299
+ * { "id": "re_3QZ6bCPQm4n8Xa2L1k2l3m4n", "status": "succeeded", "amount": 2500, "currency": "eur" }
300
+ */
103
301
  create: (input: RefundInput) => Promise<{
104
302
  id: string;
105
303
  status: string;
@@ -1,44 +1,242 @@
1
1
  import { jsonClient } from '../http.js';
2
2
  export { TotalumError, isTotalumError } from '../errors.js';
3
3
  /**
4
- * `totalum.payments` over the Totalum API `/v1/payments/*` with a project key (scope `payments`): Stripe
5
- * Connect direct charges on the project's own connected account. Card data never passes through here — send buyers to
6
- * the hosted Checkout `url` (SAQ-A); the Totalum API refuses card fields with `CARD_DATA_NOT_ACCEPTED`. Stripe test mode on the
7
- * preview and in the sandbox; live mode only on the published site, once the owner onboarded and switched live on.
8
- * Payment events (`payments.<Stripe type>`) reach the app's own `POST /api/webhooks/stripe` with no setup — test mode's
9
- * on the preview, live mode's on the published site — signed by Totalum: verify them with `totalum.webhooks.verify`.
4
+ * Creates the `payments` namespace (the same object as `totalum.payments`, scope `payments`): Stripe Connect charges
5
+ * on the project's own connected account. Card data never passes through here — send buyers to the hosted Checkout
6
+ * `url`; card fields are refused with `CARD_DATA_NOT_ACCEPTED`. Stripe test mode on the preview and in the sandbox;
7
+ * live mode only on the published site, once the owner onboarded and switched live on. Payment events
8
+ * (`payments.<Stripe type>`) reach the app's own `POST /api/webhooks/stripe` with no setup — test mode's on the
9
+ * preview, live mode's on the published site — signed by Totalum: verify them with `totalum.webhooks.verify`.
10
+ *
11
+ * @param options - The project key, API URL and environment; each falls back to its environment variable.
12
+ * @returns The `payments` namespace: the methods below.
13
+ * @example
14
+ * import { totalumPayments } from 'totalum-sdk/payments';
15
+ *
16
+ * const payments = totalumPayments();
17
+ * const status = await payments.getStatus();
10
18
  */
11
19
  export function totalumPayments(options = {}) {
12
20
  const call = jsonClient(options);
13
21
  return {
14
- /** Whether the project's Stripe account can take payments (charges, payouts, pending requirements). */
22
+ /**
23
+ * Whether the project's Stripe account can take payments right now: use it to show "Connect Stripe" to the owner or
24
+ * to hide a Buy button while payments are off. Free.
25
+ *
26
+ * @returns The state of the connected Stripe account in the current mode (test on the preview, live on the
27
+ * published site).
28
+ * @throws PAYMENTS_NOT_ENABLED (403) Payments are not configured for this environment.
29
+ * @example
30
+ * const status = await totalum.payments.getStatus();
31
+ * if (!status.chargesEnabled) return <p>Payments are not set up yet.</p>;
32
+ * //=>
33
+ * {
34
+ * "enabled": true,
35
+ * "mode": "connect",
36
+ * "accountId": "acct_1QZ3kPQm4n8Xa2Lb",
37
+ * "livemode": true,
38
+ * "chargesEnabled": true,
39
+ * "payoutsEnabled": true,
40
+ * "detailsSubmitted": true,
41
+ * "requirements": { "currentlyDue": [], "pastDue": [], "eventuallyDue": [] },
42
+ * "liveEnabled": true,
43
+ * "disconnectedAt": null
44
+ * }
45
+ */
15
46
  getStatus: () => call('GET', '/v1/payments/status'),
16
- /** Stripe's hosted onboarding for the project owner (single use; never for the agent's sandbox key). */
47
+ /**
48
+ * A single-use link to Stripe's hosted onboarding, where the project owner connects (or updates) the Stripe account
49
+ * the app's payments go to. Only for the owner: the coding agent's sandbox key is refused. The live account can
50
+ * only be connected once the project is published.
51
+ *
52
+ * @param input - Where Stripe sends the owner back, and the account's country.
53
+ * @returns The onboarding URL to redirect the owner to, and when it expires.
54
+ * @throws PAYMENTS_PUBLISH_FIRST (409) The project is not published yet: publish it, then connect Stripe.
55
+ * @throws MISSING_INPUT (400) `country` is needed to create the live Stripe account.
56
+ * @throws FORBIDDEN_SCOPE (403) Called with the agent's sandbox key: onboarding links are for the project owner.
57
+ * @throws STRIPE_ERROR (400) Stripe refused the request; `details.stripeCode` says why.
58
+ * @example
59
+ * const { url } = await totalum.payments.onboardingLink({
60
+ * returnUrl: 'https://acme.totalum.app/admin/payments?connected=1',
61
+ * refreshUrl: 'https://acme.totalum.app/admin/payments',
62
+ * country: 'ES',
63
+ * });
64
+ * //=>
65
+ * { "url": "https://connect.stripe.com/setup/e/acct_1QZ3kPQm4n8Xa2Lb/Xq3VbD9yPpLk", "expiresAt": "2026-10-11T18:05:00.000Z" }
66
+ */
17
67
  onboardingLink: (input) => call('POST', '/v1/payments/onboarding-link', input),
18
68
  /**
19
- * A hosted Checkout Session: `payment`, `subscription`, or `setup` (save a card for later). Redirect to `url`. A
20
- * subscription needs a recurring price: an inline item with `recurring: { interval: 'month' }`, or a `priceId`;
21
- * its `metadata` is also set on the subscription.
69
+ * Creates a Stripe Checkout page for a one-off payment (`payment`), a subscription (`subscription`) or saving a card
70
+ * for later (`setup`), and returns its URL: redirect the buyer there. Put your own ids (order, user) in `metadata`
71
+ * to find them again on the return page or in the webhook. A subscription needs a recurring price: an inline item
72
+ * with `recurring: { interval: 'month' }`, or a `priceId`; its `metadata` is also set on the subscription.
73
+ *
74
+ * @param input - What is sold, the mode, and where Stripe sends the buyer afterwards.
75
+ * @returns The Checkout Session id and the URL to redirect the buyer to.
76
+ * @throws CARD_DATA_NOT_ACCEPTED (400) The request contained card fields: card data is only ever typed on Stripe's page.
77
+ * @throws PAYMENTS_LIVE_ONBOARDING_REQUIRED (403) Live mode, but the owner has not finished Stripe onboarding.
78
+ * @throws PAYMENTS_ACCOUNT_DISCONNECTED (403) The owner disconnected the Stripe account: they must connect it again.
79
+ * @throws PAYMENTS_NOT_ENABLED (403) Payments are not configured for this environment.
80
+ * @throws STRIPE_ERROR (400) Stripe refused the session (an unknown `priceId`, a bad currency); `details.stripeCode`.
81
+ * @throws STRIPE_RATE_LIMITED (429) Stripe's own rate limit; retry shortly.
82
+ * @example
83
+ * // src/app/api/checkout/route.ts
84
+ * const { url } = await totalum.payments.createCheckout({
85
+ * mode: 'payment',
86
+ * lineItems: [{ name: 'Concert ticket', unitAmount: 2500, currency: 'eur', quantity: 2 }],
87
+ * successUrl: 'https://acme.totalum.app/orders/ord_42?session_id={CHECKOUT_SESSION_ID}',
88
+ * cancelUrl: 'https://acme.totalum.app/cart',
89
+ * customerEmail: 'ana@example.com',
90
+ * metadata: { orderId: 'ord_42' },
91
+ * });
92
+ * return Response.redirect(url, 303);
93
+ * //=>
94
+ * { "id": "cs_test_a1B2c3D4e5F6g7H8i9J0", "url": "https://checkout.stripe.com/c/pay/cs_test_a1B2c3D4e5F6g7H8i9J0#fid…" }
95
+ * @example
96
+ * // a monthly subscription with a 14-day trial
97
+ * const { url } = await totalum.payments.createCheckout({
98
+ * mode: 'subscription',
99
+ * lineItems: [{ name: 'Pro', unitAmount: 1900, currency: 'eur', quantity: 1, recurring: { interval: 'month' } }],
100
+ * trialDays: 14,
101
+ * successUrl: 'https://acme.totalum.app/billing?session_id={CHECKOUT_SESSION_ID}',
102
+ * cancelUrl: 'https://acme.totalum.app/pricing',
103
+ * metadata: { userId: user.id },
104
+ * });
22
105
  */
23
106
  createCheckout: (input) => call('POST', '/v1/payments/checkout', input),
24
107
  /**
25
- * A Checkout Session read back (`paymentStatus: 'paid'`, your `metadata`): the page Stripe returns the buyer to
26
- * (`successUrl` with `?session_id={CHECKOUT_SESSION_ID}`) confirms the payment with it — no webhook needed. A
108
+ * Reads a Checkout Session back, to confirm a payment on the page Stripe returns the buyer to (`successUrl` with
109
+ * `?session_id={CHECKOUT_SESSION_ID}`): when `paymentStatus` is `'paid'`, mark the order paid — no webhook needed. A
27
110
  * subscription checkout also gives `subscriptionId` and `customerId`: store both on the member.
111
+ *
112
+ * @param sessionId - The `session_id` Stripe added to `successUrl` (`cs_test_…` or `cs_live_…`).
113
+ * @returns The session's status, amount, your `metadata` and the ids it created.
114
+ * @throws VALIDATION_ERROR (400) Not a Checkout Session id.
115
+ * @throws STRIPE_ERROR (404) No such session on this project's Stripe account.
116
+ * @example
117
+ * // src/app/orders/[id]/page.tsx
118
+ * const session = await totalum.payments.getCheckout(searchParams.session_id);
119
+ * if (session.paymentStatus === 'paid') await markOrderPaid(session.metadata.orderId);
120
+ * //=>
121
+ * {
122
+ * "id": "cs_test_a1B2c3D4e5F6g7H8i9J0",
123
+ * "status": "complete",
124
+ * "paymentStatus": "paid",
125
+ * "amountTotal": 5000,
126
+ * "currency": "eur",
127
+ * "metadata": { "orderId": "ord_42" },
128
+ * "paymentIntentId": "pi_3QZ4mNPQm4n8Xa2L0x1y2z3a",
129
+ * "customerEmail": "ana@example.com",
130
+ * "clientReferenceId": null,
131
+ * "customerId": null,
132
+ * "subscriptionId": null
133
+ * }
28
134
  */
29
135
  getCheckout: (sessionId) => call('GET', `/v1/payments/checkout/${encodeURIComponent(sessionId)}`),
30
- /** Stripe's hosted portal where a customer manages their subscriptions and saved cards. */
136
+ /**
137
+ * A link to Stripe's hosted customer portal, where a subscriber updates their card, downloads invoices or cancels
138
+ * their subscription. Show it as "Manage billing".
139
+ *
140
+ * @param input - The Stripe customer and where to send them back.
141
+ * @returns The portal URL to redirect the customer to.
142
+ * @throws STRIPE_ERROR (404) No such customer on this project's Stripe account.
143
+ * @throws PAYMENTS_LIVE_ONBOARDING_REQUIRED (403) Live mode, but the owner has not finished Stripe onboarding.
144
+ * @example
145
+ * const { url } = await totalum.payments.customerPortalLink({
146
+ * customerId: member.stripeCustomerId,
147
+ * returnUrl: 'https://acme.totalum.app/account',
148
+ * });
149
+ * //=>
150
+ * { "url": "https://billing.stripe.com/p/session/test_YWNjdF8xUVozaw…" }
151
+ */
31
152
  customerPortalLink: (input) => call('POST', '/v1/payments/customer-portal-link', input),
32
153
  subscriptions: {
33
- /** A subscription: access while `status` is `'active'` or `'trialing'`; `currentPeriodEnd` = next renewal. */
154
+ /**
155
+ * Reads a subscription: give access while `status` is `'active'` or `'trialing'`; `currentPeriodEnd` is the next
156
+ * renewal (or the end of access when `cancelAtPeriodEnd` is true).
157
+ *
158
+ * @param subscriptionId - The subscription id (`sub_…`) from `getCheckout()` or a payment webhook.
159
+ * @returns The subscription's status, renewal date and metadata.
160
+ * @throws VALIDATION_ERROR (400) Not a subscription id.
161
+ * @throws STRIPE_ERROR (404) No such subscription on this project's Stripe account.
162
+ * @example
163
+ * const sub = await totalum.payments.subscriptions.get(member.subscriptionId);
164
+ * const active = sub.status === 'active' || sub.status === 'trialing';
165
+ * //=>
166
+ * {
167
+ * "id": "sub_1QZ5aBPQm4n8Xa2LcD3eF4gH",
168
+ * "status": "active",
169
+ * "customerId": "cus_R2x9YpL0aBcDeF",
170
+ * "cancelAtPeriodEnd": false,
171
+ * "currentPeriodEnd": "2026-11-11T17:40:00.000Z",
172
+ * "canceledAt": null,
173
+ * "metadata": { "userId": "user_42" }
174
+ * }
175
+ */
34
176
  get: (subscriptionId) => call('GET', `/v1/payments/subscriptions/${encodeURIComponent(subscriptionId)}`),
35
- /** `cancelAtPeriodEnd: true` ends it when the paid period ends (access until then); `false` resumes it. */
177
+ /**
178
+ * Cancels a subscription at the end of the paid period (`cancelAtPeriodEnd: true`, the usual "Cancel" button: the
179
+ * member keeps access until then), or undoes that (`false`).
180
+ *
181
+ * @param subscriptionId - The subscription id (`sub_…`).
182
+ * @param input - Whether it ends at the end of the period.
183
+ * @returns The updated subscription.
184
+ * @throws VALIDATION_ERROR (400) Not a subscription id.
185
+ * @throws STRIPE_ERROR (404) No such subscription on this project's Stripe account.
186
+ * @example
187
+ * const sub = await totalum.payments.subscriptions.update(member.subscriptionId, { cancelAtPeriodEnd: true });
188
+ * //=>
189
+ * {
190
+ * "id": "sub_1QZ5aBPQm4n8Xa2LcD3eF4gH",
191
+ * "status": "active",
192
+ * "customerId": "cus_R2x9YpL0aBcDeF",
193
+ * "cancelAtPeriodEnd": true,
194
+ * "currentPeriodEnd": "2026-11-11T17:40:00.000Z",
195
+ * "canceledAt": null,
196
+ * "metadata": { "userId": "user_42" }
197
+ * }
198
+ */
36
199
  update: (subscriptionId, input) => call('PATCH', `/v1/payments/subscriptions/${encodeURIComponent(subscriptionId)}`, input),
37
- /** Cancel now: no more charges, `status: 'canceled'` at once. */
200
+ /**
201
+ * Cancels a subscription immediately: no more charges, `status: 'canceled'` at once, access ends now. To let the
202
+ * member keep the period they paid for, use `update(id, { cancelAtPeriodEnd: true })` instead.
203
+ *
204
+ * @param subscriptionId - The subscription id (`sub_…`).
205
+ * @returns The canceled subscription.
206
+ * @throws VALIDATION_ERROR (400) Not a subscription id.
207
+ * @throws STRIPE_ERROR (404) No such subscription on this project's Stripe account.
208
+ * @example
209
+ * const sub = await totalum.payments.subscriptions.cancel('sub_1QZ5aBPQm4n8Xa2LcD3eF4gH');
210
+ * //=>
211
+ * {
212
+ * "id": "sub_1QZ5aBPQm4n8Xa2LcD3eF4gH",
213
+ * "status": "canceled",
214
+ * "customerId": "cus_R2x9YpL0aBcDeF",
215
+ * "cancelAtPeriodEnd": false,
216
+ * "currentPeriodEnd": "2026-11-11T17:40:00.000Z",
217
+ * "canceledAt": "2026-10-11T18:02:11.000Z",
218
+ * "metadata": { "userId": "user_42" }
219
+ * }
220
+ */
38
221
  cancel: (subscriptionId) => call('DELETE', `/v1/payments/subscriptions/${encodeURIComponent(subscriptionId)}`),
39
222
  },
40
223
  refunds: {
41
- /** Refund a payment (all of it, or `amount` in the smallest unit). */
224
+ /**
225
+ * Refunds a payment, in full or in part. The buyer gets the money back on their card in a few days.
226
+ *
227
+ * @param input - The payment to refund and, for a partial refund, how much.
228
+ * @returns The refund's id, status and amount.
229
+ * @throws STRIPE_ERROR (400) Stripe refused it (already refunded, an amount above what is left); `details.stripeCode`.
230
+ * @throws PAYMENTS_LIVE_ONBOARDING_REQUIRED (403) Live mode, but the owner has not finished Stripe onboarding.
231
+ * @example
232
+ * const refund = await totalum.payments.refunds.create({
233
+ * paymentIntentId: 'pi_3QZ4mNPQm4n8Xa2L0x1y2z3a',
234
+ * amount: 2500,
235
+ * reason: 'requested_by_customer',
236
+ * });
237
+ * //=>
238
+ * { "id": "re_3QZ6bCPQm4n8Xa2L1k2l3m4n", "status": "succeeded", "amount": 2500, "currency": "eur" }
239
+ */
42
240
  create: (input) => call('POST', '/v1/payments/refunds', input),
43
241
  },
44
242
  };