@faire-auth/stripe 1.0.3 → 1.1.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.
package/dist/index.d.cts CHANGED
@@ -1,2 +1,315 @@
1
- import { i as Subscription, n as stripe, r as StripePlan, t as StripePlugin } from "./index-6UTHxaqg.cjs";
2
- export { StripePlan, StripePlugin, Subscription, stripe };
1
+ import { ContextVars, InferOptionSchema } from "faire-auth";
2
+ import { Context } from "@faire-auth/core/types";
3
+ import Stripe from "stripe";
4
+ import { Session, User } from "faire-auth/db";
5
+ declare const subscriptions: AuthPluginSchema;
6
+ declare const user: AuthPluginSchema;
7
+ type StripePlan = {
8
+ /**
9
+ * Monthly price id
10
+ */
11
+ priceId?: string;
12
+ /**
13
+ * To use lookup key instead of price id
14
+ *
15
+ * https://docs.stripe.com/products-prices/
16
+ * manage-prices#lookup-keys
17
+ */
18
+ lookupKey?: string;
19
+ /**
20
+ * A yearly discount price id
21
+ *
22
+ * useful when you want to offer a discount for
23
+ * yearly subscription
24
+ */
25
+ annualDiscountPriceId?: string;
26
+ /**
27
+ * To use lookup key instead of price id
28
+ *
29
+ * https://docs.stripe.com/products-prices/
30
+ * manage-prices#lookup-keys
31
+ */
32
+ annualDiscountLookupKey?: string;
33
+ /**
34
+ * Plan name
35
+ */
36
+ name: string;
37
+ /**
38
+ * Limits for the plan
39
+ */
40
+ limits?: Record<string, number>;
41
+ /**
42
+ * Plan group name
43
+ *
44
+ * useful when you want to group plans or
45
+ * when a user can subscribe to multiple plans.
46
+ */
47
+ group?: string;
48
+ /**
49
+ * Free trial days
50
+ */
51
+ freeTrial?: {
52
+ /**
53
+ * Number of days
54
+ */
55
+ days: number;
56
+ /**
57
+ * A function that will be called when the trial
58
+ * starts.
59
+ *
60
+ * @param subscription
61
+ * @returns
62
+ */
63
+ onTrialStart?: (subscription: Subscription) => Promise<void>;
64
+ /**
65
+ * A function that will be called when the trial
66
+ * ends
67
+ *
68
+ * @param subscription - Subscription
69
+ * @returns
70
+ */
71
+ onTrialEnd?: <V extends object>(data: {
72
+ subscription: Subscription;
73
+ }, ctx: Context<ContextVars<V>>) => Promise<void>;
74
+ /**
75
+ * A function that will be called when the trial
76
+ * expired.
77
+ * @param subscription - Subscription
78
+ * @returns
79
+ */
80
+ onTrialExpired?: <V extends object>(subscription: Subscription, ctx: Context<ContextVars<V>>) => Promise<void>;
81
+ };
82
+ };
83
+ interface Subscription {
84
+ /**
85
+ * Database identifier
86
+ */
87
+ id: string;
88
+ /**
89
+ * The plan name
90
+ */
91
+ plan: string;
92
+ /**
93
+ * Stripe customer id
94
+ */
95
+ stripeCustomerId?: string;
96
+ /**
97
+ * Stripe subscription id
98
+ */
99
+ stripeSubscriptionId?: string;
100
+ /**
101
+ * Trial start date
102
+ */
103
+ trialStart?: Date;
104
+ /**
105
+ * Trial end date
106
+ */
107
+ trialEnd?: Date;
108
+ /**
109
+ * Price Id for the subscription
110
+ */
111
+ priceId?: string;
112
+ /**
113
+ * To what reference id the subscription belongs to
114
+ * @example
115
+ * - userId for a user
116
+ * - workspace id for a saas platform
117
+ * - website id for a hosting platform
118
+ *
119
+ * @default - userId
120
+ */
121
+ referenceId: string;
122
+ /**
123
+ * Subscription status
124
+ */
125
+ status: "active" | "canceled" | "incomplete" | "incomplete_expired" | "past_due" | "paused" | "trialing" | "unpaid";
126
+ /**
127
+ * The billing cycle start date
128
+ */
129
+ periodStart?: Date;
130
+ /**
131
+ * The billing cycle end date
132
+ */
133
+ periodEnd?: Date;
134
+ /**
135
+ * Whether this subscription will (if status=active)
136
+ * or did (if status=canceled) cancel at the end of the current billing period.
137
+ */
138
+ cancelAtPeriodEnd?: boolean;
139
+ /**
140
+ * If the subscription is scheduled to be canceled,
141
+ * this is the time at which the cancellation will take effect.
142
+ */
143
+ cancelAt?: Date;
144
+ /**
145
+ * If the subscription has been canceled, this is the time when it was canceled.
146
+ *
147
+ * Note: If the subscription was canceled with `cancel_at_period_end`,
148
+ * this reflects the cancellation request time, not when the subscription actually ends.
149
+ */
150
+ canceledAt?: Date;
151
+ /**
152
+ * If the subscription has ended, the date the subscription ended.
153
+ */
154
+ endedAt?: Date;
155
+ /**
156
+ * A field to group subscriptions so you can have multiple subscriptions
157
+ * for one reference id
158
+ */
159
+ groupId?: string;
160
+ /**
161
+ * Number of seats for the subscription (useful for team plans)
162
+ */
163
+ seats?: number;
164
+ }
165
+ interface SubscriptionOptions {
166
+ enabled: boolean;
167
+ /**
168
+ * Subscription Configuration
169
+ */
170
+ /**
171
+ * List of plan
172
+ */
173
+ plans: StripePlan[] | (() => StripePlan[] | Promise<StripePlan[]>);
174
+ /**
175
+ * Require email verification before a user is allowed to upgrade
176
+ * their subscriptions
177
+ *
178
+ * @default false
179
+ */
180
+ requireEmailVerification?: boolean;
181
+ /**
182
+ * A callback to run after a user has subscribed to a package
183
+ * @param event - Stripe Event
184
+ * @param subscription - Subscription Data
185
+ * @returns
186
+ */
187
+ onSubscriptionComplete?: <V extends object>(data: {
188
+ event: Stripe.Event;
189
+ stripeSubscription: Stripe.Subscription;
190
+ subscription: Subscription;
191
+ plan: StripePlan;
192
+ }, ctx: Context<ContextVars<V>>) => Promise<void>;
193
+ /**
194
+ * A callback to run after a user is about to cancel their subscription
195
+ * @returns
196
+ */
197
+ onSubscriptionUpdate?: (data: {
198
+ event: Stripe.Event;
199
+ subscription: Subscription;
200
+ }) => Promise<void>;
201
+ /**
202
+ * A callback to run after a user is about to cancel their subscription
203
+ * @returns
204
+ */
205
+ onSubscriptionCancel?: (data: {
206
+ event?: Stripe.Event | undefined;
207
+ subscription: Subscription;
208
+ stripeSubscription: Stripe.Subscription;
209
+ cancellationDetails?: Stripe.Subscription.CancellationDetails | null;
210
+ }) => Promise<void>;
211
+ /**
212
+ * A function to check if the reference id is valid
213
+ * and belongs to the user
214
+ *
215
+ * @param data - data containing user, session and referenceId
216
+ * @param ctx - the context object
217
+ * @returns
218
+ */
219
+ authorizeReference?: <V extends object>(data: {
220
+ user: User;
221
+ session: Session;
222
+ referenceId: string;
223
+ action: "upgrade-subscription" | "list-subscription" | "cancel-subscription" | "restore-subscription" | "billing-portal";
224
+ }, ctx: Context<ContextVars<V>>) => Promise<boolean>;
225
+ /**
226
+ * A callback to run after a user has deleted their subscription
227
+ * @returns
228
+ */
229
+ onSubscriptionDeleted?: (data: {
230
+ event: Stripe.Event;
231
+ stripeSubscription: Stripe.Subscription;
232
+ subscription: Subscription;
233
+ }) => Promise<void>;
234
+ /**
235
+ * parameters for session create params
236
+ *
237
+ * @param data - data containing user, session and plan
238
+ * @param ctx - the context object
239
+ */
240
+ getCheckoutSessionParams?: <V extends object>(data: {
241
+ user: User;
242
+ session: Session;
243
+ plan: StripePlan;
244
+ subscription: Subscription;
245
+ }, ctx: Context<ContextVars<V>>) => Promise<{
246
+ params?: Stripe.Checkout.SessionCreateParams;
247
+ options?: Stripe.RequestOptions;
248
+ }> | {
249
+ params?: Stripe.Checkout.SessionCreateParams;
250
+ options?: Stripe.RequestOptions;
251
+ };
252
+ /**
253
+ * Enable organization subscription
254
+ */
255
+ organization?: {
256
+ enabled: boolean;
257
+ };
258
+ }
259
+ interface StripeOptions {
260
+ /**
261
+ * Stripe Client
262
+ */
263
+ stripeClient: Stripe;
264
+ /**
265
+ * Stripe Webhook Secret
266
+ *
267
+ * @description Stripe webhook secret key
268
+ */
269
+ stripeWebhookSecret: string;
270
+ /**
271
+ * Enable customer creation when a user signs up
272
+ */
273
+ createCustomerOnSignUp?: boolean;
274
+ /**
275
+ * A callback to run after a customer has been created
276
+ * @param customer - Customer Data
277
+ * @param stripeCustomer - Stripe Customer Data
278
+ * @returns
279
+ */
280
+ onCustomerCreate?: <V extends object>(data: {
281
+ stripeCustomer: Stripe.Customer;
282
+ user: User;
283
+ }, ctx: Context<ContextVars<V>>) => Promise<void>;
284
+ /**
285
+ * A custom function to get the customer create
286
+ * params
287
+ * @param data - data containing user and session
288
+ * @returns
289
+ */
290
+ getCustomerCreateParams?: <V extends object>(data: {
291
+ user: User;
292
+ session: Session;
293
+ }, ctx: Context<ContextVars<V>>) => Promise<{}>;
294
+ /**
295
+ * Subscriptions
296
+ */
297
+ subscription?: {
298
+ enabled: false;
299
+ } | ({
300
+ enabled: true;
301
+ } & SubscriptionOptions);
302
+ /**
303
+ * A callback to run after a stripe event is received
304
+ * @param event - Stripe Event
305
+ * @returns
306
+ */
307
+ onEvent?: (event: Stripe.Event) => Promise<void>;
308
+ /**
309
+ * Schema for the stripe plugin
310
+ */
311
+ schema?: InferOptionSchema<typeof subscriptions & typeof user>;
312
+ }
313
+ export declare const stripe: <O extends StripeOptions>(options: O) => FaireAuthPlugin;
314
+ export type StripePlugin<O extends StripeOptions> = ReturnType<typeof stripe<O>>;
315
+ export type { StripePlan, Subscription };
package/dist/index.d.mts CHANGED
@@ -1,2 +1,315 @@
1
- import { i as Subscription, n as stripe, r as StripePlan, t as StripePlugin } from "./index-CYV4awaB.mjs";
2
- export { StripePlan, StripePlugin, Subscription, stripe };
1
+ import { ContextVars, InferOptionSchema } from "faire-auth";
2
+ import Stripe from "stripe";
3
+ import { Session, User } from "faire-auth/db";
4
+ import { Context } from "@faire-auth/core/types";
5
+ declare const subscriptions: AuthPluginSchema;
6
+ declare const user: AuthPluginSchema;
7
+ type StripePlan = {
8
+ /**
9
+ * Monthly price id
10
+ */
11
+ priceId?: string;
12
+ /**
13
+ * To use lookup key instead of price id
14
+ *
15
+ * https://docs.stripe.com/products-prices/
16
+ * manage-prices#lookup-keys
17
+ */
18
+ lookupKey?: string;
19
+ /**
20
+ * A yearly discount price id
21
+ *
22
+ * useful when you want to offer a discount for
23
+ * yearly subscription
24
+ */
25
+ annualDiscountPriceId?: string;
26
+ /**
27
+ * To use lookup key instead of price id
28
+ *
29
+ * https://docs.stripe.com/products-prices/
30
+ * manage-prices#lookup-keys
31
+ */
32
+ annualDiscountLookupKey?: string;
33
+ /**
34
+ * Plan name
35
+ */
36
+ name: string;
37
+ /**
38
+ * Limits for the plan
39
+ */
40
+ limits?: Record<string, number>;
41
+ /**
42
+ * Plan group name
43
+ *
44
+ * useful when you want to group plans or
45
+ * when a user can subscribe to multiple plans.
46
+ */
47
+ group?: string;
48
+ /**
49
+ * Free trial days
50
+ */
51
+ freeTrial?: {
52
+ /**
53
+ * Number of days
54
+ */
55
+ days: number;
56
+ /**
57
+ * A function that will be called when the trial
58
+ * starts.
59
+ *
60
+ * @param subscription
61
+ * @returns
62
+ */
63
+ onTrialStart?: (subscription: Subscription) => Promise<void>;
64
+ /**
65
+ * A function that will be called when the trial
66
+ * ends
67
+ *
68
+ * @param subscription - Subscription
69
+ * @returns
70
+ */
71
+ onTrialEnd?: <V extends object>(data: {
72
+ subscription: Subscription;
73
+ }, ctx: Context<ContextVars<V>>) => Promise<void>;
74
+ /**
75
+ * A function that will be called when the trial
76
+ * expired.
77
+ * @param subscription - Subscription
78
+ * @returns
79
+ */
80
+ onTrialExpired?: <V extends object>(subscription: Subscription, ctx: Context<ContextVars<V>>) => Promise<void>;
81
+ };
82
+ };
83
+ interface Subscription {
84
+ /**
85
+ * Database identifier
86
+ */
87
+ id: string;
88
+ /**
89
+ * The plan name
90
+ */
91
+ plan: string;
92
+ /**
93
+ * Stripe customer id
94
+ */
95
+ stripeCustomerId?: string;
96
+ /**
97
+ * Stripe subscription id
98
+ */
99
+ stripeSubscriptionId?: string;
100
+ /**
101
+ * Trial start date
102
+ */
103
+ trialStart?: Date;
104
+ /**
105
+ * Trial end date
106
+ */
107
+ trialEnd?: Date;
108
+ /**
109
+ * Price Id for the subscription
110
+ */
111
+ priceId?: string;
112
+ /**
113
+ * To what reference id the subscription belongs to
114
+ * @example
115
+ * - userId for a user
116
+ * - workspace id for a saas platform
117
+ * - website id for a hosting platform
118
+ *
119
+ * @default - userId
120
+ */
121
+ referenceId: string;
122
+ /**
123
+ * Subscription status
124
+ */
125
+ status: "active" | "canceled" | "incomplete" | "incomplete_expired" | "past_due" | "paused" | "trialing" | "unpaid";
126
+ /**
127
+ * The billing cycle start date
128
+ */
129
+ periodStart?: Date;
130
+ /**
131
+ * The billing cycle end date
132
+ */
133
+ periodEnd?: Date;
134
+ /**
135
+ * Whether this subscription will (if status=active)
136
+ * or did (if status=canceled) cancel at the end of the current billing period.
137
+ */
138
+ cancelAtPeriodEnd?: boolean;
139
+ /**
140
+ * If the subscription is scheduled to be canceled,
141
+ * this is the time at which the cancellation will take effect.
142
+ */
143
+ cancelAt?: Date;
144
+ /**
145
+ * If the subscription has been canceled, this is the time when it was canceled.
146
+ *
147
+ * Note: If the subscription was canceled with `cancel_at_period_end`,
148
+ * this reflects the cancellation request time, not when the subscription actually ends.
149
+ */
150
+ canceledAt?: Date;
151
+ /**
152
+ * If the subscription has ended, the date the subscription ended.
153
+ */
154
+ endedAt?: Date;
155
+ /**
156
+ * A field to group subscriptions so you can have multiple subscriptions
157
+ * for one reference id
158
+ */
159
+ groupId?: string;
160
+ /**
161
+ * Number of seats for the subscription (useful for team plans)
162
+ */
163
+ seats?: number;
164
+ }
165
+ interface SubscriptionOptions {
166
+ enabled: boolean;
167
+ /**
168
+ * Subscription Configuration
169
+ */
170
+ /**
171
+ * List of plan
172
+ */
173
+ plans: StripePlan[] | (() => StripePlan[] | Promise<StripePlan[]>);
174
+ /**
175
+ * Require email verification before a user is allowed to upgrade
176
+ * their subscriptions
177
+ *
178
+ * @default false
179
+ */
180
+ requireEmailVerification?: boolean;
181
+ /**
182
+ * A callback to run after a user has subscribed to a package
183
+ * @param event - Stripe Event
184
+ * @param subscription - Subscription Data
185
+ * @returns
186
+ */
187
+ onSubscriptionComplete?: <V extends object>(data: {
188
+ event: Stripe.Event;
189
+ stripeSubscription: Stripe.Subscription;
190
+ subscription: Subscription;
191
+ plan: StripePlan;
192
+ }, ctx: Context<ContextVars<V>>) => Promise<void>;
193
+ /**
194
+ * A callback to run after a user is about to cancel their subscription
195
+ * @returns
196
+ */
197
+ onSubscriptionUpdate?: (data: {
198
+ event: Stripe.Event;
199
+ subscription: Subscription;
200
+ }) => Promise<void>;
201
+ /**
202
+ * A callback to run after a user is about to cancel their subscription
203
+ * @returns
204
+ */
205
+ onSubscriptionCancel?: (data: {
206
+ event?: Stripe.Event | undefined;
207
+ subscription: Subscription;
208
+ stripeSubscription: Stripe.Subscription;
209
+ cancellationDetails?: Stripe.Subscription.CancellationDetails | null;
210
+ }) => Promise<void>;
211
+ /**
212
+ * A function to check if the reference id is valid
213
+ * and belongs to the user
214
+ *
215
+ * @param data - data containing user, session and referenceId
216
+ * @param ctx - the context object
217
+ * @returns
218
+ */
219
+ authorizeReference?: <V extends object>(data: {
220
+ user: User;
221
+ session: Session;
222
+ referenceId: string;
223
+ action: "upgrade-subscription" | "list-subscription" | "cancel-subscription" | "restore-subscription" | "billing-portal";
224
+ }, ctx: Context<ContextVars<V>>) => Promise<boolean>;
225
+ /**
226
+ * A callback to run after a user has deleted their subscription
227
+ * @returns
228
+ */
229
+ onSubscriptionDeleted?: (data: {
230
+ event: Stripe.Event;
231
+ stripeSubscription: Stripe.Subscription;
232
+ subscription: Subscription;
233
+ }) => Promise<void>;
234
+ /**
235
+ * parameters for session create params
236
+ *
237
+ * @param data - data containing user, session and plan
238
+ * @param ctx - the context object
239
+ */
240
+ getCheckoutSessionParams?: <V extends object>(data: {
241
+ user: User;
242
+ session: Session;
243
+ plan: StripePlan;
244
+ subscription: Subscription;
245
+ }, ctx: Context<ContextVars<V>>) => Promise<{
246
+ params?: Stripe.Checkout.SessionCreateParams;
247
+ options?: Stripe.RequestOptions;
248
+ }> | {
249
+ params?: Stripe.Checkout.SessionCreateParams;
250
+ options?: Stripe.RequestOptions;
251
+ };
252
+ /**
253
+ * Enable organization subscription
254
+ */
255
+ organization?: {
256
+ enabled: boolean;
257
+ };
258
+ }
259
+ interface StripeOptions {
260
+ /**
261
+ * Stripe Client
262
+ */
263
+ stripeClient: Stripe;
264
+ /**
265
+ * Stripe Webhook Secret
266
+ *
267
+ * @description Stripe webhook secret key
268
+ */
269
+ stripeWebhookSecret: string;
270
+ /**
271
+ * Enable customer creation when a user signs up
272
+ */
273
+ createCustomerOnSignUp?: boolean;
274
+ /**
275
+ * A callback to run after a customer has been created
276
+ * @param customer - Customer Data
277
+ * @param stripeCustomer - Stripe Customer Data
278
+ * @returns
279
+ */
280
+ onCustomerCreate?: <V extends object>(data: {
281
+ stripeCustomer: Stripe.Customer;
282
+ user: User;
283
+ }, ctx: Context<ContextVars<V>>) => Promise<void>;
284
+ /**
285
+ * A custom function to get the customer create
286
+ * params
287
+ * @param data - data containing user and session
288
+ * @returns
289
+ */
290
+ getCustomerCreateParams?: <V extends object>(data: {
291
+ user: User;
292
+ session: Session;
293
+ }, ctx: Context<ContextVars<V>>) => Promise<{}>;
294
+ /**
295
+ * Subscriptions
296
+ */
297
+ subscription?: {
298
+ enabled: false;
299
+ } | ({
300
+ enabled: true;
301
+ } & SubscriptionOptions);
302
+ /**
303
+ * A callback to run after a stripe event is received
304
+ * @param event - Stripe Event
305
+ * @returns
306
+ */
307
+ onEvent?: (event: Stripe.Event) => Promise<void>;
308
+ /**
309
+ * Schema for the stripe plugin
310
+ */
311
+ schema?: InferOptionSchema<typeof subscriptions & typeof user>;
312
+ }
313
+ export declare const stripe: <O extends StripeOptions>(options: O) => FaireAuthPlugin;
314
+ export type StripePlugin<O extends StripeOptions> = ReturnType<typeof stripe<O>>;
315
+ export type { StripePlan, Subscription };