From 21feaa8509842259ebddd46739fa634afb07b72e Mon Sep 17 00:00:00 2001 From: Bereket Engida Date: Thu, 6 Mar 2025 20:27:38 +0300 Subject: [PATCH] fix(stripe): call onCustomerCreate callback handle error logging --- packages/stripe/src/index.ts | 41 +++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/packages/stripe/src/index.ts b/packages/stripe/src/index.ts index 38612d118f..c540ca2624 100644 --- a/packages/stripe/src/index.ts +++ b/packages/stripe/src/index.ts @@ -1,6 +1,7 @@ import { type GenericEndpointContext, type BetterAuthPlugin, + logger, } from "better-auth"; import { createAuthEndpoint, createAuthMiddleware } from "better-auth/plugins"; import Stripe from "stripe"; @@ -17,7 +18,12 @@ import { onSubscriptionDeleted, onSubscriptionUpdated, } from "./hooks"; -import type { InputSubscription, StripeOptions, Subscription } from "./types"; +import type { + Customer, + InputSubscription, + StripeOptions, + Subscription, +} from "./types"; import { getPlanByName, getPlanByPriceId, getPlans } from "./utils"; import { getSchema } from "./schema"; @@ -760,18 +766,29 @@ export const stripe = (options: O) => { userId: user.id, }, }); - await ctx.context.adapter.update({ - model: "user", - update: { - stripeCustomerId: stripeCustomer.id, - }, - where: [ - { - field: "id", - value: user.id, + const customer = await ctx.context.adapter.update( + { + model: "user", + update: { + stripeCustomerId: stripeCustomer.id, }, - ], - }); + where: [ + { + field: "id", + value: user.id, + }, + ], + }, + ); + if (!customer) { + logger.error("#BETTER_AUTH: Failed to create customer"); + } else { + await options.onCustomerCreate?.({ + customer, + stripeCustomer, + user, + }); + } } }, },