@better-auth/stripe 1.5.0-beta.2 → 1.5.0-beta.3

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/src/schema.ts CHANGED
@@ -77,6 +77,17 @@ export const user = {
77
77
  },
78
78
  } satisfies BetterAuthPluginDBSchema;
79
79
 
80
+ export const organization = {
81
+ organization: {
82
+ fields: {
83
+ stripeCustomerId: {
84
+ type: "string",
85
+ required: false,
86
+ },
87
+ },
88
+ },
89
+ } satisfies BetterAuthPluginDBSchema;
90
+
80
91
  export const getSchema = (options: StripeOptions) => {
81
92
  let baseSchema = {};
82
93
 
@@ -91,6 +102,13 @@ export const getSchema = (options: StripeOptions) => {
91
102
  };
92
103
  }
93
104
 
105
+ if (options.organization?.enabled) {
106
+ baseSchema = {
107
+ ...baseSchema,
108
+ ...organization,
109
+ };
110
+ }
111
+
94
112
  if (
95
113
  options.schema &&
96
114
  !options.subscription?.enabled &&
package/src/types.ts CHANGED
@@ -4,8 +4,32 @@ import type {
4
4
  Session,
5
5
  User,
6
6
  } from "better-auth";
7
+ import type { Organization } from "better-auth/plugins/organization";
7
8
  import type Stripe from "stripe";
8
- import type { subscriptions, user } from "./schema";
9
+ import type { organization, subscriptions, user } from "./schema";
10
+
11
+ export type AuthorizeReferenceAction =
12
+ | "upgrade-subscription"
13
+ | "list-subscription"
14
+ | "cancel-subscription"
15
+ | "restore-subscription"
16
+ | "billing-portal";
17
+
18
+ export type CustomerType = "user" | "organization";
19
+
20
+ export type WithStripeCustomerId = {
21
+ stripeCustomerId?: string;
22
+ };
23
+
24
+ // TODO: Types extended by a plugin should be moved into that plugin.
25
+ export type WithActiveOrganizationId = {
26
+ activeOrganizationId?: string;
27
+ };
28
+
29
+ export type StripeCtxSession = {
30
+ session: Session & WithActiveOrganizationId;
31
+ user: User & WithStripeCustomerId;
32
+ };
9
33
 
10
34
  export type StripePlan = {
11
35
  /**
@@ -253,12 +277,7 @@ export type SubscriptionOptions = {
253
277
  user: User & Record<string, any>;
254
278
  session: Session & Record<string, any>;
255
279
  referenceId: string;
256
- action:
257
- | "upgrade-subscription"
258
- | "list-subscription"
259
- | "cancel-subscription"
260
- | "restore-subscription"
261
- | "billing-portal";
280
+ action: AuthorizeReferenceAction;
262
281
  },
263
282
  ctx: GenericEndpointContext,
264
283
  ) => Promise<boolean>)
@@ -313,14 +332,6 @@ export type SubscriptionOptions = {
313
332
  options?: Stripe.RequestOptions;
314
333
  })
315
334
  | undefined;
316
- /**
317
- * Enable organization subscription
318
- */
319
- organization?:
320
- | {
321
- enabled: boolean;
322
- }
323
- | undefined;
324
335
  };
325
336
 
326
337
  export interface StripeOptions {
@@ -348,7 +359,7 @@ export interface StripeOptions {
348
359
  | ((
349
360
  data: {
350
361
  stripeCustomer: Stripe.Customer;
351
- user: User & { stripeCustomerId: string };
362
+ user: User & WithStripeCustomerId;
352
363
  },
353
364
  ctx: GenericEndpointContext,
354
365
  ) => Promise<void>)
@@ -378,6 +389,49 @@ export interface StripeOptions {
378
389
  } & SubscriptionOptions)
379
390
  )
380
391
  | undefined;
392
+ /**
393
+ * Organization Stripe integration
394
+ *
395
+ * Enable organizations to have their own Stripe customer ID
396
+ */
397
+ organization?:
398
+ | {
399
+ /**
400
+ * Enable organization Stripe customer
401
+ */
402
+ enabled: true;
403
+ /**
404
+ * A custom function to get the customer create params
405
+ * for organization customers.
406
+ *
407
+ * @param organization - the organization
408
+ * @param ctx - the context object
409
+ * @returns
410
+ */
411
+ getCustomerCreateParams?:
412
+ | ((
413
+ organization: Organization,
414
+ ctx: GenericEndpointContext,
415
+ ) => Promise<Partial<Stripe.CustomerCreateParams>>)
416
+ | undefined;
417
+ /**
418
+ * A callback to run after an organization customer has been created
419
+ *
420
+ * @param data - data containing stripeCustomer and organization
421
+ * @param ctx - the context object
422
+ * @returns
423
+ */
424
+ onCustomerCreate?:
425
+ | ((
426
+ data: {
427
+ stripeCustomer: Stripe.Customer;
428
+ organization: Organization & WithStripeCustomerId;
429
+ },
430
+ ctx: GenericEndpointContext,
431
+ ) => Promise<void>)
432
+ | undefined;
433
+ }
434
+ | undefined;
381
435
  /**
382
436
  * A callback to run after a stripe event is received
383
437
  * @param event - Stripe Event
@@ -387,7 +441,9 @@ export interface StripeOptions {
387
441
  /**
388
442
  * Schema for the stripe plugin
389
443
  */
390
- schema?: InferOptionSchema<typeof subscriptions & typeof user> | undefined;
444
+ schema?:
445
+ | InferOptionSchema<
446
+ typeof subscriptions & typeof user & typeof organization
447
+ >
448
+ | undefined;
391
449
  }
392
-
393
- export interface InputSubscription extends Omit<Subscription, "id"> {}
package/src/utils.ts CHANGED
@@ -57,3 +57,14 @@ export function isPendingCancel(sub: Subscription): boolean {
57
57
  export function isStripePendingCancel(stripeSub: Stripe.Subscription): boolean {
58
58
  return !!(stripeSub.cancel_at_period_end || stripeSub.cancel_at);
59
59
  }
60
+
61
+ /**
62
+ * Escapes a value for use in Stripe search queries.
63
+ * Stripe search query uses double quotes for string values,
64
+ * and double quotes within the value need to be escaped with backslash.
65
+ *
66
+ * @see https://docs.stripe.com/search#search-query-language
67
+ */
68
+ export function escapeStripeSearchValue(value: string): string {
69
+ return value.replace(/"/g, '\\"');
70
+ }