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:
@@ -5,22 +5,30 @@ import { UpsertOrderParams, UpsertSubscriptionParams } from '../types';
|
|||||||
* @description Represents an abstract class for handling billing webhook events.
|
* @description Represents an abstract class for handling billing webhook events.
|
||||||
*/
|
*/
|
||||||
export abstract class BillingWebhookHandlerService {
|
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>;
|
abstract verifyWebhookSignature(request: Request): Promise<unknown>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name handleWebhookEvent
|
||||||
|
* @description Handle the webhook event from the billing provider
|
||||||
|
* @param event
|
||||||
|
* @param params
|
||||||
|
*/
|
||||||
abstract handleWebhookEvent(
|
abstract handleWebhookEvent(
|
||||||
event: unknown,
|
event: unknown,
|
||||||
params: {
|
params: {
|
||||||
// this method is called when a checkout session is completed
|
// this method is called when a checkout session is completed
|
||||||
onCheckoutSessionCompleted: (
|
onCheckoutSessionCompleted: (
|
||||||
subscription: UpsertSubscriptionParams | UpsertOrderParams,
|
subscription: UpsertSubscriptionParams | UpsertOrderParams,
|
||||||
customerId: string,
|
|
||||||
) => Promise<unknown>;
|
) => Promise<unknown>;
|
||||||
|
|
||||||
// this method is called when a subscription is updated
|
// this method is called when a subscription is updated
|
||||||
onSubscriptionUpdated: (
|
onSubscriptionUpdated: (
|
||||||
subscription: UpsertSubscriptionParams,
|
subscription: UpsertSubscriptionParams,
|
||||||
customerId: string,
|
|
||||||
) => Promise<unknown>;
|
) => Promise<unknown>;
|
||||||
|
|
||||||
// this method is called when a subscription is deleted
|
// this method is called when a subscription is deleted
|
||||||
@@ -30,15 +38,12 @@ export abstract class BillingWebhookHandlerService {
|
|||||||
// one-time payments
|
// one-time payments
|
||||||
onPaymentSucceeded: (sessionId: string) => Promise<unknown>;
|
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
|
// this method is called when a payment is failed. This is used for
|
||||||
// one-time payments
|
// one-time payments
|
||||||
onPaymentFailed: (sessionId: string) => Promise<unknown>;
|
onPaymentFailed: (sessionId: string) => Promise<unknown>;
|
||||||
|
|
||||||
// generic handler for any event
|
// generic handler for any event
|
||||||
onEvent?: <Data>(event: string, data: Data) => Promise<unknown>;
|
onEvent?: <Data>(data: Data) => Promise<unknown>;
|
||||||
},
|
},
|
||||||
): Promise<unknown>;
|
): Promise<unknown>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,7 @@
|
|||||||
import { Database } from '@kit/supabase/database';
|
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 =
|
export type UpsertSubscriptionParams =
|
||||||
Database['public']['Functions']['upsert_subscription']['Args'] & {
|
Database['public']['Functions']['upsert_subscription']['Args'];
|
||||||
line_items: LineItems & {
|
|
||||||
interval: string;
|
|
||||||
subscription_id: string;
|
|
||||||
interval_count: number;
|
|
||||||
type: 'per_seat' | 'flat' | 'metered';
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export type UpsertOrderParams =
|
export type UpsertOrderParams =
|
||||||
Database['public']['Functions']['upsert_order']['Args'] & {
|
Database['public']['Functions']['upsert_order']['Args'];
|
||||||
line_items: LineItems;
|
|
||||||
};
|
|
||||||
@@ -26,8 +26,7 @@ interface CustomHandlersParams {
|
|||||||
) => Promise<unknown>;
|
) => Promise<unknown>;
|
||||||
onPaymentSucceeded: (sessionId: string) => Promise<unknown>;
|
onPaymentSucceeded: (sessionId: string) => Promise<unknown>;
|
||||||
onPaymentFailed: (sessionId: string) => Promise<unknown>;
|
onPaymentFailed: (sessionId: string) => Promise<unknown>;
|
||||||
onInvoicePaid: (data: UpsertSubscriptionParams) => Promise<unknown>;
|
onEvent?: <Data>(data: Data) => Promise<unknown>;
|
||||||
onEvent?: (event: string, data: unknown) => Promise<unknown>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,7 +62,7 @@ class BillingEventHandlerService {
|
|||||||
*/
|
*/
|
||||||
async handleWebhookEvent(
|
async handleWebhookEvent(
|
||||||
request: Request,
|
request: Request,
|
||||||
params: Partial<CustomHandlersParams> = {},
|
params: Partial<CustomHandlersParams> = {}
|
||||||
) {
|
) {
|
||||||
const event = await this.strategy.verifyWebhookSignature(request);
|
const event = await this.strategy.verifyWebhookSignature(request);
|
||||||
|
|
||||||
@@ -274,24 +273,6 @@ class BillingEventHandlerService {
|
|||||||
|
|
||||||
logger.info(ctx, 'Successfully updated payment status');
|
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,
|
onEvent: params.onEvent,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,8 +97,7 @@ export class LemonSqueezyWebhookHandlerService
|
|||||||
onSubscriptionDeleted: (subscriptionId: string) => Promise<unknown>;
|
onSubscriptionDeleted: (subscriptionId: string) => Promise<unknown>;
|
||||||
onPaymentSucceeded: (sessionId: string) => Promise<unknown>;
|
onPaymentSucceeded: (sessionId: string) => Promise<unknown>;
|
||||||
onPaymentFailed: (sessionId: string) => Promise<unknown>;
|
onPaymentFailed: (sessionId: string) => Promise<unknown>;
|
||||||
onInvoicePaid: (data: UpsertSubscriptionParams) => Promise<unknown>;
|
onEvent?: (event: OrderWebhook | SubscriptionWebhook) => Promise<unknown>;
|
||||||
onEvent?: (event: string) => Promise<unknown>;
|
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const eventName = event.meta.event_name;
|
const eventName = event.meta.event_name;
|
||||||
@@ -133,6 +132,10 @@ export class LemonSqueezyWebhookHandlerService
|
|||||||
}
|
}
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
|
if (params.onEvent) {
|
||||||
|
return params.onEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
const logger = await getLogger();
|
const logger = await getLogger();
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
|
|||||||
@@ -58,6 +58,12 @@ export class StripeWebhookHandlerService
|
|||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name handleWebhookEvent
|
||||||
|
* @description Handle the webhook event from the billing provider
|
||||||
|
* @param event
|
||||||
|
* @param params
|
||||||
|
*/
|
||||||
async handleWebhookEvent(
|
async handleWebhookEvent(
|
||||||
event: Stripe.Event,
|
event: Stripe.Event,
|
||||||
params: {
|
params: {
|
||||||
@@ -70,8 +76,7 @@ export class StripeWebhookHandlerService
|
|||||||
onSubscriptionDeleted: (subscriptionId: string) => Promise<unknown>;
|
onSubscriptionDeleted: (subscriptionId: string) => Promise<unknown>;
|
||||||
onPaymentSucceeded: (sessionId: string) => Promise<unknown>;
|
onPaymentSucceeded: (sessionId: string) => Promise<unknown>;
|
||||||
onPaymentFailed: (sessionId: string) => Promise<unknown>;
|
onPaymentFailed: (sessionId: string) => Promise<unknown>;
|
||||||
onInvoicePaid: (data: UpsertSubscriptionParams) => Promise<unknown>;
|
onEvent?: (event: Stripe.Event) => Promise<unknown>;
|
||||||
onEvent: (eventType: string) => Promise<unknown>;
|
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
@@ -109,7 +114,7 @@ export class StripeWebhookHandlerService
|
|||||||
|
|
||||||
default: {
|
default: {
|
||||||
if (params.onEvent) {
|
if (params.onEvent) {
|
||||||
return params.onEvent(event.type);
|
return params.onEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Logger = await getLogger();
|
const Logger = await getLogger();
|
||||||
|
|||||||
Reference in New Issue
Block a user