Line data Source code
1 : import { useTelemetry } from '@comfyorg/telemetry'
2 : import { useRoute, useRouter } from 'vue-router'
3 :
4 : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
5 : import { useSubscriptionDialog } from '@/platform/billing/subscription/composables/useSubscriptionDialog'
6 : import {
7 : clearPreservedQuery,
8 : hydratePreservedQuery,
9 : mergePreservedQueryIntoQuery
10 : } from '@/platform/navigation/preservedQueryManager'
11 : import { PRESERVED_QUERY_NAMESPACES } from '@/platform/navigation/preservedQueryNamespaces'
12 : import type { TeamCreditStops } from '@comfyorg/billing-core'
13 : import type { SubscriptionCheckoutSelection } from '@/platform/billing/composables/types'
14 : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
15 : import { useBillingUI } from '@/platform/billing/composables/useBillingUI'
16 :
17 2355 : const NAMESPACE = PRESERVED_QUERY_NAMESPACES.PRICING
18 :
19 0 : function isCheckoutTier(
20 : value: string
21 : ): value is Extract<
22 : SubscriptionCheckoutSelection,
23 : { planMode: 'personal' }
24 : >['tierKey'] {
25 0 : return value === 'standard' || value === 'creator' || value === 'pro'
26 : }
27 :
28 0 : function isBillingCycle(
29 : value: unknown
30 : ): value is SubscriptionCheckoutSelection['billingCycle'] {
31 0 : return value === 'monthly' || value === 'yearly'
32 : }
33 :
34 0 : function getTeamCheckoutRequest(
35 : pricing: string,
36 : stop: unknown,
37 : cycle: unknown
38 : ):
39 : | {
40 : stop: string
41 : billingCycle: SubscriptionCheckoutSelection['billingCycle']
42 : }
43 : | undefined {
44 0 : if (
45 : pricing !== 'team' ||
46 : typeof stop !== 'string' ||
47 : !stop ||
48 : !isBillingCycle(cycle)
49 : ) {
50 0 : return
51 : }
52 :
53 0 : return { stop, billingCycle: cycle }
54 : }
55 :
56 0 : export function getPricingCheckoutSelection(
57 : pricing: string,
58 : stop: unknown,
59 : cycle: unknown,
60 : teamCreditStops: TeamCreditStops | null
61 : ): SubscriptionCheckoutSelection | undefined {
62 0 : if (isCheckoutTier(pricing)) {
63 0 : if (stop !== undefined || !isBillingCycle(cycle)) return
64 :
65 0 : return {
66 : planMode: 'personal',
67 : tierKey: pricing,
68 : billingCycle: cycle
69 : }
70 : }
71 :
72 0 : const teamCheckoutRequest = getTeamCheckoutRequest(pricing, stop, cycle)
73 0 : if (!teamCheckoutRequest) return
74 :
75 0 : const catalogStop = teamCreditStops?.stops.find(
76 : (candidate) => candidate.id === teamCheckoutRequest.stop
77 : )
78 0 : if (!catalogStop) return
79 :
80 0 : return {
81 : planMode: 'team',
82 : stop: {
83 : id: catalogStop.id,
84 : credits: catalogStop.credits,
85 : usd: catalogStop[teamCheckoutRequest.billingCycle].list_price_cents / 100,
86 : discountedUsd:
87 : catalogStop[teamCheckoutRequest.billingCycle].price_cents / 100,
88 : periodChargeUsd: {
89 : monthly: catalogStop.monthly.period_charge_cents / 100,
90 : yearly: catalogStop.yearly.period_charge_cents / 100
91 : }
92 : },
93 : billingCycle: teamCheckoutRequest.billingCycle
94 : }
95 : }
96 :
97 0 : export function usePricingTableUrlLoader() {
98 0 : const route = useRoute()
99 0 : const router = useRouter()
100 0 : const subscriptionDialog = useSubscriptionDialog()
101 0 : const { teamCreditStops, fetchPlans } = useBillingContext()
102 0 : const { permissions, canOpenPricingSurface } = useBillingUI()
103 0 : const { initialize: initializeCapabilities } = useBillingCapabilities()
104 :
105 0 : function reportRefusal(
106 : reason: 'pricing_surface_unavailable' | 'cannot_manage_subscription',
107 : phase: 'before_catalog_fetch' | 'after_catalog_fetch'
108 : ) {
109 0 : useTelemetry()?.trackBillingEvent({
110 : operation: 'entry',
111 : stage: 'pricing_link_refused',
112 : outcome: 'pending',
113 : reason,
114 : phase
115 : })
116 : }
117 :
118 0 : async function loadPricingTableFromUrl() {
119 0 : hydratePreservedQuery(NAMESPACE)
120 0 : const query =
121 : mergePreservedQueryIntoQuery(NAMESPACE, route.query) ?? route.query
122 0 : const param = query.pricing
123 0 : if (
124 : param === undefined &&
125 : query.stop === undefined &&
126 : query.cycle === undefined
127 : ) {
128 0 : return
129 : }
130 :
131 0 : const cleanQuery = { ...query }
132 0 : delete cleanQuery.pricing
133 0 : delete cleanQuery.stop
134 0 : delete cleanQuery.cycle
135 0 : router.replace({ query: cleanQuery }).catch((error) => {
136 0 : console.warn(
137 : '[usePricingTableUrlLoader] Failed to clean URL params:',
138 : error
139 : )
140 : })
141 0 : clearPreservedQuery(NAMESPACE)
142 :
143 0 : if (typeof param !== 'string' || !param) return
144 :
145 0 : await initializeCapabilities()
146 0 : if (!permissions.value.canManageSubscription) {
147 0 : reportRefusal('cannot_manage_subscription', 'before_catalog_fetch')
148 0 : return
149 : }
150 0 : if (!canOpenPricingSurface.value) {
151 0 : reportRefusal('pricing_surface_unavailable', 'before_catalog_fetch')
152 0 : return
153 : }
154 :
155 0 : const teamCheckoutRequest = getTeamCheckoutRequest(
156 : param,
157 : query.stop,
158 : query.cycle
159 : )
160 0 : if (teamCheckoutRequest && !teamCreditStops.value) {
161 0 : try {
162 0 : await fetchPlans()
163 : } catch (error) {
164 0 : console.error(
165 : '[usePricingTableUrlLoader] Failed to load Team pricing plans:',
166 : error
167 : )
168 : }
169 :
170 : const canManageSubscription: unknown =
171 : permissions.value.canManageSubscription
172 0 : if (canManageSubscription !== true) {
173 0 : reportRefusal('cannot_manage_subscription', 'after_catalog_fetch')
174 0 : return
175 : }
176 : const canOpenPricing: unknown = canOpenPricingSurface.value
177 0 : if (canOpenPricing !== true) {
178 0 : reportRefusal('pricing_surface_unavailable', 'after_catalog_fetch')
179 0 : return
180 : }
181 : const availableTeamCreditStops: unknown = teamCreditStops.value
182 0 : if (!availableTeamCreditStops) {
183 0 : await subscriptionDialog.showPricingTable({
184 : reason: 'deep_link',
185 : sameTab: true,
186 : planMode: 'team'
187 : })
188 0 : return
189 : }
190 : }
191 :
192 0 : const initialCheckout = getPricingCheckoutSelection(
193 : param,
194 : query.stop,
195 : query.cycle,
196 : teamCreditStops.value
197 : )
198 0 : if (isCheckoutTier(param) && !initialCheckout) return
199 0 : if (teamCheckoutRequest && !initialCheckout) {
200 0 : await subscriptionDialog.showPricingTable({
201 : reason: 'deep_link',
202 : sameTab: true,
203 : planMode: 'team'
204 : })
205 0 : return
206 : }
207 0 : if (
208 : !initialCheckout &&
209 : (query.stop !== undefined || query.cycle !== undefined)
210 : ) {
211 0 : return
212 : }
213 :
214 0 : const planMode = initialCheckout
215 : ? initialCheckout.planMode
216 : : param === 'team' || param === 'personal'
217 : ? param
218 : : undefined
219 :
220 0 : if (!initialCheckout && !['1', 'team', 'personal'].includes(param)) return
221 : const canManageSubscription: unknown =
222 : permissions.value.canManageSubscription
223 0 : if (canManageSubscription !== true) return
224 :
225 0 : await subscriptionDialog.showPricingTable({
226 : reason: 'deep_link',
227 : sameTab: true,
228 : planMode,
229 : initialCheckout
230 : })
231 : }
232 :
233 0 : return {
234 : loadPricingTableFromUrl
235 : }
236 : }
|