Update and refactor billing services and types

Several updates and refactorings have been made to the billing services and types. The "onInvoicePaid" method and some types related to line items have been removed. The comments and arguments for the "verifyWebhookSignature" and "handleWebhookEvent" methods in service classes have been updated for clarity. The "onEvent" method's arguments have also been adjusted in multiple places to improve consistency.
This commit is contained in:
gbuomprisco
2024-06-11 23:35:32 +08:00
parent ce18a342ba
commit bb6f26f240
5 changed files with 29 additions and 52 deletions

View File

@@ -5,22 +5,30 @@ import { UpsertOrderParams, UpsertSubscriptionParams } from '../types';
* @description Represents an abstract class for handling billing webhook events.
*/
export abstract class BillingWebhookHandlerService {
// Verifies the webhook signature - should throw an error if the signature is invalid
/**
* @name verifyWebhookSignature
* @description Verify the webhook signature
* @param request
*/
abstract verifyWebhookSignature(request: Request): Promise<unknown>;
/**
* @name handleWebhookEvent
* @description Handle the webhook event from the billing provider
* @param event
* @param params
*/
abstract handleWebhookEvent(
event: unknown,
params: {
// this method is called when a checkout session is completed
onCheckoutSessionCompleted: (
subscription: UpsertSubscriptionParams | UpsertOrderParams,
customerId: string,
) => Promise<unknown>;
// this method is called when a subscription is updated
onSubscriptionUpdated: (
subscription: UpsertSubscriptionParams,
customerId: string,
) => Promise<unknown>;
// this method is called when a subscription is deleted
@@ -30,15 +38,12 @@ export abstract class BillingWebhookHandlerService {
// one-time payments
onPaymentSucceeded: (sessionId: string) => Promise<unknown>;
// this method is called when an invoice is paid. This is used for recurring payments
onInvoicePaid: (data: UpsertSubscriptionParams) => Promise<unknown>;
// this method is called when a payment is failed. This is used for
// one-time payments
onPaymentFailed: (sessionId: string) => Promise<unknown>;
// generic handler for any event
onEvent?: <Data>(event: string, data: Data) => Promise<unknown>;
onEvent?: <Data>(data: Data) => Promise<unknown>;
},
): Promise<unknown>;
}

View File

@@ -1,24 +1,7 @@
import { Database } from '@kit/supabase/database';
type LineItems = Array<{
id: string;
quantity: number;
product_id: string;
variant_id: string;
price_amount: number;
}>;
export type UpsertSubscriptionParams =
Database['public']['Functions']['upsert_subscription']['Args'] & {
line_items: LineItems & {
interval: string;
subscription_id: string;
interval_count: number;
type: 'per_seat' | 'flat' | 'metered';
};
};
Database['public']['Functions']['upsert_subscription']['Args'];
export type UpsertOrderParams =
Database['public']['Functions']['upsert_order']['Args'] & {
line_items: LineItems;
};
Database['public']['Functions']['upsert_order']['Args'];

View File

@@ -26,8 +26,7 @@ interface CustomHandlersParams {
) => Promise<unknown>;
onPaymentSucceeded: (sessionId: string) => Promise<unknown>;
onPaymentFailed: (sessionId: string) => Promise<unknown>;
onInvoicePaid: (data: UpsertSubscriptionParams) => Promise<unknown>;
onEvent?: (event: string, data: unknown) => Promise<unknown>;
onEvent?: <Data>(data: Data) => Promise<unknown>;
}
/**
@@ -63,7 +62,7 @@ class BillingEventHandlerService {
*/
async handleWebhookEvent(
request: Request,
params: Partial<CustomHandlersParams> = {},
params: Partial<CustomHandlersParams> = {}
) {
const event = await this.strategy.verifyWebhookSignature(request);
@@ -274,24 +273,6 @@ class BillingEventHandlerService {
logger.info(ctx, 'Successfully updated payment status');
},
onInvoicePaid: async (data) => {
const logger = await getLogger();
const ctx = {
namespace: this.namespace,
subscriptionId: data.target_subscription_id,
};
logger.info(ctx, 'Processing invoice paid event...');
// by default we don't need to do anything here
// but we allow consumers to provide custom handlers for the event
if (params.onInvoicePaid) {
await params.onInvoicePaid(data);
}
logger.info(ctx, 'Invoice paid event processed successfully');
},
onEvent: params.onEvent,
});
}

View File

@@ -97,8 +97,7 @@ 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>;
onEvent?: (event: OrderWebhook | SubscriptionWebhook) => Promise<unknown>;
},
) {
const eventName = event.meta.event_name;
@@ -133,6 +132,10 @@ export class LemonSqueezyWebhookHandlerService
}
default: {
if (params.onEvent) {
return params.onEvent(event);
}
const logger = await getLogger();
logger.info(

View File

@@ -58,6 +58,12 @@ export class StripeWebhookHandlerService
return event;
}
/**
* @name handleWebhookEvent
* @description Handle the webhook event from the billing provider
* @param event
* @param params
*/
async handleWebhookEvent(
event: Stripe.Event,
params: {
@@ -70,8 +76,7 @@ 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>;
onEvent?: (event: Stripe.Event) => Promise<unknown>;
},
) {
switch (event.type) {
@@ -109,7 +114,7 @@ export class StripeWebhookHandlerService
default: {
if (params.onEvent) {
return params.onEvent(event.type);
return params.onEvent(event);
}
const Logger = await getLogger();