Refactor billing event handler and fix minor bugs

Refactored the billing-event-handler.service.ts by introducing new types `UpsertSubscriptionParams` and `UpsertOrderParams` to simplify method parameters. Fixed minor bugs including the correction of an error message in lemon-squeezy-webhook-handler.service.ts and adjusting the handling of attribute types. Also updated comment in billing-webhook-handler.service.ts for better clarity.
This commit is contained in:
giancarlo
2024-04-16 12:32:15 +08:00
parent ebb8fc08fe
commit 8fde758671
3 changed files with 34 additions and 15 deletions

View File

@@ -36,7 +36,7 @@ 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 // this method is called when an invoice is paid. This is used for recurring payments
onInvoicePaid: (data: UpsertSubscriptionParams) => Promise<unknown>; 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

View File

@@ -6,6 +6,29 @@ import { BillingWebhookHandlerService } from '@kit/billing';
import { getLogger } from '@kit/shared/logger'; import { getLogger } from '@kit/shared/logger';
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;
}>;
type UpsertSubscriptionParams =
Database['public']['Functions']['upsert_subscription']['Args'] & {
line_items: LineItems & {
interval: string;
subscription_id: string;
interval_count: number;
type: 'per_seat' | 'flat' | 'metered';
};
};
type UpsertOrderParams =
Database['public']['Functions']['upsert_order']['Args'] & {
line_items: LineItems;
};
/** /**
* @name CustomHandlersParams * @name CustomHandlersParams
* @description Allow consumers to provide custom handlers for the billing events * @description Allow consumers to provide custom handlers for the billing events
@@ -14,19 +37,15 @@ import { Database } from '@kit/supabase/database';
interface CustomHandlersParams { interface CustomHandlersParams {
onSubscriptionDeleted: (subscriptionId: string) => Promise<unknown>; onSubscriptionDeleted: (subscriptionId: string) => Promise<unknown>;
onSubscriptionUpdated: ( onSubscriptionUpdated: (
subscription: Database['public']['Functions']['upsert_subscription']['Args'], subscription: UpsertSubscriptionParams,
) => Promise<unknown>; ) => Promise<unknown>;
onCheckoutSessionCompleted: ( onCheckoutSessionCompleted: (
subscription: subscription: UpsertSubscriptionParams | UpsertOrderParams,
| Database['public']['Functions']['upsert_subscription']['Args']
| Database['public']['Functions']['upsert_order']['Args'],
customerId: string, customerId: string,
) => 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: ( onInvoicePaid: (data: UpsertSubscriptionParams) => Promise<unknown>;
data: Database['public']['Functions']['upsert_subscription']['Args'],
) => Promise<unknown>;
onEvent?: (event: string, data: unknown) => Promise<unknown>; onEvent?: (event: string, data: unknown) => Promise<unknown>;
} }

View File

@@ -145,7 +145,7 @@ export class LemonSqueezyWebhookHandlerService
eventType: eventName, eventType: eventName,
name: this.namespace, name: this.namespace,
}, },
`Unhandle Lemon Squeezy event type`, `Unhandled Lemon Squeezy event type`,
); );
return; return;
@@ -190,9 +190,9 @@ export class LemonSqueezyWebhookHandlerService
total_amount: attrs.first_order_item.price, total_amount: attrs.first_order_item.price,
line_items: [ line_items: [
{ {
id: attrs.first_order_item.id, id: attrs.first_order_item.id.toString(),
product_id: attrs.first_order_item.product_id, product_id: attrs.first_order_item.product_id.toString(),
variant_id: attrs.first_order_item.variant_id, variant_id: attrs.first_order_item.variant_id.toString(),
price_amount: attrs.first_order_item.price, price_amount: attrs.first_order_item.price,
quantity: 1, quantity: 1,
}, },
@@ -248,7 +248,7 @@ export class LemonSqueezyWebhookHandlerService
product: productId.toString(), product: productId.toString(),
variant: variantId.toString(), variant: variantId.toString(),
quantity: order.data.attributes.first_order_item.quantity, quantity: order.data.attributes.first_order_item.quantity,
unitAmount: order.data.attributes.first_order_item.price, priceAmount: order.data.attributes.first_order_item.price,
}, },
]; ];
@@ -312,7 +312,7 @@ export class LemonSqueezyWebhookHandlerService
quantity: number; quantity: number;
product: string; product: string;
variant: string; variant: string;
unitAmount: number; priceAmount: number;
}, },
>(params: { >(params: {
id: string; id: string;
@@ -342,7 +342,7 @@ export class LemonSqueezyWebhookHandlerService
subscription_id: params.id, subscription_id: params.id,
product_id: item.product, product_id: item.product,
variant_id: item.variant, variant_id: item.variant,
price_amount: item.unitAmount, price_amount: item.priceAmount,
type: getLineItemTypeById(this.config, item.id), type: getLineItemTypeById(this.config, item.id),
}; };
}); });