From 7dcd688ca90f90b62a57b4b8d90bbe9e447334b1 Mon Sep 17 00:00:00 2001 From: giancarlo Date: Sun, 14 Apr 2024 18:25:33 +0800 Subject: [PATCH] Update user billing flow and improve error handling Updated the user billing process to add a return to home function and redirect users to their specific account page after payment. Enhanced error handling in the billing event handler to handle scenarios where no account id is found in the subscription. Removed unused billing page object from the stripe page object. --- apps/e2e/tests/user-billing/user-billing.spec.ts | 5 ++++- apps/e2e/tests/utils/billing.po.ts | 1 + apps/e2e/tests/utils/stripe.po.ts | 3 --- .../home/[account]/billing/return/page.tsx | 16 ++++++++++++++-- .../billing-event-handler.service.ts | 9 +++++++++ .../services/stripe-webhook-handler.service.ts | 2 +- 6 files changed, 29 insertions(+), 7 deletions(-) diff --git a/apps/e2e/tests/user-billing/user-billing.spec.ts b/apps/e2e/tests/user-billing/user-billing.spec.ts index 34280b7fc..0d818ac19 100644 --- a/apps/e2e/tests/user-billing/user-billing.spec.ts +++ b/apps/e2e/tests/user-billing/user-billing.spec.ts @@ -20,8 +20,11 @@ test.describe('User Billing', () => { await po.billing.stripe.submitForm(); await expect(po.billing.successStatus()).toBeVisible(); + await po.billing.returnToHome(); - await page.goto('/home/billing'); + await page.locator('a', { + hasText: 'Billing', + }).click(); await expect(await po.billing.getStatus()).toContainText('active'); await expect(po.billing.manageBillingButton()).toBeVisible(); diff --git a/apps/e2e/tests/utils/billing.po.ts b/apps/e2e/tests/utils/billing.po.ts index e91f5d36d..f657f3a71 100644 --- a/apps/e2e/tests/utils/billing.po.ts +++ b/apps/e2e/tests/utils/billing.po.ts @@ -29,6 +29,7 @@ export class BillingPageObject { } async returnToHome() { + await this.page.waitForTimeout(500); await this.successStatus().locator('button').click(); } diff --git a/apps/e2e/tests/utils/stripe.po.ts b/apps/e2e/tests/utils/stripe.po.ts index 37113bdf2..e9498600f 100644 --- a/apps/e2e/tests/utils/stripe.po.ts +++ b/apps/e2e/tests/utils/stripe.po.ts @@ -1,13 +1,10 @@ import { Page, expect } from '@playwright/test'; -import { BillingPageObject } from './billing.po'; export class StripePageObject { private readonly page: Page; - public readonly billing: BillingPageObject; constructor(page: Page) { this.page = page; - this.billing = new BillingPageObject(page); } getStripeCheckoutIframe() { diff --git a/apps/web/app/(dashboard)/home/[account]/billing/return/page.tsx b/apps/web/app/(dashboard)/home/[account]/billing/return/page.tsx index 036c43e87..e7c5a7bf6 100644 --- a/apps/web/app/(dashboard)/home/[account]/billing/return/page.tsx +++ b/apps/web/app/(dashboard)/home/[account]/billing/return/page.tsx @@ -14,6 +14,10 @@ interface SessionPageProps { searchParams: { session_id: string; }; + + params: { + account: string; + }; } const LazyEmbeddedCheckout = dynamic( @@ -27,7 +31,10 @@ const LazyEmbeddedCheckout = dynamic( }, ); -async function ReturnCheckoutSessionPage({ searchParams }: SessionPageProps) { +async function ReturnCheckoutSessionPage({ + searchParams, + params, +}: SessionPageProps) { const sessionId = searchParams.session_id; if (!sessionId) { @@ -45,12 +52,17 @@ async function ReturnCheckoutSessionPage({ searchParams }: SessionPageProps) { ); } + const redirectPath = pathsConfig.app.accountHome.replace( + '[account]', + params.account, + ); + return ( <>
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 4e296e7ea..1701f7257 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 @@ -68,6 +68,15 @@ export class BillingEventHandlerService { logger.info(ctx, 'Processing subscription updated event ...'); + if (!subscription.target_account_id) { + logger.info( + `No account id found in subscription. This event may have arrived before we created a subscription. Exiting...`, + subscription, + ); + + return; + } + // Handle the subscription updated event // here we update the subscription in the database const { error } = await client.rpc('upsert_subscription', subscription); diff --git a/packages/billing/stripe/src/services/stripe-webhook-handler.service.ts b/packages/billing/stripe/src/services/stripe-webhook-handler.service.ts index 942c1a69b..6fd26675f 100644 --- a/packages/billing/stripe/src/services/stripe-webhook-handler.service.ts +++ b/packages/billing/stripe/src/services/stripe-webhook-handler.service.ts @@ -90,7 +90,7 @@ export class StripeWebhookHandlerService case 'customer.subscription.updated': { return this.handleSubscriptionUpdatedEvent( event, - params.onCheckoutSessionCompleted, + params.onSubscriptionUpdated, ); }