Add custom handlers for billing events
The code introduces custom handlers for different billing events like subscription deletion, subscription update, checkout session completion, payment successes and failures, invoice payment, and a generic event handler. These customer handlers allow consumers to add their own custom behaviors when certain billing events occur. This flexibility can be utilized to better adapt the system to various business requirements and rules. Also, the invoice payment event and a generic event handler were added.
This commit is contained in:
@@ -77,6 +77,8 @@ export class StripeWebhookHandlerService
|
||||
onSubscriptionDeleted: (subscriptionId: string) => Promise<unknown>;
|
||||
onPaymentSucceeded: (sessionId: string) => Promise<unknown>;
|
||||
onPaymentFailed: (sessionId: string) => Promise<unknown>;
|
||||
onInvoicePaid: (data: UpsertSubscriptionParams) => Promise<unknown>;
|
||||
onEvent: (eventType: string) => Promise<unknown>;
|
||||
},
|
||||
) {
|
||||
switch (event.type) {
|
||||
@@ -105,6 +107,10 @@ export class StripeWebhookHandlerService
|
||||
return this.handleAsyncPaymentFailed(event, params.onPaymentFailed);
|
||||
}
|
||||
|
||||
case 'invoice.paid': {
|
||||
return this.handleInvoicePaid(event, params.onInvoicePaid);
|
||||
}
|
||||
|
||||
case 'checkout.session.async_payment_succeeded': {
|
||||
return this.handleAsyncPaymentSucceeded(
|
||||
event,
|
||||
@@ -113,6 +119,10 @@ export class StripeWebhookHandlerService
|
||||
}
|
||||
|
||||
default: {
|
||||
if (params.onEvent) {
|
||||
return params.onEvent(event.type);
|
||||
}
|
||||
|
||||
const Logger = await getLogger();
|
||||
|
||||
Logger.info(
|
||||
@@ -315,6 +325,34 @@ export class StripeWebhookHandlerService
|
||||
trial_ends_at: getISOString(params.trialEndsAt),
|
||||
};
|
||||
}
|
||||
|
||||
private async handleInvoicePaid(
|
||||
event: Stripe.InvoicePaidEvent,
|
||||
onInvoicePaid: (params: UpsertSubscriptionParams) => Promise<unknown>,
|
||||
) {
|
||||
const stripe = await this.loadStripe();
|
||||
|
||||
const subscriptionId = event.data.object.subscription as string;
|
||||
const subscription = await stripe.subscriptions.retrieve(subscriptionId);
|
||||
|
||||
const accountId = subscription.metadata.accountId as string;
|
||||
|
||||
const payload = this.buildSubscriptionPayload({
|
||||
customerId: subscription.customer as string,
|
||||
id: subscriptionId,
|
||||
accountId,
|
||||
lineItems: subscription.items.data,
|
||||
status: subscription.status,
|
||||
currency: subscription.currency,
|
||||
periodStartsAt: subscription.current_period_start,
|
||||
periodEndsAt: subscription.current_period_end,
|
||||
cancelAtPeriodEnd: subscription.cancel_at_period_end,
|
||||
trialStartsAt: subscription.trial_start,
|
||||
trialEndsAt: subscription.trial_end,
|
||||
});
|
||||
|
||||
return onInvoicePaid(payload);
|
||||
}
|
||||
}
|
||||
|
||||
function getISOString(date: number | null) {
|
||||
|
||||
Reference in New Issue
Block a user