Line data Source code
1 : /**
2 : * Layer C of the billing rollout: which origin a hosted billing action opens.
3 : *
4 : * The server decides, through `hosted_billing_destination`. `stripe` means the
5 : * URL the backend already handed back — `action_url`, `payment_method_url`, a
6 : * portal link — is opened unchanged, so the host never composes a provider
7 : * URL of its own. `billing_web` means the host mints a `/v1/<intent>` entry
8 : * from `@comfyorg/billing-contract`, the one definition of that route surface.
9 : *
10 : * Every hosted surface reads the same flag, so they flip together and stay
11 : * independent of Layer A: whether an action reached here over the SDK rail or
12 : * the legacy client says nothing about where it should land.
13 : *
14 : * Anything unresolved fails closed to the provider — no configured billing-web
15 : * origin, an environment whose return target has no destination, an entry the
16 : * contract refuses to build. A customer on the provider page is served; one on
17 : * an origin nobody hosts, or with no way back, is stranded.
18 : */
19 : import type {
20 : BillingEntryInput,
21 : BillingEnvironment,
22 : BillingIntent,
23 : ReturnTarget
24 : } from '@comfyorg/billing-contract'
25 : import {
26 : buildBillingEntryUrl,
27 : resolveReturnTarget
28 : } from '@comfyorg/billing-contract'
29 : import type { DeployEnv } from '@comfyorg/shared-frontend-utils/telemetry'
30 :
31 : import type { HostedBillingDestination } from '@/platform/billing/billingWeb'
32 : import { getBillingWebUrl } from '@/platform/billing/billingWeb'
33 : import { getComfyCloudBaseUrl } from '@/config/comfyApi'
34 : import { resolveDeployEnv } from '@comfyorg/telemetry/rumContext'
35 :
36 : export type HostedBillingRoute =
37 : | { readonly kind: 'billing_web'; readonly url: URL }
38 : | { readonly kind: 'provider' }
39 :
40 : /** The caller-supplied part of the entry, as opposed to the environment the host resolves for itself. */
41 : export type HostedBillingRouteFields = Pick<
42 : BillingEntryInput,
43 : 'plan' | 'workspaceId' | 'teamCreditStopId' | 'correlationId' | 'source'
44 : >
45 :
46 2356 : const PROVIDER: HostedBillingRoute = { kind: 'provider' }
47 :
48 2356 : const PRODUCT = 'comfyui'
49 2356 : const RETURN_TO: ReturnTarget = 'comfyui_workspace'
50 :
51 2356 : const BILLING_ENVIRONMENT_BY_DEPLOY_ENV: Readonly<
52 : Record<DeployEnv, BillingEnvironment>
53 : > = {
54 : 'prod-v2': 'production',
55 : 'stg-v2': 'staging',
56 : 'test-v2': 'test'
57 : }
58 :
59 0 : function hostnameOf(url: string): string | undefined {
60 0 : try {
61 0 : return new URL(url).hostname
62 : } catch {
63 0 : return undefined
64 : }
65 : }
66 :
67 : /**
68 : * The family the session was minted against, not the label on this build: a
69 : * developer running the frontend locally still talks to a deployed backend,
70 : * so the cloud base URL classifies the trip and `window.location` cannot. A
71 : * base URL that will not parse classifies nothing, so it fails closed like any
72 : * other unresolved environment rather than throwing through the caller.
73 : */
74 0 : export function hostBillingEnvironment(
75 : cloudBaseUrl = getComfyCloudBaseUrl()
76 : ): BillingEnvironment | undefined {
77 0 : const hostname = hostnameOf(cloudBaseUrl)
78 0 : if (hostname === undefined) return undefined
79 :
80 0 : const deployEnv = resolveDeployEnv(hostname)
81 0 : return deployEnv === undefined
82 : ? undefined
83 : : BILLING_ENVIRONMENT_BY_DEPLOY_ENV[deployEnv]
84 : }
85 :
86 : /**
87 : * Re-bases a contract route under the configured billing-web path, which the
88 : * builder drops because an entry URL is only ever routing input. A prefixed
89 : * deployment (`https://host/billing/`) serves `/v1/<intent>` beneath its own
90 : * prefix, so the origin alone would point at nothing.
91 : */
92 0 : function underBase(base: URL, entry: URL): URL {
93 0 : const prefix = base.pathname.replace(/\/$/, '')
94 0 : return new URL(`${prefix}${entry.pathname}${entry.search}`, base.origin)
95 : }
96 :
97 0 : export function hostedBillingRoute(
98 : destination: HostedBillingDestination,
99 : intent: BillingIntent,
100 : fields: HostedBillingRouteFields = {},
101 : billingWebBase: URL | null = getBillingWebUrl(),
102 : environment: BillingEnvironment | undefined = hostBillingEnvironment()
103 : ): HostedBillingRoute {
104 0 : if (destination !== 'billing_web' || !billingWebBase) return PROVIDER
105 0 : if (!environment || !resolveReturnTarget(RETURN_TO, environment)) {
106 0 : return PROVIDER
107 : }
108 :
109 0 : const entry = buildBillingEntryUrl({
110 : ...fields,
111 : billingOrigin: billingWebBase,
112 : intent,
113 : product: PRODUCT,
114 : returnTo: RETURN_TO
115 : })
116 0 : return entry.status === 'ok'
117 : ? { kind: 'billing_web', url: underBase(billingWebBase, entry.url) }
118 : : PROVIDER
119 : }
|