@@ -2,8 +2,8 @@ import type { Session, User } from "better-auth";
22import type { Organization } from "better-auth/plugins/organization" ;
33import type Chargebee from "chargebee" ;
44import type {
5- Event as ChargebeeEvent ,
65 Subscription as ChargebeeSubscription ,
6+ WebhookEvent as ChargebeeWebhookEvent ,
77 Customer ,
88 WebhookHandler ,
99} from "chargebee" ;
@@ -136,43 +136,68 @@ export type SubscriptionOptions = {
136136 ) => Promise < boolean > ;
137137} ;
138138
139- export type WebhookEvent = ChargebeeEvent ;
139+ export type WebhookEvent = ChargebeeWebhookEvent ;
140+
141+ /**
142+ * Event bus seam used to decouple webhook ingestion from processing.
143+ *
144+ * When provided via {@link ChargebeeOptions.webhookEventBus}, the webhook
145+ * endpoint validates and parses each incoming Chargebee event and then calls
146+ * `publish` instead of running the DB-sync hooks inline. The application is
147+ * expected to push the event onto its own queue and later process it from a
148+ * consumer using `createChargebeeWebhookProcessor`.
149+ */
150+ export interface ChargebeeWebhookEventBus {
151+ /** Called at the HTTP endpoint for every validated, parsed event. */
152+ publish ( event : WebhookEvent ) : Promise < void > | void ;
153+ }
140154
141155// Use native Chargebee customer creation params
142156export type ChargebeeCustomerCreateParams = Partial < Customer . CreateInputParam > ;
143157
144158export interface ChargebeeOptions {
145- chargebeeClient : InstanceType < typeof Chargebee > ;
146- webhookUsername ?: string ;
147- webhookPassword ?: string ;
148- createCustomerOnSignUp ?: boolean ;
149- /**
150- * Return additional params to pass to `cb.customer.create` for user customers.
151- * Use this to pass fields like `first_name`, `last_name`, or any other
152- * Chargebee customer params. The `ctx` argument is only available when the
153- * customer is created on-demand (e.g. at subscription time), not during sign-up.
154- */
155- getCustomerCreateParams ?: (
156- user : User ,
157- ctx ?: Record < string , unknown > ,
158- ) =>
159- | Promise < Partial < ChargebeeCustomerCreateParams > >
160- | Partial < ChargebeeCustomerCreateParams > ;
161- onCustomerCreate ?: ( params : CustomerCreateParams ) => Promise < void > | void ;
162- webhookHandler ?: ( handler : WebhookHandler ) => void ;
163- subscription ?: SubscriptionOptions ;
164- organization ?: {
165- enabled : boolean ;
159+ chargebeeClient : InstanceType < typeof Chargebee > ;
160+ webhookUsername ?: string ;
161+ webhookPassword ?: string ;
162+ createCustomerOnSignUp ?: boolean ;
163+ /**
164+ * Return additional params to pass to `cb.customer.create` for user customers.
165+ * Use this to pass fields like `first_name`, `last_name`, or any other
166+ * Chargebee customer params. The `ctx` argument is only available when the
167+ * customer is created on-demand (e.g. at subscription time), not during sign-up.
168+ */
166169 getCustomerCreateParams ?: (
167- organization : Organization & WithChargebeeCustomerId ,
168- ctx : Record < string , unknown > ,
169- ) => Promise < Partial < ChargebeeCustomerCreateParams > > ;
170- onCustomerCreate ?: (
171- params : OrganizationCustomerCreateParams ,
172- ctx : Record < string , unknown > ,
173- ) => Promise < void > | void ;
174- } ;
175- }
170+ user : User ,
171+ ctx ?: Record < string , unknown > ,
172+ ) =>
173+ | Promise < Partial < ChargebeeCustomerCreateParams > >
174+ | Partial < ChargebeeCustomerCreateParams > ;
175+ onCustomerCreate ?: ( params : CustomerCreateParams ) => Promise < void > | void ;
176+ webhookHandler ?: ( handler : WebhookHandler ) => void ;
177+ /**
178+ * Optional event bus used to decouple webhook ingestion from processing.
179+ *
180+ * When set, the webhook endpoint in the app is exptected to validate and
181+ * parses each event and calls `webhookEventBus.publish(event)` (typically pushing it onto an application
182+ * queue) instead of running the DB-sync hooks inline. Process queued events
183+ * later with `createChargebeeWebhookProcessor`.
184+ *
185+ * When not set, events are processed synchronously within the request.
186+ */
187+ webhookEventBus ?: ChargebeeWebhookEventBus ;
188+ subscription ?: SubscriptionOptions ;
189+ organization ?: {
190+ enabled : boolean ;
191+ getCustomerCreateParams ?: (
192+ organization : Organization & WithChargebeeCustomerId ,
193+ ctx : Record < string , unknown > ,
194+ ) => Promise < Partial < ChargebeeCustomerCreateParams > > ;
195+ onCustomerCreate ?: (
196+ params : OrganizationCustomerCreateParams ,
197+ ctx : Record < string , unknown > ,
198+ ) => Promise < void > | void ;
199+ } ;
200+ }
176201
177202export interface Subscription {
178203 id : string ;
0 commit comments