@better-auth/stripe 1.4.0-beta.12 → 1.4.0-beta.13

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.
@@ -1,23 +1,23 @@
1
1
 
2
- > @better-auth/stripe@1.4.0-beta.12 build /home/runner/work/better-auth/better-auth/packages/stripe
2
+ > @better-auth/stripe@1.4.0-beta.13 build /home/runner/work/better-auth/better-auth/packages/stripe
3
3
  > tsdown
4
4
 
5
- ℹ tsdown v0.15.6 powered by rolldown v1.0.0-beta.43
5
+ ℹ tsdown v0.15.9 powered by rolldown v1.0.0-beta.44
6
6
  ℹ Using tsdown config: /home/runner/work/better-auth/better-auth/packages/stripe/tsdown.config.ts
7
7
  ℹ entry: src/client.ts, src/index.ts
8
8
  ℹ tsconfig: tsconfig.json
9
9
  ℹ Build start
10
- ℹ [CJS] dist/index.cjs 38.27 kB │ gzip: 7.50 kB
10
+ ℹ [CJS] dist/index.cjs 38.46 kB │ gzip: 7.55 kB
11
11
  ℹ [CJS] dist/client.cjs  0.28 kB │ gzip: 0.20 kB
12
- ℹ [CJS] 2 files, total: 38.54 kB
13
- ℹ [ESM] dist/index.js 35.98 kB │ gzip: 6.97 kB
12
+ ℹ [CJS] 2 files, total: 38.73 kB
13
+ ℹ [ESM] dist/index.js 36.17 kB │ gzip: 7.02 kB
14
14
  ℹ [ESM] dist/client.js  0.26 kB │ gzip: 0.20 kB
15
15
  ℹ [ESM] dist/client.d.ts  0.60 kB │ gzip: 0.34 kB
16
- ℹ [ESM] dist/index.d.ts  0.12 kB │ gzip: 0.09 kB
17
- ℹ [ESM] dist/index-BMkRYEm9.d.ts 24.80 kB │ gzip: 4.88 kB
18
- ℹ [ESM] 5 files, total: 61.76 kB
19
- ℹ [CJS] dist/client.d.cts  0.60 kB │ gzip: 0.33 kB
20
- ℹ [CJS] dist/index.d.cts  0.12 kB │ gzip: 0.10 kB
21
- ℹ [CJS] dist/index-BOfvWLTo.d.cts 24.80 kB │ gzip: 4.88 kB
22
- ℹ [CJS] 3 files, total: 25.52 kB
23
- ✔ Build complete in 5874ms
16
+ ℹ [ESM] dist/index.d.ts  0.16 kB │ gzip: 0.12 kB
17
+ ℹ [ESM] dist/index-BSdp1oW9.d.ts 24.70 kB │ gzip: 4.92 kB
18
+ ℹ [ESM] 5 files, total: 61.91 kB
19
+ ℹ [CJS] dist/client.d.cts  0.61 kB │ gzip: 0.34 kB
20
+ ℹ [CJS] dist/index.d.cts  0.17 kB │ gzip: 0.13 kB
21
+ ℹ [CJS] dist/index-zlkU4RN-.d.cts 24.70 kB │ gzip: 4.92 kB
22
+ ℹ [CJS] 3 files, total: 25.48 kB
23
+ ✔ Build complete in 9414ms
package/dist/client.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { stripe } from "./index-BOfvWLTo.cjs";
1
+ import { n as stripe } from "./index-zlkU4RN-.cjs";
2
2
 
3
3
  //#region src/client.d.ts
