From 8fde758671b50b2cf838e65095371c770e8372fc Mon Sep 17 00:00:00 2001 From: giancarlo Date: Tue, 16 Apr 2024 12:32:15 +0800 Subject: [PATCH] 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. --- .../billing-webhook-handler.service.ts | 2 +- .../billing-event-handler.service.ts | 33 +++++++++++++++---- .../lemon-squeezy-webhook-handler.service.ts | 14 ++++---- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/packages/billing/core/src/services/billing-webhook-handler.service.ts b/packages/billing/core/src/services/billing-webhook-handler.service.ts index c69869338..ac7574a58 100644 --- a/packages/billing/core/src/services/billing-webhook-handler.service.ts +++ b/packages/billing/core/src/services/billing-webhook-handler.service.ts @@ -36,7 +36,7 @@ export abstract class BillingWebhookHandlerService { // one-time payments onPaymentSucceeded: (sessionId: string) => Promise; - // 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; // this method is called when a payment is failed. This is used for diff --git a/packages/billing/gateway/src/server/services/billing-event-handler/billing-event-handler.service.ts b/packages/billing/gateway/src/server/services/billing-event-handler/billing-event-handler.service.ts index 06251a007..f8e233a32 100644 --- a/packages/billing/gateway/src/server/services/billing-event-handler/billing-event-handler.service.ts +++ b/packages/billing/gateway/src/server/services/billing-event-handler/billing-event-handler.service.ts @@ -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; onSubscriptionUpdated: ( - subscription: Database['public']['Functions']['upsert_subscription']['Args'], + subscription: UpsertSubscriptionParams, ) => Promise; onCheckoutSessionCompleted: ( - subscription: - | Database['public']['Functions']['upsert_subscription']['Args'] - | Database['public']['Functions']['upsert_order']['Args'], + subscription: UpsertSubscriptionParams | UpsertOrderParams, customerId: string, ) => Promise; onPaymentSucceeded: (sessionId: string) => Promise; onPaymentFailed: (sessionId: string) => Promise; - onInvoicePaid: ( - data: Database['public']['Functions']['upsert_subscription']['Args'], - ) => Promise; + onInvoicePaid: (data: UpsertSubscriptionParams) => Promise; onEvent?: (event: string, data: unknown) => Promise; } diff --git a/packages/billing/lemon-squeezy/src/services/lemon-squeezy-webhook-handler.service.ts b/packages/billing/lemon-squeezy/src/services/lemon-squeezy-webhook-handler.service.ts index b30c3aed3..55d4fdfab 100644 --- a/packages/billing/lemon-squeezy/src/services/lemon-squeezy-webhook-handler.service.ts +++ b/packages/billing/lemon-squeezy/src/services/lemon-squeezy-webhook-handler.service.ts @@ -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), }; });