Add handling for paid invoices in billing services
In response to paid invoices, a new callback method has been introduced in the billing services. This feature includes changes in billing-webhook-handler.service.ts, lemon-squeezy-webhook-handler.service.ts, and other related files. The new method fetches and handles paid invoice information from Stripe and LemonSqueezy subscriptions.
This commit is contained in:
@@ -88,7 +88,10 @@ export class StripeWebhookHandlerService
|
||||
onSubscriptionDeleted: (subscriptionId: string) => Promise<unknown>;
|
||||
onPaymentSucceeded: (sessionId: string) => Promise<unknown>;
|
||||
onPaymentFailed: (sessionId: string) => Promise<unknown>;
|
||||
onEvent?: (event: Stripe.Event) => Promise<unknown>;
|
||||
onInvoicePaid: (
|
||||
data: UpsertSubscriptionParams,
|
||||
) => Promise<unknown>;
|
||||
onEvent?(event: Stripe.Event): Promise<unknown>;
|
||||
},
|
||||
) {
|
||||
switch (event.type) {
|
||||
@@ -124,6 +127,10 @@ export class StripeWebhookHandlerService
|
||||
);
|
||||
}
|
||||
|
||||
case 'invoice.paid': {
|
||||
return this.handleInvoicePaid(event, params.onInvoicePaid);
|
||||
}
|
||||
|
||||
default: {
|
||||
if (params.onEvent) {
|
||||
return params.onEvent(event);
|
||||
@@ -285,6 +292,45 @@ export class StripeWebhookHandlerService
|
||||
return onSubscriptionDeletedCallback(event.data.object.id);
|
||||
}
|
||||
|
||||
private async handleInvoicePaid(
|
||||
event: Stripe.InvoicePaidEvent,
|
||||
onInvoicePaid: (
|
||||
data: UpsertSubscriptionParams,
|
||||
) => Promise<unknown>,
|
||||
) {
|
||||
const stripe = await this.loadStripe();
|
||||
|
||||
const invoice = event.data.object;
|
||||
const subscriptionId = invoice.subscription as string;
|
||||
|
||||
const subscription = await stripe.subscriptions.retrieve(subscriptionId, {
|
||||
expand: ['line_items'],
|
||||
});
|
||||
|
||||
const accountId = subscription.metadata.accountId as string;
|
||||
|
||||
const subscriptionPayloadBuilderService =
|
||||
createStripeSubscriptionPayloadBuilderService();
|
||||
|
||||
const payload = subscriptionPayloadBuilderService
|
||||
.withBillingConfig(this.config)
|
||||
.build({
|
||||
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);
|
||||
}
|
||||
|
||||
private async loadStripe() {
|
||||
if (!this.stripe) {
|
||||
this.stripe = await createStripeClient();
|
||||
|
||||
Reference in New Issue
Block a user