Stripe plugin create a new customer on subscription.upgrade click.

I have 2 different buttons to subscription.upgrade in my app. After user is registered customer is created in stripe according to createCustomerOnSignUp: true, in plugin config. But when I navigate to one of my 'Subscribe' buttons and click on it -> subscription.upgrade just creates new user in stripe instead of triggering checkout session:
2025-04-20 14:38:59 --> customer.created [evt_1RG3hf4FS9Rcg327BHPwlIez]
2025-04-20 14:38:59 <-- [200] POST http://localhost:3000/api/auth/stripe/webhook [evt_1RG3hf4FS9Rcg327BHPwlIez]
2025-04-20 14:39:30 --> customer.created [evt_1RG3iA4FS9Rcg3277wHm3FS7]
2025-04-20 14:39:30 <-- [200] POST http://localhost:3000/api/auth/stripe/webhook [evt_1RG3iA4FS9Rcg3277wHm3FS7]
2025-04-20 14:39:58 --> customer.created [evt_1RG3ic4FS9Rcg327i3WXWvDt]
2025-04-20 14:39:58 <-- [200] POST http://localhost:3000/api/auth/stripe/webhook [evt_1RG3ic4FS9Rcg327i3WXWvDt]
2025-04-20 14:40:09 --> customer.created [evt_1RG3in4FS9Rcg327ktiFZ3YX]
2025-04-20 14:40:09 <-- [200] POST http://localhost:3000/api/auth/stripe/webhook [evt_1RG3in4FS9Rcg327ktiFZ3YX]
2025-04-20 14:38:59 --> customer.created [evt_1RG3hf4FS9Rcg327BHPwlIez]
2025-04-20 14:38:59 <-- [200] POST http://localhost:3000/api/auth/stripe/webhook [evt_1RG3hf4FS9Rcg327BHPwlIez]
2025-04-20 14:39:30 --> customer.created [evt_1RG3iA4FS9Rcg3277wHm3FS7]
2025-04-20 14:39:30 <-- [200] POST http://localhost:3000/api/auth/stripe/webhook [evt_1RG3iA4FS9Rcg3277wHm3FS7]
2025-04-20 14:39:58 --> customer.created [evt_1RG3ic4FS9Rcg327i3WXWvDt]
2025-04-20 14:39:58 <-- [200] POST http://localhost:3000/api/auth/stripe/webhook [evt_1RG3ic4FS9Rcg327i3WXWvDt]
2025-04-20 14:40:09 --> customer.created [evt_1RG3in4FS9Rcg327ktiFZ3YX]
2025-04-20 14:40:09 <-- [200] POST http://localhost:3000/api/auth/stripe/webhook [evt_1RG3in4FS9Rcg327ktiFZ3YX]
CustomerId also assigned properly to user initially after signup. Does anyone know why this happens and how to fix it ?
7 Replies
Duki
Duki•6mo ago
I experience the same behavior. Have you found out why this is happening yet?
Glen Kurio
Glen KurioOP•6mo ago
Still have no idea. No response. I just implemented Stripe separately. @bekacru Can you help us out with this?
Duki
Duki•4mo ago
I created an issue on this btw, because I am still experiencing it and it's quite annoying. https://github.com/better-auth/better-auth/issues/2440 BUT! I think I know why this is happening! @Glen Kurio Are you using a secondary storage for your sessions, like redis or something? Because I do have a secondary storage and I noticed that after a user registers, the customer gets created, but then the user in the session is not updated with the new customerId only the main db has this new entry. So when that user with the misaligned session tries to activate a subscription, their customerId is null, which is probably why a new customer is created, either by stripe or BA (that I dont know yet). @bekacru Is there something special we have to do to keep the user data in the session in the secondary storage in sync with the user data in DB after a registration (maybe not only after a registration, because what if createCustomerOnSignUp is set to false?)? Are we responsible to add a hook for this or something? Perhaps it's worth noting that I do the registration server-side.
GitHub
stripe subscription.upgrade creates a new customer every time · Is...
Is this suited for github? Yes, this is suited for github To Reproduce Setup BA with the stripe plugin Make sure you add createCustomerOnSignUp: true to the stripe plugin config Locally forward str...
Duki
Duki•4mo ago
@bekacru @Ping Sorry if I'm pinging you guys too often, but this issue has been around for a long time and it's quite annoying 🙈 Can you please help me out here? I'm too unfamiliar with BA's codebase. Could this issue maybe be fixed by calling await ctx.context.internalAdapter.update instead of await ctx.context.adapter.update in the BA "/subscription/upgrade" endpoint? At both lines packages/stripe/src/index.ts:242 & packages/stripe/src/index.ts:982. According to the docs the internalAdapter also takes care of updating the user data in the secondary storage, wheras the adapter simply updates the DB. Could this be the issue? I tried to find a workaround with a BA hook, to manually update the user in my secondary storage after a customer is created, but I somehow can't get the current session token. Both the session and newSession are null in the after or before hook
if (ctx.path === '/stripe/webhook') {
if (ctx.body?.type === 'customer.created') {
// ctx.session and ctx.newSession are null here
// There is also no session cookie in the headers here
}
}
if (ctx.path === '/stripe/webhook') {
if (ctx.body?.type === 'customer.created') {
// ctx.session and ctx.newSession are null here
// There is also no session cookie in the headers here
}
}
Also when I try to update the user myself via
await ctx.context.internalAdapter.updateUserByEmail(
customerData.email,
{
stripeCustomerId: customerData.id
},
ctx
);
await ctx.context.internalAdapter.updateUserByEmail(
customerData.email,
{
stripeCustomerId: customerData.id
},
ctx
);
It won't do anything to the secondary storage.
bekacru
bekacru•4mo ago
will get it fixed for 1.2.9 - tonight or tmrw
Duki
Duki•3mo ago
I just upgraded and tested it again. The bug is still there. The stripeCustomerId still remains null in the secondary storage after signing in. 🙈 Just upgraded to 1.3.3and cannot reproduce this anymore. WOOH! Good job team BA! 🥳 Actually @bekacru the underlying issue that the stripeCustomerId in the secondary storage isn't updated after a signUp still remains. This leads to my user data in locals (in SvelteKit) to be inconsistent with the data from the db. I explained it a bit further in this thread here: https://discord.com/channels/1288403910284935179/1363601316202680432/1380533147741192302 And a possible solution here: https://discord.com/channels/1288403910284935179/1363601316202680432/1381170380495913001 What do you think @bekacru ?
bekacru
bekacru•3mo ago
sorry for the late repsponse here. Opened a PR. This should fix it I think https://github.com/better-auth/better-auth/pull/3635
GitHub
fix(stripe): update customer id should also trigger secondary stora...
Summary by cubic Updating a user&amp;#39;s Stripe customer ID now also updates the secondary storage to keep user data in sync. Refactors Removed the old Customer type and related code. Switched ...

Did you find this page helpful?