LCOV - code coverage report
Current view: top level - src/platform/billing - openHostedBillingTab.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 11.6 % 43 5
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : /**
       2              :  * The one place that opens a hosted billing tab. Every entry point — the
       3              :  * avatar menu's Plans and pricing, the workspace settings' Billing &
       4              :  * invoices, and a future in-app subscribe CTA — routes through here so the
       5              :  * workspace the host is showing is always the one billing-web operates on,
       6              :  * and a return to the app always refreshes the same state.
       7              :  */
       8              : import type { BillingIntent } from '@comfyorg/billing-contract'
       9              : 
      10              : import { useFeatureFlags } from '@/composables/useFeatureFlags'
      11              : import { useTelemetry } from '@comfyorg/telemetry'
      12              : import type { PaymentIntentSource } from '@comfyorg/telemetry/types'
      13              : import { reserveBillingTab } from '@/platform/billing/billingTab'
      14              : import { refreshBillingAccount } from '@/platform/billing/billingAccountRefresh'
      15              : import { readCheckoutOperation } from '@/platform/billing/billingRefresh'
      16              : import { hostedBillingRoute } from '@/platform/billing/hostedBillingRoutes'
      17              : import { registerRefreshOnReturn } from '@/platform/billing/refreshOnReturn'
      18              : import { useWorkspaceIdentity } from '@/platform/workspace/ports/workspaceIdentity'
      19              : import { createJourneyId } from '@/platform/billing/utils/checkoutJourney'
      20              : 
      21              : export interface OpenHostedBillingTabOptions {
      22              :   readonly plan?: string
      23              :   readonly teamCreditStopId?: string
      24              :   /** Why the customer asked, when the click knew. */
      25              :   readonly source?: PaymentIntentSource
      26              :   /** The cloud journey this hands off; one is minted when the entry has none. */
      27              :   readonly journeyId?: string
      28              : }
      29              : 
      30         2356 : let stopReturnRefresh: (() => void) | null = null
      31         2356 : let stopOperationWatch: (() => void) | null = null
      32              : 
      33            0 : export function disarmHostedBillingReturnRefresh(): void {
      34            0 :   stopReturnRefresh?.()
      35            0 :   stopReturnRefresh = null
      36            0 :   stopOperationWatch?.()
      37            0 :   stopOperationWatch = null
      38              : }
      39              : 
      40         2356 : const OPERATION_INTENTS: ReadonlySet<BillingIntent> = new Set([
      41              :   'checkout',
      42              :   'subscription'
      43              : ])
      44         2356 : const OPERATION_POLL_MS = 4_000
      45         2356 : const OPERATION_WATCH_MS = 15 * 60_000
      46              : 
      47              : /**
      48              :  * A payment billing-web takes never pushes back to this tab, but a status read
      49              :  * resumes the operation the server reports pending, and that operation then
      50              :  * polls itself and shows the same progress and outcome toasts the embedded
      51              :  * checkout shows. This reads until one is found, for the workspace the tab
      52              :  * was opened for only. A read still in flight skips the tick, and a failed
      53              :  * read waits for the next one.
      54              :  */
      55            0 : function watchForHostedOperation(
      56              :   readOperation: () => Promise<boolean>,
      57              :   workspaceId: string | undefined
      58              : ) {
      59            0 :   const workspaceStore = useWorkspaceIdentity()
      60            0 :   const startedAt = Date.now()
      61            0 :   let reading = false
      62            0 :   const timer = setInterval(() => {
      63            0 :     if (
      64              :       Date.now() - startedAt >= OPERATION_WATCH_MS ||
      65              :       (workspaceStore.activeWorkspaceId ?? undefined) !== workspaceId
      66              :     ) {
      67            0 :       clearInterval(timer)
      68            0 :       return
      69              :     }
      70            0 :     if (reading) return
      71            0 :     reading = true
      72            0 :     void readOperation()
      73              :       .then((found) => {
      74            0 :         if (found) clearInterval(timer)
      75              :       })
      76              :       .catch(() => undefined)
      77              :       .finally(() => {
      78            0 :         reading = false
      79              :       })
      80              :   }, OPERATION_POLL_MS)
      81            0 :   return () => clearInterval(timer)
      82              : }
      83              : 
      84            0 : function armReturnRefresh(
      85              :   intent: BillingIntent,
      86              :   workspaceId: string | undefined
      87              : ): void {
      88            0 :   disarmHostedBillingReturnRefresh()
      89            0 :   stopReturnRefresh = registerRefreshOnReturn(refreshBillingAccount)
      90            0 :   if (OPERATION_INTENTS.has(intent)) {
      91            0 :     stopOperationWatch = watchForHostedOperation(
      92              :       readCheckoutOperation,
      93              :       workspaceId
      94              :     )
      95              :   }
      96              : }
      97              : 
      98              : export type HostedBillingTabOutcome = 'opened' | 'unavailable' | 'blocked'
      99              : 
     100              : /**
     101              :  * Opens `intent` in a hosted billing tab carrying the active workspace, and
     102              :  * arms the same-state refresh for when the customer comes back. Reports
     103              :  * `unavailable` when the destination isn't billing_web and `blocked` when the
     104              :  * browser refused the tab.
     105              :  */
     106            0 : export function openHostedBillingTabOutcome(
     107              :   intent: BillingIntent,
     108              :   options: OpenHostedBillingTabOptions = {}
     109              : ): HostedBillingTabOutcome {
     110            0 :   const { flags } = useFeatureFlags()
     111            0 :   const workspaceId = useWorkspaceIdentity().activeWorkspaceId ?? undefined
     112            0 :   const correlationId = options.journeyId ?? createJourneyId()
     113            0 :   const route = hostedBillingRoute(flags.hostedBillingDestination, intent, {
     114              :     plan: options.plan,
     115              :     teamCreditStopId: options.teamCreditStopId,
     116              :     workspaceId,
     117              :     correlationId,
     118              :     source: options.source
     119              :   })
     120            0 :   if (route.kind !== 'billing_web') return 'unavailable'
     121            0 :   const tab = reserveBillingTab()
     122            0 :   tab?.navigate(route.url.href)
     123            0 :   const result = tab ? 'opened' : 'blocked'
     124            0 :   useTelemetry()?.trackBillingEvent({
     125              :     operation: 'web_handoff',
     126              :     stage: 'opened',
     127              :     outcome: 'pending',
     128              :     intent,
     129              :     result,
     130              :     payment_intent_source: options.source,
     131              :     correlation_id: correlationId
     132              :   })
     133            0 :   if (result === 'opened') armReturnRefresh(intent, workspaceId)
     134            0 :   return result
     135              : }
     136              : 
     137              : /**
     138              :  * {@link openHostedBillingTabOutcome} for callers whose fallback is the same
     139              :  * whether the destination isn't billing_web or the tab was blocked.
     140              :  */
     141            0 : export function openHostedBillingTab(
     142              :   intent: BillingIntent,
     143              :   options: OpenHostedBillingTabOptions = {}
     144              : ): boolean {
     145            0 :   return openHostedBillingTabOutcome(intent, options) === 'opened'
     146              : }
        

Generated by: LCOV version 2.0-1