@better-auth/stripe 1.7.0-beta.9 → 1.7.0-rc.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/client.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { l as StripePlan, n as stripe } from "./index-DtTC1CXc.mjs";
1
+ import { l as StripePlan, n as stripe } from "./index-XU8GMPhh.mjs";
2
2
  import * as better_auth0 from "better-auth";
3
3
 
4
4
  //#region src/error-codes.d.ts
package/dist/client.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { n as STRIPE_ERROR_CODES, t as PACKAGE_VERSION } from "./version-Cc64Y-9x.mjs";
1
+ import { n as STRIPE_ERROR_CODES, t as PACKAGE_VERSION } from "./version-BLZwIhZw.mjs";
2
2
  //#region src/client.ts
3
3
  const stripeClient = (options) => {
4
4
  return {
@@ -576,6 +576,8 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
576
576
  }>)[];
577
577
  }>) => Promise<{
578
578
  session: StripeCtxSession;
579
+ }>) | ((inputContext: better_call0.MiddlewareInputContext<better_call0.MiddlewareOptions>) => Promise<{
580
+ referenceId: string;
579
581
  }>) | ((inputContext: better_call0.MiddlewareInputContext<better_call0.MiddlewareOptions>) => Promise<void>))[];
580
582
  }, {
581
583
  url: string;
@@ -637,6 +639,8 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
637
639
  }>)[];
638
640
  }>) => Promise<{
639
641
  session: StripeCtxSession;
642
+ }>) | ((inputContext: better_call0.MiddlewareInputContext<better_call0.MiddlewareOptions>) => Promise<{
643
+ referenceId: string;
640
644
  }>) | ((inputContext: better_call0.MiddlewareInputContext<better_call0.MiddlewareOptions>) => Promise<void>))[];
641
645
  }, {
642
646
  url: string;
@@ -683,7 +687,9 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
683
687
  }>)[];
684
688
  }>) => Promise<{
685
689
  session: StripeCtxSession;
686
- }>) | ((inputContext: better_call0.MiddlewareInputContext<better_call0.MiddlewareOptions>) => Promise<void>))[];
690
+ }>) | ((inputContext: better_call0.MiddlewareInputContext<better_call0.MiddlewareOptions>) => Promise<{
691
+ referenceId: string;
692
+ }>))[];
687
693
  }, Stripe.Response<Stripe.Subscription>>;
688
694
  listActiveSubscriptions: better_call0.StrictEndpoint<"/subscription/list", {
689
695
  method: "GET";
@@ -725,7 +731,9 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
725
731
  }>)[];
726
732
  }>) => Promise<{
727
733
  session: StripeCtxSession;
728
- }>) | ((inputContext: better_call0.MiddlewareInputContext<better_call0.MiddlewareOptions>) => Promise<void>))[];
734
+ }>) | ((inputContext: better_call0.MiddlewareInputContext<better_call0.MiddlewareOptions>) => Promise<{
735
+ referenceId: string;
736
+ }>))[];
729
737
  }, {
730
738
  limits: Record<string, unknown> | undefined;
731
739
  priceId: string | undefined;
@@ -801,6 +809,8 @@ declare const stripe: <O extends StripeOptions>(options: O) => {
801
809
  }>)[];
802
810
  }>) => Promise<{
803
811
  session: StripeCtxSession;
812
+ }>) | ((inputContext: better_call0.MiddlewareInputContext<better_call0.MiddlewareOptions>) => Promise<{
813
+ referenceId: string;
804
814
  }>) | ((inputContext: better_call0.MiddlewareInputContext<better_call0.MiddlewareOptions>) => Promise<void>))[];