4
4
  declare const stripeClient: <O extends {
package/dist/client.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { stripe } from "./index-BMkRYEm9.js";
1
+ import { n as stripe } from "./index-BSdp1oW9.js";
2
2
 
3
3
  //#region src/client.d.ts
4
4
  declare const stripeClient: <O extends {
@@ -209,6 +209,99 @@ interface Subscription {
209
209
  */
210
210
  seats?: number;
211
211
  }
212
+ type SubscriptionOptions = {
213
+ /**
214
+ * Subscription Configuration
215
+ */
216
+ /**
217
+ * List of plan
218
+ */
219
+ plans: StripePlan[] | (() => StripePlan[] | Promise<StripePlan[]>);
220
+ /**
221
+ * Require email verification before a user is allowed to upgrade
222
+ * their subscriptions
223
+ *
224
+ * @default false
225
+ */
226
+ requireEmailVerification?: boolean;
227
+ /**
228
+ * A callback to run after a user has subscribed to a package
229
+ * @param event - Stripe Event
230
+ * @param subscription - Subscription Data
231
+ * @returns
232
+ */
233
+ onSubscriptionComplete?: (data: {
234
+ event: Stripe.Event;
235
+ stripeSubscription: Stripe.Subscription;
236
+ subscription: Subscription;
237
+ plan: StripePlan;
238
+ }, ctx: GenericEndpointContext) => Promise<void>;
239
+ /**
240
+ * A callback to run after a user is about to cancel their subscription
241
+ * @returns
242
+ */
243
+ onSubscriptionUpdate?: (data: {
244
+ event: Stripe.Event;
245
+ subscription: Subscription;
246
+ }) => Promise<void>;
247
+ /**
248
+ * A callback to run after a user is about to cancel their subscription
249
+ * @returns
250
+ */
251
+ onSubscriptionCancel?: (data: {
252
+ event?: Stripe.Event;
253
+ subscription: Subscription;
254
+ stripeSubscription: Stripe.Subscription;
255
+ cancellationDetails?: Stripe.Subscription.CancellationDetails | null;
256
+ }) => Promise<void>;
257
+ /**
258
+ * A function to check if the reference id is valid
259
+ * and belongs to the user
260
+ *
261
+ * @param data - data containing user, session and referenceId
262
+ * @param ctx - the context object
263
+ * @returns
264
+ */
265
+ authorizeReference?: (data: {
266
+ user: User & Record<string, any>;
267
+ session: Session & Record<string, any>;
268
+ referenceId: string;
269
+ action: "upgrade-subscription" | "list-subscription" | "cancel-subscription" | "restore-subscription" | "billing-portal";
270
+ }, ctx: GenericEndpointContext) => Promise<boolean>;
271
+ /**
272
+ * A callback to run after a user has deleted their subscription
273
+ * @returns
274
+ */
275
+ onSubscriptionDeleted?: (data: {
276
+ event: Stripe.Event;
277
+ stripeSubscription: Stripe.Subscription;
278
+ subscription: Subscription;
279
+ }) => Promise<void>;
280
+ /**
281
+ * parameters for session create params
282
+ *
283
+ * @param data - data containing user, session and plan
284
+ * @param ctx - the context object
285
+ */
286
+ getCheckoutSessionParams?: (data: {
287
+ user: User & Record<string, any>;
288
+ session: Session & Record<string, any>;
289
+ plan: StripePlan;
290
+ subscription: Subscription;
291
+ }, ctx: GenericEndpointContext) => Promise<{
292
+ params?: Stripe.Checkout.SessionCreateParams;
293
+ options?: Stripe.RequestOptions;
294
+ }> | {
295
+ params?: Stripe.Checkout.SessionCreateParams;
296
+ options?: Stripe.RequestOptions;
297
+ };
298
+ /**
299
+ * Enable organization subscription
300
+ */
301
+ organization?: {
302
+ enabled: boolean;
303
+ };
304
+ };
212
305
  interface StripeOptions {
213
306
  /**
214
307
  * Stripe Client
@@ -247,99 +340,10 @@ interface StripeOptions {
247
340
  * Subscriptions
248
341
  */
249
342
  subscription?: {
250
- enabled: boolean;
251
- /**
252
- * Subscription Configuration
253
- */
254
- /**
255
- * List of plan
256
- */
257
- plans: StripePlan[] | (() => StripePlan[] | Promise<StripePlan[]>);
258
- /**
259
- * Require email verification before a user is allowed to upgrade
260
- * their subscriptions
261
- *
262
- * @default false
263
- */
264
- requireEmailVerification?: boolean;
265
- /**
266
- * A callback to run after a user has subscribed to a package
267
- * @param event - Stripe Event
268
- * @param subscription - Subscription Data
269
- * @returns
270
- */
271
- onSubscriptionComplete?: (data: {
272
- event: Stripe.Event;
273
- stripeSubscription: Stripe.Subscription;
274
- subscription: Subscription;
275
- plan: StripePlan;
276
- }, ctx: GenericEndpointContext) => Promise<void>;
277
- /**
278
- * A callback to run after a user is about to cancel their subscription
279
- * @returns
280
- */
281
- onSubscriptionUpdate?: (data: {
282
- event: Stripe.Event;
283
- subscription: Subscription;
284
- }) => Promise<void>;
285
- /**
286
- * A callback to run after a user is about to cancel their subscription
287
- * @returns
288
- */
289
- onSubscriptionCancel?: (data: {
290
- event?: Stripe.Event;
291
- subscription: Subscription;
292
- stripeSubscription: Stripe.Subscription;
293
- cancellationDetails?: Stripe.Subscription.CancellationDetails | null;
294
- }) => Promise<void>;
295
- /**
296
- * A function to check if the reference id is valid
297
- * and belongs to the user
298
- *
299
- * @param data - data containing user, session and referenceId
300
- * @param ctx - the context object
301
- * @returns
302
- */
303
- authorizeReference?: (data: {
304
- user: User & Record<string, any>;
305
- session: Session & Record<string, any>;
306
- referenceId: string;
307
- action: "upgrade-subscription" | "list-subscription" | "cancel-subscription" | "restore-subscription" | "billing-portal";
308
- }, ctx: GenericEndpointContext) => Promise<boolean>;
309
- /**
310
- * A callback to run after a user has deleted their subscription
311
- * @returns
312
- */
313
- onSubscriptionDeleted?: (data: {
314
- event: Stripe.Event;
315
- stripeSubscription: Stripe.Subscription;
316
- subscription: Subscription;
317
- }) => Promise<void>;
318
- /**
319
- * parameters for session create params
320
- *
321
- * @param data - data containing user, session and plan
322
- * @param ctx - the context object
323
- */
324
- getCheckoutSessionParams?: (data: {
325
- user: User & Record<string, any>;
326
- session: Session & Record<string, any>;
327
- plan: StripePlan;
328
- subscription: Subscription;
329
- }, ctx: GenericEndpointContext) => Promise<{
330
- params?: Stripe.Checkout.SessionCreateParams;
331
- options?: Stripe.RequestOptions;
332
- }> | {
333
- params?: Stripe.Checkout.SessionCreateParams;
334
- options?: Stripe.RequestOptions;
335
- };
336
- /**
337
- * Enable organization subscription
338
- */
339
- organization?: {
340
- enabled: boolean;
341
- };
342
- };
343
+ enabled: false;
344
+ } | ({
345
+ enabled: true;
346
+ } & SubscriptionOptions);
343
347
  /**
344
348
  * A callback to run after a stripe event is received
345
349
  * @param event - Stripe Event
@@ -369,7 +373,7 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
369
373
  success: boolean;
370
374
  }>;
371
375
  } & (O["subscription"] extends {
372
- enabled: boolean;
376
+ enabled: true;
373
377
  } ? {
374
378
  /**
375
379
  * ### Endpoint
@@ -665,7 +669,7 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
665
669
  } & {
666
670
  use: any[];
667
671
  }, {
668
- status: ("OK" | "CREATED" | "ACCEPTED" | "NO_CONTENT" | "MULTIPLE_CHOICES" | "MOVED_PERMANENTLY" | "FOUND" | "SEE_OTHER" | "NOT_MODIFIED" | "TEMPORARY_REDIRECT" | "BAD_REQUEST" | "UNAUTHORIZED" | "PAYMENT_REQUIRED" | "FORBIDDEN" | "NOT_FOUND" | "METHOD_NOT_ALLOWED" | "NOT_ACCEPTABLE" | "PROXY_AUTHENTICATION_REQUIRED" | "REQUEST_TIMEOUT" | "CONFLICT" | "GONE" | "LENGTH_REQUIRED" | "PRECONDITION_FAILED" | "PAYLOAD_TOO_LARGE" | "URI_TOO_LONG" | "UNSUPPORTED_MEDIA_TYPE" | "RANGE_NOT_SATISFIABLE" | "EXPECTATION_FAILED" | "I'M_A_TEAPOT" | "MISDIRECTED_REQUEST" | "UNPROCESSABLE_ENTITY" | "LOCKED" | "FAILED_DEPENDENCY" | "TOO_EARLY" | "UPGRADE_REQUIRED" | "PRECONDITION_REQUIRED" | "TOO_MANY_REQUESTS" | "REQUEST_HEADER_FIELDS_TOO_LARGE" | "UNAVAILABLE_FOR_LEGAL_REASONS" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT" | "HTTP_VERSION_NOT_SUPPORTED" | "VARIANT_ALSO_NEGOTIATES" | "INSUFFICIENT_STORAGE" | "LOOP_DETECTED" | "NOT_EXTENDED" | "NETWORK_AUTHENTICATION_REQUIRED") | better_call0.Status;
672
+ status: ("NOT_FOUND" | "FOUND" | "OK" | "CREATED" | "ACCEPTED" | "NO_CONTENT" | "MULTIPLE_CHOICES" | "MOVED_PERMANENTLY" | "SEE_OTHER" | "NOT_MODIFIED" | "TEMPORARY_REDIRECT" | "BAD_REQUEST" | "UNAUTHORIZED" | "PAYMENT_REQUIRED" | "FORBIDDEN" | "METHOD_NOT_ALLOWED" | "NOT_ACCEPTABLE" | "PROXY_AUTHENTICATION_REQUIRED" | "REQUEST_TIMEOUT" | "CONFLICT" | "GONE" | "LENGTH_REQUIRED" | "PRECONDITION_FAILED" | "PAYLOAD_TOO_LARGE" | "URI_TOO_LONG" | "UNSUPPORTED_MEDIA_TYPE" | "RANGE_NOT_SATISFIABLE" | "EXPECTATION_FAILED" | "I'M_A_TEAPOT" | "MISDIRECTED_REQUEST" | "UNPROCESSABLE_ENTITY" | "LOCKED" | "FAILED_DEPENDENCY" | "TOO_EARLY" | "UPGRADE_REQUIRED" | "PRECONDITION_REQUIRED" | "TOO_MANY_REQUESTS" | "REQUEST_HEADER_FIELDS_TOO_LARGE" | "UNAVAILABLE_FOR_LEGAL_REASONS" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT" | "HTTP_VERSION_NOT_SUPPORTED" | "VARIANT_ALSO_NEGOTIATES" | "INSUFFICIENT_STORAGE" | "LOOP_DETECTED" | "NOT_EXTENDED" | "NETWORK_AUTHENTICATION_REQUIRED") | better_call0.Status;
669
673
  body: ({
670
674
  message?: string;
671
675
  code?: string;
@@ -715,7 +719,7 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
715
719
  redirect: boolean;
716
720
  }>;
717
721
  } : {});
718
- init(ctx: better_auth0.AuthContext<better_auth0.BetterAuthOptions>): {
722
+ init(ctx: better_auth0.AuthContext): {
719
723
  options: {
720
724
  databaseHooks: {
721
725
  user: {
@@ -728,7 +732,7 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
728
732
  emailVerified: boolean;
729
733
  name: string;
730
734
  image?: string | null | undefined;
731
- } & Record<string, unknown>, ctx: GenericEndpointContext<better_auth0.BetterAuthOptions> | undefined): Promise<void>;
735
+ } & Record<string, unknown>, ctx: GenericEndpointContext | undefined): Promise<void>;
732
736
  };
733
737
  update: {
734
738
  after(user: {
@@ -739,7 +743,7 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
739
743
  emailVerified: boolean;
740
744
  name: string;
741
745
  image?: string | null | undefined;
742
- } & Record<string, unknown>, ctx: GenericEndpointContext<better_auth0.BetterAuthOptions> | undefined): Promise<void>;
746
+ } & Record<string, unknown>, ctx: GenericEndpointContext | undefined): Promise<void>;
743
747
  };
744
748
  };
745
749
  };
@@ -757,5 +761,6 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
757
761
  readonly SUBSCRIPTION_NOT_SCHEDULED_FOR_CANCELLATION: "Subscription is not scheduled for cancellation";
758
762
  };
759
763
  };
764
+ type StripePlugin<O$1 extends StripeOptions> = ReturnType<typeof stripe<O$1>>;
760
765
  //#endregion
761
- export { type StripePlan, type Subscription, stripe };
766
+ export { Subscription as i, stripe as n, StripePlan as r, StripePlugin as t };
@@ -209,6 +209,99 @@ interface Subscription {
209
209
  */
210
210
  seats?: number;
211
211
  }
212
+ type SubscriptionOptions = {
213
+ /**
214
+ * Subscription Configuration
215
+ */
216
+ /**
217
+ * List of plan
218
+ */
219
+ plans: StripePlan[] | (() => StripePlan[] | Promise<StripePlan[]>);
220
+ /**
221
+ * Require email verification before a user is allowed to upgrade
222
+ * their subscriptions
223
+ *
224
+ * @default false
225
+ */
226
+ requireEmailVerification?: boolean;
227
+ /**
228
+ * A callback to run after a user has subscribed to a package
229
+ * @param event - Stripe Event
230
+ * @param subscription - Subscription Data
231
+ * @returns
232
+ */
233
+ onSubscriptionComplete?: (data: {
234
+ event: Stripe.Event;
235
+ stripeSubscription: Stripe.Subscription;
236
+ subscription: Subscription;
237
+ plan: StripePlan;
238
+ }, ctx: GenericEndpointContext) => Promise<void>;
239
+ /**
240
+ * A callback to run after a user is about to cancel their subscription
241
+ * @returns
242
+ */
243
+ onSubscriptionUpdate?: (data: {
244
+ event: Stripe.Event;
245
+ subscription: Subscription;
246
+ }) => Promise<void>;
247
+ /**
248
+ * A callback to run after a user is about to cancel their subscription
249
+ * @returns
250
+ */
251
+ onSubscriptionCancel?: (data: {
252
+ event?: Stripe.Event;
253
+ subscription: Subscription;
254
+ stripeSubscription: Stripe.Subscription;
255
+ cancellationDetails?: Stripe.Subscription.CancellationDetails | null;
256
+ }) => Promise<void>;
257
+ /**
258
+ * A function to check if the reference id is valid
259
+ * and belongs to the user
260
+ *
261
+ * @param data - data containing user, session and referenceId
262
+ * @param ctx - the context object
263
+ * @returns
264
+ */
265
+ authorizeReference?: (data: {
266
+ user: User & Record<string, any>;
267
+ session: Session & Record<string, any>;
268
+ referenceId: string;
269
+ action: "upgrade-subscription" | "list-subscription" | "cancel-subscription" | "restore-subscription" | "billing-portal";
270
+ }, ctx: GenericEndpointContext) => Promise<boolean>;
271
+ /**
272
+ * A callback to run after a user has deleted their subscription
273
+ * @returns
274
+ */
275
+ onSubscriptionDeleted?: (data: {
276
+ event: Stripe.Event;
277
+ stripeSubscription: Stripe.Subscription;
278
+ subscription: Subscription;
279
+ }) => Promise<void>;
280
+ /**
281
+ * parameters for session create params
282
+ *
283
+ * @param data - data containing user, session and plan
284
+ * @param ctx - the context object
285
+ */
286
+ getCheckoutSessionParams?: (data: {
287
+ user: User & Record<string, any>;
288
+ session: Session & Record<string, any>;
289
+ plan: StripePlan;
290
+ subscription: Subscription;
291
+ }, ctx: GenericEndpointContext) => Promise<{
292
+ params?: Stripe.Checkout.SessionCreateParams;
293
+ options?: Stripe.RequestOptions;
294
+ }> | {
295
+ params?: Stripe.Checkout.SessionCreateParams;
296
+ options?: Stripe.RequestOptions;
297
+ };
298
+ /**
299
+ * Enable organization subscription
300
+ */
301
+ organization?: {
302
+ enabled: boolean;
303
+ };
304
+ };
212
305
  interface StripeOptions {
213
306
  /**
214
307
  * Stripe Client
@@ -247,99 +340,10 @@ interface StripeOptions {
247
340
  * Subscriptions
248
341
  */
249
342
  subscription?: {
250
- enabled: boolean;
251
- /**
252
- * Subscription Configuration
253
- */
254
- /**
255
- * List of plan
256
- */
257
- plans: StripePlan[] | (() => StripePlan[] | Promise<StripePlan[]>);
258
- /**
259
- * Require email verification before a user is allowed to upgrade
260
- * their subscriptions
261
- *
262
- * @default false
263
- */
264
- requireEmailVerification?: boolean;
265
- /**
266
- * A callback to run after a user has subscribed to a package
267
- * @param event - Stripe Event
268
- * @param subscription - Subscription Data
269
- * @returns
270
- */
271
- onSubscriptionComplete?: (data: {
272
- event: Stripe.Event;
273
- stripeSubscription: Stripe.Subscription;
274
- subscription: Subscription;
275
- plan: StripePlan;
276
- }, ctx: GenericEndpointContext) => Promise<void>;
277
- /**
278
- * A callback to run after a user is about to cancel their subscription
279
- * @returns
280
- */
281
- onSubscriptionUpdate?: (data: {
282
- event: Stripe.Event;
283
- subscription: Subscription;
284
- }) => Promise<void>;
285
- /**
286
- * A callback to run after a user is about to cancel their subscription
287
- * @returns
288
- */
289
- onSubscriptionCancel?: (data: {
290
- event?: Stripe.Event;
291
- subscription: Subscription;
292
- stripeSubscription: Stripe.Subscription;
293
- cancellationDetails?: Stripe.Subscription.CancellationDetails | null;
294
- }) => Promise<void>;
295
- /**
296
- * A function to check if the reference id is valid
297
- * and belongs to the user
298
- *
299
- * @param data - data containing user, session and referenceId
300
- * @param ctx - the context object
301
- * @returns
302
- */
303
- authorizeReference?: (data: {
304
- user: User & Record<string, any>;
305
- session: Session & Record<string, any>;
306
- referenceId: string;
307
- action: "upgrade-subscription" | "list-subscription" | "cancel-subscription" | "restore-subscription" | "billing-portal";
308
- }, ctx: GenericEndpointContext) => Promise<boolean>;
309
- /**
310
- * A callback to run after a user has deleted their subscription
311
- * @returns
312
- */
313
- onSubscriptionDeleted?: (data: {
314
- event: Stripe.Event;
315
- stripeSubscription: Stripe.Subscription;
316
- subscription: Subscription;
317
- }) => Promise<void>;
318
- /**
319
- * parameters for session create params
320
- *
321
- * @param data - data containing user, session and plan
322
- * @param ctx - the context object
323
- */
324
- getCheckoutSessionParams?: (data: {
325
- user: User & Record<string, any>;
326
- session: Session & Record<string, any>;
327
- plan: StripePlan;
328
- subscription: Subscription;
329
- }, ctx: GenericEndpointContext) => Promise<{
330
- params?: Stripe.Checkout.SessionCreateParams;
331
- options?: Stripe.RequestOptions;
332
- }> | {
333
- params?: Stripe.Checkout.SessionCreateParams;
334
- options?: Stripe.RequestOptions;
335
- };
336
- /**
337
- * Enable organization subscription
338
- */
339
- organization?: {
340
- enabled: boolean;
341
- };
342
- };
343
+ enabled: false;
344
+ } | ({
345
+ enabled: true;
346
+ } & SubscriptionOptions);
343
347
  /**
344
348
  * A callback to run after a stripe event is received
345
349
  * @param event - Stripe Event
@@ -369,7 +373,7 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
369
373
  success: boolean;
370
374
  }>;
371
375
  } & (O["subscription"] extends {
372
- enabled: boolean;
376
+ enabled: true;
373
377
  } ? {
374
378
  /**
375
379
  * ### Endpoint
@@ -665,7 +669,7 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
665
669
  } & {
666
670
  use: any[];
667
671
  }, {
668
- status: ("OK" | "CREATED" | "ACCEPTED" | "NO_CONTENT" | "MULTIPLE_CHOICES" | "MOVED_PERMANENTLY" | "FOUND" | "SEE_OTHER" | "NOT_MODIFIED" | "TEMPORARY_REDIRECT" | "BAD_REQUEST" | "UNAUTHORIZED" | "PAYMENT_REQUIRED" | "FORBIDDEN" | "NOT_FOUND" | "METHOD_NOT_ALLOWED" | "NOT_ACCEPTABLE" | "PROXY_AUTHENTICATION_REQUIRED" | "REQUEST_TIMEOUT" | "CONFLICT" | "GONE" | "LENGTH_REQUIRED" | "PRECONDITION_FAILED" | "PAYLOAD_TOO_LARGE" | "URI_TOO_LONG" | "UNSUPPORTED_MEDIA_TYPE" | "RANGE_NOT_SATISFIABLE" | "EXPECTATION_FAILED" | "I'M_A_TEAPOT" | "MISDIRECTED_REQUEST" | "UNPROCESSABLE_ENTITY" | "LOCKED" | "FAILED_DEPENDENCY" | "TOO_EARLY" | "UPGRADE_REQUIRED" | "PRECONDITION_REQUIRED" | "TOO_MANY_REQUESTS" | "REQUEST_HEADER_FIELDS_TOO_LARGE" | "UNAVAILABLE_FOR_LEGAL_REASONS" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT" | "HTTP_VERSION_NOT_SUPPORTED" | "VARIANT_ALSO_NEGOTIATES" | "INSUFFICIENT_STORAGE" | "LOOP_DETECTED" | "NOT_EXTENDED" | "NETWORK_AUTHENTICATION_REQUIRED") | better_call0.Status;
672
+ status: ("NOT_FOUND" | "FOUND" | "OK" | "CREATED" | "ACCEPTED" | "NO_CONTENT" | "MULTIPLE_CHOICES" | "MOVED_PERMANENTLY" | "SEE_OTHER" | "NOT_MODIFIED" | "TEMPORARY_REDIRECT" | "BAD_REQUEST" | "UNAUTHORIZED" | "PAYMENT_REQUIRED" | "FORBIDDEN" | "METHOD_NOT_ALLOWED" | "NOT_ACCEPTABLE" | "PROXY_AUTHENTICATION_REQUIRED" | "REQUEST_TIMEOUT" | "CONFLICT" | "GONE" | "LENGTH_REQUIRED" | "PRECONDITION_FAILED" | "PAYLOAD_TOO_LARGE" | "URI_TOO_LONG" | "UNSUPPORTED_MEDIA_TYPE" | "RANGE_NOT_SATISFIABLE" | "EXPECTATION_FAILED" | "I'M_A_TEAPOT" | "MISDIRECTED_REQUEST" | "UNPROCESSABLE_ENTITY" | "LOCKED" | "FAILED_DEPENDENCY" | "TOO_EARLY" | "UPGRADE_REQUIRED" | "PRECONDITION_REQUIRED" | "TOO_MANY_REQUESTS" | "REQUEST_HEADER_FIELDS_TOO_LARGE" | "UNAVAILABLE_FOR_LEGAL_REASONS" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT" | "HTTP_VERSION_NOT_SUPPORTED" | "VARIANT_ALSO_NEGOTIATES" | "INSUFFICIENT_STORAGE" | "LOOP_DETECTED" | "NOT_EXTENDED" | "NETWORK_AUTHENTICATION_REQUIRED") | better_call0.Status;
669
673
  body: ({
670
674
  message?: string;
671
675
  code?: string;
@@ -715,7 +719,7 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
715
719
  redirect: boolean;
716
720
  }>;
717
721
  } : {});
718
- init(ctx: better_auth0.AuthContext<better_auth0.BetterAuthOptions>): {
722
+ init(ctx: better_auth0.AuthContext): {
719
723
  options: {
720
724
  databaseHooks: {
721
725
  user: {
@@ -728,7 +732,7 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
728
732
  emailVerified: boolean;
729
733
  name: string;
730
734
  image?: string | null | undefined;
731
- } & Record<string, unknown>, ctx: GenericEndpointContext<better_auth0.BetterAuthOptions> | undefined): Promise<void>;
735
+ } & Record<string, unknown>, ctx: GenericEndpointContext | undefined): Promise<void>;
732
736
  };
733
737
  update: {
734
738
  after(user: {
@@ -739,7 +743,7 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
739
743
  emailVerified: boolean;
740
744
  name: string;
741
745
  image?: string | null | undefined;
742
- } & Record<string, unknown>, ctx: GenericEndpointContext<better_auth0.BetterAuthOptions> | undefined): Promise<void>;
746
+ } & Record<string, unknown>, ctx: GenericEndpointContext | undefined): Promise<void>;
743
747
  };
744
748
  };
745
749
  };
@@ -757,5 +761,6 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
757
761
  readonly SUBSCRIPTION_NOT_SCHEDULED_FOR_CANCELLATION: "Subscription is not scheduled for cancellation";
758
762
  };
759
763
  };
764
+ type StripePlugin<O$1 extends StripeOptions> = ReturnType<typeof stripe<O$1>>;
760
765
  //#endregion
761
- export { type StripePlan, type Subscription, stripe };
766
+ export { Subscription as i, stripe as n, StripePlan as r, StripePlugin as t };
package/dist/index.cjs CHANGED
@@ -38,14 +38,15 @@ let __better_auth_core_utils = require("@better-auth/core/utils");
38
38
  __better_auth_core_utils = __toESM(__better_auth_core_utils);
39
39
 
40
40
  //#region src/utils.ts
41
- async function getPlans(options) {
42
- return typeof options?.subscription?.plans === "function" ? await options.subscription?.plans() : options.subscription?.plans;
41
+ async function getPlans(subscriptionOptions) {
42
+ if (subscriptionOptions?.enabled) return typeof subscriptionOptions.plans === "function" ? await subscriptionOptions.plans() : subscriptionOptions.plans;
43
+ throw new Error("Subscriptions are not enabled in the Stripe options.");
43
44
  }
44
45
  async function getPlanByPriceInfo(options, priceId, priceLookupKey) {
45
- return await getPlans(options).then((res) => res?.find((plan) => plan.priceId === priceId || plan.annualDiscountPriceId === priceId || priceLookupKey && (plan.lookupKey === priceLookupKey || plan.annualDiscountLookupKey === priceLookupKey)));
46
+ return await getPlans(options.subscription).then((res) => res?.find((plan) => plan.priceId === priceId || plan.annualDiscountPriceId === priceId || priceLookupKey && (plan.lookupKey === priceLookupKey || plan.annualDiscountLookupKey === priceLookupKey)));
46
47
  }
47
48
  async function getPlanByName(options, name) {
48
- return await getPlans(options).then((res) => res?.find((plan) => plan.name.toLowerCase() === name.toLowerCase()));
49
+ return await getPlans(options.subscription).then((res) => res?.find((plan) => plan.name.toLowerCase() === name.toLowerCase()));
49
50
  }
50
51
 
51
52
  //#endregion
@@ -307,11 +308,12 @@ async function resolvePriceIdFromLookupKey(stripeClient, lookupKey) {
307
308
  }
308
309
  const stripe$1 = (options) => {
309
310
  const client = options.stripeClient;
311
+ const subscriptionOptions = options.subscription;
310
312
  const referenceMiddleware = (action) => (0, __better_auth_core_api.createAuthMiddleware)(async (ctx) => {
311
313
  const session = ctx.context.session;
312
314
  if (!session) throw new better_auth_api.APIError("UNAUTHORIZED");
313
315
  const referenceId = ctx.body?.referenceId || ctx.query?.referenceId || session.user.id;
314
- if (ctx.body?.referenceId && !options.subscription?.authorizeReference) {
316
+ if (ctx.body?.referenceId && !subscriptionOptions.authorizeReference) {
315
317
  better_auth.logger.error(`Passing referenceId into a subscription action isn't allowed if subscription.authorizeReference isn't defined in your stripe plugin config.`);
316
318
  throw new better_auth_api.APIError("BAD_REQUEST", { message: "Reference id is not allowed. Read server logs for more details." });
317
319
  }
@@ -319,7 +321,7 @@ const stripe$1 = (options) => {
319
321
  * if referenceId is the same as the active session user's id
320
322
  */
321
323
  const sameReference = ctx.query?.referenceId === session.user.id || ctx.body?.referenceId === session.user.id;
322
- if (!(ctx.body?.referenceId || ctx.query?.referenceId ? await options.subscription?.authorizeReference?.({
324
+ if (!(ctx.body?.referenceId || ctx.query?.referenceId ? await subscriptionOptions.authorizeReference?.({
323
325
  user: session.user,
324
326
  session: session.session,
325
327
  referenceId,
@@ -350,7 +352,7 @@ const stripe$1 = (options) => {
350
352
  ]
351
353
  }, async (ctx) => {
352
354
  const { user: user$1, session } = ctx.context.session;
353
- if (!user$1.emailVerified && options.subscription?.requireEmailVerification) throw new better_auth_api.APIError("BAD_REQUEST", { message: STRIPE_ERROR_CODES.EMAIL_VERIFICATION_REQUIRED });
355
+ if (!user$1.emailVerified && subscriptionOptions.requireEmailVerification) throw new better_auth_api.APIError("BAD_REQUEST", { message: STRIPE_ERROR_CODES.EMAIL_VERIFICATION_REQUIRED });
354
356
  const referenceId = ctx.body.referenceId || user$1.id;
355
357
  const plan = await getPlanByName(options, ctx.body.plan);
356
358
  if (!plan) throw new better_auth_api.APIError("BAD_REQUEST", { message: STRIPE_ERROR_CODES.SUBSCRIPTION_PLAN_NOT_FOUND });
@@ -502,7 +504,7 @@ const stripe$1 = (options) => {
502
504
  ctx.context.logger.error("Subscription ID not found");
503
505
  throw new better_auth_api.APIError("INTERNAL_SERVER_ERROR");
504
506
  }
505
- const params = await options.subscription?.getCheckoutSessionParams?.({
507
+ const params = await subscriptionOptions.getCheckoutSessionParams?.({
506
508
  user: user$1,
507
509
  session,
508
510
  plan,
@@ -589,7 +591,7 @@ const stripe$1 = (options) => {
589
591
  value: subscription.id
590
592
  }]
591
593
  });
592
- await options.subscription?.onSubscriptionCancel?.({
594
+ await subscriptionOptions.onSubscriptionCancel?.({
593
595
  subscription,
594
596
  cancellationDetails: currentSubscription.cancellation_details,
595
597
  stripeSubscription: currentSubscription,
@@ -737,7 +739,7 @@ const stripe$1 = (options) => {
737
739
  }]
738
740
  });
739
741
  if (!subscriptions$1.length) return [];
740
- const plans = await getPlans(options);
742
+ const plans = await getPlans(options.subscription);
741
743
  if (!plans) return [];
742
744
  const subs = subscriptions$1.map((sub) => {
743
745
  const plan = plans.find((p) => p.name.toLowerCase() === sub.plan.toLowerCase());
package/dist/index.d.cts CHANGED
@@ -1,2 +1,2 @@
1
- import { StripePlan, Subscription, stripe } from "./index-BOfvWLTo.cjs";
2
- export { StripePlan, Subscription, stripe };
1
+ import { i as Subscription, n as stripe, r as StripePlan, t as StripePlugin } from "./index-zlkU4RN-.cjs";
2
+ export { StripePlan, StripePlugin, Subscription, stripe };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { StripePlan, Subscription, stripe } from "./index-BMkRYEm9.js";
2
- export { StripePlan, Subscription, stripe };
1
+ import { i as Subscription, n as stripe, r as StripePlan, t as StripePlugin } from "./index-BSdp1oW9.js";
2
+ export { StripePlan, StripePlugin, Subscription, stripe };
package/dist/index.js CHANGED
@@ -8,14 +8,15 @@ import { defu } from "defu";
8
8
  import { defineErrorCodes } from "@better-auth/core/utils";
9
9
 
10
10
  //#region src/utils.ts
11
- async function getPlans(options) {
12
- return typeof options?.subscription?.plans === "function" ? await options.subscription?.plans() : options.subscription?.plans;
11
+ async function getPlans(subscriptionOptions) {
12
+ if (subscriptionOptions?.enabled) return typeof subscriptionOptions.plans === "function" ? await subscriptionOptions.plans() : subscriptionOptions.plans;
13
+ throw new Error("Subscriptions are not enabled in the Stripe options.");
13
14
  }
14
15
  async function getPlanByPriceInfo(options, priceId, priceLookupKey) {
15
- return await getPlans(options).then((res) => res?.find((plan) => plan.priceId === priceId || plan.annualDiscountPriceId === priceId || priceLookupKey && (plan.lookupKey === priceLookupKey || plan.annualDiscountLookupKey === priceLookupKey)));
16
+ return await getPlans(options.subscription).then((res) => res?.find((plan) => plan.priceId === priceId || plan.annualDiscountPriceId === priceId || priceLookupKey && (plan.lookupKey === priceLookupKey || plan.annualDiscountLookupKey === priceLookupKey)));
16
17
  }
17
18
  async function getPlanByName(options, name) {
18
- return await getPlans(options).then((res) => res?.find((plan) => plan.name.toLowerCase() === name.toLowerCase()));
19
+ return await getPlans(options.subscription).then((res) => res?.find((plan) => plan.name.toLowerCase() === name.toLowerCase()));
19
20
  }
20
21
 
21
22
  //#endregion
@@ -277,11 +278,12 @@ async function resolvePriceIdFromLookupKey(stripeClient, lookupKey) {
277
278
  }
278
279
  const stripe = (options) => {
279
280
  const client = options.stripeClient;
281
+ const subscriptionOptions = options.subscription;
280
282
  const referenceMiddleware = (action) => createAuthMiddleware(async (ctx) => {
281
283
  const session = ctx.context.session;
282
284
  if (!session) throw new APIError("UNAUTHORIZED");
283
285
  const referenceId = ctx.body?.referenceId || ctx.query?.referenceId || session.user.id;
284
- if (ctx.body?.referenceId && !options.subscription?.authorizeReference) {
286
+ if (ctx.body?.referenceId && !subscriptionOptions.authorizeReference) {
285
287
  logger.error(`Passing referenceId into a subscription action isn't allowed if subscription.authorizeReference isn't defined in your stripe plugin config.`);
286
288
  throw new APIError("BAD_REQUEST", { message: "Reference id is not allowed. Read server logs for more details." });
287
289
  }
@@ -289,7 +291,7 @@ const stripe = (options) => {
289
291
  * if referenceId is the same as the active session user's id
290
292
  */
291
293
  const sameReference = ctx.query?.referenceId === session.user.id || ctx.body?.referenceId === session.user.id;
292
- if (!(ctx.body?.referenceId || ctx.query?.referenceId ? await options.subscription?.authorizeReference?.({
294
+ if (!(ctx.body?.referenceId || ctx.query?.referenceId ? await subscriptionOptions.authorizeReference?.({
293
295
  user: session.user,
294
296
  session: session.session,
295
297
  referenceId,
@@ -320,7 +322,7 @@ const stripe = (options) => {
320
322
  ]
321
323
  }, async (ctx) => {
322
324
  const { user: user$1, session } = ctx.context.session;
323
- if (!user$1.emailVerified && options.subscription?.requireEmailVerification) throw new APIError("BAD_REQUEST", { message: STRIPE_ERROR_CODES.EMAIL_VERIFICATION_REQUIRED });
325
+ if (!user$1.emailVerified && subscriptionOptions.requireEmailVerification) throw new APIError("BAD_REQUEST", { message: STRIPE_ERROR_CODES.EMAIL_VERIFICATION_REQUIRED });
324
326
  const referenceId = ctx.body.referenceId || user$1.id;
325
327
  const plan = await getPlanByName(options, ctx.body.plan);
326
328
  if (!plan) throw new APIError("BAD_REQUEST", { message: STRIPE_ERROR_CODES.SUBSCRIPTION_PLAN_NOT_FOUND });
@@ -472,7 +474,7 @@ const stripe = (options) => {
472
474
  ctx.context.logger.error("Subscription ID not found");
473
475
  throw new APIError("INTERNAL_SERVER_ERROR");
474
476
  }
475
- const params = await options.subscription?.getCheckoutSessionParams?.({
477
+ const params = await subscriptionOptions.getCheckoutSessionParams?.({
476
478
  user: user$1,
477
479
  session,
478
480
  plan,
@@ -559,7 +561,7 @@ const stripe = (options) => {
559
561
  value: subscription.id
560
562
  }]
561
563
  });
562
- await options.subscription?.onSubscriptionCancel?.({
564
+ await subscriptionOptions.onSubscriptionCancel?.({
563
565
  subscription,
564
566
  cancellationDetails: currentSubscription.cancellation_details,
565
567
  stripeSubscription: currentSubscription,
@@ -707,7 +709,7 @@ const stripe = (options) => {
707
709
  }]
708
710
  });
709
711
  if (!subscriptions$1.length) return [];
710
- const plans = await getPlans(options);
712
+ const plans = await getPlans(options.subscription);
711
713
  if (!plans) return [];
712
714
  const subs = subscriptions$1.map((sub) => {
713
715
  const plan = plans.find((p) => p.name.toLowerCase() === sub.plan.toLowerCase());
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@better-auth/stripe",
3
3
  "author": "Bereket Engida",
4
- "version": "1.4.0-beta.12",
4
+ "version": "1.4.0-beta.13",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "license": "MIT",
@@ -17,11 +17,13 @@
17
17
  },
18
18
  "exports": {
19
19
  ".": {
20
+ "better-auth-dev-source": "./src/index.ts",
20
21
  "types": "./dist/index.d.ts",
21
22
  "import": "./dist/index.js",
22
23
  "require": "./dist/index.cjs"
23
24
  },
24
25
  "./client": {
26
+ "better-auth-dev-source": "./src/client.ts",
25
27
  "types": "./dist/client.d.ts",
26
28
  "import": "./dist/client.js",
27
29
  "require": "./dist/client.cjs"
@@ -43,15 +45,15 @@
43
45
  },
44
46
  "peerDependencies": {
45
47
  "stripe": "^18 || ^19",
46
- "@better-auth/core": "1.4.0-beta.12",
47
- "better-auth": "1.4.0-beta.12"
48
+ "@better-auth/core": "1.4.0-beta.13",
49
+ "better-auth": "1.4.0-beta.13"
48
50
  },
49
51
  "devDependencies": {
50
52
  "better-call": "1.0.24",
51
53
  "stripe": "^19.1.0",
52
- "tsdown": "^0.15.6",
53
- "@better-auth/core": "1.4.0-beta.12",
54
- "better-auth": "1.4.0-beta.12"
54
+ "tsdown": "^0.15.9",
55
+ "@better-auth/core": "1.4.0-beta.13",
56
+ "better-auth": "1.4.0-beta.13"
55
57
  },
56
58
  "scripts": {
57
59
  "test": "vitest",
package/src/index.ts CHANGED
@@ -26,6 +26,7 @@ import type {
26
26
  StripeOptions,
27
27
  StripePlan,
28
28
  Subscription,
29
+ SubscriptionOptions,
29
30
  } from "./types";
30
31
  import { getPlanByName, getPlanByPriceInfo, getPlans } from "./utils";
31
32
  import { getSchema } from "./schema";
@@ -69,6 +70,7 @@ async function resolvePriceIdFromLookupKey(
69
70
 
70
71
  export const stripe = <O extends StripeOptions>(options: O) => {
71
72
  const client = options.stripeClient;
73
+ const subscriptionOptions = options.subscription as SubscriptionOptions;
72
74
 
73
75
  const referenceMiddleware = (
74
76
  action:
@@ -86,7 +88,7 @@ export const stripe = <O extends StripeOptions>(options: O) => {
86
88
  const referenceId =
87
89
  ctx.body?.referenceId || ctx.query?.referenceId || session.user.id;
88
90
 
89
- if (ctx.body?.referenceId && !options.subscription?.authorizeReference) {
91
+ if (ctx.body?.referenceId && !subscriptionOptions.authorizeReference) {
90
92
  logger.error(
91
93
  `Passing referenceId into a subscription action isn't allowed if subscription.authorizeReference isn't defined in your stripe plugin config.`,
92
94
  );
@@ -103,7 +105,7 @@ export const stripe = <O extends StripeOptions>(options: O) => {
103
105
  ctx.body?.referenceId === session.user.id;
104
106
  const isAuthorized =
105
107
  ctx.body?.referenceId || ctx.query?.referenceId
106
- ? (await options.subscription?.authorizeReference?.(
108
+ ? (await subscriptionOptions.authorizeReference?.(
107
109
  {
108
110
  user: session.user,
109
111
  session: session.session,
@@ -248,7 +250,7 @@ export const stripe = <O extends StripeOptions>(options: O) => {
248
250
  const { user, session } = ctx.context.session;
249
251
  if (
250
252
  !user.emailVerified &&
251
- options.subscription?.requireEmailVerification
253
+ subscriptionOptions.requireEmailVerification
252
254
  ) {
253
255
  throw new APIError("BAD_REQUEST", {
254
256
  message: STRIPE_ERROR_CODES.EMAIL_VERIFICATION_REQUIRED,
@@ -531,7 +533,7 @@ export const stripe = <O extends StripeOptions>(options: O) => {
531
533
  throw new APIError("INTERNAL_SERVER_ERROR");
532
534
  }
533
535
 
534
- const params = await options.subscription?.getCheckoutSessionParams?.(
536
+ const params = await subscriptionOptions.getCheckoutSessionParams?.(
535
537
  {
536
538
  user,
537
539
  session,
@@ -691,7 +693,7 @@ export const stripe = <O extends StripeOptions>(options: O) => {
691
693
  },
692
694
  ],
693
695
  });
694
- await options.subscription?.onSubscriptionCancel?.({
696
+ await subscriptionOptions.onSubscriptionCancel?.({
695
697
  subscription,
696
698
  cancellationDetails: currentSubscription.cancellation_details,
697
699
  stripeSubscription: currentSubscription,
@@ -1030,7 +1032,7 @@ export const stripe = <O extends StripeOptions>(options: O) => {
1030
1032
  if (!subscriptions.length) {
1031
1033
  return [];
1032
1034
  }
1033
- const plans = await getPlans(options);
1035
+ const plans = await getPlans(options.subscription);
1034
1036
  if (!plans) {
1035
1037
  return [];
1036
1038
  }
@@ -1310,7 +1312,7 @@ export const stripe = <O extends StripeOptions>(options: O) => {
1310
1312
  ...((options.subscription?.enabled
1311
1313
  ? subscriptionEndpoints
1312
1314
  : {}) as O["subscription"] extends {
1313
- enabled: boolean;
1315
+ enabled: true;
1314
1316
  }
1315
1317
  ? typeof subscriptionEndpoints
1316
1318
  : {}),
@@ -1420,4 +1422,8 @@ export const stripe = <O extends StripeOptions>(options: O) => {
1420
1422
  } satisfies BetterAuthPlugin;
1421
1423
  };
1422
1424
 
1425
+ export type StripePlugin<O extends StripeOptions> = ReturnType<
1426
+ typeof stripe<O>
1427
+ >;
1428
+
1423
1429
  export type { Subscription, StripePlan };
@@ -1,17 +1,57 @@
1
- import { betterAuth, type User } from "better-auth";
1
+ import { type Auth, betterAuth, type User } from "better-auth";
2
2
  import { memoryAdapter } from "better-auth/adapters/memory";
3
3
  import { createAuthClient } from "better-auth/client";
4
4
  import { setCookieToHeader } from "better-auth/cookies";
5
5
  import { bearer } from "better-auth/plugins";
6
6
  import Stripe from "stripe";
7
7
  import { vi } from "vitest";
8
- import { stripe } from ".";
8
+ import { stripe, type StripePlugin } from ".";
9
9
  import { stripeClient } from "./client";
10
10
  import type { StripeOptions, Subscription } from "./types";
11
- import { expect, describe, it, beforeEach } from "vitest";
11
+ import { expect, describe, it, beforeEach, expectTypeOf } from "vitest";
12
12
  import { runWithEndpointContext } from "@better-auth/core/context";
13
13
  import type { GenericEndpointContext } from "@better-auth/core";
14
14
 
15
+ describe("stripe type", () => {
16
+ it("should api endpoint exists", () => {
17
+ type Plugins = [
18
+ StripePlugin<{
19
+ stripeClient: Stripe;
20
+ stripeWebhookSecret: string;
21
+ subscription: {
22
+ enabled: false;
23
+ };
24
+ }>,
25
+ ];
26
+ type MyAuth = Auth<{
27
+ plugins: Plugins;
28
+ }>;
29
+ expectTypeOf<MyAuth["api"]["stripeWebhook"]>().toBeFunction();
30
+ });
31
+
32
+ it("should have subscription endpoints", () => {
33
+ type Plugins = [
34
+ StripePlugin<{
35
+ stripeClient: Stripe;
36
+ stripeWebhookSecret: string;
37
+ subscription: {
38
+ enabled: true;
39
+ plans: [];
40
+ };
41
+ }>,
42
+ ];
43
+ type MyAuth = Auth<{
44
+ plugins: Plugins;
45
+ }>;
46
+ expectTypeOf<MyAuth["api"]["stripeWebhook"]>().toBeFunction();
47
+ expectTypeOf<MyAuth["api"]["subscriptionSuccess"]>().toBeFunction();
48
+ expectTypeOf<MyAuth["api"]["listActiveSubscriptions"]>().toBeFunction();
49
+ expectTypeOf<MyAuth["api"]["cancelSubscriptionCallback"]>().toBeFunction();
50
+ expectTypeOf<MyAuth["api"]["cancelSubscription"]>().toBeFunction();
51
+ expectTypeOf<MyAuth["api"]["restoreSubscription"]>().toBeFunction();
52
+ });
53
+ });
54
+
15
55
  describe("stripe", async () => {
16
56
  const mockStripe = {
17
57
  prices: {
@@ -1782,7 +1822,9 @@ describe("stripe", async () => {
1782
1822
  "http://localhost:3000/api/auth/stripe/webhook",
1783
1823
  {
1784
1824
  method: "POST",
1785
- headers: {},
1825
+ headers: {
1826
+ "content-type": "application/json",
1827
+ },
1786
1828
  body: JSON.stringify({ type: "test.event" }),
1787
1829
  },
1788
1830
  );
package/src/types.ts CHANGED
@@ -164,6 +164,116 @@ export interface Subscription {
164
164
  seats?: number;
165
165
  }
166
166
 
167
+ export type SubscriptionOptions = {
168
+ /**
169
+ * Subscription Configuration
170
+ */
171
+ /**
172
+ * List of plan
173
+ */
174
+ plans: StripePlan[] | (() => StripePlan[] | Promise<StripePlan[]>);
175
+ /**
176
+ * Require email verification before a user is allowed to upgrade
177
+ * their subscriptions
178
+ *
179
+ * @default false
180
+ */
181
+ requireEmailVerification?: boolean;
182
+ /**
183
+ * A callback to run after a user has subscribed to a package
184
+ * @param event - Stripe Event
185
+ * @param subscription - Subscription Data
186
+ * @returns
187
+ */
188
+ onSubscriptionComplete?: (
189
+ data: {
190
+ event: Stripe.Event;
191
+ stripeSubscription: Stripe.Subscription;
192
+ subscription: Subscription;
193
+ plan: StripePlan;
194
+ },
195
+ ctx: GenericEndpointContext,
196
+ ) => Promise<void>;
197
+ /**
198
+ * A callback to run after a user is about to cancel their subscription
199
+ * @returns
200
+ */
201
+ onSubscriptionUpdate?: (data: {
202
+ event: Stripe.Event;
203
+ subscription: Subscription;
204
+ }) => Promise<void>;
205
+ /**
206
+ * A callback to run after a user is about to cancel their subscription
207
+ * @returns
208
+ */
209
+ onSubscriptionCancel?: (data: {
210
+ event?: Stripe.Event;
211
+ subscription: Subscription;
212
+ stripeSubscription: Stripe.Subscription;
213
+ cancellationDetails?: Stripe.Subscription.CancellationDetails | null;
214
+ }) => Promise<void>;
215
+ /**
216
+ * A function to check if the reference id is valid
217
+ * and belongs to the user
218
+ *
219
+ * @param data - data containing user, session and referenceId
220
+ * @param ctx - the context object
221
+ * @returns
222
+ */
223
+ authorizeReference?: (
224
+ data: {
225
+ user: User & Record<string, any>;
226
+ session: Session & Record<string, any>;
227
+ referenceId: string;
228
+ action:
229
+ | "upgrade-subscription"
230
+ | "list-subscription"
231
+ | "cancel-subscription"
232
+ | "restore-subscription"
233
+ | "billing-portal";
234
+ },
235
+ ctx: GenericEndpointContext,
236
+ ) => Promise<boolean>;
237
+ /**
238
+ * A callback to run after a user has deleted their subscription
239
+ * @returns
240
+ */
241
+ onSubscriptionDeleted?: (data: {
242
+ event: Stripe.Event;
243
+ stripeSubscription: Stripe.Subscription;
244
+ subscription: Subscription;
245
+ }) => Promise<void>;
246
+ /**
247
+ * parameters for session create params
248
+ *
249
+ * @param data - data containing user, session and plan
250
+ * @param ctx - the context object
251
+ */
252
+ getCheckoutSessionParams?: (
253
+ data: {
254
+ user: User & Record<string, any>;
255
+ session: Session & Record<string, any>;
256
+ plan: StripePlan;
257
+ subscription: Subscription;
258
+ },
259
+ ctx: GenericEndpointContext,
260
+ ) =>
261
+ | Promise<{
262
+ params?: Stripe.Checkout.SessionCreateParams;
263
+ options?: Stripe.RequestOptions;
264
+ }>
265
+ | {
266
+ params?: Stripe.Checkout.SessionCreateParams;
267
+ options?: Stripe.RequestOptions;
268
+ };
269
+ /**
270
+ * Enable organization subscription
271
+ */
272
+ organization?: {
273
+ enabled: boolean;
274
+ };
275
+ };
276
+
167
277
  export interface StripeOptions {
168
278
  /**
169
279
  * Stripe Client
@@ -205,116 +315,13 @@ export interface StripeOptions {
205
315
  /**
206
316
  * Subscriptions
207
317
  */
208
- subscription?: {
209
- enabled: boolean;
210
- /**
211
- * Subscription Configuration
212
- */
213
- /**
214
- * List of plan
215
- */
216
- plans: StripePlan[] | (() => StripePlan[] | Promise<StripePlan[]>);
217
- /**
218
- * Require email verification before a user is allowed to upgrade
219
- * their subscriptions
220
- *
221
- * @default false
222
- */
223
- requireEmailVerification?: boolean;
224
- /**
225
- * A callback to run after a user has subscribed to a package
226
- * @param event - Stripe Event
227
- * @param subscription - Subscription Data
228
- * @returns
229
- */
230
- onSubscriptionComplete?: (
231
- data: {
232
- event: Stripe.Event;
233
- stripeSubscription: Stripe.Subscription;
234
- subscription: Subscription;
235
- plan: StripePlan;
236
- },
237
- ctx: GenericEndpointContext,
238
- ) => Promise<void>;
239
- /**
240
- * A callback to run after a user is about to cancel their subscription
241
- * @returns
242
- */
243
- onSubscriptionUpdate?: (data: {
244
- event: Stripe.Event;
245
- subscription: Subscription;
246
- }) => Promise<void>;
247
- /**
248
- * A callback to run after a user is about to cancel their subscription
249
- * @returns
250
- */
251
- onSubscriptionCancel?: (data: {
252
- event?: Stripe.Event;
253
- subscription: Subscription;
254
- stripeSubscription: Stripe.Subscription;
255
- cancellationDetails?: Stripe.Subscription.CancellationDetails | null;
256
- }) => Promise<void>;
257
- /**
258
- * A function to check if the reference id is valid
259
- * and belongs to the user
260
- *
261
- * @param data - data containing user, session and referenceId
262
- * @param ctx - the context object
263
- * @returns
264
- */
265
- authorizeReference?: (
266
- data: {
267
- user: User & Record<string, any>;
268
- session: Session & Record<string, any>;
269
- referenceId: string;
270
- action:
271
- | "upgrade-subscription"
272
- | "list-subscription"
273
- | "cancel-subscription"
274
- | "restore-subscription"
275
- | "billing-portal";
276
- },
277
- ctx: GenericEndpointContext,
278
- ) => Promise<boolean>;
279
- /**
280
- * A callback to run after a user has deleted their subscription
281
- * @returns
282
- */
283
- onSubscriptionDeleted?: (data: {
284
- event: Stripe.Event;
285
- stripeSubscription: Stripe.Subscription;
286
- subscription: Subscription;
287
- }) => Promise<void>;
288
- /**
289
- * parameters for session create params
290
- *
291
- * @param data - data containing user, session and plan
292
- * @param ctx - the context object
293
- */
294
- getCheckoutSessionParams?: (
295
- data: {
296
- user: User & Record<string, any>;
297
- session: Session & Record<string, any>;
298
- plan: StripePlan;
299
- subscription: Subscription;
300
- },
301
- ctx: GenericEndpointContext,
302
- ) =>
303
- | Promise<{
304
- params?: Stripe.Checkout.SessionCreateParams;
305
- options?: Stripe.RequestOptions;
306
- }>
307
- | {
308
- params?: Stripe.Checkout.SessionCreateParams;
309
- options?: Stripe.RequestOptions;
310
- };
311
- /**
312
- * Enable organization subscription
313
- */
314
- organization?: {
315
- enabled: boolean;
316
- };
317
- };
318
+ subscription?:
319
+ | {
320
+ enabled: false;
321
+ }
322
+ | ({
323
+ enabled: true;
324
+ } & SubscriptionOptions);
318
325
  /**
319
326
  * A callback to run after a stripe event is received
320
327
  * @param event - Stripe Event
package/src/utils.ts CHANGED
@@ -1,9 +1,14 @@
1
1
  import type { StripeOptions } from "./types";
2
2
 
3
- export async function getPlans(options: StripeOptions) {
4
- return typeof options?.subscription?.plans === "function"
5
- ? await options.subscription?.plans()
6
- : options.subscription?.plans;
3
+ export async function getPlans(
4
+ subscriptionOptions: StripeOptions["subscription"],
5
+ ) {
6
+ if (subscriptionOptions?.enabled) {
7
+ return typeof subscriptionOptions.plans === "function"
8
+ ? await subscriptionOptions.plans()
9
+ : subscriptionOptions.plans;
10
+ }
11
+ throw new Error("Subscriptions are not enabled in the Stripe options.");
7
12
  }
8
13
 
9
14
  export async function getPlanByPriceInfo(
@@ -11,7 +16,7 @@ export async function getPlanByPriceInfo(
11
16
  priceId: string,
12
17
  priceLookupKey: string | null,
13
18
  ) {
14
- return await getPlans(options).then((res) =>
19
+ return await getPlans(options.subscription).then((res) =>
15
20
  res?.find(
16
21
  (plan) =>
17
22
  plan.priceId === priceId ||
@@ -24,7 +29,7 @@ export async function getPlanByPriceInfo(
24
29
  }
25
30
 
26
31
  export async function getPlanByName(options: StripeOptions, name: string) {
27
- return await getPlans(options).then((res) =>
32
+ return await getPlans(options.subscription).then((res) =>
28
33
  res?.find((plan) => plan.name.toLowerCase() === name.toLowerCase()),
29
34
  );
30
35
  }
package/tsconfig.json CHANGED
@@ -1,9 +1,14 @@
1
1
  {
2
- "extends": "../../tsconfig.json",
2
+ "extends": "../../tsconfig.base.json",
3
3
  "compilerOptions": {
4
- "rootDir": "./src",
5
- "outDir": "./dist",
6
4
  "lib": ["esnext", "dom", "dom.iterable"]
7
5
  },
8
- "include": ["src"]
6
+ "references": [
7
+ {
8
+ "path": "../better-auth/tsconfig.json"
9
+ },
10
+ {
11
+ "path": "../core/tsconfig.json"
12
+ }
13
+ ]
9
14
  }
package/tsdown.config.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { defineConfig } from "tsdown";
2
2
 
3
3
  export default defineConfig({
4
- dts: true,
4
+ dts: { build: true, incremental: true },
5
5
  format: ["esm", "cjs"],
6
6
  entry: ["./src/index.ts", "./src/client.ts"],
7
7
  external: ["better-auth", "better-call", "@better-fetch/fetch", "stripe"],