@better-auth/stripe 1.2.2 → 1.2.3-beta.2

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,5 +1,5 @@
1
1
 
2
- > @better-auth/stripe@1.2.2 build /home/runner/work/better-auth/better-auth/packages/stripe
2
+ > @better-auth/stripe@1.2.3-beta.2 build /home/runner/work/better-auth/better-auth/packages/stripe
3
3
  > unbuild
4
4
 
5
5
  [info] Automatically detected entries: src/index, src/client [esm] [cjs] [dts]
@@ -9,9 +9,9 @@
9
9
 
10
10
  [log] dist/client.cjs (total size: 160 B, chunk size: 160 B, exports: stripeClient)
11
11
 
12
- [log] dist/index.mjs (total size: 30.4 kB, chunk size: 30.4 kB, exports: stripe)
12
+ [log] dist/index.mjs (total size: 30.3 kB, chunk size: 30.3 kB, exports: stripe)
13
13
 
14
14
  [log] dist/client.mjs (total size: 133 B, chunk size: 133 B, exports: stripeClient)
15
15
 
16
- Σ Total dist size (byte size): 171 kB
16
+ Σ Total dist size (byte size): 170 kB
17
17
  [log]
package/dist/index.cjs CHANGED
@@ -107,12 +107,19 @@ async function onSubscriptionUpdated(ctx, options, event) {
107
107
  where: [{ field: "stripeCustomerId", value: customerId }]
108
108
  });
109
109
  if (subs.length > 1) {
110
- betterAuth.logger.warn(
111
- `Stripe webhook error: Multiple subscriptions found for customerId: ${customerId} and no referenceId or subscriptionId is provided`
110
+ const activeSub = subs.find(
111
+ (sub) => sub.status === "active" || sub.status === "trialing"
112
112
  );
113
- return;
113
+ if (!activeSub) {
114
+ betterAuth.logger.warn(
115
+ `Stripe webhook error: Multiple subscriptions found for customerId: ${customerId} and no active subscription is found`
116
+ );
117
+ return;
118
+ }
119
+ subscription = activeSub;
120
+ } else {
121
+ subscription = subs[0];
114
122
  }
115
- subscription = subs[0];
116
123
  }
117
124
  const seats = subscriptionUpdated.items.data[0].quantity;
118
125
  await ctx.context.adapter.update({
@@ -132,8 +139,8 @@ async function onSubscriptionUpdated(ctx, options, event) {
132
139
  },
133
140
  where: [
134
141
  {
135
- field: "referenceId",
136
- value: subscription.referenceId
142
+ field: "id",
143
+ value: subscription.id
137
144
  }
138
145
  ]
139
146
  });
@@ -152,13 +159,7 @@ async function onSubscriptionUpdated(ctx, options, event) {
152
159
  });
