better-auth-streampay 2.2.1 → 3.0.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/README.md +69 -482
- package/dist/client.cjs +1 -0
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +7 -3
- package/dist/client.d.ts +7 -3
- package/dist/client.js +1 -0
- package/dist/client.js.map +1 -1
- package/dist/index.cjs +671 -241
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +13 -2
- package/dist/index.d.ts +13 -2
- package/dist/index.js +671 -241
- package/dist/index.js.map +1 -1
- package/dist/{streampay-B3vf7Hvq.d.cts → streampay-uhs67tYl.d.cts} +441 -264
- package/dist/{streampay-B3vf7Hvq.d.ts → streampay-uhs67tYl.d.ts} +441 -264
- package/docs/integration.md +716 -0
- package/docs/staging-validation.md +159 -0
- package/docs/subscriptions.md +271 -0
- package/package.json +12 -4
package/README.md
CHANGED
|
@@ -1,115 +1,72 @@
|
|
|
1
1
|
# better-auth-streampay
|
|
2
2
|
|
|
3
|
-
Add StreamPay
|
|
3
|
+
Add StreamPay checkout, billing, and signed webhooks to a Better Auth app. This is a community
|
|
4
|
+
plugin and is not affiliated with Stream.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
The plugin supports hosted checkout, consumer records, subscription plans and access checks, a
|
|
7
|
+
consumer billing portal, admin billing actions, and webhook verification and replay.
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
For advanced checkout, consumer matching, portal, subscription lifecycle, admin, and webhook
|
|
10
|
+
examples, see the [integration guide](docs/integration.md). For subscription columns and upgrade
|
|
11
|
+
migrations, see the [subscription data model](docs/subscriptions.md).
|
|
8
12
|
|
|
9
|
-
|
|
10
|
-
npx skills add Genie-sa/better-auth-streampay
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
Then ask your coding agent to add StreamPay to your Better Auth app.
|
|
13
|
+
## Requirements
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
- Node.js `>=20.19.0`
|
|
16
|
+
- Better Auth `^1.6.0`
|
|
17
|
+
- Stream SDK `@streamsdk/typescript ^1.1.3`
|
|
18
|
+
- Zod `^3.24.0` or `^4.0.0`
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
- open hosted checkout
|
|
19
|
-
- show a billing portal
|
|
20
|
-
- manage subscriptions
|
|
21
|
-
- expose admin billing actions
|
|
22
|
-
- verify and handle signed webhooks
|
|
23
|
-
|
|
24
|
-
## Install
|
|
20
|
+
Install the plugin and SDK with your package manager:
|
|
25
21
|
|
|
26
22
|
```bash
|
|
27
23
|
pnpm add better-auth-streampay @streamsdk/typescript
|
|
28
24
|
```
|
|
29
25
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
- `better-auth ^1.5.0`
|
|
33
|
-
- `@streamsdk/typescript ^1.1.3`
|
|
34
|
-
- `zod ^3.24.0 || ^4.0.0`
|
|
35
|
-
|
|
36
|
-
## Basic setup
|
|
26
|
+
Keep Better Auth and Zod installed in your app. Set `STREAMPAY_API_KEY` on the server. Add
|
|
27
|
+
`STREAMPAY_WEBHOOK_SECRET` when you enable webhook handling.
|
|
37
28
|
|
|
38
|
-
|
|
29
|
+
## Server setup
|
|
39
30
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
STREAMPAY_WEBHOOK_SECRET=
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
Create one SDK client and pass it to the plugin:
|
|
31
|
+
Create one SDK client and register the plugin with the features your app uses. This example enables
|
|
32
|
+
checkout and signed webhooks.
|
|
46
33
|
|
|
47
34
|
```ts
|
|
48
35
|
import StreamSDK from "@streamsdk/typescript";
|
|
49
36
|
import { betterAuth } from "better-auth";
|
|
50
|
-
import {
|
|
51
|
-
checkout,
|
|
52
|
-
portal,
|
|
53
|
-
streampay,
|
|
54
|
-
subscriptions,
|
|
55
|
-
webhooks,
|
|
56
|
-
} from "better-auth-streampay";
|
|
37
|
+
import { checkout, streampay, webhooks } from "better-auth-streampay";
|
|
57
38
|
|
|
58
|
-
const
|
|
39
|
+
const client = StreamSDK.init(process.env.STREAMPAY_API_KEY!);
|
|
59
40
|
|
|
60
41
|
export const auth = betterAuth({
|
|
61
42
|
plugins: [
|
|
62
43
|
streampay({
|
|
63
|
-
client
|
|
44
|
+
client,
|
|
64
45
|
use: [
|
|
65
|
-
checkout(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
plans: [
|
|
69
|
-
{
|
|
70
|
-
name: "pro",
|
|
71
|
-
productId: "your-recurring-product-id",
|
|
72
|
-
priceInSmallestUnit: 9900,
|
|
73
|
-
billingInterval: "MONTH",
|
|
74
|
-
limits: { reports: true },
|
|
75
|
-
},
|
|
76
|
-
],
|
|
77
|
-
}),
|
|
78
|
-
webhooks({
|
|
79
|
-
secret: process.env.STREAMPAY_WEBHOOK_SECRET!,
|
|
46
|
+
checkout({
|
|
47
|
+
products: [{ slug: "starter", productId: "your-payment-product-id" }],
|
|
48
|
+
authenticatedUsersOnly: true,
|
|
80
49
|
}),
|
|
50
|
+
webhooks({ secret: process.env.STREAMPAY_WEBHOOK_SECRET! }),
|
|
81
51
|
],
|
|
82
52
|
}),
|
|
83
53
|
],
|
|
84
54
|
});
|
|
85
55
|
```
|
|
86
56
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
## Database
|
|
90
|
-
|
|
91
|
-
For a new Better Auth managed database:
|
|
57
|
+
Register your webhook endpoint in the StreamPay dashboard. With the default Better Auth base path,
|
|
58
|
+
use:
|
|
92
59
|
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
For Drizzle or Prisma:
|
|
98
|
-
|
|
99
|
-
```bash
|
|
100
|
-
npx auth@latest generate --config path/to/auth.ts
|
|
60
|
+
```text
|
|
61
|
+
https://your-app.com/api/auth/streampay/webhooks
|
|
101
62
|
```
|
|
102
63
|
|
|
103
|
-
|
|
104
|
-
schema but never changes your database at runtime. For an existing subscription table, backfill
|
|
105
|
-
`seats` to `1` in the same migration.
|
|
106
|
-
|
|
107
|
-
`streampayConsumerId` is unique. Before applying the generated unique index to an existing
|
|
108
|
-
database, resolve any duplicate non-null consumer IDs. Checkout fails closed when a consumer link
|
|
109
|
-
cannot be stored safely.
|
|
64
|
+
Keep API keys and webhook secrets on the server. Do not put them in browser code.
|
|
110
65
|
|
|
111
66
|
## Client setup
|
|
112
67
|
|
|
68
|
+
For React, register the client plugin:
|
|
69
|
+
|
|
113
70
|
```ts
|
|
114
71
|
import { createAuthClient } from "better-auth/react";
|
|
115
72
|
import { streampayClient } from "better-auth-streampay/client";
|
|
@@ -119,433 +76,63 @@ export const authClient = createAuthClient({
|
|
|
119
76
|
});
|
|
120
77
|
```
|
|
121
78
|
|
|
122
|
-
Use the matching Better Auth client for Vue, Svelte, or Solid.
|
|
123
|
-
|
|
124
|
-
## Consumers
|
|
125
|
-
|
|
126
|
-
By default, the plugin creates a StreamPay consumer when the user first needs one. This keeps a
|
|
127
|
-
StreamPay outage from blocking sign-up.
|
|
128
|
-
|
|
129
|
-
To create the consumer during sign-up:
|
|
130
|
-
|
|
131
|
-
```ts
|
|
132
|
-
streampay({
|
|
133
|
-
client: streamPayClient,
|
|
134
|
-
createConsumerOnSignUp: true,
|
|
135
|
-
use: [],
|
|
136
|
-
});
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
To reuse an existing StreamPay consumer after a verified email match:
|
|
140
|
-
|
|
141
|
-
```ts
|
|
142
|
-
streampay({
|
|
143
|
-
client: streamPayClient,
|
|
144
|
-
createConsumerOnSignUp: true,
|
|
145
|
-
claimExistingConsumerBy: ["email"],
|
|
146
|
-
use: [],
|
|
147
|
-
});
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
Only enable reuse when your app verifies the matching email or phone number.
|
|
151
|
-
|
|
152
|
-
## Checkout
|
|
79
|
+
Use the matching Better Auth client package for Vue, Svelte, or Solid. Open a hosted checkout link
|
|
80
|
+
with:
|
|
153
81
|
|
|
154
82
|
```ts
|
|
155
|
-
checkout({
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
],
|
|
159
|
-
successUrl: "/billing/success",
|
|
160
|
-
failureUrl: "/billing/failed",
|
|
161
|
-
authenticatedUsersOnly: true,
|
|
162
|
-
});
|
|
83
|
+
const { data, error } = await authClient.checkout({ slug: "starter", redirect: false });
|
|
84
|
+
if (error) throw new Error(error.message);
|
|
85
|
+
if (data) window.location.assign(data.url);
|
|
163
86
|
```
|
|
164
87
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
```ts
|
|
168
|
-
const { data } = await authClient.checkout({
|
|
169
|
-
slug: "starter",
|
|
170
|
-
redirect: false,
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
window.location.href = data.url;
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
You can also pass StreamPay product IDs directly with `products`.
|
|
177
|
-
|
|
178
|
-
### Server-authoritative checkout
|
|
88
|
+
## Database setup
|
|
179
89
|
|
|
180
|
-
|
|
90
|
+
The plugin declares Better Auth schema fields. It does not change your database at runtime. Generate
|
|
91
|
+
and apply a migration before deploying the plugin:
|
|
181
92
|
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
checkout({
|
|
186
|
-
authenticatedUsersOnly: true,
|
|
187
|
-
|
|
188
|
-
resolveCheckout: async ({ user, body }) => {
|
|
189
|
-
const order = await loadAuthorizedOrder(user?.id, body.referenceId);
|
|
190
|
-
if (!order) {
|
|
191
|
-
throw new APIError("FORBIDDEN", {
|
|
192
|
-
code: "ORDER_NOT_AVAILABLE",
|
|
193
|
-
message: "This order is not available for checkout.",
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return {
|
|
198
|
-
products: [{ productId: order.productId, quantity: order.quantity }],
|
|
199
|
-
successUrl: `https://shop.example.com/orders/${order.id}?checkout=success`,
|
|
200
|
-
failureUrl: `https://shop.example.com/orders/${order.id}?checkout=failed`,
|
|
201
|
-
maxNumberOfPayments: 1,
|
|
202
|
-
validUntil: order.validUntil,
|
|
203
|
-
metadata: { flow: "store" },
|
|
204
|
-
};
|
|
205
|
-
},
|
|
206
|
-
|
|
207
|
-
onCheckoutCreated: async ({ referenceId, paymentLinkId, payload }) => {
|
|
208
|
-
await saveOrderPaymentLink({ referenceId, paymentLinkId, payload });
|
|
209
|
-
},
|
|
210
|
-
});
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
The client then sends only app-owned reference data:
|
|
214
|
-
|
|
215
|
-
```ts
|
|
216
|
-
await authClient.checkout({ referenceId: order.id, redirect: false });
|
|
93
|
+
```bash
|
|
94
|
+
npx auth@latest generate --config path/to/auth.ts
|
|
217
95
|
```
|
|
218
96
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
resolver, the existing client-driven checkout behavior is unchanged.
|
|
222
|
-
|
|
223
|
-
Invalid values returned by `resolveCheckout` produce a generic 500 response. Validation details are
|
|
224
|
-
written to the server log without echoing the invalid values to the client.
|
|
97
|
+
For a Better Auth managed database, use `npx auth@latest migrate --config path/to/auth.ts` instead.
|
|
98
|
+
Review generated changes and apply them with your normal database migration process.
|
|
225
99
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
100
|
+
For an existing installation, review the [database upgrade instructions](docs/integration.md#database)
|
|
101
|
+
and [subscription migrations](docs/subscriptions.md#migration). Apply new columns and backfill
|
|
102
|
+
existing rows before deploying. Resolve duplicate non-null `streampayConsumerId` values before applying
|
|
103
|
+
the unique index. If you enable generic webhook deduplication, migrate the `streampayWebhookEvent`
|
|
104
|
+
table before setting `deduplicate: true`.
|
|
230
105
|
|
|
231
106
|
## Billing portal
|
|
232
107
|
|
|
233
|
-
`portal()` adds
|
|
234
|
-
|
|
235
|
-
- `
|
|
236
|
-
|
|
237
|
-
-
|
|
238
|
-
|
|
239
|
-
```ts
|
|
240
|
-
const state = await authClient.consumer.state();
|
|
241
|
-
const subscriptions = await authClient.consumer.subscriptions.list();
|
|
242
|
-
const invoices = await authClient.consumer.invoices.list();
|
|
243
|
-
```
|
|
244
|
-
|
|
245
|
-
## Subscriptions
|
|
246
|
-
|
|
247
|
-
```ts
|
|
248
|
-
subscriptions({
|
|
249
|
-
plans: [
|
|
250
|
-
{
|
|
251
|
-
name: "pro-monthly",
|
|
252
|
-
productId: "product-id",
|
|
253
|
-
priceInSmallestUnit: 9900,
|
|
254
|
-
currency: "SAR",
|
|
255
|
-
billingInterval: "MONTH",
|
|
256
|
-
billingIntervalCount: 1,
|
|
257
|
-
trialPeriodDays: 7,
|
|
258
|
-
group: "main",
|
|
259
|
-
seatBilling: {
|
|
260
|
-
default: 3,
|
|
261
|
-
minimum: 1,
|
|
262
|
-
maximum: 100,
|
|
263
|
-
},
|
|
264
|
-
limits: { projects: 50, reports: true },
|
|
265
|
-
},
|
|
266
|
-
],
|
|
267
|
-
|
|
268
|
-
onSubscriptionActivated: async ({ subscription, user }) => {
|
|
269
|
-
// Run app-specific work here.
|
|
270
|
-
},
|
|
271
|
-
});
|
|
272
|
-
```
|
|
273
|
-
|
|
274
|
-
Plan names and product IDs must be unique. Prices use the smallest currency unit. For SAR,
|
|
275
|
-
`9900` means `99.00 SAR`.
|
|
276
|
-
|
|
277
|
-
`priceInSmallestUnit` is the price per seat. `seatBilling` controls billed quantity; `limits`
|
|
278
|
-
controls application access. Set `customerEditable: true` with explicit minimum and maximum bounds
|
|
279
|
-
to let customers change quantity in hosted checkout.
|
|
108
|
+
`portal()` adds authenticated consumer state, subscription, and invoice endpoints. Stream SDK 1.1.3
|
|
109
|
+
does not provide hosted portal session creation. To open Stream's hosted portal, configure the
|
|
110
|
+
server-side `createSession` callback with the REST endpoint. The [integration guide](docs/integration.md#billing-portal)
|
|
111
|
+
has a working bridge example and client call. The callback receives the consumer ID resolved from
|
|
112
|
+
the signed-in session.
|
|
280
113
|
|
|
281
|
-
|
|
282
|
-
app needs a stricter or more flexible rule.
|
|
114
|
+
## Security and access
|
|
283
115
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
const { data } = await authClient.subscription.upgrade({
|
|
288
|
-
plan: "pro-monthly",
|
|
289
|
-
seats: 5,
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
window.location.href = data.url;
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
On the success page:
|
|
296
|
-
|
|
297
|
-
```ts
|
|
298
|
-
await authClient.subscription.success({
|
|
299
|
-
query: { subscriptionId: data.subscriptionId },
|
|
300
|
-
});
|
|
301
|
-
```
|
|
302
|
-
|
|
303
|
-
Then refresh the subscription data used by your UI.
|
|
304
|
-
|
|
305
|
-
### Manage a subscription
|
|
306
|
-
|
|
307
|
-
```ts
|
|
308
|
-
await authClient.subscription.changePlan({
|
|
309
|
-
subscriptionId,
|
|
310
|
-
plan: "pro-yearly",
|
|
311
|
-
seats: 8, // optional; defaults to the current quantity
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
await authClient.subscription.updateSeats({
|
|
315
|
-
subscriptionId,
|
|
316
|
-
seats: 12,
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
await authClient.subscription.pendingChange.cancel({ subscriptionId });
|
|
320
|
-
|
|
321
|
-
await authClient.subscription.cancel({
|
|
322
|
-
subscriptionId,
|
|
323
|
-
cancelAtPeriodEnd: true,
|
|
324
|
-
});
|
|
325
|
-
|
|
326
|
-
await authClient.subscription.uncancel({ subscriptionId });
|
|
327
|
-
```
|
|
116
|
+
Checkout can require an authenticated session with `authenticatedUsersOnly`. Cross-account
|
|
117
|
+
subscription actions require an `authorizeReference` callback. Webhook signatures are checked
|
|
118
|
+
against the configured secret, and signature age is bounded by the webhook tolerance setting.
|
|
328
119
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
120
|
+
Subscription features and limits default to access for `active`, `trialing`, `frozen`, and
|
|
121
|
+
`past_due` states. Set `accessStatuses` to use a different policy. When provider products no longer
|
|
122
|
+
match a configured plan, billing details remain readable while feature and limit checks deny access.
|
|
332
123
|
|
|
333
|
-
|
|
334
|
-
subscriptions are canceled at once.
|
|
124
|
+
## Checks and coverage
|
|
335
125
|
|
|
336
|
-
|
|
126
|
+
Run the repository checks with:
|
|
337
127
|
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
subscriptionId,
|
|
341
|
-
freezeStartDatetime: new Date().toISOString(),
|
|
342
|
-
freezeEndDatetime: null,
|
|
343
|
-
});
|
|
344
|
-
|
|
345
|
-
await authClient.subscription.unfreeze({ subscriptionId });
|
|
346
|
-
|
|
347
|
-
await authClient.subscription.freeze.cancel({
|
|
348
|
-
subscriptionId,
|
|
349
|
-
freezeId: freeze.id!,
|
|
350
|
-
});
|
|
351
|
-
```
|
|
352
|
-
|
|
353
|
-
### Read access and limits
|
|
354
|
-
|
|
355
|
-
```ts
|
|
356
|
-
const current = await authClient.subscription.current({
|
|
357
|
-
query: { group: "main" },
|
|
358
|
-
});
|
|
359
|
-
|
|
360
|
-
const feature = await authClient.subscription.hasFeature({
|
|
361
|
-
query: { feature: "reports", group: "main" },
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
const projects = await authClient.subscription.checkLimit({
|
|
365
|
-
query: { feature: "projects", count: 4, group: "main" },
|
|
366
|
-
});
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
`checkLimit` reads configured entitlements. Use `current.data?.seats` for licensed-member counts.
|
|
370
|
-
|
|
371
|
-
Pass `group` for grouped plans. Leave it out only for a plan without a group.
|
|
372
|
-
|
|
373
|
-
See [the subscription data model](docs/subscriptions.md) for columns, lifecycle, and migration
|
|
374
|
-
details.
|
|
375
|
-
|
|
376
|
-
By default, `active`, `trialing`, `frozen`, and `past_due` subscriptions can use plan
|
|
377
|
-
features. Change this with `accessStatuses`.
|
|
378
|
-
|
|
379
|
-
## Server-side calls
|
|
380
|
-
|
|
381
|
-
Every checkout, portal, subscription, and admin action is also available on `auth.api`. Types
|
|
382
|
-
come from Better Auth.
|
|
383
|
-
|
|
384
|
-
Browser calls follow the route path, such as `authClient.subscription.cancel`. Server calls use
|
|
385
|
-
the endpoint name, such as `auth.api.cancelSubscription`.
|
|
386
|
-
|
|
387
|
-
```ts
|
|
388
|
-
import { headers } from "next/headers";
|
|
389
|
-
|
|
390
|
-
const current = await auth.api.currentSubscription({
|
|
391
|
-
query: { group: "main" },
|
|
392
|
-
headers: await headers(),
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
await auth.api.cancelSubscription({
|
|
396
|
-
body: { subscriptionId, cancelAtPeriodEnd: true },
|
|
397
|
-
headers: await headers(),
|
|
398
|
-
});
|
|
399
|
-
```
|
|
400
|
-
|
|
401
|
-
Pass the request headers so Better Auth can read the session.
|
|
402
|
-
|
|
403
|
-
Webhook delivery is not an `auth.api` action. It uses the raw HTTP body to verify the signature.
|
|
404
|
-
|
|
405
|
-
## Cross-account access
|
|
406
|
-
|
|
407
|
-
User actions use the signed-in user's ID by default.
|
|
408
|
-
|
|
409
|
-
To manage an organization or another app-owned reference, add `authorizeReference`:
|
|
410
|
-
|
|
411
|
-
```ts
|
|
412
|
-
subscriptions({
|
|
413
|
-
plans,
|
|
414
|
-
authorizeReference: async ({ user, referenceId, referenceType, action }) => {
|
|
415
|
-
return canManageBilling(user, referenceId, referenceType, action);
|
|
416
|
-
},
|
|
417
|
-
});
|
|
418
|
-
```
|
|
419
|
-
|
|
420
|
-
Without this callback, cross-account actions return `FORBIDDEN`.
|
|
421
|
-
|
|
422
|
-
## Admin
|
|
423
|
-
|
|
424
|
-
`admin()` adds billing actions for payments, subscriptions, freezes, consumers, invoices,
|
|
425
|
-
products, coupons, payment links, and webhook retries.
|
|
426
|
-
|
|
427
|
-
```ts
|
|
428
|
-
import { admin } from "better-auth-streampay";
|
|
429
|
-
|
|
430
|
-
admin({
|
|
431
|
-
adminRoles: ["admin", "billing"],
|
|
432
|
-
isAdmin: async (user) => user.email.endsWith("@example.com"),
|
|
433
|
-
});
|
|
434
|
-
```
|
|
435
|
-
|
|
436
|
-
Calls use names such as:
|
|
437
|
-
|
|
438
|
-
- `auth.api.adminListPayments`
|
|
439
|
-
- `auth.api.adminGetSubscription`
|
|
440
|
-
- `auth.api.adminCreateProduct`
|
|
441
|
-
- `auth.api.adminReplayWebhookEvent`
|
|
442
|
-
|
|
443
|
-
The IDE shows the body, query, and response types for every action.
|
|
444
|
-
|
|
445
|
-
Make sure your Better Auth route accepts `GET`, `POST`, `PATCH`, `PUT`, and `DELETE`.
|
|
446
|
-
|
|
447
|
-
## Webhooks
|
|
448
|
-
|
|
449
|
-
Register this URL in the StreamPay dashboard:
|
|
450
|
-
|
|
451
|
-
```text
|
|
452
|
-
https://your-app.com/api/auth/streampay/webhooks
|
|
453
|
-
```
|
|
454
|
-
|
|
455
|
-
Then add the handlers you need:
|
|
456
|
-
|
|
457
|
-
```ts
|
|
458
|
-
webhooks({
|
|
459
|
-
secret: process.env.STREAMPAY_WEBHOOK_SECRET!,
|
|
460
|
-
|
|
461
|
-
onPaymentSucceeded: async (event) => {},
|
|
462
|
-
onPaymentFailed: async (event) => {},
|
|
463
|
-
onSubscriptionActivated: async (event) => {},
|
|
464
|
-
onSubscriptionCanceled: async (event) => {},
|
|
465
|
-
onPayload: async (event) => {},
|
|
466
|
-
});
|
|
467
|
-
```
|
|
468
|
-
|
|
469
|
-
The plugin:
|
|
470
|
-
|
|
471
|
-
- checks the webhook signature
|
|
472
|
-
- rejects old signatures
|
|
473
|
-
- deduplicates subscription sync and lifecycle callbacks
|
|
474
|
-
- retries temporary failures
|
|
475
|
-
- stores failed subscription events for admin replay
|
|
476
|
-
|
|
477
|
-
The StreamPay SDK does not export webhook payload types. This package provides checked event
|
|
478
|
-
types based on StreamPay's documented payloads.
|
|
479
|
-
|
|
480
|
-
To rotate a secret:
|
|
481
|
-
|
|
482
|
-
```ts
|
|
483
|
-
webhooks({
|
|
484
|
-
secret: [
|
|
485
|
-
process.env.STREAMPAY_WEBHOOK_SECRET!,
|
|
486
|
-
process.env.STREAMPAY_WEBHOOK_SECRET_OLD!,
|
|
487
|
-
],
|
|
488
|
-
});
|
|
489
|
-
```
|
|
490
|
-
|
|
491
|
-
Remove the old secret after StreamPay uses the new one.
|
|
492
|
-
|
|
493
|
-
## What owns each job
|
|
494
|
-
|
|
495
|
-
StreamPay owns:
|
|
496
|
-
|
|
497
|
-
- charges
|
|
498
|
-
- invoices
|
|
499
|
-
- subscription state
|
|
500
|
-
- trials
|
|
501
|
-
- renewal attempts
|
|
502
|
-
- cancellation timing
|
|
503
|
-
|
|
504
|
-
The plugin owns:
|
|
505
|
-
|
|
506
|
-
- Better Auth access checks
|
|
507
|
-
- local subscription rows
|
|
508
|
-
- plan features and limits
|
|
509
|
-
- webhook checks and retries
|
|
510
|
-
- checkout recovery
|
|
511
|
-
|
|
512
|
-
Do not edit subscription rows by hand. Let provider responses and webhooks update them.
|
|
513
|
-
|
|
514
|
-
## Errors
|
|
515
|
-
|
|
516
|
-
Errors use stable codes:
|
|
517
|
-
|
|
518
|
-
```ts
|
|
519
|
-
import { $ERROR_CODES } from "better-auth-streampay";
|
|
520
|
-
|
|
521
|
-
if (error.code === $ERROR_CODES.SUBSCRIPTION_ALREADY_ACTIVE.code) {
|
|
522
|
-
// Show the current plan.
|
|
523
|
-
}
|
|
128
|
+
```bash
|
|
129
|
+
pnpm check
|
|
524
130
|
```
|
|
525
131
|
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
-
|
|
529
|
-
-
|
|
530
|
-
- `NOT_FOUND`
|
|
531
|
-
- `SUBSCRIPTION_ALREADY_ACTIVE`
|
|
532
|
-
- `SUBSCRIPTION_INVALID_STATE`
|
|
533
|
-
- `SUBSCRIPTION_PLAN_CHANGE_ALREADY_SCHEDULED`
|
|
534
|
-
- `WEBHOOK_REPLAY_IN_PROGRESS`
|
|
535
|
-
|
|
536
|
-
## Useful exports
|
|
537
|
-
|
|
538
|
-
```ts
|
|
539
|
-
import {
|
|
540
|
-
StreamPayAmount,
|
|
541
|
-
checkLimit,
|
|
542
|
-
findConsumerByExternalId,
|
|
543
|
-
formatStreamPayError,
|
|
544
|
-
hasFeature,
|
|
545
|
-
parseStreamPayError,
|
|
546
|
-
verifyWebhook,
|
|
547
|
-
} from "better-auth-streampay";
|
|
548
|
-
```
|
|
132
|
+
`pnpm test` excludes live StreamPay tests. `pnpm test:coverage` measures local source coverage, but
|
|
133
|
+
does not prove behavior against StreamPay. `pnpm test:postgres` uses a dedicated test database for
|
|
134
|
+
database-specific behavior. Live provider coverage, test limits, and validation details are in
|
|
135
|
+
[staging validation](docs/staging-validation.md).
|
|
549
136
|
|
|
550
137
|
## License
|
|
551
138
|
|
package/dist/client.cjs
CHANGED
package/dist/client.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/error-codes.ts","../src/client.ts"],"names":[],"mappings":";;;AAMA,SAAS,iBACR,KAAA,EACqD;AACrD,EAAA,MAAM,MAAM,EAAC;AACb,EAAA,KAAA,MAAW,GAAA,IAAO,MAAA,CAAO,IAAA,CAAK,KAAK,CAAA,EAA+B;AACjE,IAAA,GAAA,CAAI,GAAG,CAAA,GAAI;AAAA,MACV,IAAA,EAAM,GAAA;AAAA,MACN,OAAA,EAAS,MAAM,GAAG,CAAA;AAAA,MAClB,UAAU,MAAM;AAAA,KACjB;AAAA,EACD;AACA,EAAA,OAAO,GAAA;AACR;AAEO,IAAM,eAAe,gBAAA,CAAiB;AAAA,EAC5C,gBAAA,EAAkB,6BAAA;AAAA,EAClB,YAAA,EAAc,qCAAA;AAAA,EACd,SAAA,EAAW,oDAAA;AAAA,EACX,SAAA,EAAW,uCAAA;AAAA,EACX,kBAAA,EAAoB,iEAAA;AAAA,EACpB,gCAAA,EAAkC,+DAAA;AAAA,EAClC,oCAAA,EAAsC,iDAAA;AAAA,EACtC,6BAAA,EACC,mFAAA;AAAA,EACD,qBAAA,EAAuB,qDAAA;AAAA,EACvB,iBAAA,EAAmB,qDAAA;AAAA,EACnB,qBAAA,EAAuB,qDAAA;AAAA,EACvB,wBAAA,EAA0B,yCAAA;AAAA,EAC1B,qBAAA,EAAuB,4CAAA;AAAA,EACvB,wBAAA,EAA0B,0CAAA;AAAA,EAC1B,2BAAA,EAA6B,kEAAA;AAAA,EAC7B,sBAAA,EAAwB,uDAAA;AAAA,EACxB,cAAA,EAAgB,0EAAA;AAAA,EAChB,aAAA,EAAe,yEAAA;AAAA,EACf,gCAAA,EAAkC,iDAAA;AAAA,EAClC,sBAAA,EAAwB,kCAAA;AAAA,EACxB,2BAAA,EAA6B,oDAAA;AAAA,EAC7B,2BAAA,EAA6B,4DAAA;AAAA,EAC7B,iCAAA,EACC,uEAAA;AAAA,EACD,6BAAA,EAA+B,uCAAA;AAAA,EAC/B,2BAAA,EAA6B,qCAAA;AAAA,EAC7B,oBAAA,EAAsB,+BAAA;AAAA,EACtB,qCAAA,EACC,gEAAA;AAAA,EACD,4BAAA,EAA8B,oDAAA;AAAA,EAC9B,+BAAA,EAAiC,oDAAA;AAAA,EACjC,0CAAA,EACC,uDAAA;AAAA,EACD,yCAAA,EACC,6EAAA;AAAA,EACD,0CAAA,EACC,2EAAA;AAAA,EACD,gCAAA,EACC,4EAAA;AAAA,EACD,0CAAA,EACC,qEAAA;AAAA,EACD,0BAAA,EAA4B,0DAAA;AAAA,EAC5B,8BAAA,EAAgC,0DAAA;AAAA,EAChC,qCAAA,EACC,+DAAA;AAAA,EACD,0BAAA,EAA4B,+CAAA;AAAA,EAC5B,OAAA,EAAS;AACV,CAAC;;;AC/DM,IAAM,kBAAkB,MAAM;AACpC,EAAA,OAAO;AAAA,IACN,EAAA,EAAI,kBAAA;AAAA,IACJ,oBAAoB,EAAC;AAAA,IACrB;AAAA,GACD;AACD","file":"client.cjs","sourcesContent":["export interface ErrorCodeEntry<K extends string> {\n\tcode: K;\n\tmessage: string;\n\ttoString: () => K;\n}\n\nfunction defineErrorCodes<const Codes extends Record<string, string>>(\n\tcodes: Codes,\n): { [K in keyof Codes & string]: ErrorCodeEntry<K> } {\n\tconst out = {} as { [K in keyof Codes & string]: ErrorCodeEntry<K> };\n\tfor (const key of Object.keys(codes) as (keyof Codes & string)[]) {\n\t\tout[key] = {\n\t\t\tcode: key,\n\t\t\tmessage: codes[key] as Codes[typeof key],\n\t\t\ttoString: () => key,\n\t\t};\n\t}\n\treturn out;\n}\n\nexport const $ERROR_CODES = defineErrorCodes({\n\tVALIDATION_ERROR: \"Request input is not valid.\",\n\tUNAUTHORIZED: \"Missing or invalid API credentials.\",\n\tFORBIDDEN: \"You do not have permission to perform this action.\",\n\tNOT_FOUND: \"The requested resource was not found.\",\n\tCONSUMER_DUPLICATE: \"A consumer with one of the provided identifiers already exists.\",\n\tSTREAMPAY_CONSUMER_LINK_CONFLICT: \"This StreamPay consumer is already linked to another account.\",\n\tSTREAMPAY_CONSUMER_LINK_WRITE_FAILED: \"The StreamPay consumer link could not be saved.\",\n\tCONSUMER_HAS_ONGOING_ACTIVITY:\n\t\t\"The consumer has ongoing invoices or active subscriptions and cannot be modified.\",\n\tINVOICE_INVALID_STATE: \"The invoice is in a state that forbids this action.\",\n\tPAYMENT_DUPLICATE: \"A payment with the same information already exists.\",\n\tPAYMENT_INVALID_STATE: \"The payment is in a state that forbids this action.\",\n\tPAYMENT_ALREADY_REFUNDED: \"This payment has already been refunded.\",\n\tPAYMENT_REFUND_FAILED: \"The payment processor rejected the refund.\",\n\tPAYMENT_GATEWAY_DECLINED: \"The payment was declined by the gateway.\",\n\tPAYMENT_GATEWAY_UNAVAILABLE: \"The payment gateway is temporarily unavailable. Try again later.\",\n\tPAYMENT_METHOD_INVALID: \"The supplied payment method is invalid for this flow.\",\n\tPRODUCT_LOCKED: \"The product is referenced by a finalized invoice and cannot be modified.\",\n\tCOUPON_LOCKED: \"The coupon is referenced by a finalized invoice and cannot be modified.\",\n\tCHECKOUT_CLIENT_FIELDS_FORBIDDEN: \"Checkout fields must be resolved by the server.\",\n\tSUBSCRIPTION_NOT_FOUND: \"The subscription does not exist.\",\n\tSUBSCRIPTION_PLAN_NOT_FOUND: \"The requested subscription plan is not configured.\",\n\tSUBSCRIPTION_ALREADY_ACTIVE: \"An active subscription already exists for this plan group.\",\n\tSUBSCRIPTION_CHECKOUT_IN_PROGRESS:\n\t\t\"A subscription checkout is already being created for this plan group.\",\n\tSUBSCRIPTION_ALREADY_CANCELED: \"The subscription is already canceled.\",\n\tSUBSCRIPTION_ALREADY_FROZEN: \"The subscription is already frozen.\",\n\tSUBSCRIPTION_EXPIRED: \"The subscription has expired.\",\n\tSUBSCRIPTION_ALREADY_SCHEDULED_CANCEL:\n\t\t\"The subscription is already scheduled to cancel at period end.\",\n\tSUBSCRIPTION_ALREADY_ON_PLAN: \"The subscription is already on the requested plan.\",\n\tSUBSCRIPTION_SEAT_COUNT_INVALID: \"The requested seat count is invalid for this plan.\",\n\tSUBSCRIPTION_SEAT_CHANGE_ALREADY_SCHEDULED:\n\t\t\"A different subscription change is already scheduled.\",\n\tSUBSCRIPTION_IMMEDIATE_CANCEL_UNSUPPORTED:\n\t\t\"StreamPay does not support immediate cancellation for active subscriptions.\",\n\tSUBSCRIPTION_PERIOD_END_CANCEL_UNSUPPORTED:\n\t\t\"StreamPay only supports period-end cancellation for active subscriptions.\",\n\tSUBSCRIPTION_PLAN_GROUP_MISMATCH:\n\t\t\"Subscription plan changes must stay within the same configured plan group.\",\n\tSUBSCRIPTION_PLAN_CHANGE_ALREADY_SCHEDULED:\n\t\t\"A different plan change is already scheduled for this subscription.\",\n\tSUBSCRIPTION_INVALID_STATE: \"The subscription is in a state that forbids this action.\",\n\tSUBSCRIPTION_FREEZE_NOT_ACTIVE: \"No active freeze period was found for this subscription.\",\n\tSUBSCRIPTION_REFERENCE_NOT_AUTHORIZED:\n\t\t\"You are not authorized to act on this subscription reference.\",\n\tWEBHOOK_REPLAY_IN_PROGRESS: \"This webhook event is already being replayed.\",\n\tUNKNOWN: \"An unexpected error occurred.\",\n});\n\nexport type StreamPayErrorCode = keyof typeof $ERROR_CODES;\n\nconst RAW_CODE_MAP: Record<string, StreamPayErrorCode> = {\n\tINVALID_PARAMETERS: \"VALIDATION_ERROR\",\n\n\tPERMISSION_FORBIDDEN: \"FORBIDDEN\",\n\tBRANCH_ACCESS_DENIED: \"FORBIDDEN\",\n\tBRANCH_NOT_FOUND: \"NOT_FOUND\",\n\n\tDUPLICATE_CONSUMER: \"CONSUMER_DUPLICATE\",\n\tDUPLICATE_CARD_TOKEN: \"CONSUMER_DUPLICATE\",\n\tCONSUMER_HAS_ONGOING_INVOICES: \"CONSUMER_HAS_ONGOING_ACTIVITY\",\n\tCONSUMER_HAS_ONGOING_SUBSCRIPTIONS: \"CONSUMER_HAS_ONGOING_ACTIVITY\",\n\n\tINVOICE_FINALISED: \"INVOICE_INVALID_STATE\",\n\tINVOICE_INVALID_STATUS: \"INVOICE_INVALID_STATE\",\n\tINVOICE_CONSENT_CONSUMED: \"INVOICE_INVALID_STATE\",\n\tINVOICE_CONSENT_EXPIRED: \"INVOICE_INVALID_STATE\",\n\tINVOICE_TOTAL_MISMATCH_PAYMENT_AMOUNT_SUM: \"INVOICE_INVALID_STATE\",\n\tINVOICE_PAYMENT_COUNT_NOT_MATCHED: \"INVOICE_INVALID_STATE\",\n\n\tDUPLICATE_PAYMENT: \"PAYMENT_DUPLICATE\",\n\tPAYMENT_IN_PROGRESS: \"PAYMENT_INVALID_STATE\",\n\tPAYMENT_FINALIZED: \"PAYMENT_INVALID_STATE\",\n\tPAYMEND_UNPAID: \"PAYMENT_INVALID_STATE\",\n\tINVALID_STATUS: \"PAYMENT_INVALID_STATE\",\n\n\tPAYMENT_REFUNDED_ALREADY: \"PAYMENT_ALREADY_REFUNDED\",\n\tPAYMENT_REFUNDED_FAILED: \"PAYMENT_REFUND_FAILED\",\n\n\tPAYMENT_GATEWAY_DECLINED: \"PAYMENT_GATEWAY_DECLINED\",\n\tINSUFFICIENT_FUNDS: \"PAYMENT_GATEWAY_DECLINED\",\n\tMOYASAR_PAYMENT_INFO_MISMATCH: \"PAYMENT_GATEWAY_DECLINED\",\n\tMOYASAR_BAD_REQUEST: \"PAYMENT_GATEWAY_DECLINED\",\n\tMOYASAR_UNAUTHORIZED: \"PAYMENT_GATEWAY_DECLINED\",\n\tMOYASAR_FORBIDDEN: \"PAYMENT_GATEWAY_DECLINED\",\n\tMOYASAR_NOT_FOUND: \"PAYMENT_GATEWAY_DECLINED\",\n\tMOYASAR_METHOD_NOT_ALLOWED: \"PAYMENT_GATEWAY_DECLINED\",\n\n\tMOYASAR_TIMEOUT: \"PAYMENT_GATEWAY_UNAVAILABLE\",\n\tMOYASAR_TOO_MANY_REQUESTS: \"PAYMENT_GATEWAY_UNAVAILABLE\",\n\tMOYASAR_INTERNAL_SERVER_ERROR: \"PAYMENT_GATEWAY_UNAVAILABLE\",\n\tMOYASAR_SERVICE_UNAVAILABLE: \"PAYMENT_GATEWAY_UNAVAILABLE\",\n\n\tMOYASAR_INVALID_CARD_TOKEN: \"PAYMENT_METHOD_INVALID\",\n\tMANUAL_INVOICE_CARD_ID: \"PAYMENT_METHOD_INVALID\",\n\tONE_OFF_INVALID_PAYMENT_FLOW: \"PAYMENT_METHOD_INVALID\",\n\tONE_OFF_CARD_ID: \"PAYMENT_METHOD_INVALID\",\n\tAUTO_INVOICE_MISSING_CARD_ID: \"PAYMENT_METHOD_INVALID\",\n\tPAYMENT_FLOW_NOT_ALLOWED: \"PAYMENT_METHOD_INVALID\",\n\n\tPRODUCT_USED_IN_FINALIZED_INVOICE: \"PRODUCT_LOCKED\",\n\tCOUPON_USED_IN_FINALIZED_INVOICE: \"COUPON_LOCKED\",\n\n\tSUBSCRIPTION_ALREADY_ACTIVE: \"SUBSCRIPTION_ALREADY_ACTIVE\",\n\tSUBSCRIPTION_NOT_FOUND: \"SUBSCRIPTION_NOT_FOUND\",\n\tSUBSCRIPTION_ALREADY_CANCELED: \"SUBSCRIPTION_ALREADY_CANCELED\",\n\tSUBSCRIPTION_ALREADY_FROZEN: \"SUBSCRIPTION_ALREADY_FROZEN\",\n\tSUBSCRIPTION_NOT_FROZEN: \"SUBSCRIPTION_FREEZE_NOT_ACTIVE\",\n\tSUBSCRIPTION_EXPIRED: \"SUBSCRIPTION_EXPIRED\",\n};\n\nexport function mapToErrorCode(\n\trawCode: string | undefined,\n\tstatus: number | undefined,\n): StreamPayErrorCode {\n\tif (rawCode && rawCode in RAW_CODE_MAP) {\n\t\tconst mapped = RAW_CODE_MAP[rawCode];\n\t\tif (mapped) return mapped;\n\t}\n\tif (status === 401) return \"UNAUTHORIZED\";\n\tif (status === 403) return \"FORBIDDEN\";\n\tif (status === 404) return \"NOT_FOUND\";\n\tif (status === 422) return \"VALIDATION_ERROR\";\n\treturn \"UNKNOWN\";\n}\n","import type { BetterAuthClientPlugin } from \"better-auth\";\nimport { $ERROR_CODES } from \"./error-codes\";\nimport type { streampay } from \"./streampay\";\n\nexport { $ERROR_CODES, type StreamPayErrorCode } from \"./error-codes\";\n\nexport const streampayClient = () => {\n\treturn {\n\t\tid: \"streampay-client\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof streampay>,\n\t\t$ERROR_CODES,\n\t} satisfies BetterAuthClientPlugin;\n};\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/error-codes.ts","../src/client.ts"],"names":[],"mappings":";;;AAMA,SAAS,iBACR,KAAA,EACqD;AACrD,EAAA,MAAM,MAAM,EAAC;AACb,EAAA,KAAA,MAAW,GAAA,IAAO,MAAA,CAAO,IAAA,CAAK,KAAK,CAAA,EAA+B;AACjE,IAAA,GAAA,CAAI,GAAG,CAAA,GAAI;AAAA,MACV,IAAA,EAAM,GAAA;AAAA,MACN,OAAA,EAAS,MAAM,GAAG,CAAA;AAAA,MAClB,UAAU,MAAM;AAAA,KACjB;AAAA,EACD;AACA,EAAA,OAAO,GAAA;AACR;AAEO,IAAM,eAAe,gBAAA,CAAiB;AAAA,EAC5C,gBAAA,EAAkB,6BAAA;AAAA,EAClB,YAAA,EAAc,qCAAA;AAAA,EACd,SAAA,EAAW,oDAAA;AAAA,EACX,SAAA,EAAW,uCAAA;AAAA,EACX,kBAAA,EAAoB,iEAAA;AAAA,EACpB,gCAAA,EAAkC,+DAAA;AAAA,EAClC,oCAAA,EAAsC,iDAAA;AAAA,EACtC,6BAAA,EACC,mFAAA;AAAA,EACD,qBAAA,EAAuB,qDAAA;AAAA,EACvB,iBAAA,EAAmB,qDAAA;AAAA,EACnB,qBAAA,EAAuB,qDAAA;AAAA,EACvB,wBAAA,EAA0B,yCAAA;AAAA,EAC1B,qBAAA,EAAuB,4CAAA;AAAA,EACvB,wBAAA,EAA0B,0CAAA;AAAA,EAC1B,2BAAA,EAA6B,kEAAA;AAAA,EAC7B,sBAAA,EAAwB,uDAAA;AAAA,EACxB,cAAA,EAAgB,0EAAA;AAAA,EAChB,aAAA,EAAe,yEAAA;AAAA,EACf,gCAAA,EAAkC,iDAAA;AAAA,EAClC,sBAAA,EAAwB,kCAAA;AAAA,EACxB,2BAAA,EAA6B,oDAAA;AAAA,EAC7B,2BAAA,EAA6B,4DAAA;AAAA,EAC7B,iCAAA,EACC,uEAAA;AAAA,EACD,6BAAA,EAA+B,uCAAA;AAAA,EAC/B,2BAAA,EAA6B,qCAAA;AAAA,EAC7B,oBAAA,EAAsB,+BAAA;AAAA,EACtB,qCAAA,EACC,gEAAA;AAAA,EACD,4BAAA,EAA8B,oDAAA;AAAA,EAC9B,+BAAA,EAAiC,oDAAA;AAAA,EACjC,0CAAA,EACC,uDAAA;AAAA,EACD,yCAAA,EACC,6EAAA;AAAA,EACD,0CAAA,EACC,2EAAA;AAAA,EACD,gCAAA,EACC,4EAAA;AAAA,EACD,0CAAA,EACC,qEAAA;AAAA,EACD,0BAAA,EAA4B,0DAAA;AAAA,EAC5B,8BAAA,EAAgC,0DAAA;AAAA,EAChC,qCAAA,EACC,+DAAA;AAAA,EACD,0BAAA,EAA4B,+CAAA;AAAA,EAC5B,OAAA,EAAS;AACV,CAAC;;;AC/DM,IAAM,kBAAkB,MAAM;AACpC,EAAA,OAAO;AAAA,IACN,EAAA,EAAI,kBAAA;AAAA,IACJ,oBAAoB,EAAC;AAAA,IACrB,WAAA,EAAa,EAAE,0BAAA,EAA4B,MAAA,EAAQ,aAAa,MAAA,EAAO;AAAA,IACvE;AAAA,GACD;AACD","file":"client.cjs","sourcesContent":["export interface ErrorCodeEntry<K extends string> {\n\tcode: K;\n\tmessage: string;\n\ttoString: () => K;\n}\n\nfunction defineErrorCodes<const Codes extends Record<string, string>>(\n\tcodes: Codes,\n): { [K in keyof Codes & string]: ErrorCodeEntry<K> } {\n\tconst out = {} as { [K in keyof Codes & string]: ErrorCodeEntry<K> };\n\tfor (const key of Object.keys(codes) as (keyof Codes & string)[]) {\n\t\tout[key] = {\n\t\t\tcode: key,\n\t\t\tmessage: codes[key] as Codes[typeof key],\n\t\t\ttoString: () => key,\n\t\t};\n\t}\n\treturn out;\n}\n\nexport const $ERROR_CODES = defineErrorCodes({\n\tVALIDATION_ERROR: \"Request input is not valid.\",\n\tUNAUTHORIZED: \"Missing or invalid API credentials.\",\n\tFORBIDDEN: \"You do not have permission to perform this action.\",\n\tNOT_FOUND: \"The requested resource was not found.\",\n\tCONSUMER_DUPLICATE: \"A consumer with one of the provided identifiers already exists.\",\n\tSTREAMPAY_CONSUMER_LINK_CONFLICT: \"This StreamPay consumer is already linked to another account.\",\n\tSTREAMPAY_CONSUMER_LINK_WRITE_FAILED: \"The StreamPay consumer link could not be saved.\",\n\tCONSUMER_HAS_ONGOING_ACTIVITY:\n\t\t\"The consumer has ongoing invoices or active subscriptions and cannot be modified.\",\n\tINVOICE_INVALID_STATE: \"The invoice is in a state that forbids this action.\",\n\tPAYMENT_DUPLICATE: \"A payment with the same information already exists.\",\n\tPAYMENT_INVALID_STATE: \"The payment is in a state that forbids this action.\",\n\tPAYMENT_ALREADY_REFUNDED: \"This payment has already been refunded.\",\n\tPAYMENT_REFUND_FAILED: \"The payment processor rejected the refund.\",\n\tPAYMENT_GATEWAY_DECLINED: \"The payment was declined by the gateway.\",\n\tPAYMENT_GATEWAY_UNAVAILABLE: \"The payment gateway is temporarily unavailable. Try again later.\",\n\tPAYMENT_METHOD_INVALID: \"The supplied payment method is invalid for this flow.\",\n\tPRODUCT_LOCKED: \"The product is referenced by a finalized invoice and cannot be modified.\",\n\tCOUPON_LOCKED: \"The coupon is referenced by a finalized invoice and cannot be modified.\",\n\tCHECKOUT_CLIENT_FIELDS_FORBIDDEN: \"Checkout fields must be resolved by the server.\",\n\tSUBSCRIPTION_NOT_FOUND: \"The subscription does not exist.\",\n\tSUBSCRIPTION_PLAN_NOT_FOUND: \"The requested subscription plan is not configured.\",\n\tSUBSCRIPTION_ALREADY_ACTIVE: \"An active subscription already exists for this plan group.\",\n\tSUBSCRIPTION_CHECKOUT_IN_PROGRESS:\n\t\t\"A subscription checkout is already being created for this plan group.\",\n\tSUBSCRIPTION_ALREADY_CANCELED: \"The subscription is already canceled.\",\n\tSUBSCRIPTION_ALREADY_FROZEN: \"The subscription is already frozen.\",\n\tSUBSCRIPTION_EXPIRED: \"The subscription has expired.\",\n\tSUBSCRIPTION_ALREADY_SCHEDULED_CANCEL:\n\t\t\"The subscription is already scheduled to cancel at period end.\",\n\tSUBSCRIPTION_ALREADY_ON_PLAN: \"The subscription is already on the requested plan.\",\n\tSUBSCRIPTION_SEAT_COUNT_INVALID: \"The requested seat count is invalid for this plan.\",\n\tSUBSCRIPTION_SEAT_CHANGE_ALREADY_SCHEDULED:\n\t\t\"A different subscription change is already scheduled.\",\n\tSUBSCRIPTION_IMMEDIATE_CANCEL_UNSUPPORTED:\n\t\t\"StreamPay does not support immediate cancellation for active subscriptions.\",\n\tSUBSCRIPTION_PERIOD_END_CANCEL_UNSUPPORTED:\n\t\t\"StreamPay only supports period-end cancellation for active subscriptions.\",\n\tSUBSCRIPTION_PLAN_GROUP_MISMATCH:\n\t\t\"Subscription plan changes must stay within the same configured plan group.\",\n\tSUBSCRIPTION_PLAN_CHANGE_ALREADY_SCHEDULED:\n\t\t\"A different plan change is already scheduled for this subscription.\",\n\tSUBSCRIPTION_INVALID_STATE: \"The subscription is in a state that forbids this action.\",\n\tSUBSCRIPTION_FREEZE_NOT_ACTIVE: \"No active freeze period was found for this subscription.\",\n\tSUBSCRIPTION_REFERENCE_NOT_AUTHORIZED:\n\t\t\"You are not authorized to act on this subscription reference.\",\n\tWEBHOOK_REPLAY_IN_PROGRESS: \"This webhook event is already being replayed.\",\n\tUNKNOWN: \"An unexpected error occurred.\",\n});\n\nexport type StreamPayErrorCode = keyof typeof $ERROR_CODES;\n\nconst RAW_CODE_MAP: Record<string, StreamPayErrorCode> = {\n\tINVALID_PARAMETERS: \"VALIDATION_ERROR\",\n\n\tPERMISSION_FORBIDDEN: \"FORBIDDEN\",\n\tBRANCH_ACCESS_DENIED: \"FORBIDDEN\",\n\tBRANCH_NOT_FOUND: \"NOT_FOUND\",\n\n\tDUPLICATE_CONSUMER: \"CONSUMER_DUPLICATE\",\n\tDUPLICATE_CARD_TOKEN: \"CONSUMER_DUPLICATE\",\n\tCONSUMER_HAS_ONGOING_INVOICES: \"CONSUMER_HAS_ONGOING_ACTIVITY\",\n\tCONSUMER_HAS_ONGOING_SUBSCRIPTIONS: \"CONSUMER_HAS_ONGOING_ACTIVITY\",\n\n\tINVOICE_FINALISED: \"INVOICE_INVALID_STATE\",\n\tINVOICE_INVALID_STATUS: \"INVOICE_INVALID_STATE\",\n\tINVOICE_CONSENT_CONSUMED: \"INVOICE_INVALID_STATE\",\n\tINVOICE_CONSENT_EXPIRED: \"INVOICE_INVALID_STATE\",\n\tINVOICE_TOTAL_MISMATCH_PAYMENT_AMOUNT_SUM: \"INVOICE_INVALID_STATE\",\n\tINVOICE_PAYMENT_COUNT_NOT_MATCHED: \"INVOICE_INVALID_STATE\",\n\n\tDUPLICATE_PAYMENT: \"PAYMENT_DUPLICATE\",\n\tPAYMENT_IN_PROGRESS: \"PAYMENT_INVALID_STATE\",\n\tPAYMENT_FINALIZED: \"PAYMENT_INVALID_STATE\",\n\tPAYMEND_UNPAID: \"PAYMENT_INVALID_STATE\",\n\tINVALID_STATUS: \"PAYMENT_INVALID_STATE\",\n\n\tPAYMENT_REFUNDED_ALREADY: \"PAYMENT_ALREADY_REFUNDED\",\n\tPAYMENT_REFUNDED_FAILED: \"PAYMENT_REFUND_FAILED\",\n\n\tPAYMENT_GATEWAY_DECLINED: \"PAYMENT_GATEWAY_DECLINED\",\n\tINSUFFICIENT_FUNDS: \"PAYMENT_GATEWAY_DECLINED\",\n\tMOYASAR_PAYMENT_INFO_MISMATCH: \"PAYMENT_GATEWAY_DECLINED\",\n\tMOYASAR_BAD_REQUEST: \"PAYMENT_GATEWAY_DECLINED\",\n\tMOYASAR_UNAUTHORIZED: \"PAYMENT_GATEWAY_DECLINED\",\n\tMOYASAR_FORBIDDEN: \"PAYMENT_GATEWAY_DECLINED\",\n\tMOYASAR_NOT_FOUND: \"PAYMENT_GATEWAY_DECLINED\",\n\tMOYASAR_METHOD_NOT_ALLOWED: \"PAYMENT_GATEWAY_DECLINED\",\n\n\tMOYASAR_TIMEOUT: \"PAYMENT_GATEWAY_UNAVAILABLE\",\n\tMOYASAR_TOO_MANY_REQUESTS: \"PAYMENT_GATEWAY_UNAVAILABLE\",\n\tMOYASAR_INTERNAL_SERVER_ERROR: \"PAYMENT_GATEWAY_UNAVAILABLE\",\n\tMOYASAR_SERVICE_UNAVAILABLE: \"PAYMENT_GATEWAY_UNAVAILABLE\",\n\n\tMOYASAR_INVALID_CARD_TOKEN: \"PAYMENT_METHOD_INVALID\",\n\tMANUAL_INVOICE_CARD_ID: \"PAYMENT_METHOD_INVALID\",\n\tONE_OFF_INVALID_PAYMENT_FLOW: \"PAYMENT_METHOD_INVALID\",\n\tONE_OFF_CARD_ID: \"PAYMENT_METHOD_INVALID\",\n\tAUTO_INVOICE_MISSING_CARD_ID: \"PAYMENT_METHOD_INVALID\",\n\tPAYMENT_FLOW_NOT_ALLOWED: \"PAYMENT_METHOD_INVALID\",\n\n\tPRODUCT_USED_IN_FINALIZED_INVOICE: \"PRODUCT_LOCKED\",\n\tCOUPON_USED_IN_FINALIZED_INVOICE: \"COUPON_LOCKED\",\n\n\tSUBSCRIPTION_ALREADY_ACTIVE: \"SUBSCRIPTION_ALREADY_ACTIVE\",\n\tSUBSCRIPTION_NOT_FOUND: \"SUBSCRIPTION_NOT_FOUND\",\n\tSUBSCRIPTION_ALREADY_CANCELED: \"SUBSCRIPTION_ALREADY_CANCELED\",\n\tSUBSCRIPTION_ALREADY_FROZEN: \"SUBSCRIPTION_ALREADY_FROZEN\",\n\tSUBSCRIPTION_NOT_FROZEN: \"SUBSCRIPTION_FREEZE_NOT_ACTIVE\",\n\tSUBSCRIPTION_EXPIRED: \"SUBSCRIPTION_EXPIRED\",\n};\n\nexport function mapToErrorCode(\n\trawCode: string | undefined,\n\tstatus: number | undefined,\n): StreamPayErrorCode {\n\tif (rawCode && rawCode in RAW_CODE_MAP) {\n\t\tconst mapped = RAW_CODE_MAP[rawCode];\n\t\tif (mapped) return mapped;\n\t}\n\tif (status === 401) return \"UNAUTHORIZED\";\n\tif (status === 403) return \"FORBIDDEN\";\n\tif (status === 404) return \"NOT_FOUND\";\n\tif (status === 422) return \"VALIDATION_ERROR\";\n\treturn \"UNKNOWN\";\n}\n","import type { BetterAuthClientPlugin } from \"better-auth\";\nimport { $ERROR_CODES } from \"./error-codes\";\nimport type { streampay } from \"./streampay\";\n\nexport { $ERROR_CODES, type StreamPayErrorCode } from \"./error-codes\";\n\nexport const streampayClient = () => {\n\treturn {\n\t\tid: \"streampay-client\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof streampay>,\n\t\tpathMethods: { \"/consumer/portal/session\": \"POST\", \"/checkout\": \"POST\" },\n\t\t$ERROR_CODES,\n\t} satisfies BetterAuthClientPlugin;\n};\n"]}
|