totalum-sdk 1.11.0 → 1.11.1
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.
- package/CHANGELOG.md +9 -0
- package/dist/_types/{coerce.d-BKhuaDya.d.ts → coerce.d-D_rylvPs.d.ts} +1 -1
- package/dist/_types/{errors.d-RTRkidv5.d.ts → errors.d-BPpY5QKu.d.ts} +20 -2
- package/dist/_types/{integrations.d-CzTa9T-L.d.ts → integrations.d-BTZHy48D.d.ts} +2 -2
- package/dist/_types/{ops.d-CNsrT4Ma.d.ts → ops.d-Ce5zJyX0.d.ts} +2 -2
- package/dist/ai/index.d.ts +275 -23
- package/dist/ai/index.js +305 -24
- package/dist/analytics/index.d.ts +58 -12
- package/dist/analytics/index.js +46 -3
- package/dist/browser/index.d.ts +208 -18
- package/dist/browser/index.js +122 -13
- package/dist/connections/index.d.ts +305 -23
- package/dist/connections/index.js +302 -21
- package/dist/cron/index.d.ts +147 -20
- package/dist/cron/index.js +157 -15
- package/dist/d1/errors.js +15 -0
- package/dist/d1/index.d.ts +254 -10
- package/dist/d1/index.js +24 -4
- package/dist/d1/session.js +26 -2
- package/dist/email/index.d.ts +65 -10
- package/dist/email/index.js +60 -5
- package/dist/errors.js +17 -0
- package/dist/files/index.d.ts +243 -20
- package/dist/files/index.js +247 -14
- package/dist/index.d.ts +24 -9
- package/dist/index.js +19 -5
- package/dist/logs/index.d.ts +48 -10
- package/dist/logs/index.js +39 -4
- package/dist/payments/index.d.ts +221 -23
- package/dist/payments/index.js +216 -18
- package/dist/pdf/index.d.ts +59 -9
- package/dist/pdf/index.js +54 -4
- package/dist/react/index.d.ts +78 -6
- package/dist/react/index.js +51 -5
- package/dist/realtime/index.d.ts +55 -8
- package/dist/realtime/index.js +53 -6
- package/dist/scan/index.d.ts +78 -10
- package/dist/scan/index.js +63 -3
- package/dist/seo/index.d.ts +43 -7
- package/dist/seo/index.js +39 -3
- package/dist/speech/index.d.ts +34 -10
- package/dist/speech/index.js +25 -5
- package/dist/vectors/index.d.ts +96 -9
- package/dist/vectors/index.js +93 -7
- package/dist/web/index.d.ts +168 -15
- package/dist/web/index.js +163 -10
- package/dist/webhooks/index.d.ts +122 -15
- package/dist/webhooks/index.js +119 -13
- package/package.json +2 -1
- package/totalum-sdk.md +8841 -754
- package/totalum-sdk.signatures.json +1 -1
package/dist/payments/index.d.ts
CHANGED
|
@@ -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-
|
|
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-
|
|
3
|
-
import { T as TotalumClientOptions } from '../_types/errors.d-
|
|
4
|
-
export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-
|
|
5
|
-
import '../_types/coerce.d-
|
|
1
|
+
import { O as OnboardingLinkInput, C as CheckoutInput, h as CustomerPortalLinkInput, G as SubscriptionUpdateInput, R as RefundInput } from '../_types/integrations.d-BTZHy48D.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-BTZHy48D.js';
|
|
3
|
+
import { T as TotalumClientOptions } from '../_types/errors.d-BPpY5QKu.js';
|
|
4
|
+
export { a as TotalumError, b as TotalumErrorCode, i as isTotalumError } from '../_types/errors.d-BPpY5QKu.js';
|
|
5
|
+
import '../_types/coerce.d-D_rylvPs.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* `
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
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
|
-
*
|
|
49
|
-
*
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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;
|
package/dist/payments/index.js
CHANGED
|
@@ -1,44 +1,242 @@
|
|
|
1
1
|
import { jsonClient } from '../http.js';
|
|
2
2
|
export { TotalumError, isTotalumError } from '../errors.js';
|
|
3
3
|
/**
|
|
4
|
-
* `
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
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
|
-
*
|
|
26
|
-
*
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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
|
};
|