805
815
  }, {
806
816
  url: string;
package/dist/index.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { a as CheckoutSessionLocale, c as StripeOptions, d as SubscriptionOptions, f as WithActiveOrganizationId, i as CheckoutSessionLineItem, l as StripePlan, n as stripe, o as CustomerType, p as WithStripeCustomerId, r as AuthorizeReferenceAction, s as StripeCtxSession, t as StripePlugin, u as Subscription } from "./index-DtTC1CXc.mjs";
1
+ import { a as CheckoutSessionLocale, c as StripeOptions, d as SubscriptionOptions, f as WithActiveOrganizationId, i as CheckoutSessionLineItem, l as StripePlan, n as stripe, o as CustomerType, p as WithStripeCustomerId, r as AuthorizeReferenceAction, s as StripeCtxSession, t as StripePlugin, u as Subscription } from "./index-XU8GMPhh.mjs";
2
2
  export { AuthorizeReferenceAction, CheckoutSessionLineItem, CheckoutSessionLocale, CustomerType, StripeCtxSession, StripeOptions, StripePlan, StripePlugin, Subscription, SubscriptionOptions, WithActiveOrganizationId, WithStripeCustomerId, stripe };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { n as STRIPE_ERROR_CODES, t as PACKAGE_VERSION } from "./version-Cc64Y-9x.mjs";
1
+ import { n as STRIPE_ERROR_CODES, t as PACKAGE_VERSION } from "./version-BLZwIhZw.mjs";
2
2
  import { APIError, HIDE_METADATA } from "better-auth";
3
3
  import { defu } from "defu";
4
4
  import { createAuthEndpoint, createAuthMiddleware } from "@better-auth/core/api";
@@ -451,12 +451,14 @@ async function onSubscriptionDeleted(ctx, options, event) {
451
451
  const stripeSessionMiddleware = createAuthMiddleware({ use: [sessionMiddleware] }, async (ctx) => {
452
452
  return { session: ctx.context.session };
453
453
  });
454
- const referenceMiddleware = (subscriptionOptions, action) => createAuthMiddleware(async (ctx) => {
454
+ const referenceMiddleware = (options, action) => createAuthMiddleware(async (ctx) => {
455
+ const subscriptionOptions = options.subscription;
455
456
  const ctxSession = ctx.context.session;
456
457
  if (!ctxSession) throw APIError$1.from("UNAUTHORIZED", STRIPE_ERROR_CODES.UNAUTHORIZED);
457
458
  const customerType = ctx.body?.customerType || ctx.query?.customerType;
458
459
  const explicitReferenceId = ctx.body?.referenceId || ctx.query?.referenceId;
459
460
  if (customerType === "organization") {
461
+ if (!options.organization?.enabled) throw APIError$1.from("BAD_REQUEST", STRIPE_ERROR_CODES.ORGANIZATION_SUBSCRIPTION_NOT_ENABLED);
460
462
  if (!subscriptionOptions.authorizeReference) {
461
463
  ctx.context.logger.error(`Organization subscriptions require authorizeReference to be defined in your stripe plugin config.`);
462
464
  throw APIError$1.from("BAD_REQUEST", STRIPE_ERROR_CODES.AUTHORIZE_REFERENCE_REQUIRED);
@@ -469,10 +471,10 @@ const referenceMiddleware = (subscriptionOptions, action) => createAuthMiddlewar
469
471
  referenceId,
470
472
  action
471
473
  }, ctx)) throw APIError$1.from("UNAUTHORIZED", STRIPE_ERROR_CODES.UNAUTHORIZED);
472
- return;
474
+ return { referenceId };
473
475
  }
474
- if (!explicitReferenceId) return;
475
- if (explicitReferenceId === ctxSession.user.id) return;
476
+ const referenceId = explicitReferenceId || ctxSession.user.id;
477
+ if (!explicitReferenceId || explicitReferenceId === ctxSession.user.id) return { referenceId };
476
478
  if (!subscriptionOptions.authorizeReference) {
477
479
  ctx.context.logger.error(`Passing referenceId into a subscription action isn't allowed if subscription.authorizeReference isn't defined in your stripe plugin config.`);
478
480
  throw APIError$1.from("BAD_REQUEST", STRIPE_ERROR_CODES.REFERENCE_ID_NOT_ALLOWED);
@@ -480,9 +482,10 @@ const referenceMiddleware = (subscriptionOptions, action) => createAuthMiddlewar
480
482
  if (!await subscriptionOptions.authorizeReference({
481
483
  user: ctxSession.user,
482
484
  session: ctxSession.session,
483
- referenceId: explicitReferenceId,
485
+ referenceId,
484
486
  action
485
487
  }, ctx)) throw APIError$1.from("UNAUTHORIZED", STRIPE_ERROR_CODES.UNAUTHORIZED);
488
+ return { referenceId };
486
489
  });
487
490
  //#endregion
488
491
  //#region src/routes.ts
@@ -520,21 +523,6 @@ function getUrl(ctx, url) {
520
523
  return `${ctx.context.options.baseURL}${url.startsWith("/") ? url : `/${url}`}`;
521
524
  }
522
525
  /**
523
- * Determines the reference ID based on customer type.
524
- * - `user` (default): uses userId
525
- * - `organization`: uses activeOrganizationId from session
526
- * @internal
527
- */
528
- function getReferenceId(ctxSession, customerType, options) {
529
- const { user, session } = ctxSession;
530
- if ((customerType || "user") === "organization") {
531
- if (!options.organization?.enabled) throw APIError$1.from("BAD_REQUEST", STRIPE_ERROR_CODES.ORGANIZATION_SUBSCRIPTION_NOT_ENABLED);
532
- if (!session.activeOrganizationId) throw APIError$1.from("BAD_REQUEST", STRIPE_ERROR_CODES.ORGANIZATION_NOT_FOUND);
533
- return session.activeOrganizationId;
534
- }
535
- return user.id;
536
- }
537
- /**
538
526
  * Retrieve the subscription a row points to, or `null` if it no longer exists
539
527
  * (`resource_missing`) so callers can treat the row as stale.
540
528
  * @internal
@@ -592,7 +580,7 @@ const upgradeSubscription = (options) => {
592
580
  metadata: { openapi: { operationId: "upgradeSubscription" } },
593
581
  use: [
594
582
  stripeSessionMiddleware,
595
- referenceMiddleware(subscriptionOptions, "upgrade-subscription"),
583
+ referenceMiddleware(options, "upgrade-subscription"),
596
584
  originCheck((c) => {
597
585
  return [
598
586
  c.body.successUrl,
@@ -604,7 +592,7 @@ const upgradeSubscription = (options) => {
604
592
  }, async (ctx) => {
605
593
  const { user, session } = ctx.context.session;
606
594
  const customerType = ctx.body.customerType || "user";
607
- const referenceId = ctx.body.referenceId || getReferenceId(ctx.context.session, customerType, options);
595
+ const referenceId = ctx.context.referenceId;
608
596
  if (!user.emailVerified && subscriptionOptions.requireEmailVerification) throw APIError$1.from("BAD_REQUEST", STRIPE_ERROR_CODES.EMAIL_VERIFICATION_REQUIRED);
609
597
  const plan = await getPlanByName(options, ctx.body.plan);
610
598
  if (!plan) throw APIError$1.from("BAD_REQUEST", STRIPE_ERROR_CODES.SUBSCRIPTION_PLAN_NOT_FOUND);
@@ -1097,19 +1085,17 @@ const cancelSubscriptionBodySchema = z.object({
1097
1085
  */
1098
1086
  const cancelSubscription = (options) => {
1099
1087
  const client = options.stripeClient;
1100
- const subscriptionOptions = options.subscription;
1101
1088
  return createAuthEndpoint("/subscription/cancel", {
1102
1089
  method: "POST",
1103
1090
  body: cancelSubscriptionBodySchema,
1104
1091
  metadata: { openapi: { operationId: "cancelSubscription" } },
1105
1092
  use: [
1106
1093
  stripeSessionMiddleware,
1107
- referenceMiddleware(subscriptionOptions, "cancel-subscription"),
1094
+ referenceMiddleware(options, "cancel-subscription"),
1108
1095
  originCheck((ctx) => ctx.body.returnUrl)
1109
1096
  ]
1110
1097
  }, async (ctx) => {
1111
- const customerType = ctx.body.customerType || "user";
1112
- const referenceId = ctx.body.referenceId || getReferenceId(ctx.context.session, customerType, options);
1098
+ const referenceId = ctx.context.referenceId;
1113
1099
  let subscription = ctx.body.subscriptionId ? await ctx.context.adapter.findOne({
1114
1100
  model: "subscription",
1115
1101
  where: [{
@@ -1184,15 +1170,13 @@ const restoreSubscriptionBodySchema = z.object({
1184
1170
  });
1185
1171
  const restoreSubscription = (options) => {
1186
1172
  const client = options.stripeClient;
1187
- const subscriptionOptions = options.subscription;
1188
1173
  return createAuthEndpoint("/subscription/restore", {
1189
1174
  method: "POST",
1190
1175
  body: restoreSubscriptionBodySchema,
1191
1176
  metadata: { openapi: { operationId: "restoreSubscription" } },
1192
- use: [stripeSessionMiddleware, referenceMiddleware(subscriptionOptions, "restore-subscription")]
1177
+ use: [stripeSessionMiddleware, referenceMiddleware(options, "restore-subscription")]
1193
1178
  }, async (ctx) => {
1194
- const customerType = ctx.body.customerType || "user";
1195
- const referenceId = ctx.body.referenceId || getReferenceId(ctx.context.session, customerType, options);
1179
+ const referenceId = ctx.context.referenceId;
1196
1180
  let subscription = ctx.body.subscriptionId ? await ctx.context.adapter.findOne({
1197
1181
  model: "subscription",
1198
1182
  where: [{
@@ -1287,15 +1271,13 @@ const listActiveSubscriptionsQuerySchema = z.optional(z.object({
1287
1271
  * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/stripe#api-method-subscription-list)
1288
1272
  */
1289
1273
  const listActiveSubscriptions = (options) => {
1290
- const subscriptionOptions = options.subscription;
1291
1274
  return createAuthEndpoint("/subscription/list", {
1292
1275
  method: "GET",
1293
1276
  query: listActiveSubscriptionsQuerySchema,
1294
1277
  metadata: { openapi: { operationId: "listActiveSubscriptions" } },
1295
- use: [stripeSessionMiddleware, referenceMiddleware(subscriptionOptions, "list-subscription")]
1278
+ use: [stripeSessionMiddleware, referenceMiddleware(options, "list-subscription")]
1296
1279
  }, async (ctx) => {
1297
- const customerType = ctx.query?.customerType || "user";
1298
- const referenceId = ctx.query?.referenceId || getReferenceId(ctx.context.session, customerType, options);
1280
+ const referenceId = ctx.context.referenceId;
1299
1281
  const subscriptions = await ctx.context.adapter.findMany({
1300
1282
  model: "subscription",
1301
1283
  where: [{
@@ -1412,20 +1394,19 @@ const createBillingPortalBodySchema = z.object({
1412
1394
  });
1413
1395
  const createBillingPortal = (options) => {
1414
1396
  const client = options.stripeClient;
1415
- const subscriptionOptions = options.subscription;
1416
1397
  return createAuthEndpoint("/subscription/billing-portal", {
1417
1398
  method: "POST",
1418
1399
  body: createBillingPortalBodySchema,
1419
1400
  metadata: { openapi: { operationId: "createBillingPortal" } },
1420
1401
  use: [
1421
1402
  stripeSessionMiddleware,
1422
- referenceMiddleware(subscriptionOptions, "billing-portal"),
1403
+ referenceMiddleware(options, "billing-portal"),
1423
1404
  originCheck((ctx) => ctx.body.returnUrl)
1424
1405
  ]
1425
1406
  }, async (ctx) => {
1426
1407
  const { user } = ctx.context.session;
1427
1408
  const customerType = ctx.body.customerType || "user";
1428
- const referenceId = ctx.body.referenceId || getReferenceId(ctx.context.session, customerType, options);
1409
+ const referenceId = ctx.context.referenceId;
1429
1410
  let customerId;
1430
1411
  if (customerType === "organization") {
1431
1412
  customerId = (await ctx.context.adapter.findOne({
@@ -27,6 +27,6 @@ const STRIPE_ERROR_CODES = defineErrorCodes({
27
27
  });
28
28
  //#endregion
29
29
  //#region src/version.ts
30
- const PACKAGE_VERSION = "1.7.0-beta.9";
30
+ const PACKAGE_VERSION = "1.7.0-rc.0";
31
31
  //#endregion
32
32
  export { STRIPE_ERROR_CODES as n, PACKAGE_VERSION as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/stripe",
3
- "version": "1.7.0-beta.9",
3
+ "version": "1.7.0-rc.0",
4
4
  "description": "Stripe plugin for Better Auth",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -55,17 +55,17 @@
55
55
  "zod": "^4.3.6"
56
56
  },
57
57
  "devDependencies": {
58
- "better-call": "1.3.6",
58
+ "better-call": "1.3.7",
59
59
  "stripe": "^22.0.1",
60
60
  "tsdown": "0.21.1",
61
- "@better-auth/core": "1.7.0-beta.9",
62
- "better-auth": "1.7.0-beta.9"
61
+ "@better-auth/core": "1.7.0-rc.0",
62
+ "better-auth": "1.7.0-rc.0"
63
63
  },
64
64
  "peerDependencies": {
65
- "better-call": "1.3.6",
65
+ "better-call": "1.3.7",
66
66
  "stripe": "^18 || ^19 || ^20 || ^21 || ^22",
67
- "@better-auth/core": "^1.7.0-beta.9",
68
- "better-auth": "^1.7.0-beta.9"
67
+ "@better-auth/core": "^1.7.0-rc.0",
68
+ "better-auth": "^1.7.0-rc.0"
69
69
  },
70
70
  "scripts": {
71
71
  "build": "tsdown",