153
160
  if (plan) {
154
161
  if (subscriptionUpdated.status === "active" && subscription.status === "trialing" && plan.freeTrial?.onTrialEnd) {
155
- const user = await ctx.context.adapter.findOne({
156
- model: "user",
157
- where: [{ field: "id", value: subscription.referenceId }]
158
- });
159
- if (user) {
160
- await plan.freeTrial.onTrialEnd({ subscription, user }, ctx.request);
161
- }
162
+ await plan.freeTrial.onTrialEnd({ subscription }, ctx.request);
162
163
  }
163
164
  if (subscriptionUpdated.status === "incomplete_expired" && subscription.status === "trialing" && plan.freeTrial?.onTrialExpired) {
164
165
  await plan.freeTrial.onTrialExpired(subscription, ctx.request);
@@ -189,8 +190,8 @@ async function onSubscriptionDeleted(ctx, options, event) {
189
190
  model: "subscription",
190
191
  where: [
191
192
  {
192
- field: "stripeSubscriptionId",
193
- value: subscriptionId
193
+ field: "id",
194
+ value: subscription.id
194
195
  }
195
196
  ],
196
197
  update: {
package/dist/index.d.cts CHANGED
@@ -64,7 +64,6 @@ type Plan = {
64
64
  */
65
65
  onTrialEnd?: (data: {
66
66
  subscription: Subscription;
67
- user: User & Record<string, any>;
68
67
  }, request?: Request) => Promise<void>;
69
68
  /**
70
69
  * A function that will be called when the trial
package/dist/index.d.mts CHANGED
@@ -64,7 +64,6 @@ type Plan = {
64
64
  */
65
65
  onTrialEnd?: (data: {
66
66
  subscription: Subscription;
67
- user: User & Record<string, any>;
68
67
  }, request?: Request) => Promise<void>;
69
68
  /**
70
69
  * A function that will be called when the trial
package/dist/index.d.ts CHANGED
@@ -64,7 +64,6 @@ type Plan = {
64
64
  */
65
65
  onTrialEnd?: (data: {
66
66
  subscription: Subscription;
67
- user: User & Record<string, any>;
68
67
  }, request?: Request) => Promise<void>;
69
68
  /**
70
69
  * A function that will be called when the trial
package/dist/index.mjs CHANGED
@@ -105,12 +105,19 @@ async function onSubscriptionUpdated(ctx, options, event) {
105
105
  where: [{ field: "stripeCustomerId", value: customerId }]
106
106
  });
107
107
  if (subs.length > 1) {
108
- logger.warn(
109
- `Stripe webhook error: Multiple subscriptions found for customerId: ${customerId} and no referenceId or subscriptionId is provided`
108
+ const activeSub = subs.find(
109
+ (sub) => sub.status === "active" || sub.status === "trialing"
110
110
  );
111
- return;
111
+ if (!activeSub) {
112
+ logger.warn(
113
+ `Stripe webhook error: Multiple subscriptions found for customerId: ${customerId} and no active subscription is found`
114
+ );
115
+ return;
116
+ }
117
+ subscription = activeSub;
118
+ } else {
119
+ subscription = subs[0];
112
120
  }
113
- subscription = subs[0];
114
121
  }
115
122
  const seats = subscriptionUpdated.items.data[0].quantity;
116
123
  await ctx.context.adapter.update({
@@ -130,8 +137,8 @@ async function onSubscriptionUpdated(ctx, options, event) {
130
137
  },
131
138
  where: [
132
139
  {
133
- field: "referenceId",
134
- value: subscription.referenceId
140
+ field: "id",
141
+ value: subscription.id
135
142
  }
136
143
  ]
137
144
  });
@@ -150,13 +157,7 @@ async function onSubscriptionUpdated(ctx, options, event) {
150
157
  });
151
158
  if (plan) {
152
159
  if (subscriptionUpdated.status === "active" && subscription.status === "trialing" && plan.freeTrial?.onTrialEnd) {
153
- const user = await ctx.context.adapter.findOne({
154
- model: "user",
155
- where: [{ field: "id", value: subscription.referenceId }]
156
- });
157
- if (user) {
158
- await plan.freeTrial.onTrialEnd({ subscription, user }, ctx.request);
159
- }
160
+ await plan.freeTrial.onTrialEnd({ subscription }, ctx.request);
160
161
  }
161
162
  if (subscriptionUpdated.status === "incomplete_expired" && subscription.status === "trialing" && plan.freeTrial?.onTrialExpired) {
162
163
  await plan.freeTrial.onTrialExpired(subscription, ctx.request);
@@ -187,8 +188,8 @@ async function onSubscriptionDeleted(ctx, options, event) {
187
188
  model: "subscription",
188
189
  where: [
189
190
  {
190
- field: "stripeSubscriptionId",
191
- value: subscriptionId
191
+ field: "id",
192
+ value: subscription.id
192
193
  }
193
194
  ],
194
195
  update: {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@better-auth/stripe",
3
3
  "author": "Bereket Engida",
4
- "version": "1.2.2",
4
+ "version": "1.2.3-beta.2",
5
5
  "main": "dist/index.cjs",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "zod": "^3.24.1",
38
- "better-auth": "^1.2.2"
38
+ "better-auth": "^1.2.3-beta.2"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/better-sqlite3": "^7.6.12",
package/src/hooks.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { logger, type GenericEndpointContext, type User } from "better-auth";
1
+ import { logger, type GenericEndpointContext } from "better-auth";
2
2
  import type Stripe from "stripe";
3
3
  import type { InputSubscription, StripeOptions, Subscription } from "./types";
4
4
  import { getPlanByPriceId } from "./utils";
@@ -109,12 +109,19 @@ export async function onSubscriptionUpdated(
109
109
  where: [{ field: "stripeCustomerId", value: customerId }],
110
110
  });
111
111
  if (subs.length > 1) {
112
- logger.warn(
113
- `Stripe webhook error: Multiple subscriptions found for customerId: ${customerId} and no referenceId or subscriptionId is provided`,
112
+ const activeSub = subs.find(
113
+ (sub) => sub.status === "active" || sub.status === "trialing",
114
114
  );
115
- return;
115
+ if (!activeSub) {
116
+ logger.warn(
117
+ `Stripe webhook error: Multiple subscriptions found for customerId: ${customerId} and no active subscription is found`,
118
+ );
119
+ return;
120
+ }
121
+ subscription = activeSub;
122
+ } else {
123
+ subscription = subs[0];
116
124
  }
117
- subscription = subs[0];
118
125
  }
119
126
 
120
127
  const seats = subscriptionUpdated.items.data[0].quantity;
@@ -137,8 +144,8 @@ export async function onSubscriptionUpdated(
137
144
  },
138
145
  where: [
139
146
  {
140
- field: "referenceId",
141
- value: subscription.referenceId,
147
+ field: "id",
148
+ value: subscription.id,
142
149
  },
143
150
  ],
144
151
  });
@@ -165,13 +172,7 @@ export async function onSubscriptionUpdated(
165
172
  subscription.status === "trialing" &&
166
173
  plan.freeTrial?.onTrialEnd
167
174
  ) {
168
- const user = await ctx.context.adapter.findOne<User>({
169
- model: "user",
170
- where: [{ field: "id", value: subscription.referenceId }],
171
- });
172
- if (user) {
173
- await plan.freeTrial.onTrialEnd({ subscription, user }, ctx.request);
174
- }
175
+ await plan.freeTrial.onTrialEnd({ subscription }, ctx.request);
175
176
  }
176
177
  if (
177
178
  subscriptionUpdated.status === "incomplete_expired" &&
@@ -211,8 +212,8 @@ export async function onSubscriptionDeleted(
211
212
  model: "subscription",
212
213
  where: [
213
214
  {
214
- field: "stripeSubscriptionId",
215
- value: subscriptionId,
215
+ field: "id",
216
+ value: subscription.id,
216
217
  },
217
218
  ],
218
219
  update: {
package/src/types.ts CHANGED
@@ -61,7 +61,6 @@ export type Plan = {
61
61
  onTrialEnd?: (
62
62
  data: {
63
63
  subscription: Subscription;
64
- user: User & Record<string, any>;
65
64
  },
66
65
  request?: Request,
67
66
  ) => Promise<void>;