Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
69926cd
FE-3140 refactor(billing): move the billing telemetry event contract …
dante01yoon Oct 1, 2026
be72e3e
FE-3140 refactor(billing): move the checkout journey event contract t…
dante01yoon Oct 1, 2026
9951086
FE-3140 refactor(billing): import the billing event contract from acc…
dante01yoon Oct 1, 2026
3b41c16
FE-3140 feat(billing): add the billing web payload builder and guard …
dante01yoon Oct 1, 2026
d6cd070
FE-3140 feat(billing-web): take the typed billing event in the teleme…
dante01yoon Oct 1, 2026
c24ff3d
FE-3140 refactor(telemetry): accept every billing event name as a tel…
dante01yoon Oct 1, 2026
bd1f0bb
FE-3115 feat(billing): add the entry event to the billing telemetry c…
dante01yoon Oct 1, 2026
64b8b33
test(billing): expect paywall impressions and add-credits clicks to r…
dante01yoon Oct 1, 2026
7817b69
FE-3115 feat(billing): report paywall impressions and add-credits cli…
dante01yoon Oct 1, 2026
a7ed781
FE-3120 feat(billing-web): report the entry link once per tab entry (…
dante01yoon Oct 1, 2026
d1ba9a0
Merge remote-tracking branch 'origin/main' into jaewon/fe-3140-move-t…
dante01yoon Oct 1, 2026
6ea5270
Merge remote-tracking branch 'origin/jaewon/fe-3140-move-the-billing-…
dante01yoon Oct 1, 2026
ce06a6f
test(billing-web): call the typed billing event in the RUM user tests
dante01yoon Oct 1, 2026
91a0b81
Merge remote-tracking branch 'origin/jaewon/fe-3140-move-the-billing-…
dante01yoon Oct 1, 2026
97dcd6e
FE-3120 feat(billing-web): report the session journey and the code a …
dante01yoon Oct 1, 2026
3cf3017
Merge remote-tracking branch 'origin/jaewon/fe-3140-move-the-billing-…
dante01yoon Oct 1, 2026
ee54b2c
FE-3115 feat(billing): carry the entry source through top-up, downgra…
dante01yoon Oct 1, 2026
38f7e8b
FE-3120 feat(billing-web): report a click on each way back to the hos…
dante01yoon Oct 1, 2026
cbbe1c4
FE-3117 feat(billing): share the SDK operation mapper and report it f…
dante01yoon Oct 2, 2026
06036c2
Merge remote-tracking branch 'origin/jaewon/fe-3140-move-the-billing-…
dante01yoon Oct 2, 2026
5063e1b
Merge remote-tracking branch 'origin/main' into jaewon/fe-3115-cloud-…
dante01yoon Oct 2, 2026
880ac9b
test(billing): expect a confirmed cancel flow's fallback to stay quie…
dante01yoon Oct 2, 2026
e1ba955
FE-3115 fix(billing): keep a confirmed cancel flow's fallback quiet a…
dante01yoon Oct 2, 2026
a7fa732
FE-3117 feat(billing-web): report the cancel flow's intent, abandoned…
dante01yoon Oct 2, 2026
ca4c510
Merge remote-tracking branch 'origin/main' into work-fe3115
dante01yoon Oct 3, 2026
9460619
FE-3121 feat(billing): report portal opens, failures and returns on t…
dante01yoon Oct 3, 2026
83f87ca
Merge remote-tracking branch 'origin/main' into fe3115
dante01yoon Oct 3, 2026
f906990
test: expect portal events from the payment recovery portal
dante01yoon Oct 3, 2026
5952377
feat: report payment recovery portal opens, failures and returns
dante01yoon Oct 3, 2026
b3e4590
Merge remote-tracking branch 'origin/main' into work-19973
dante01yoon Oct 3, 2026
4aa1e68
test: cover a blocked recovery retry and set up rails per row
dante01yoon Oct 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ describe('the portal events', () => {
outcome: 'pending',
target: 'manage_subscription'
}
},
{
name: 'the portal opening to recover a failed payment',
event: {
operation: 'portal',
stage: 'opened',
outcome: 'pending',
target: 'payment_recovery',
billing_client: 'legacy'
},
eventName: 'billing.portal.opened',
payload: {
operation: 'portal',
stage: 'opened',
outcome: 'pending',
target: 'payment_recovery',
billing_client: 'legacy'
}
}
])('names and reports $name', ({ event, eventName, payload }) => {
expect(getBillingTelemetryEventName(event)).toBe(eventName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import type { BillingFailed } from './stages.js'

/** The part of the provider's hosted portal the customer was sent to. */
/**
* The part of the provider's hosted portal the customer was sent to.
* `payment_recovery` is a checkout that stopped on a failed or outstanding
* payment and sent the customer to settle it.
*/
export type BillingPortalTarget =
| 'payment_methods'
| 'invoices'
| 'manage_subscription'
| 'payment_recovery'

/**
* A trip to the provider's hosted portal. What the customer does there (adding,
Expand Down
201 changes: 201 additions & 0 deletions src/platform/workspace/composables/useSubscriptionCheckout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,207 @@ describe('useSubscriptionCheckout', () => {
})
})

describe('payment recovery portal telemetry', () => {
function portalEvents() {
return (vi.mocked(useTelemetry()?.trackBillingEvent)?.mock.calls ?? [])
.map(([event]) => event)
.filter((event) => event.operation === 'portal')
}

function leaveAndReturn() {
window.dispatchEvent(new Event('blur'))
window.dispatchEvent(new Event('focus'))
}

function useRailPortal(outcome: SubscriptionRailOutcome<string>) {
mockSubscriptionRail.value = railStub({
openPaymentPortal: vi
.fn<NonNullable<SubscriptionRailStub['openPaymentPortal']>>()
.mockResolvedValue(outcome)
})
}

it.for<{
name: string
arrange: () => void
billingClient: 'sdk' | 'legacy'
}>([
{
name: 'the legacy client',
arrange: () => {},
billingClient: 'legacy'
},
{
name: 'the SDK rail',
arrange: () =>
useRailPortal({
status: 'ok',
value: 'https://billing.stripe.com/rail-portal'
}),
billingClient: 'sdk'
},
{
name: 'the legacy client when the rail route is not deployed',
arrange: () => useRailPortal({ status: 'unavailable' }),
billingClient: 'legacy'
}
])(
'reports the recovery portal opening from $name and one return',
async ({ arrange, billingClient }) => {
arrange()

await submitRejectedPreview('SUBSCRIPTION_PAYMENT_REQUIRED')
leaveAndReturn()
leaveAndReturn()

expect(portalEvents()).toEqual([
{
operation: 'portal',
stage: 'opened',
outcome: 'pending',
target: 'payment_recovery',
billing_client: billingClient
},
{
operation: 'portal',
stage: 'returned',
outcome: 'pending',
target: 'payment_recovery',
billing_client: billingClient
}
])
}
)

it('reports a blocked recovery tab as a failed open and the retry as the open', async () => {
mockOpen.mockReturnValueOnce(null)
await submitRejectedPreview('SUBSCRIPTION_PAYMENT_REQUIRED')

expect(portalEvents()).toEqual([
{
operation: 'portal',
stage: 'failed',
outcome: 'failure',
target: 'payment_recovery',
billing_client: 'legacy',
failure_category: 'redirect',
error_code: 'payment_popup_blocked'
}
])

const [{ detail }] = mockToastAdd.mock.calls.at(-1)!
mockOpen.mockReturnValueOnce({})
detail.onAction()
leaveAndReturn()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

expect(portalEvents().slice(1)).toEqual([
{
operation: 'portal',
stage: 'opened',
outcome: 'pending',
target: 'payment_recovery',
billing_client: 'legacy'
},
{
operation: 'portal',
stage: 'returned',
outcome: 'pending',
target: 'payment_recovery',
billing_client: 'legacy'
}
])
})

it('reports a retry that is blocked again as a second failed open', async () => {
mockOpen.mockReturnValueOnce(null)
await submitRejectedPreview('SUBSCRIPTION_PAYMENT_REQUIRED')

const [{ detail }] = mockToastAdd.mock.calls.at(-1)!
mockOpen.mockReturnValueOnce(null)
detail.onAction()
leaveAndReturn()

const blocked = {
operation: 'portal',
stage: 'failed',
outcome: 'failure',
target: 'payment_recovery',
billing_client: 'legacy',
failure_category: 'redirect',
error_code: 'payment_popup_blocked'
}
expect(portalEvents()).toEqual([blocked, blocked])
})

it.for<{
name: string
arrange: () => void
failure: Record<string, unknown>
}>([
{
name: 'a refused legacy portal request',
arrange: () =>
mockGetPaymentPortalUrl.mockRejectedValueOnce(
new WorkspaceApiError('Forbidden', 403)
),
failure: { failure_category: 'api_rejected' }
},
{
name: 'a legacy portal request that never reached the server',
arrange: () =>
mockGetPaymentPortalUrl.mockRejectedValueOnce(
new WorkspaceApiError('Network down')
),
failure: { failure_category: 'network' }
},
{
name: 'a rail portal failure',
arrange: () =>
useRailPortal({
status: 'error',
error: new WorkspaceApiError('Forbidden', 403)
}),
failure: { failure_category: 'api_rejected' }
},
{
name: 'a portal address that fails validation',
arrange: () =>
mockGetPaymentPortalUrl.mockResolvedValueOnce({
url: 'https://billing.stripe.com.evil.test/portal'
}),
failure: { billing_client: 'legacy', failure_category: 'unknown' }
}
])(
'reports $name as a failed open and keeps the error report',
async ({ arrange, failure }) => {
arrange()

await submitRejectedPreview('SUBSCRIPTION_PAYMENT_REQUIRED')
leaveAndReturn()

expect(portalEvents()).toEqual([
{
operation: 'portal',
stage: 'failed',
outcome: 'failure',
target: 'payment_recovery',
...failure
}
])
expect(mockReportError).toHaveBeenCalledWith(expect.any(Error), {
surface: 'workspace',
errorType: 'billing_portal_open_failure'
})
}
)

it('reports nothing when the rejection needs no payment recovery', async () => {
await submitRejectedPreview('TRANSITION_NOT_ALLOWED')

expect(portalEvents()).toEqual([])
})
})

it('shows error toast when plan slug is not found', async () => {
const checkout = await setup()
mockPlans.value = []
Expand Down
30 changes: 23 additions & 7 deletions src/platform/workspace/composables/useSubscriptionCheckout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
BillingClient,
CheckoutEntryFlow,
CheckoutJourneyPhaseEvent,
SubscriptionCheckoutType
Expand All @@ -22,6 +23,7 @@ import { useTelemetry } from '@/platform/telemetry'
import { reportError } from '@/platform/telemetry/reportError'
import type { PaymentIntentSource } from '@/platform/telemetry/types'
import { categorizeBillingApiError } from '@/platform/telemetry/utils/billingFailureCategory'
import { createBillingPortalReporter } from '@/platform/telemetry/utils/billingPortalTelemetry'
import { api } from '@/scripts/api'
import { useAuthStore } from '@/stores/authStore'
import type {
Expand Down Expand Up @@ -575,13 +577,18 @@ export function useSubscriptionCheckout(
* a failure throws into the caller's catch, where a legacy throw already
* lands.
*/
async function readPaymentPortalUrl(returnUrl: string): Promise<string> {
async function readPaymentPortalUrl(
returnUrl: string
): Promise<{ url: string; billingClient: BillingClient }> {
if (subscriptionRail) {
const outcome = await subscriptionRail.openPaymentPortal(returnUrl)
if (outcome.status === 'ok') return outcome.value
if (outcome.status === 'ok') {
return { url: outcome.value, billingClient: 'sdk' }
}
if (outcome.status === 'error') throw outcome.error
}
return (await workspaceApi.getPaymentPortalUrl(returnUrl)).url
const { url } = await workspaceApi.getPaymentPortalUrl(returnUrl)
return { url, billingClient: 'legacy' }
}

async function recoverOutstandingPayment(
Expand All @@ -608,11 +615,13 @@ export function useSubscriptionCheckout(
}
if (!requiresRecovery || !isCurrent()) return null

const portal = createBillingPortalReporter(telemetry, 'payment_recovery')
let billingClient: BillingClient | undefined
try {
const returnUrl = `${globalThis.location.origin}${globalThis.location.pathname}`
const portalUrl = parseBillingPortalUrl(
await readPaymentPortalUrl(returnUrl)
)
const portalResponse = await readPaymentPortalUrl(returnUrl)
billingClient = portalResponse.billingClient
const portalUrl = parseBillingPortalUrl(portalResponse.url)
if (!isCurrent()) return null
if (!portalUrl) {
throw new Error(
Expand All @@ -623,6 +632,7 @@ export function useSubscriptionCheckout(
}
const paymentWindow = window.open(portalUrl.href, '_blank')
if (!paymentWindow) {
portal.blocked(billingClient)
// The open above ran after an await, so it had no user gesture behind
// it and got blocked. The toast's own button click is a gesture, so
// retrying from there isn't blocked.
Expand All @@ -637,17 +647,23 @@ export function useSubscriptionCheckout(
// checkout reset); a stale click must not reopen its captured URL.
onAction: () => {
if (!isCurrent()) return
window.open(portalUrl.href, '_blank')
if (window.open(portalUrl.href, '_blank')) {
portal.opened(billingClient)
} else {
portal.blocked(billingClient)
}
armPaymentRecoveryReturnRefresh()
}
}
})
return 'blocked'
}
portal.opened(billingClient)
armPaymentRecoveryReturnRefresh()
return 'opened'
} catch (portalError) {
if (!isCurrent()) return null
portal.failed(portalError, billingClient)
reportError(portalError, {
surface: 'workspace',
errorType: 'billing_portal_open_failure'
Expand Down
Loading