export type SelfServiceSubscriptionSelection = { readonly productId: string; readonly priceId: string; readonly billingPeriod: number; readonly currency: string; readonly officeSeatQuantity: number; readonly fieldSeatQuantity: number; readonly storageAddOnCodes: readonly string[]; readonly emailMarketingAddOnCode?: string | null; }; export type CheckoutRequest = { readonly fingerprint: string; readonly key: string; readonly createdAt: number }; type CheckoutRequestStorage = Pick; const CHECKOUT_REQUEST_STORAGE_PREFIX = "hivegrid.billing.checkout-request.v1"; const CHECKOUT_REQUEST_MAX_AGE_MS = 30 * 60 * 1000; const CHECKOUT_REQUEST_KEY_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; export function clearPersistedCheckoutRequest(namespace: string, storage?: Pick): void { const checkoutStorage = storage ?? (typeof window === "undefined" ? null : window.sessionStorage); if (!checkoutStorage) return; try { checkoutStorage.removeItem(`${CHECKOUT_REQUEST_STORAGE_PREFIX}:${namespace}`); } catch { // Storage can be unavailable in hardened/private browser contexts. } } function checkoutRequestFingerprint(selection: SelfServiceSubscriptionSelection, paymentMethod: string | null): string { return JSON.stringify({ ...selection, storageAddOnCodes: [...selection.storageAddOnCodes].sort(), paymentMethod, }); } function persistedCheckoutRequest(storage: CheckoutRequestStorage, storageKey: string, fingerprint: string, now: number): CheckoutRequest | null { try { const parsed = JSON.parse(storage.getItem(storageKey) ?? "null") as Partial | null; if ( !parsed || parsed.fingerprint !== fingerprint || typeof parsed.key !== "string" || !CHECKOUT_REQUEST_KEY_PATTERN.test(parsed.key) || typeof parsed.createdAt !== "number" || !Number.isFinite(parsed.createdAt) || parsed.createdAt > now || now - parsed.createdAt > CHECKOUT_REQUEST_MAX_AGE_MS ) { storage.removeItem(storageKey); return null; } return { fingerprint, key: parsed.key, createdAt: parsed.createdAt }; } catch { return null; } } export function stableCheckoutRequestKey( current: CheckoutRequest | null, selection: SelfServiceSubscriptionSelection, paymentMethod: string | null, options?: { readonly storage: CheckoutRequestStorage; readonly namespace: string; readonly now?: number; readonly createKey?: () => string; } ): CheckoutRequest { const fingerprint = checkoutRequestFingerprint(selection, paymentMethod); const now = options?.now ?? Date.now(); if (current?.fingerprint === fingerprint && current.createdAt <= now && now - current.createdAt <= CHECKOUT_REQUEST_MAX_AGE_MS) return current; const storageKey = options ? `${CHECKOUT_REQUEST_STORAGE_PREFIX}:${options.namespace}` : null; const persisted = options && storageKey ? persistedCheckoutRequest(options.storage, storageKey, fingerprint, now) : null; if (persisted) return persisted; const next = { fingerprint, key: options?.createKey?.() ?? globalThis.crypto.randomUUID(), createdAt: now }; if (options && storageKey) { try { options.storage.setItem(storageKey, JSON.stringify(next)); } catch { // Storage can be unavailable in hardened/private browser contexts. The in-memory key still protects retries in this render. } } return next; } export function browserCheckoutRequestOptions(namespace: string): Parameters[3] { try { return { storage: window.sessionStorage, namespace }; } catch { return undefined; } }