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:
@@ -12,10 +12,6 @@ import { initializeLemonSqueezyClient } from './lemon-squeezy-sdk';
|
||||
|
||||
/**
|
||||
* Creates a checkout for a Lemon Squeezy product.
|
||||
*
|
||||
* @param {object} params - The parameters for creating the checkout.
|
||||
* @return {Promise<object>} - A promise that resolves to the created Lemon Squeezy checkout.
|
||||
* @throws {Error} - If no line items are found in the subscription.
|
||||
*/
|
||||
export async function createLemonSqueezyCheckout(
|
||||
params: z.infer<typeof CreateBillingCheckoutSchema>,
|
||||
|
||||
@@ -95,6 +95,8 @@ export class LemonSqueezyWebhookHandlerService
|
||||
onSubscriptionDeleted: (subscriptionId: string) => Promise<unknown>;
|
||||
onPaymentSucceeded: (sessionId: string) => Promise<unknown>;
|
||||
onPaymentFailed: (sessionId: string) => Promise<unknown>;
|
||||
onInvoicePaid: (data: UpsertSubscriptionParams) => Promise<unknown>;
|
||||
onEvent?: (event: string) => Promise<unknown>;
|
||||
},
|
||||
) {
|
||||
const eventName = event.meta.event_name;
|
||||
@@ -128,6 +130,13 @@ export class LemonSqueezyWebhookHandlerService
|
||||
);
|
||||
}
|
||||
|
||||
case 'subscription_payment_success': {
|
||||
return this.handleInvoicePaid(
|
||||
event as SubscriptionWebhook,
|
||||
params.onInvoicePaid,
|
||||
);
|
||||
}
|
||||
|
||||
default: {
|
||||
const logger = await getLogger();
|
||||
|
||||
@@ -276,6 +285,18 @@ export class LemonSqueezyWebhookHandlerService
|
||||
);
|
||||
}
|
||||
|
||||
private handleInvoicePaid(
|
||||
subscription: SubscriptionWebhook,
|
||||
onInvoicePaidCallback: (
|
||||
subscription: UpsertSubscriptionParams,
|
||||
) => Promise<unknown>,
|
||||
) {
|
||||
return this.handleSubscriptionCreatedEvent(
|
||||
subscription,
|
||||
onInvoicePaidCallback,
|
||||
);
|
||||
}
|
||||
|
||||
private handleSubscriptionDeletedEvent(
|
||||
subscription: SubscriptionWebhook,
|
||||
onSubscriptionDeletedCallback: (subscriptionId: string) => Promise<unknown>,
|
||||
|
||||
Reference in New Issue
Block a user