@better-auth/stripe 1.2.0-beta.18
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/.turbo/turbo-build.log +17 -0
- package/LICENSE.md +17 -0
- package/build.config.ts +12 -0
- package/dist/client.cjs +10 -0
- package/dist/client.d.cts +25 -0
- package/dist/client.d.mts +25 -0
- package/dist/client.d.ts +25 -0
- package/dist/client.mjs +8 -0
- package/dist/index.cjs +779 -0
- package/dist/index.d.cts +884 -0
- package/dist/index.d.mts +884 -0
- package/dist/index.d.ts +884 -0
- package/dist/index.mjs +777 -0
- package/package.json +52 -0
- package/src/client.ts +31 -0
- package/src/hooks.ts +180 -0
- package/src/index.ts +642 -0
- package/src/schema.ts +62 -0
- package/src/stripe.test.ts +456 -0
- package/src/types.ts +323 -0
- package/src/utils.ts +22 -0
- package/tsconfig.json +20 -0
- package/vitest.config.ts +10 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import type { Session, User } from "better-auth";
|
|
2
|
+
import type Stripe from "stripe";
|
|
3
|
+
|
|
4
|
+
export type Plan = {
|
|
5
|
+
/**
|
|
6
|
+
* Monthly price id
|
|
7
|
+
*/
|
|
8
|
+
priceId?: string;
|
|
9
|
+
/**
|
|
10
|
+
* To use lookup key instead of price id
|
|
11
|
+
*
|
|
12
|
+
* https://docs.stripe.com/products-prices/
|
|
13
|
+
* manage-prices#lookup-keys
|
|
14
|
+
*/
|
|
15
|
+
lookupKey?: string;
|
|
16
|
+
/**
|
|
17
|
+
* A yearly discount price id
|
|
18
|
+
*
|
|
19
|
+
* useful when you want to offer a discount for
|
|
20
|
+
* yearly subscription
|
|
21
|
+
*/
|
|
22
|
+
annualDiscountPriceId?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Plan name
|
|
25
|
+
*/
|
|
26
|
+
name: string;
|
|
27
|
+
/**
|
|
28
|
+
* Limits for the plan
|
|
29
|
+
*/
|
|
30
|
+
limits?: Record<string, number>;
|
|
31
|
+
/**
|
|
32
|
+
* Plan group name
|
|
33
|
+
*
|
|
34
|
+
* useful when you want to group plans or
|
|
35
|
+
* when a user can subscribe to multiple plans.
|
|
36
|
+
*/
|
|
37
|
+
group?: string;
|
|
38
|
+
/**
|
|
39
|
+
* Free trial days
|
|
40
|
+
*/
|
|
41
|
+
freeTrial?: {
|
|
42
|
+
/**
|
|
43
|
+
* Number of days
|
|
44
|
+
*/
|
|
45
|
+
days: number;
|
|
46
|
+
/**
|
|
47
|
+
* Only available for new users or users without existing subscription
|
|
48
|
+
*
|
|
49
|
+
* @default true
|
|
50
|
+
*/
|
|
51
|
+
forNewUsersOnly?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* A function that will be called when the trial
|
|
54
|
+
* starts.
|
|
55
|
+
*
|
|
56
|
+
* @param subscription
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
onTrialStart?: (subscription: Subscription) => Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* A function that will be called when the trial
|
|
62
|
+
* ends
|
|
63
|
+
*
|
|
64
|
+
* @param subscription - Subscription
|
|
65
|
+
* @returns
|
|
66
|
+
*/
|
|
67
|
+
onTrialEnd?: (
|
|
68
|
+
data: {
|
|
69
|
+
subscription: Subscription;
|
|
70
|
+
user: User & Record<string, any>;
|
|
71
|
+
},
|
|
72
|
+
request?: Request,
|
|
73
|
+
) => Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* A function that will be called when the trial
|
|
76
|
+
* expired.
|
|
77
|
+
* @param subscription - Subscription
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
onTrialExpired?: (subscription: Subscription) => Promise<void>;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export interface Subscription {
|
|
85
|
+
/**
|
|
86
|
+
* Database identifier
|
|
87
|
+
*/
|
|
88
|
+
id: string;
|
|
89
|
+
/**
|
|
90
|
+
* The plan name
|
|
91
|
+
*/
|
|
92
|
+
plan: string;
|
|
93
|
+
/**
|
|
94
|
+
* Stripe customer id
|
|
95
|
+
*/
|
|
96
|
+
stripeCustomerId?: string;
|
|
97
|
+
/**
|
|
98
|
+
* Stripe subscription id
|
|
99
|
+
*/
|
|
100
|
+
stripeSubscriptionId?: string;
|
|
101
|
+
/**
|
|
102
|
+
* Trial start date
|
|
103
|
+
*/
|
|
104
|
+
trialStart?: Date;
|
|
105
|
+
/**
|
|
106
|
+
* Trial end date
|
|
107
|
+
*/
|
|
108
|
+
trialEnd?: Date;
|
|
109
|
+
/**
|
|
110
|
+
* Price Id for the subscription
|
|
111
|
+
*/
|
|
112
|
+
priceId?: string;
|
|
113
|
+
/**
|
|
114
|
+
* To what reference id the subscription belongs to
|
|
115
|
+
* @example
|
|
116
|
+
* - userId for a user
|
|
117
|
+
* - workspace id for a saas platform
|
|
118
|
+
* - website id for a hosting platform
|
|
119
|
+
*
|
|
120
|
+
* @default - userId
|
|
121
|
+
*/
|
|
122
|
+
referenceId: string;
|
|
123
|
+
/**
|
|
124
|
+
* Subscription status
|
|
125
|
+
*/
|
|
126
|
+
status:
|
|
127
|
+
| "active"
|
|
128
|
+
| "canceled"
|
|
129
|
+
| "incomplete"
|
|
130
|
+
| "incomplete_expired"
|
|
131
|
+
| "past_due"
|
|
132
|
+
| "paused"
|
|
133
|
+
| "trialing"
|
|
134
|
+
| "unpaid";
|
|
135
|
+
/**
|
|
136
|
+
* The billing cycle start date
|
|
137
|
+
*/
|
|
138
|
+
periodStart?: Date;
|
|
139
|
+
/**
|
|
140
|
+
* The billing cycle end date
|
|
141
|
+
*/
|
|
142
|
+
periodEnd?: Date;
|
|
143
|
+
/**
|
|
144
|
+
* Cancel at period end
|
|
145
|
+
*/
|
|
146
|
+
cancelAtPeriodEnd?: boolean;
|
|
147
|
+
/**
|
|
148
|
+
* A field to group subscriptions so you can have multiple subscriptions
|
|
149
|
+
* for one reference id
|
|
150
|
+
*/
|
|
151
|
+
groupId?: string;
|
|
152
|
+
/**
|
|
153
|
+
* Number of seats for the subscription (useful for team plans)
|
|
154
|
+
*/
|
|
155
|
+
seats?: number;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export interface StripeOptions {
|
|
159
|
+
/**
|
|
160
|
+
* Stripe Client
|
|
161
|
+
*/
|
|
162
|
+
stripeClient: Stripe;
|
|
163
|
+
/**
|
|
164
|
+
* Stripe Webhook Secret
|
|
165
|
+
*
|
|
166
|
+
* @description Stripe webhook secret key
|
|
167
|
+
*/
|
|
168
|
+
stripeWebhookSecret: string;
|
|
169
|
+
/**
|
|
170
|
+
* Enable customer creation when a user signs up
|
|
171
|
+
*/
|
|
172
|
+
createCustomerOnSignUp?: boolean;
|
|
173
|
+
/**
|
|
174
|
+
* A callback to run after a customer has been created
|
|
175
|
+
* @param customer - Customer Data
|
|
176
|
+
* @param stripeCustomer - Stripe Customer Data
|
|
177
|
+
* @returns
|
|
178
|
+
*/
|
|
179
|
+
onCustomerCreate?: (
|
|
180
|
+
data: {
|
|
181
|
+
customer: Customer;
|
|
182
|
+
stripeCustomer: Stripe.Customer;
|
|
183
|
+
user: User;
|
|
184
|
+
},
|
|
185
|
+
request?: Request,
|
|
186
|
+
) => Promise<void>;
|
|
187
|
+
/**
|
|
188
|
+
* A custom function to get the customer create
|
|
189
|
+
* params
|
|
190
|
+
* @param data - data containing user and session
|
|
191
|
+
* @returns
|
|
192
|
+
*/
|
|
193
|
+
getCustomerCreateParams?: (
|
|
194
|
+
data: {
|
|
195
|
+
user: User;
|
|
196
|
+
session: Session;
|
|
197
|
+
},
|
|
198
|
+
request?: Request,
|
|
199
|
+
) => Promise<{}>;
|
|
200
|
+
/**
|
|
201
|
+
* Subscriptions
|
|
202
|
+
*/
|
|
203
|
+
subscription?: {
|
|
204
|
+
enabled: boolean;
|
|
205
|
+
/**
|
|
206
|
+
* Subscription Configuration
|
|
207
|
+
*/
|
|
208
|
+
/**
|
|
209
|
+
* List of plan
|
|
210
|
+
*/
|
|
211
|
+
plans: Plan[] | (() => Promise<Plan[]>);
|
|
212
|
+
/**
|
|
213
|
+
* Require email verification before a user is allowed to upgrade
|
|
214
|
+
* their subscriptions
|
|
215
|
+
*
|
|
216
|
+
* @default false
|
|
217
|
+
*/
|
|
218
|
+
requireEmailVerification?: boolean;
|
|
219
|
+
/**
|
|
220
|
+
* A callback to run after a user has subscribed to a package
|
|
221
|
+
* @param event - Stripe Event
|
|
222
|
+
* @param subscription - Subscription Data
|
|
223
|
+
* @returns
|
|
224
|
+
*/
|
|
225
|
+
onSubscriptionComplete?: (
|
|
226
|
+
data: {
|
|
227
|
+
event: Stripe.Event;
|
|
228
|
+
stripeSubscription: Stripe.Subscription;
|
|
229
|
+
subscription: Subscription;
|
|
230
|
+
plan: Plan;
|
|
231
|
+
},
|
|
232
|
+
request?: Request,
|
|
233
|
+
) => Promise<void>;
|
|
234
|
+
/**
|
|
235
|
+
* A callback to run after a user is about to cancel their subscription
|
|
236
|
+
* @returns
|
|
237
|
+
*/
|
|
238
|
+
onSubscriptionUpdate?: (data: {
|
|
239
|
+
event: Stripe.Event;
|
|
240
|
+
subscription: Subscription;
|
|
241
|
+
}) => Promise<void>;
|
|
242
|
+
/**
|
|
243
|
+
* A callback to run after a user is about to cancel their subscription
|
|
244
|
+
* @returns
|
|
245
|
+
*/
|
|
246
|
+
onSubscriptionCancel?: (data: {
|
|
247
|
+
event: Stripe.Event;
|
|
248
|
+
subscription: Subscription;
|
|
249
|
+
stripeSubscription: Stripe.Subscription;
|
|
250
|
+
cancellationDetails?: Stripe.Subscription.CancellationDetails;
|
|
251
|
+
}) => Promise<void>;
|
|
252
|
+
/**
|
|
253
|
+
* A function to check if the reference id is valid
|
|
254
|
+
* and belongs to the user
|
|
255
|
+
*
|
|
256
|
+
* @param data - data containing user, session and referenceId
|
|
257
|
+
* @param request - Request Object
|
|
258
|
+
* @returns
|
|
259
|
+
*/
|
|
260
|
+
authorizeReference?: (
|
|
261
|
+
data: {
|
|
262
|
+
user: User & Record<string, any>;
|
|
263
|
+
session: Session & Record<string, any>;
|
|
264
|
+
referenceId: string;
|
|
265
|
+
action:
|
|
266
|
+
| "upgrade-subscription"
|
|
267
|
+
| "list-subscription"
|
|
268
|
+
| "cancel-subscription";
|
|
269
|
+
},
|
|
270
|
+
request?: Request,
|
|
271
|
+
) => Promise<boolean>;
|
|
272
|
+
/**
|
|
273
|
+
* A callback to run after a user has deleted their subscription
|
|
274
|
+
* @returns
|
|
275
|
+
*/
|
|
276
|
+
onSubscriptionDeleted?: (data: {
|
|
277
|
+
event: Stripe.Event;
|
|
278
|
+
stripeSubscription: Stripe.Subscription;
|
|
279
|
+
subscription: Subscription;
|
|
280
|
+
}) => Promise<void>;
|
|
281
|
+
/**
|
|
282
|
+
* parameters for session create params
|
|
283
|
+
*
|
|
284
|
+
* @param data - data containing user, session and plan
|
|
285
|
+
* @param request - Request Object
|
|
286
|
+
*/
|
|
287
|
+
getCheckoutSessionParams?: (
|
|
288
|
+
data: {
|
|
289
|
+
user: User & Record<string, any>;
|
|
290
|
+
session: Session & Record<string, any>;
|
|
291
|
+
plan: Plan;
|
|
292
|
+
subscription: Subscription;
|
|
293
|
+
},
|
|
294
|
+
request?: Request,
|
|
295
|
+
) =>
|
|
296
|
+
| Promise<{
|
|
297
|
+
params?: Stripe.Checkout.SessionCreateParams;
|
|
298
|
+
options?: Stripe.RequestOptions;
|
|
299
|
+
}>
|
|
300
|
+
| {
|
|
301
|
+
params?: Stripe.Checkout.SessionCreateParams;
|
|
302
|
+
options?: Stripe.RequestOptions;
|
|
303
|
+
};
|
|
304
|
+
/**
|
|
305
|
+
* Enable organization subscription
|
|
306
|
+
*/
|
|
307
|
+
organization?: {
|
|
308
|
+
enabled: boolean;
|
|
309
|
+
};
|
|
310
|
+
};
|
|
311
|
+
onEvent?: (event: Stripe.Event) => Promise<void>;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export interface Customer {
|
|
315
|
+
id: string;
|
|
316
|
+
stripeCustomerId?: string;
|
|
317
|
+
userId: string;
|
|
318
|
+
createdAt: Date;
|
|
319
|
+
updatedAt: Date;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export interface InputSubscription extends Omit<Subscription, "id"> {}
|
|
323
|
+
export interface InputCustomer extends Omit<Customer, "id"> {}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { StripeOptions } from "./types";
|
|
2
|
+
|
|
3
|
+
export async function getPlans(options: StripeOptions) {
|
|
4
|
+
return typeof options?.subscription?.plans === "function"
|
|
5
|
+
? await options.subscription?.plans()
|
|
6
|
+
: options.subscription?.plans;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export async function getPlanByPriceId(
|
|
10
|
+
options: StripeOptions,
|
|
11
|
+
priceId: string,
|
|
12
|
+
) {
|
|
13
|
+
return await getPlans(options).then((res) =>
|
|
14
|
+
res?.find((plan) => plan.priceId === priceId),
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function getPlanByName(options: StripeOptions, name: string) {
|
|
19
|
+
return await getPlans(options).then((res) =>
|
|
20
|
+
res?.find((plan) => plan.name.toLowerCase() === name.toLowerCase()),
|
|
21
|
+
);
|
|
22
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"esModuleInterop": true,
|
|
4
|
+
"skipLibCheck": true,
|
|
5
|
+
"target": "es2022",
|
|
6
|
+
"allowJs": true,
|
|
7
|
+
"resolveJsonModule": true,
|
|
8
|
+
"module": "ESNext",
|
|
9
|
+
"noEmit": true,
|
|
10
|
+
"moduleResolution": "Bundler",
|
|
11
|
+
"moduleDetection": "force",
|
|
12
|
+
"isolatedModules": true,
|
|
13
|
+
"verbatimModuleSyntax": true,
|
|
14
|
+
"strict": true,
|
|
15
|
+
"noImplicitOverride": true,
|
|
16
|
+
"noFallthroughCasesInSwitch": true
|
|
17
|
+
},
|
|
18
|
+
"exclude": ["node_modules", "dist"],
|
|
19
|
+
"include": ["src"]
|
|
20
|
+
}
|