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
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>;
// 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 { 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
* @description Allow consumers to provide custom handlers for the billing events
@@ -14,19 +37,15 @@ import { Database } from '@kit/supabase/database';
interface CustomHandlersParams {
onSubscriptionDeleted: (subscriptionId: string) => Promise<unknown>;
onSubscriptionUpdated: (
subscription: Database['public']['Functions']['upsert_subscription']['Args'],
subscription: UpsertSubscriptionParams,
) => Promise<unknown>;
onCheckoutSessionCompleted: (
subscription:
| Database['public']['Functions']['upsert_subscription']['Args']
| Database['public']['Functions']['upsert_order']['Args'],
subscription: UpsertSubscriptionParams | UpsertOrderParams,
customerId: string,
) => Promise<unknown>;
onPaymentSucceeded: (sessionId: string) => Promise<unknown>;
onPaymentFailed: (sessionId: string) => Promise<unknown>;
onInvoicePaid: (
data: Database['public']['Functions']['upsert_subscription']['Args'],
) => Promise<unknown>;
onInvoicePaid: (data: UpsertSubscriptionParams) => Promise<unknown>;
onEvent?: (event: string, data: unknown) => Promise<unknown>;
}

View File

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