Line data Source code
1 : import type { BillingEnvironment } from '@comfyorg/billing-contract'
2 :
3 : import { getBillingWebUrl } from '@/platform/billing/billingWeb'
4 : import { isCloud } from '@/platform/distribution/types'
5 : import { hostBillingEnvironment } from '@/platform/billing/hostedBillingRoutes'
6 :
7 2356 : const TOPUP_RETURN_PATH = '/return/topup'
8 :
9 2356 : const BILLING_WEB_ORIGINS: Readonly<Record<BillingEnvironment, string>> = {
10 : production: 'https://billing.comfy.org',
11 : staging: 'https://stagingbilling.comfy.org',
12 : test: 'https://testbilling.comfy.org'
13 : }
14 :
15 : interface TopupCheckoutReturnContext {
16 : readonly servesCloudWeb: boolean
17 : readonly currentUrl: string
18 : readonly billingWebBase: URL | null
19 : readonly environment: BillingEnvironment | undefined
20 : }
21 :
22 0 : function currentContext(): TopupCheckoutReturnContext {
23 0 : return {
24 : servesCloudWeb: isCloud && window.__comfyDesktop2 === undefined,
25 : currentUrl: window.location.href,
26 : billingWebBase: getBillingWebUrl(),
27 : environment: hostBillingEnvironment()
28 : }
29 : }
30 :
31 : /**
32 : * Where hosted top-up checkout returns: the current page on cloud web, else
33 : * billing-web's neutral return page; undefined when no billing-web origin resolves.
34 : */
35 0 : export function topupCheckoutReturnUrl(
36 : context: TopupCheckoutReturnContext = currentContext()
37 : ): string | undefined {
38 0 : if (context.servesCloudWeb) return context.currentUrl
39 :
40 0 : const base =
41 : context.billingWebBase ??
42 : (context.environment && new URL(BILLING_WEB_ORIGINS[context.environment]))
43 0 : if (!base) return undefined
44 :
45 0 : const prefix = base.pathname.replace(/\/$/, '')
46 0 : return new URL(`${prefix}${TOPUP_RETURN_PATH}`, base.origin).href
47 : }
|