@payment-kit-js/vanilla 0.5.14 → 0.5.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{airwallex-apple-pay-adapter-CHAnLP_x.mjs → airwallex-apple-pay-adapter-Bs8AdQKY.mjs} +8 -2
- package/dist/airwallex-apple-pay-adapter-Bs8AdQKY.mjs.map +1 -0
- package/dist/{airwallex-apple-pay-adapter-Bb0CreWN.d.mts → airwallex-apple-pay-adapter-D8enI9Vq.d.mts} +3 -1
- package/dist/airwallex-apple-pay-adapter-D8enI9Vq.d.mts.map +1 -0
- package/dist/cdn/paymentkit.js +17 -7
- package/dist/cdn/paymentkit.js.map +2 -2
- package/dist/cdn/paymentkit.min.js +9 -9
- package/dist/cdn/paymentkit.min.js.map +3 -3
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/payment-methods/airwallex-apple-pay-adapter.d.mts +1 -1
- package/dist/payment-methods/airwallex-apple-pay-adapter.mjs +1 -1
- package/dist/payment-methods/apple-pay.d.mts +3 -1
- package/dist/payment-methods/apple-pay.d.mts.map +1 -1
- package/dist/payment-methods/apple-pay.mjs +6 -4
- package/dist/payment-methods/apple-pay.mjs.map +1 -1
- package/package.json +2 -2
- package/dist/airwallex-apple-pay-adapter-Bb0CreWN.d.mts.map +0 -1
- package/dist/airwallex-apple-pay-adapter-CHAnLP_x.mjs.map +0 -1
package/dist/index.mjs
CHANGED
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["STATIC_ENVIRONMENT_URLS: Record<Exclude<PaymentKitEnvironment, \"tunnel\">, EnvironmentUrls>","PaymentKit: PaymentKitType","PACKAGE_VERSION","externalFuncsMapByPm: ExternalFuncsMapByPm<PaymentMethods>","submit: PaymentKitReturnType[\"submit\"]"],"sources":["../package.json","../src/analytics/mock-adapter.ts","../src/analytics/posthog-adapter.ts","../src/analytics/service.ts","../src/analytics/checkout-timing.ts","../src/types.ts","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@payment-kit-js/vanilla\",\n \"version\": \"0.5.14\",\n \"main\": \"./dist/index.mjs\",\n \"types\": \"./dist/index.d.mts\",\n \"module\": \"./dist/index.mjs\",\n \"exports\": {\n \".\": \"./dist/index.mjs\",\n \"./payment-methods/affirm\": \"./dist/payment-methods/affirm.mjs\",\n \"./payment-methods/afterpay\": \"./dist/payment-methods/afterpay.mjs\",\n \"./payment-methods/airwallex-apple-pay-adapter\": \"./dist/payment-methods/airwallex-apple-pay-adapter.mjs\",\n \"./payment-methods/airwallex-google-pay-adapter\": \"./dist/payment-methods/airwallex-google-pay-adapter.mjs\",\n \"./payment-methods/apple-pay\": \"./dist/payment-methods/apple-pay.mjs\",\n \"./payment-methods/bnpl-shared\": \"./dist/payment-methods/bnpl-shared.mjs\",\n \"./payment-methods/card\": \"./dist/payment-methods/card.mjs\",\n \"./payment-methods/google-pay\": \"./dist/payment-methods/google-pay.mjs\",\n \"./payment-methods/klarna\": \"./dist/payment-methods/klarna.mjs\",\n \"./payment-methods/next-action-handlers\": \"./dist/payment-methods/next-action-handlers.mjs\",\n \"./payment-methods/paypal\": \"./dist/payment-methods/paypal.mjs\",\n \"./payment-methods/stripe-apple-pay-adapter\": \"./dist/payment-methods/stripe-apple-pay-adapter.mjs\",\n \"./payment-methods/stripe-google-pay-adapter\": \"./dist/payment-methods/stripe-google-pay-adapter.mjs\",\n \"./payment-methods/vgs-collect-loader\": \"./dist/payment-methods/vgs-collect-loader.mjs\",\n \"./penpal/connect-card\": \"./dist/penpal/connect-card.mjs\",\n \"./penpal/connect-tunnel-x\": \"./dist/penpal/connect-tunnel-x.mjs\",\n \"./package.json\": \"./package.json\"\n },\n \"license\": \"MIT\",\n \"description\": \"Vanilla package for PaymentKit\",\n \"files\": [\n \"dist\",\n \"dist/cdn\"\n ],\n \"scripts\": {\n \"dev\": \"yarn tsdown --watch ./src\",\n \"build:npm\": \"yarn tsdown\",\n \"build:cdn\": \"node build-cdn.mjs\",\n \"build\": \"yarn build:npm && yarn build:cdn\"\n },\n \"packageManager\": \"yarn@4.10.3\",\n \"dependencies\": {\n \"nanoid\": \"^5.0.7\",\n \"penpal\": \"^7.0.4\",\n \"posthog-js\": \"^1.196.0\",\n \"valibot\": \"^1.1.0\"\n },\n \"devDependencies\": {\n \"@pkg/sdk\": \"workspace:*\",\n \"@pkg/tsconfig\": \"workspace:*\",\n \"@stripe/stripe-js\": \"^8.6.1\",\n \"@types/applepayjs\": \"^14.0.9\",\n \"esbuild\": \"^0.24.2\",\n \"tsdown\": \"^0.15.10\",\n \"typescript\": \"^5.9.3\"\n },\n \"stableVersion\": \"0.5.14\"\n}\n","import type { AnalyticsAdapter, CapturedEvent } from \"./types\";\n\n/**\n * Mock analytics adapter for testing.\n *\n * Records all captured events for assertion in tests.\n */\nexport class MockAnalyticsAdapter implements AnalyticsAdapter {\n private events: CapturedEvent[] = [];\n private distinctId: string | null = null;\n\n identify(distinctId: string): void {\n this.distinctId = distinctId;\n }\n\n capture(event: string, properties?: Record<string, unknown>): void {\n this.events.push({\n event,\n distinct_id: this.distinctId,\n properties,\n timestamp: Date.now(),\n });\n }\n\n isInitialized(): boolean {\n return true;\n }\n\n // Test helper methods\n\n /**\n * Get all captured events.\n */\n getEvents(): CapturedEvent[] {\n return [...this.events];\n }\n\n /**\n * Get events by name.\n */\n getEventsByName(eventName: string): CapturedEvent[] {\n return this.events.filter((e) => e.event === eventName);\n }\n\n /**\n * Clear all captured events.\n */\n clear(): void {\n this.events = [];\n }\n}\n","import posthog from \"posthog-js\";\nimport type { AnalyticsAdapter } from \"./types\";\n\n// PostHog API key (PaymentKit internal)\nconst POSTHOG_API_KEY = \"phc_u9YDYUeUxo3CYn16XGrrwpVb8ycXgcjz5DarvIKRAAG\";\n\n/**\n * PostHog implementation of the analytics adapter.\n *\n * Singleton stored in window to ensure only one instance exists across\n * all script loads. Configures PostHog to use the reverse proxy at\n * /ingest to avoid ad blockers.\n */\nexport class PostHogAdapter implements AnalyticsAdapter {\n private initialized = false;\n\n /**\n * Get or create the PostHog adapter singleton (stored in window).\n *\n * @param apiBaseUrl - The API base URL for the reverse proxy\n * @throws Error if called with a different apiBaseUrl than the first call\n */\n static getInstance(apiBaseUrl: string): PostHogAdapter {\n if (typeof window === \"undefined\") {\n throw new Error(\"[PaymentKit Analytics] PostHog requires a browser environment\");\n }\n\n const existing = window.__paymentKitPostHog__;\n if (existing) {\n if (existing.apiBaseUrl !== apiBaseUrl) {\n throw new Error(\n `[PaymentKit Analytics] PostHog already initialized with apiBaseUrl \"${existing.apiBaseUrl}\", ` +\n `cannot re-initialize with different apiBaseUrl \"${apiBaseUrl}\"`,\n );\n }\n return existing.instance;\n }\n\n const instance = new PostHogAdapter(apiBaseUrl);\n window.__paymentKitPostHog__ = { instance, apiBaseUrl };\n return instance;\n }\n\n private constructor(apiBaseUrl: string) {\n this.initPostHog(apiBaseUrl);\n }\n\n private initPostHog(apiBaseUrl: string): void {\n try {\n posthog.init(POSTHOG_API_KEY, {\n // Use reverse proxy to avoid ad blockers\n api_host: `${apiBaseUrl}/ingest`,\n // PostHog UI host (for toolbar, if ever needed)\n ui_host: \"https://us.posthog.com\",\n // Memory persistence - no cookies in embedded checkout iframe\n persistence: \"memory\",\n // Disable session recording - not needed for metrics\n disable_session_recording: true,\n // Manual events only - no autocapture\n autocapture: false,\n // Manual pageview control\n capture_pageview: false,\n // Disable batching - send events immediately to avoid data loss on page close\n request_batching: false,\n // Disable bot detection - allows events from automated tests and debugging environments\n // PostHog detects Playwright/headless browsers as bots by default\n opt_out_useragent_filter: true,\n // Callback when PostHog is ready\n loaded: () => {\n this.initialized = true;\n },\n });\n } catch (e) {\n console.warn(\"[PaymentKit Analytics] Failed to initialize PostHog:\", e);\n }\n }\n\n identify(distinctId: string): void {\n try {\n posthog.identify(distinctId);\n } catch (e) {\n // Silent fail - never break checkout\n console.warn(\"[PaymentKit Analytics] Failed to identify:\", e);\n }\n }\n\n capture(event: string, properties?: Record<string, unknown>): void {\n try {\n posthog.capture(event, properties);\n } catch (e) {\n // Silent fail - never break checkout\n console.warn(\"[PaymentKit Analytics] Failed to capture event:\", e);\n }\n }\n\n isInitialized(): boolean {\n return this.initialized;\n }\n}\n","import { MockAnalyticsAdapter } from \"./mock-adapter\";\nimport { PostHogAdapter } from \"./posthog-adapter\";\nimport type { AnalyticsAdapter } from \"./types\";\n\n/**\n * Analytics service singleton.\n *\n * Provides a global interface for capturing analytics events.\n * Supports multiple adapters that receive all events (composite pattern).\n *\n * Usage:\n * // Initialize (typically done once at SDK init)\n * AnalyticsService.init(\"http://localhost:9000\");\n *\n * // Capture events - broadcasts to all registered adapters\n * AnalyticsService.capture(\"checkout_page_ready\", { session_id: \"...\" });\n *\n * // For testing - add a mock adapter\n * import { MockAnalyticsAdapter } from \"./mock-adapter\";\n * const mock = new MockAnalyticsAdapter();\n * AnalyticsService.addAdapter(mock);\n *\n * // Or use the built-in test mock (exposed globally as window.__paymentKitAnalytics__)\n * AnalyticsService.enableTestMode();\n * // Events can be read via window.__paymentKitAnalytics__.getEvents()\n */\nclass AnalyticsServiceClass {\n private adapters: AnalyticsAdapter[] = [];\n private testMock: MockAnalyticsAdapter | null = null;\n\n /**\n * Initialize the analytics service with PostHog.\n *\n * @param apiBaseUrl - The API base URL for the reverse proxy\n * @param enableTestMode - Enable test mode for capturing events in tests\n */\n init(apiBaseUrl: string, enableTestMode?: boolean): void {\n try {\n // Add PostHog adapter (singleton - safe to call multiple times with same URL)\n this.addAdapter(PostHogAdapter.getInstance(apiBaseUrl));\n\n // Enable test mode if explicitly requested\n if (enableTestMode) {\n this.enableTestMode();\n }\n } catch (e) {\n // Silent fail - analytics should never break checkout\n console.warn(\"[PaymentKit Analytics] Init failed:\", e);\n }\n }\n\n /**\n * Enable test mode by adding a mock adapter and exposing it globally.\n * The mock adapter is accessible via window.__paymentKitAnalytics__\n */\n enableTestMode(): void {\n if (this.testMock) return; // Already enabled\n\n this.testMock = new MockAnalyticsAdapter();\n this.addAdapter(this.testMock);\n\n // Expose globally for Playwright/test access\n if (typeof window !== \"undefined\") {\n window.__paymentKitAnalytics__ = this.testMock;\n }\n }\n\n /**\n * Get the test mock adapter (if test mode is enabled).\n */\n getTestMock(): MockAnalyticsAdapter | null {\n return this.testMock;\n }\n\n /**\n * Add an analytics adapter.\n * Events will be broadcast to all registered adapters.\n * Duplicate adapters (same instance) are ignored.\n *\n * @param adapter - The adapter to add\n */\n addAdapter(adapter: AnalyticsAdapter): void {\n if (!this.adapters.includes(adapter)) {\n this.adapters.push(adapter);\n }\n }\n\n /**\n * Clear all adapters.\n */\n clearAdapters(): void {\n this.adapters = [];\n this.testMock = null;\n if (typeof window !== \"undefined\") {\n delete window.__paymentKitAnalytics__;\n }\n }\n\n /**\n * Set the distinct_id for all adapters.\n * This is used to correlate frontend events with backend events.\n * Should be called with the checkout session secure token.\n */\n identify(distinctId: string): void {\n for (const adapter of this.adapters) {\n try {\n adapter.identify(distinctId);\n } catch {\n // Silent fail - never break checkout\n }\n }\n }\n\n /**\n * Capture an analytics event.\n * Broadcasts to all registered adapters. Silently fails on errors.\n */\n capture(event: string, properties?: Record<string, unknown>): void {\n for (const adapter of this.adapters) {\n try {\n adapter.capture(event, properties);\n } catch {\n // Silent fail - never break checkout\n }\n }\n }\n\n /**\n * Check if any adapter is initialized.\n */\n isInitialized(): boolean {\n return this.adapters.some((adapter) => adapter.isInitialized());\n }\n}\n\n/**\n * Global analytics service instance.\n */\nexport const AnalyticsService = new AnalyticsServiceClass();\n","import { AnalyticsService } from \"./service\";\n\n/**\n * Tracks timing metrics throughout the checkout flow.\n *\n * Create one instance per checkout session and call the track methods\n * at appropriate points in the checkout flow.\n *\n * Usage:\n * const requestId = getOrCreateCheckoutRequestId(environment);\n * const tracker = new CheckoutTimingTracker(secureToken, requestId);\n * tracker.trackPageReady();\n * // ... iframe loads ...\n * tracker.trackInputReady();\n * // ... user submits ...\n * tracker.trackSubmit();\n * // ... API response ...\n * tracker.trackSuccess(attemptId);\n */\nexport class CheckoutTimingTracker {\n // Class-level guard to prevent duplicate page_ready events across PaymentKit instances\n private static pageReadyTracked = false;\n\n private startTime: number;\n private checkoutSessionId: string;\n private requestId: string;\n private inputReadyTracked = false;\n\n constructor(checkoutSessionId: string, requestId: string) {\n this.startTime = performance.now();\n this.checkoutSessionId = checkoutSessionId;\n this.requestId = requestId;\n }\n\n /**\n * Track when SDK is initialized and ready.\n * Call immediately after PaymentKit() initialization.\n * Only tracks once per page load (subsequent calls are no-ops).\n */\n trackPageReady(): void {\n if (CheckoutTimingTracker.pageReadyTracked) return;\n CheckoutTimingTracker.pageReadyTracked = true;\n\n AnalyticsService.capture(\"checkout_page_ready\", {\n checkout_session_id: this.checkoutSessionId,\n request_id: this.requestId,\n elapsed_ms: this.getElapsedMs(),\n });\n }\n\n /**\n * Track when card input iframe is loaded and ready for input.\n * Should only be called once (for first iframe, typically card_pan).\n * Subsequent calls are no-ops.\n */\n trackInputReady(): void {\n if (this.inputReadyTracked) return;\n this.inputReadyTracked = true;\n\n AnalyticsService.capture(\"checkout_input_ready\", {\n checkout_session_id: this.checkoutSessionId,\n request_id: this.requestId,\n elapsed_ms: this.getElapsedMs(),\n });\n }\n\n /**\n * Track when user clicks submit button.\n */\n trackSubmit(): void {\n AnalyticsService.capture(\"checkout_submit\", {\n checkout_session_id: this.checkoutSessionId,\n request_id: this.requestId,\n elapsed_ms: this.getElapsedMs(),\n });\n }\n\n /**\n * Track successful checkout.\n *\n * @param checkoutAttemptId - The checkout attempt ID\n */\n trackSuccess(checkoutAttemptId: string): void {\n AnalyticsService.capture(\"checkout_success\", {\n checkout_session_id: this.checkoutSessionId,\n checkout_attempt_id: checkoutAttemptId,\n request_id: this.requestId,\n total_elapsed_ms: this.getElapsedMs(),\n });\n }\n\n /**\n * Track failed checkout.\n *\n * @param checkoutAttemptId - The checkout attempt ID (may be null if failed early)\n * @param errorCode - The error code from the checkout response\n * @param errorMessage - The customer-facing error message\n */\n trackFail(checkoutAttemptId: string | null, errorCode: string | null, errorMessage: string | null): void {\n AnalyticsService.capture(\"checkout_fail\", {\n checkout_session_id: this.checkoutSessionId,\n checkout_attempt_id: checkoutAttemptId,\n request_id: this.requestId,\n error_code: errorCode,\n error_message: errorMessage,\n total_elapsed_ms: this.getElapsedMs(),\n });\n }\n\n /**\n * Get elapsed time in milliseconds since tracker creation.\n */\n private getElapsedMs(): number {\n return Math.round(performance.now() - this.startTime);\n }\n}\n","import type { CheckoutTimingTracker } from \"./analytics\";\nimport type { CardErrorCode } from \"./penpal/connect-card\";\nimport type { TunnelXIFrameConnection } from \"./penpal/connect-tunnel-x\";\n\nexport type PaymentKitEnvironment = \"local\" | \"tunnel\" | \"sandbox\" | \"production\";\n\ntype EnvironmentUrls = {\n baseUrl: string;\n apiBaseUrl: string;\n};\n\n// TODO: Cleanup this after launch to only use SDK instead of plain fetch.\nconst STATIC_ENVIRONMENT_URLS: Record<Exclude<PaymentKitEnvironment, \"tunnel\">, EnvironmentUrls> = {\n local: {\n baseUrl: \"http://localhost:9101\",\n apiBaseUrl: \"http://localhost:9000\",\n },\n sandbox: {\n baseUrl: \"https://staging.paymentkit.com/customer\",\n apiBaseUrl: \"https://staging.paymentkit.com\",\n },\n production: {\n baseUrl: \"https://app.paymentkit.com/customer\",\n apiBaseUrl: \"https://app.paymentkit.com\",\n },\n};\n\nfunction getTunnelUrls(): EnvironmentUrls {\n if (typeof window !== \"undefined\") {\n const params = new URLSearchParams(window.location.search);\n const apiBaseUrl = params.get(\"api_base_url\");\n if (apiBaseUrl) {\n return {\n baseUrl: window.location.origin,\n apiBaseUrl,\n };\n }\n // For loclx-style tunnels where API hostname is derived from the current hostname\n const hostname = window.location.hostname;\n if (hostname.includes(\"loclx.io\")) {\n const apiHostname = hostname.replace(/cportal|playground/, \"api\");\n return {\n baseUrl: `https://${hostname}`,\n apiBaseUrl: `https://${apiHostname}`,\n };\n }\n }\n // Fallback\n return STATIC_ENVIRONMENT_URLS.local;\n}\n\nexport function getUrlsForEnvironment(environment: string): EnvironmentUrls {\n if (environment === \"tunnel\") {\n return getTunnelUrls();\n }\n const env = environment as Exclude<PaymentKitEnvironment, \"tunnel\">;\n const urls = STATIC_ENVIRONMENT_URLS[env];\n if (!urls) {\n throw new Error(`Invalid environment: ${environment}. Must be one of: local, tunnel, sandbox, production`);\n }\n return urls;\n}\n\ntype FormFieldNames =\n | \"customer_name\"\n | \"customer_email\"\n | \"customer_country\"\n | \"customer_zip_code\"\n | \"customer_business_name\"\n | \"customer_address_line1\"\n | \"customer_address_line2\"\n | \"customer_city\"\n | \"customer_state\"\n | \"customer_tax_ids\"\n | \"shipping_address_line1\"\n | \"shipping_address_line2\"\n | \"shipping_city\"\n | \"shipping_state\"\n | \"shipping_zip_code\"\n | \"shipping_country\";\n\nexport type FormErrorCodes = \"required\" | \"invalid\";\n\nexport type CardSubmitOptions = {\n skipCustomerValidation?: boolean;\n};\n\nexport type TInternalFuncs = {\n submitPayment: (\n fields: PaymentKitFields,\n options?: CardSubmitOptions,\n ) => Promise<{ data: { [key: string]: unknown }; errors?: never } | { data?: never; errors: PaymentKitErrors }>;\n cleanup?: () => void;\n};\n\nexport type PaymentKit = <T extends readonly PaymentMethod<unknown>[]>(options: {\n environment: string;\n secureToken: string;\n paymentMethods: T;\n /** Card tokenization mode from checkout session */\n cardTokenizationMode?: \"direct\" | \"vgs\";\n /** VGS vault ID from checkout session */\n vgsVaultId?: string;\n /** VGS environment from checkout session */\n vgsEnvironment?: string;\n /** @internal Enable analytics test mode for e2e testing */\n __enableAnalyticsTestMode?: boolean;\n}) => ExternalFuncsMapByPm<T> & {\n submit: PaymentKitSubmitHandler<T>;\n cleanup: () => void;\n};\n\ntype PaymentKitSubmitHandler<T extends readonly PaymentMethod<unknown>[]> = <\n N extends keyof ExternalFuncsMapByPm<T>,\n>(options: {\n fields: Partial<PaymentKitFields>;\n paymentMethod: N;\n options?: unknown;\n onError: (error: PaymentKitErrors) => void;\n onSuccess: (data: { [key: string]: unknown }) => void;\n}) => void;\n\nexport type PaymentKitStates = {\n baseUrl: string;\n apiBaseUrl: string;\n secureToken: string;\n environment: string;\n tunnelXConnection: TunnelXIFrameConnection;\n timingTracker: CheckoutTimingTracker;\n /** Checkout request ID for correlating all API calls and analytics events */\n checkoutRequestId: string;\n /** Card tokenization mode: 'direct' (KMS) or 'vgs' (VGS Collect + proxy) */\n cardTokenizationMode?: \"direct\" | \"vgs\";\n /** VGS vault ID (only when cardTokenizationMode is 'vgs') */\n vgsVaultId?: string;\n /** VGS environment: 'sandbox' or 'live' (only when cardTokenizationMode is 'vgs') */\n vgsEnvironment?: string;\n /** @internal Promise that resolves when session config (incl. VGS mode) is auto-detected */\n _sessionConfigReady?: Promise<void>;\n};\n\nexport type PaymentKitErrors = {\n root?: string;\n card_pan?: CardErrorCode;\n card_exp?: CardErrorCode;\n card_cvc?: CardErrorCode;\n paypal?: string;\n google_pay?: string;\n apple_pay?: string;\n processor_id?: string;\n amount?: string;\n currency?: string;\n country?: string;\n} & { [key in FormFieldNames]?: FormErrorCodes | string };\n\nexport type PaymentKitFields = { [key in FormFieldNames]: string };\n\nexport type PaymentMethod<TExternalFuncs = unknown, TName = string> = (paymentKitStates: PaymentKitStates) => {\n name: TName;\n externalFuncs: TExternalFuncs;\n internalFuncs: TInternalFuncs;\n};\n\nexport type ExternalFuncsMapByPm<T extends readonly PaymentMethod<unknown>[]> = {\n [K in T[number] as ReturnType<K>[\"name\"]]: ReturnType<K>[\"externalFuncs\"];\n};\n","import { version as PACKAGE_VERSION } from \"../package.json\";\nimport { AnalyticsService, CheckoutTimingTracker } from \"./analytics\";\nimport { connectToTunnelXIframe } from \"./penpal/connect-tunnel-x\";\nimport type { ExternalFuncsMapByPm, PaymentKitFields, PaymentKit as PaymentKitType } from \"./types\";\nimport { getUrlsForEnvironment } from \"./types\";\nimport { createCheckoutIFrame } from \"./utils\";\nimport { getOrCreateCheckoutRequestId } from \"./utils/checkout-request-id\";\n\ntype PaymentKitReturnType = ReturnType<PaymentKitType>;\n\nconst createTunnelXConnection = (baseUrl: string, apiBaseUrl: string, token: string) => {\n const iframe = createCheckoutIFrame(\"tunnel-x\", baseUrl, {\n checkout_token: token,\n api_base_url: apiBaseUrl,\n });\n // Tunnel-X is a hidden communication iframe, not a visible UI element\n Object.assign(iframe.style, { width: \"0\", height: \"0\", position: \"absolute\", visibility: \"hidden\" });\n document.body.appendChild(iframe);\n\n const connection = connectToTunnelXIframe(iframe, {});\n\n const unmount = () => {\n connection.destroy();\n document.body.removeChild(iframe);\n };\n\n return { unmount, connection };\n};\n\nconst PaymentKit: PaymentKitType = ({\n environment,\n secureToken,\n paymentMethods,\n cardTokenizationMode,\n vgsVaultId,\n vgsEnvironment,\n __enableAnalyticsTestMode,\n}) => {\n type PaymentMethods = typeof paymentMethods;\n\n // Resolve URLs from environment\n const { baseUrl, apiBaseUrl } = getUrlsForEnvironment(environment);\n\n // Log version for debugging\n console.log(`[PaymentKit] v${PACKAGE_VERSION} initialized (env: ${environment})`);\n\n // Generate checkout request ID for correlating all API calls and analytics events\n // This is idempotent - same ID is reused across multiple PaymentKit instantiations\n const checkoutRequestId = getOrCreateCheckoutRequestId(environment);\n console.log(`[PaymentKit] checkout_request_id: ${checkoutRequestId}`);\n\n // Initialize analytics and identify with secure token for correlation\n AnalyticsService.init(apiBaseUrl, __enableAnalyticsTestMode);\n AnalyticsService.identify(secureToken);\n\n // Create timing tracker for this checkout session\n const timingTracker = new CheckoutTimingTracker(secureToken, checkoutRequestId);\n\n // Track page ready immediately\n timingTracker.trackPageReady();\n\n const { connection: tunnelXConnection, unmount: unmountTunnelX } = createTunnelXConnection(\n baseUrl,\n apiBaseUrl,\n secureToken,\n );\n\n const paymentKitStates = {\n baseUrl,\n apiBaseUrl,\n secureToken,\n environment,\n tunnelXConnection,\n timingTracker,\n checkoutRequestId,\n cardTokenizationMode,\n vgsVaultId,\n vgsEnvironment,\n _sessionConfigReady: undefined as Promise<void> | undefined,\n };\n\n // Auto-detect VGS mode from session API if not explicitly provided.\n // The promise resolves before any card mount or submit needs the mode.\n if (cardTokenizationMode === undefined) {\n const sessionConfigUrl = `${apiBaseUrl}/api/checkout-sessions/token/${secureToken}`;\n console.log(\n `[PaymentKit] cardTokenizationMode not provided — auto-detecting from session API: ${sessionConfigUrl}`,\n );\n paymentKitStates._sessionConfigReady = fetch(sessionConfigUrl)\n .then((resp) => {\n if (!resp.ok) {\n console.warn(`[PaymentKit] Session config fetch failed with status ${resp.status}`);\n return null;\n }\n return resp.json();\n })\n .then((session) => {\n if (session && typeof session === \"object\" && typeof session.card_tokenization_mode === \"string\") {\n paymentKitStates.cardTokenizationMode = session.card_tokenization_mode || \"direct\";\n console.log(\n `[PaymentKit] Session config detected — cardTokenizationMode: ${paymentKitStates.cardTokenizationMode}`,\n );\n\n // Validate VGS config from API before trusting it\n const vaultId = session.vgs_vault_id;\n const vgsEnv = session.vgs_environment;\n if (vaultId && /^tnt[a-z0-9]+$/.test(vaultId) && (vgsEnv === \"sandbox\" || vgsEnv === \"live\")) {\n paymentKitStates.vgsVaultId = vaultId;\n paymentKitStates.vgsEnvironment = vgsEnv;\n console.log(`[PaymentKit] VGS config valid — vaultId: ${vaultId}, environment: ${vgsEnv}`);\n } else if (paymentKitStates.cardTokenizationMode === \"vgs\") {\n console.warn(\n `[PaymentKit] Invalid VGS config from session API (vaultId: ${vaultId}, env: ${vgsEnv}), falling back to direct mode`,\n );\n paymentKitStates.cardTokenizationMode = \"direct\";\n }\n } else {\n console.log(\"[PaymentKit] Session response missing card_tokenization_mode — defaulting to direct mode\");\n paymentKitStates.cardTokenizationMode = \"direct\";\n }\n })\n .catch((err) => {\n console.error(\"[PaymentKit] Session config fetch error — falling back to direct mode:\", err);\n paymentKitStates.cardTokenizationMode = \"direct\";\n });\n } else {\n console.log(`[PaymentKit] cardTokenizationMode explicitly set: ${cardTokenizationMode}`);\n }\n\n const pmInstances = paymentMethods.map((paymentMethod) => paymentMethod(paymentKitStates));\n\n const externalFuncsMapByPm: ExternalFuncsMapByPm<PaymentMethods> = pmInstances.reduce(\n (acc, { name, externalFuncs }) => {\n // @ts-expect-error - typecase this better in future\n acc[name] = externalFuncs;\n return acc;\n },\n {} as ExternalFuncsMapByPm<PaymentMethods>,\n );\n\n const submit: PaymentKitReturnType[\"submit\"] = ({\n paymentMethod: paymentMethodName,\n fields,\n options,\n onSuccess,\n onError,\n }) => {\n const paymentMethod = pmInstances.find(({ name }) => name === paymentMethodName);\n if (!paymentMethod) {\n onError({ root: \"payment_method_not_found\" });\n return;\n }\n paymentMethod.internalFuncs\n .submitPayment(fields, options)\n .then(({ data, errors }) => {\n errors ? onError(errors) : onSuccess(data);\n })\n .catch((e) => {\n console.error(\"PaymentKit:submit:catch\", e);\n\n // Try to extract error message from response\n if (e?.response?.data) {\n onError(e.response.data);\n } else if (e?.message) {\n onError({ root: e.message });\n } else {\n onError({ root: \"unknown_error\" });\n }\n });\n };\n\n const cleanup = () => {\n // Clean up all payment method instances\n for (const pm of pmInstances) {\n if (pm.internalFuncs.cleanup) {\n pm.internalFuncs.cleanup();\n }\n }\n unmountTunnelX();\n };\n\n return {\n submit,\n cleanup,\n ...externalFuncsMapByPm,\n };\n};\n\nexport type { PaymentKitFields };\n\nexport default PaymentKit;\n"],"mappings":";;;;;;cAEa;;;;;;;;;ACKb,IAAa,uBAAb,MAA8D;CAC5D,AAAQ,SAA0B,EAAE;CACpC,AAAQ,aAA4B;CAEpC,SAAS,YAA0B;AACjC,OAAK,aAAa;;CAGpB,QAAQ,OAAe,YAA4C;AACjE,OAAK,OAAO,KAAK;GACf;GACA,aAAa,KAAK;GAClB;GACA,WAAW,KAAK,KAAK;GACtB,CAAC;;CAGJ,gBAAyB;AACvB,SAAO;;;;;CAQT,YAA6B;AAC3B,SAAO,CAAC,GAAG,KAAK,OAAO;;;;;CAMzB,gBAAgB,WAAoC;AAClD,SAAO,KAAK,OAAO,QAAQ,MAAM,EAAE,UAAU,UAAU;;;;;CAMzD,QAAc;AACZ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EAAE,MAAM,aAAa;AAC1B,YAAS,QAAQ,OAAO,GAAG,UAAU,KAAK;IAC1C,CACD,OAAO,MAAM;AACZ,WAAQ,MAAM,2BAA2B,EAAE;AAG3C,OAAI,GAAG,UAAU,KACf,SAAQ,EAAE,SAAS,KAAK;YACf,GAAG,QACZ,SAAQ,EAAE,MAAM,EAAE,SAAS,CAAC;OAE5B,SAAQ,EAAE,MAAM,iBAAiB,CAAC;IAEpC;;CAGN,MAAM,gBAAgB;AAEpB,OAAK,MAAM,MAAM,YACf,KAAI,GAAG,cAAc,QACnB,IAAG,cAAc,SAAS;AAG9B,kBAAgB;;AAGlB,QAAO;EACL;EACA;EACA,GAAG;EACJ;;AAKH,kBAAe"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["STATIC_ENVIRONMENT_URLS: Record<Exclude<PaymentKitEnvironment, \"tunnel\">, EnvironmentUrls>","PaymentKit: PaymentKitType","PACKAGE_VERSION","externalFuncsMapByPm: ExternalFuncsMapByPm<PaymentMethods>","submit: PaymentKitReturnType[\"submit\"]"],"sources":["../package.json","../src/analytics/mock-adapter.ts","../src/analytics/posthog-adapter.ts","../src/analytics/service.ts","../src/analytics/checkout-timing.ts","../src/types.ts","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@payment-kit-js/vanilla\",\n \"version\": \"0.5.15\",\n \"main\": \"./dist/index.mjs\",\n \"types\": \"./dist/index.d.mts\",\n \"module\": \"./dist/index.mjs\",\n \"exports\": {\n \".\": \"./dist/index.mjs\",\n \"./payment-methods/affirm\": \"./dist/payment-methods/affirm.mjs\",\n \"./payment-methods/afterpay\": \"./dist/payment-methods/afterpay.mjs\",\n \"./payment-methods/airwallex-apple-pay-adapter\": \"./dist/payment-methods/airwallex-apple-pay-adapter.mjs\",\n \"./payment-methods/airwallex-google-pay-adapter\": \"./dist/payment-methods/airwallex-google-pay-adapter.mjs\",\n \"./payment-methods/apple-pay\": \"./dist/payment-methods/apple-pay.mjs\",\n \"./payment-methods/bnpl-shared\": \"./dist/payment-methods/bnpl-shared.mjs\",\n \"./payment-methods/card\": \"./dist/payment-methods/card.mjs\",\n \"./payment-methods/google-pay\": \"./dist/payment-methods/google-pay.mjs\",\n \"./payment-methods/klarna\": \"./dist/payment-methods/klarna.mjs\",\n \"./payment-methods/next-action-handlers\": \"./dist/payment-methods/next-action-handlers.mjs\",\n \"./payment-methods/paypal\": \"./dist/payment-methods/paypal.mjs\",\n \"./payment-methods/stripe-apple-pay-adapter\": \"./dist/payment-methods/stripe-apple-pay-adapter.mjs\",\n \"./payment-methods/stripe-google-pay-adapter\": \"./dist/payment-methods/stripe-google-pay-adapter.mjs\",\n \"./payment-methods/vgs-collect-loader\": \"./dist/payment-methods/vgs-collect-loader.mjs\",\n \"./penpal/connect-card\": \"./dist/penpal/connect-card.mjs\",\n \"./penpal/connect-tunnel-x\": \"./dist/penpal/connect-tunnel-x.mjs\",\n \"./package.json\": \"./package.json\"\n },\n \"license\": \"MIT\",\n \"description\": \"Vanilla package for PaymentKit\",\n \"files\": [\n \"dist\",\n \"dist/cdn\"\n ],\n \"scripts\": {\n \"dev\": \"yarn tsdown --watch ./src\",\n \"build:npm\": \"yarn tsdown\",\n \"build:cdn\": \"node build-cdn.mjs\",\n \"build\": \"yarn build:npm && yarn build:cdn\"\n },\n \"packageManager\": \"yarn@4.10.3\",\n \"dependencies\": {\n \"nanoid\": \"^5.0.7\",\n \"penpal\": \"^7.0.4\",\n \"posthog-js\": \"^1.196.0\",\n \"valibot\": \"^1.1.0\"\n },\n \"devDependencies\": {\n \"@pkg/sdk\": \"workspace:*\",\n \"@pkg/tsconfig\": \"workspace:*\",\n \"@stripe/stripe-js\": \"^8.6.1\",\n \"@types/applepayjs\": \"^14.0.9\",\n \"esbuild\": \"^0.24.2\",\n \"tsdown\": \"^0.15.10\",\n \"typescript\": \"^5.9.3\"\n },\n \"stableVersion\": \"0.5.15\"\n}\n","import type { AnalyticsAdapter, CapturedEvent } from \"./types\";\n\n/**\n * Mock analytics adapter for testing.\n *\n * Records all captured events for assertion in tests.\n */\nexport class MockAnalyticsAdapter implements AnalyticsAdapter {\n private events: CapturedEvent[] = [];\n private distinctId: string | null = null;\n\n identify(distinctId: string): void {\n this.distinctId = distinctId;\n }\n\n capture(event: string, properties?: Record<string, unknown>): void {\n this.events.push({\n event,\n distinct_id: this.distinctId,\n properties,\n timestamp: Date.now(),\n });\n }\n\n isInitialized(): boolean {\n return true;\n }\n\n // Test helper methods\n\n /**\n * Get all captured events.\n */\n getEvents(): CapturedEvent[] {\n return [...this.events];\n }\n\n /**\n * Get events by name.\n */\n getEventsByName(eventName: string): CapturedEvent[] {\n return this.events.filter((e) => e.event === eventName);\n }\n\n /**\n * Clear all captured events.\n */\n clear(): void {\n this.events = [];\n }\n}\n","import posthog from \"posthog-js\";\nimport type { AnalyticsAdapter } from \"./types\";\n\n// PostHog API key (PaymentKit internal)\nconst POSTHOG_API_KEY = \"phc_u9YDYUeUxo3CYn16XGrrwpVb8ycXgcjz5DarvIKRAAG\";\n\n/**\n * PostHog implementation of the analytics adapter.\n *\n * Singleton stored in window to ensure only one instance exists across\n * all script loads. Configures PostHog to use the reverse proxy at\n * /ingest to avoid ad blockers.\n */\nexport class PostHogAdapter implements AnalyticsAdapter {\n private initialized = false;\n\n /**\n * Get or create the PostHog adapter singleton (stored in window).\n *\n * @param apiBaseUrl - The API base URL for the reverse proxy\n * @throws Error if called with a different apiBaseUrl than the first call\n */\n static getInstance(apiBaseUrl: string): PostHogAdapter {\n if (typeof window === \"undefined\") {\n throw new Error(\"[PaymentKit Analytics] PostHog requires a browser environment\");\n }\n\n const existing = window.__paymentKitPostHog__;\n if (existing) {\n if (existing.apiBaseUrl !== apiBaseUrl) {\n throw new Error(\n `[PaymentKit Analytics] PostHog already initialized with apiBaseUrl \"${existing.apiBaseUrl}\", ` +\n `cannot re-initialize with different apiBaseUrl \"${apiBaseUrl}\"`,\n );\n }\n return existing.instance;\n }\n\n const instance = new PostHogAdapter(apiBaseUrl);\n window.__paymentKitPostHog__ = { instance, apiBaseUrl };\n return instance;\n }\n\n private constructor(apiBaseUrl: string) {\n this.initPostHog(apiBaseUrl);\n }\n\n private initPostHog(apiBaseUrl: string): void {\n try {\n posthog.init(POSTHOG_API_KEY, {\n // Use reverse proxy to avoid ad blockers\n api_host: `${apiBaseUrl}/ingest`,\n // PostHog UI host (for toolbar, if ever needed)\n ui_host: \"https://us.posthog.com\",\n // Memory persistence - no cookies in embedded checkout iframe\n persistence: \"memory\",\n // Disable session recording - not needed for metrics\n disable_session_recording: true,\n // Manual events only - no autocapture\n autocapture: false,\n // Manual pageview control\n capture_pageview: false,\n // Disable batching - send events immediately to avoid data loss on page close\n request_batching: false,\n // Disable bot detection - allows events from automated tests and debugging environments\n // PostHog detects Playwright/headless browsers as bots by default\n opt_out_useragent_filter: true,\n // Callback when PostHog is ready\n loaded: () => {\n this.initialized = true;\n },\n });\n } catch (e) {\n console.warn(\"[PaymentKit Analytics] Failed to initialize PostHog:\", e);\n }\n }\n\n identify(distinctId: string): void {\n try {\n posthog.identify(distinctId);\n } catch (e) {\n // Silent fail - never break checkout\n console.warn(\"[PaymentKit Analytics] Failed to identify:\", e);\n }\n }\n\n capture(event: string, properties?: Record<string, unknown>): void {\n try {\n posthog.capture(event, properties);\n } catch (e) {\n // Silent fail - never break checkout\n console.warn(\"[PaymentKit Analytics] Failed to capture event:\", e);\n }\n }\n\n isInitialized(): boolean {\n return this.initialized;\n }\n}\n","import { MockAnalyticsAdapter } from \"./mock-adapter\";\nimport { PostHogAdapter } from \"./posthog-adapter\";\nimport type { AnalyticsAdapter } from \"./types\";\n\n/**\n * Analytics service singleton.\n *\n * Provides a global interface for capturing analytics events.\n * Supports multiple adapters that receive all events (composite pattern).\n *\n * Usage:\n * // Initialize (typically done once at SDK init)\n * AnalyticsService.init(\"http://localhost:9000\");\n *\n * // Capture events - broadcasts to all registered adapters\n * AnalyticsService.capture(\"checkout_page_ready\", { session_id: \"...\" });\n *\n * // For testing - add a mock adapter\n * import { MockAnalyticsAdapter } from \"./mock-adapter\";\n * const mock = new MockAnalyticsAdapter();\n * AnalyticsService.addAdapter(mock);\n *\n * // Or use the built-in test mock (exposed globally as window.__paymentKitAnalytics__)\n * AnalyticsService.enableTestMode();\n * // Events can be read via window.__paymentKitAnalytics__.getEvents()\n */\nclass AnalyticsServiceClass {\n private adapters: AnalyticsAdapter[] = [];\n private testMock: MockAnalyticsAdapter | null = null;\n\n /**\n * Initialize the analytics service with PostHog.\n *\n * @param apiBaseUrl - The API base URL for the reverse proxy\n * @param enableTestMode - Enable test mode for capturing events in tests\n */\n init(apiBaseUrl: string, enableTestMode?: boolean): void {\n try {\n // Add PostHog adapter (singleton - safe to call multiple times with same URL)\n this.addAdapter(PostHogAdapter.getInstance(apiBaseUrl));\n\n // Enable test mode if explicitly requested\n if (enableTestMode) {\n this.enableTestMode();\n }\n } catch (e) {\n // Silent fail - analytics should never break checkout\n console.warn(\"[PaymentKit Analytics] Init failed:\", e);\n }\n }\n\n /**\n * Enable test mode by adding a mock adapter and exposing it globally.\n * The mock adapter is accessible via window.__paymentKitAnalytics__\n */\n enableTestMode(): void {\n if (this.testMock) return; // Already enabled\n\n this.testMock = new MockAnalyticsAdapter();\n this.addAdapter(this.testMock);\n\n // Expose globally for Playwright/test access\n if (typeof window !== \"undefined\") {\n window.__paymentKitAnalytics__ = this.testMock;\n }\n }\n\n /**\n * Get the test mock adapter (if test mode is enabled).\n */\n getTestMock(): MockAnalyticsAdapter | null {\n return this.testMock;\n }\n\n /**\n * Add an analytics adapter.\n * Events will be broadcast to all registered adapters.\n * Duplicate adapters (same instance) are ignored.\n *\n * @param adapter - The adapter to add\n */\n addAdapter(adapter: AnalyticsAdapter): void {\n if (!this.adapters.includes(adapter)) {\n this.adapters.push(adapter);\n }\n }\n\n /**\n * Clear all adapters.\n */\n clearAdapters(): void {\n this.adapters = [];\n this.testMock = null;\n if (typeof window !== \"undefined\") {\n delete window.__paymentKitAnalytics__;\n }\n }\n\n /**\n * Set the distinct_id for all adapters.\n * This is used to correlate frontend events with backend events.\n * Should be called with the checkout session secure token.\n */\n identify(distinctId: string): void {\n for (const adapter of this.adapters) {\n try {\n adapter.identify(distinctId);\n } catch {\n // Silent fail - never break checkout\n }\n }\n }\n\n /**\n * Capture an analytics event.\n * Broadcasts to all registered adapters. Silently fails on errors.\n */\n capture(event: string, properties?: Record<string, unknown>): void {\n for (const adapter of this.adapters) {\n try {\n adapter.capture(event, properties);\n } catch {\n // Silent fail - never break checkout\n }\n }\n }\n\n /**\n * Check if any adapter is initialized.\n */\n isInitialized(): boolean {\n return this.adapters.some((adapter) => adapter.isInitialized());\n }\n}\n\n/**\n * Global analytics service instance.\n */\nexport const AnalyticsService = new AnalyticsServiceClass();\n","import { AnalyticsService } from \"./service\";\n\n/**\n * Tracks timing metrics throughout the checkout flow.\n *\n * Create one instance per checkout session and call the track methods\n * at appropriate points in the checkout flow.\n *\n * Usage:\n * const requestId = getOrCreateCheckoutRequestId(environment);\n * const tracker = new CheckoutTimingTracker(secureToken, requestId);\n * tracker.trackPageReady();\n * // ... iframe loads ...\n * tracker.trackInputReady();\n * // ... user submits ...\n * tracker.trackSubmit();\n * // ... API response ...\n * tracker.trackSuccess(attemptId);\n */\nexport class CheckoutTimingTracker {\n // Class-level guard to prevent duplicate page_ready events across PaymentKit instances\n private static pageReadyTracked = false;\n\n private startTime: number;\n private checkoutSessionId: string;\n private requestId: string;\n private inputReadyTracked = false;\n\n constructor(checkoutSessionId: string, requestId: string) {\n this.startTime = performance.now();\n this.checkoutSessionId = checkoutSessionId;\n this.requestId = requestId;\n }\n\n /**\n * Track when SDK is initialized and ready.\n * Call immediately after PaymentKit() initialization.\n * Only tracks once per page load (subsequent calls are no-ops).\n */\n trackPageReady(): void {\n if (CheckoutTimingTracker.pageReadyTracked) return;\n CheckoutTimingTracker.pageReadyTracked = true;\n\n AnalyticsService.capture(\"checkout_page_ready\", {\n checkout_session_id: this.checkoutSessionId,\n request_id: this.requestId,\n elapsed_ms: this.getElapsedMs(),\n });\n }\n\n /**\n * Track when card input iframe is loaded and ready for input.\n * Should only be called once (for first iframe, typically card_pan).\n * Subsequent calls are no-ops.\n */\n trackInputReady(): void {\n if (this.inputReadyTracked) return;\n this.inputReadyTracked = true;\n\n AnalyticsService.capture(\"checkout_input_ready\", {\n checkout_session_id: this.checkoutSessionId,\n request_id: this.requestId,\n elapsed_ms: this.getElapsedMs(),\n });\n }\n\n /**\n * Track when user clicks submit button.\n */\n trackSubmit(): void {\n AnalyticsService.capture(\"checkout_submit\", {\n checkout_session_id: this.checkoutSessionId,\n request_id: this.requestId,\n elapsed_ms: this.getElapsedMs(),\n });\n }\n\n /**\n * Track successful checkout.\n *\n * @param checkoutAttemptId - The checkout attempt ID\n */\n trackSuccess(checkoutAttemptId: string): void {\n AnalyticsService.capture(\"checkout_success\", {\n checkout_session_id: this.checkoutSessionId,\n checkout_attempt_id: checkoutAttemptId,\n request_id: this.requestId,\n total_elapsed_ms: this.getElapsedMs(),\n });\n }\n\n /**\n * Track failed checkout.\n *\n * @param checkoutAttemptId - The checkout attempt ID (may be null if failed early)\n * @param errorCode - The error code from the checkout response\n * @param errorMessage - The customer-facing error message\n */\n trackFail(checkoutAttemptId: string | null, errorCode: string | null, errorMessage: string | null): void {\n AnalyticsService.capture(\"checkout_fail\", {\n checkout_session_id: this.checkoutSessionId,\n checkout_attempt_id: checkoutAttemptId,\n request_id: this.requestId,\n error_code: errorCode,\n error_message: errorMessage,\n total_elapsed_ms: this.getElapsedMs(),\n });\n }\n\n /**\n * Get elapsed time in milliseconds since tracker creation.\n */\n private getElapsedMs(): number {\n return Math.round(performance.now() - this.startTime);\n }\n}\n","import type { CheckoutTimingTracker } from \"./analytics\";\nimport type { CardErrorCode } from \"./penpal/connect-card\";\nimport type { TunnelXIFrameConnection } from \"./penpal/connect-tunnel-x\";\n\nexport type PaymentKitEnvironment = \"local\" | \"tunnel\" | \"sandbox\" | \"production\";\n\ntype EnvironmentUrls = {\n baseUrl: string;\n apiBaseUrl: string;\n};\n\n// TODO: Cleanup this after launch to only use SDK instead of plain fetch.\nconst STATIC_ENVIRONMENT_URLS: Record<Exclude<PaymentKitEnvironment, \"tunnel\">, EnvironmentUrls> = {\n local: {\n baseUrl: \"http://localhost:9101\",\n apiBaseUrl: \"http://localhost:9000\",\n },\n sandbox: {\n baseUrl: \"https://staging.paymentkit.com/customer\",\n apiBaseUrl: \"https://staging.paymentkit.com\",\n },\n production: {\n baseUrl: \"https://app.paymentkit.com/customer\",\n apiBaseUrl: \"https://app.paymentkit.com\",\n },\n};\n\nfunction getTunnelUrls(): EnvironmentUrls {\n if (typeof window !== \"undefined\") {\n const params = new URLSearchParams(window.location.search);\n const apiBaseUrl = params.get(\"api_base_url\");\n if (apiBaseUrl) {\n return {\n baseUrl: window.location.origin,\n apiBaseUrl,\n };\n }\n // For loclx-style tunnels where API hostname is derived from the current hostname\n const hostname = window.location.hostname;\n if (hostname.includes(\"loclx.io\")) {\n const apiHostname = hostname.replace(/cportal|playground/, \"api\");\n return {\n baseUrl: `https://${hostname}`,\n apiBaseUrl: `https://${apiHostname}`,\n };\n }\n }\n // Fallback\n return STATIC_ENVIRONMENT_URLS.local;\n}\n\nexport function getUrlsForEnvironment(environment: string): EnvironmentUrls {\n if (environment === \"tunnel\") {\n return getTunnelUrls();\n }\n const env = environment as Exclude<PaymentKitEnvironment, \"tunnel\">;\n const urls = STATIC_ENVIRONMENT_URLS[env];\n if (!urls) {\n throw new Error(`Invalid environment: ${environment}. Must be one of: local, tunnel, sandbox, production`);\n }\n return urls;\n}\n\ntype FormFieldNames =\n | \"customer_name\"\n | \"customer_email\"\n | \"customer_country\"\n | \"customer_zip_code\"\n | \"customer_business_name\"\n | \"customer_address_line1\"\n | \"customer_address_line2\"\n | \"customer_city\"\n | \"customer_state\"\n | \"customer_tax_ids\"\n | \"shipping_address_line1\"\n | \"shipping_address_line2\"\n | \"shipping_city\"\n | \"shipping_state\"\n | \"shipping_zip_code\"\n | \"shipping_country\";\n\nexport type FormErrorCodes = \"required\" | \"invalid\";\n\nexport type CardSubmitOptions = {\n skipCustomerValidation?: boolean;\n};\n\nexport type TInternalFuncs = {\n submitPayment: (\n fields: PaymentKitFields,\n options?: CardSubmitOptions,\n ) => Promise<{ data: { [key: string]: unknown }; errors?: never } | { data?: never; errors: PaymentKitErrors }>;\n cleanup?: () => void;\n};\n\nexport type PaymentKit = <T extends readonly PaymentMethod<unknown>[]>(options: {\n environment: string;\n secureToken: string;\n paymentMethods: T;\n /** Card tokenization mode from checkout session */\n cardTokenizationMode?: \"direct\" | \"vgs\";\n /** VGS vault ID from checkout session */\n vgsVaultId?: string;\n /** VGS environment from checkout session */\n vgsEnvironment?: string;\n /** @internal Enable analytics test mode for e2e testing */\n __enableAnalyticsTestMode?: boolean;\n}) => ExternalFuncsMapByPm<T> & {\n submit: PaymentKitSubmitHandler<T>;\n cleanup: () => void;\n};\n\ntype PaymentKitSubmitHandler<T extends readonly PaymentMethod<unknown>[]> = <\n N extends keyof ExternalFuncsMapByPm<T>,\n>(options: {\n fields: Partial<PaymentKitFields>;\n paymentMethod: N;\n options?: unknown;\n onError: (error: PaymentKitErrors) => void;\n onSuccess: (data: { [key: string]: unknown }) => void;\n}) => void;\n\nexport type PaymentKitStates = {\n baseUrl: string;\n apiBaseUrl: string;\n secureToken: string;\n environment: string;\n tunnelXConnection: TunnelXIFrameConnection;\n timingTracker: CheckoutTimingTracker;\n /** Checkout request ID for correlating all API calls and analytics events */\n checkoutRequestId: string;\n /** Card tokenization mode: 'direct' (KMS) or 'vgs' (VGS Collect + proxy) */\n cardTokenizationMode?: \"direct\" | \"vgs\";\n /** VGS vault ID (only when cardTokenizationMode is 'vgs') */\n vgsVaultId?: string;\n /** VGS environment: 'sandbox' or 'live' (only when cardTokenizationMode is 'vgs') */\n vgsEnvironment?: string;\n /** @internal Promise that resolves when session config (incl. VGS mode) is auto-detected */\n _sessionConfigReady?: Promise<void>;\n};\n\nexport type PaymentKitErrors = {\n root?: string;\n card_pan?: CardErrorCode;\n card_exp?: CardErrorCode;\n card_cvc?: CardErrorCode;\n paypal?: string;\n google_pay?: string;\n apple_pay?: string;\n processor_id?: string;\n amount?: string;\n currency?: string;\n country?: string;\n} & { [key in FormFieldNames]?: FormErrorCodes | string };\n\nexport type PaymentKitFields = { [key in FormFieldNames]: string };\n\nexport type PaymentMethod<TExternalFuncs = unknown, TName = string> = (paymentKitStates: PaymentKitStates) => {\n name: TName;\n externalFuncs: TExternalFuncs;\n internalFuncs: TInternalFuncs;\n};\n\nexport type ExternalFuncsMapByPm<T extends readonly PaymentMethod<unknown>[]> = {\n [K in T[number] as ReturnType<K>[\"name\"]]: ReturnType<K>[\"externalFuncs\"];\n};\n","import { version as PACKAGE_VERSION } from \"../package.json\";\nimport { AnalyticsService, CheckoutTimingTracker } from \"./analytics\";\nimport { connectToTunnelXIframe } from \"./penpal/connect-tunnel-x\";\nimport type { ExternalFuncsMapByPm, PaymentKitFields, PaymentKit as PaymentKitType } from \"./types\";\nimport { getUrlsForEnvironment } from \"./types\";\nimport { createCheckoutIFrame } from \"./utils\";\nimport { getOrCreateCheckoutRequestId } from \"./utils/checkout-request-id\";\n\ntype PaymentKitReturnType = ReturnType<PaymentKitType>;\n\nconst createTunnelXConnection = (baseUrl: string, apiBaseUrl: string, token: string) => {\n const iframe = createCheckoutIFrame(\"tunnel-x\", baseUrl, {\n checkout_token: token,\n api_base_url: apiBaseUrl,\n });\n // Tunnel-X is a hidden communication iframe, not a visible UI element\n Object.assign(iframe.style, { width: \"0\", height: \"0\", position: \"absolute\", visibility: \"hidden\" });\n document.body.appendChild(iframe);\n\n const connection = connectToTunnelXIframe(iframe, {});\n\n const unmount = () => {\n connection.destroy();\n document.body.removeChild(iframe);\n };\n\n return { unmount, connection };\n};\n\nconst PaymentKit: PaymentKitType = ({\n environment,\n secureToken,\n paymentMethods,\n cardTokenizationMode,\n vgsVaultId,\n vgsEnvironment,\n __enableAnalyticsTestMode,\n}) => {\n type PaymentMethods = typeof paymentMethods;\n\n // Resolve URLs from environment\n const { baseUrl, apiBaseUrl } = getUrlsForEnvironment(environment);\n\n // Log version for debugging\n console.log(`[PaymentKit] v${PACKAGE_VERSION} initialized (env: ${environment})`);\n\n // Generate checkout request ID for correlating all API calls and analytics events\n // This is idempotent - same ID is reused across multiple PaymentKit instantiations\n const checkoutRequestId = getOrCreateCheckoutRequestId(environment);\n console.log(`[PaymentKit] checkout_request_id: ${checkoutRequestId}`);\n\n // Initialize analytics and identify with secure token for correlation\n AnalyticsService.init(apiBaseUrl, __enableAnalyticsTestMode);\n AnalyticsService.identify(secureToken);\n\n // Create timing tracker for this checkout session\n const timingTracker = new CheckoutTimingTracker(secureToken, checkoutRequestId);\n\n // Track page ready immediately\n timingTracker.trackPageReady();\n\n const { connection: tunnelXConnection, unmount: unmountTunnelX } = createTunnelXConnection(\n baseUrl,\n apiBaseUrl,\n secureToken,\n );\n\n const paymentKitStates = {\n baseUrl,\n apiBaseUrl,\n secureToken,\n environment,\n tunnelXConnection,\n timingTracker,\n checkoutRequestId,\n cardTokenizationMode,\n vgsVaultId,\n vgsEnvironment,\n _sessionConfigReady: undefined as Promise<void> | undefined,\n };\n\n // Auto-detect VGS mode from session API if not explicitly provided.\n // The promise resolves before any card mount or submit needs the mode.\n if (cardTokenizationMode === undefined) {\n const sessionConfigUrl = `${apiBaseUrl}/api/checkout-sessions/token/${secureToken}`;\n console.log(\n `[PaymentKit] cardTokenizationMode not provided — auto-detecting from session API: ${sessionConfigUrl}`,\n );\n paymentKitStates._sessionConfigReady = fetch(sessionConfigUrl)\n .then((resp) => {\n if (!resp.ok) {\n console.warn(`[PaymentKit] Session config fetch failed with status ${resp.status}`);\n return null;\n }\n return resp.json();\n })\n .then((session) => {\n if (session && typeof session === \"object\" && typeof session.card_tokenization_mode === \"string\") {\n paymentKitStates.cardTokenizationMode = session.card_tokenization_mode || \"direct\";\n console.log(\n `[PaymentKit] Session config detected — cardTokenizationMode: ${paymentKitStates.cardTokenizationMode}`,\n );\n\n // Validate VGS config from API before trusting it\n const vaultId = session.vgs_vault_id;\n const vgsEnv = session.vgs_environment;\n if (vaultId && /^tnt[a-z0-9]+$/.test(vaultId) && (vgsEnv === \"sandbox\" || vgsEnv === \"live\")) {\n paymentKitStates.vgsVaultId = vaultId;\n paymentKitStates.vgsEnvironment = vgsEnv;\n console.log(`[PaymentKit] VGS config valid — vaultId: ${vaultId}, environment: ${vgsEnv}`);\n } else if (paymentKitStates.cardTokenizationMode === \"vgs\") {\n console.warn(\n `[PaymentKit] Invalid VGS config from session API (vaultId: ${vaultId}, env: ${vgsEnv}), falling back to direct mode`,\n );\n paymentKitStates.cardTokenizationMode = \"direct\";\n }\n } else {\n console.log(\"[PaymentKit] Session response missing card_tokenization_mode — defaulting to direct mode\");\n paymentKitStates.cardTokenizationMode = \"direct\";\n }\n })\n .catch((err) => {\n console.error(\"[PaymentKit] Session config fetch error — falling back to direct mode:\", err);\n paymentKitStates.cardTokenizationMode = \"direct\";\n });\n } else {\n console.log(`[PaymentKit] cardTokenizationMode explicitly set: ${cardTokenizationMode}`);\n }\n\n const pmInstances = paymentMethods.map((paymentMethod) => paymentMethod(paymentKitStates));\n\n const externalFuncsMapByPm: ExternalFuncsMapByPm<PaymentMethods> = pmInstances.reduce(\n (acc, { name, externalFuncs }) => {\n // @ts-expect-error - typecase this better in future\n acc[name] = externalFuncs;\n return acc;\n },\n {} as ExternalFuncsMapByPm<PaymentMethods>,\n );\n\n const submit: PaymentKitReturnType[\"submit\"] = ({\n paymentMethod: paymentMethodName,\n fields,\n options,\n onSuccess,\n onError,\n }) => {\n const paymentMethod = pmInstances.find(({ name }) => name === paymentMethodName);\n if (!paymentMethod) {\n onError({ root: \"payment_method_not_found\" });\n return;\n }\n paymentMethod.internalFuncs\n .submitPayment(fields, options)\n .then(({ data, errors }) => {\n errors ? onError(errors) : onSuccess(data);\n })\n .catch((e) => {\n console.error(\"PaymentKit:submit:catch\", e);\n\n // Try to extract error message from response\n if (e?.response?.data) {\n onError(e.response.data);\n } else if (e?.message) {\n onError({ root: e.message });\n } else {\n onError({ root: \"unknown_error\" });\n }\n });\n };\n\n const cleanup = () => {\n // Clean up all payment method instances\n for (const pm of pmInstances) {\n if (pm.internalFuncs.cleanup) {\n pm.internalFuncs.cleanup();\n }\n }\n unmountTunnelX();\n };\n\n return {\n submit,\n cleanup,\n ...externalFuncsMapByPm,\n };\n};\n\nexport type { PaymentKitFields };\n\nexport default PaymentKit;\n"],"mappings":";;;;;;cAEa;;;;;;;;;ACKb,IAAa,uBAAb,MAA8D;CAC5D,AAAQ,SAA0B,EAAE;CACpC,AAAQ,aAA4B;CAEpC,SAAS,YAA0B;AACjC,OAAK,aAAa;;CAGpB,QAAQ,OAAe,YAA4C;AACjE,OAAK,OAAO,KAAK;GACf;GACA,aAAa,KAAK;GAClB;GACA,WAAW,KAAK,KAAK;GACtB,CAAC;;CAGJ,gBAAyB;AACvB,SAAO;;;;;CAQT,YAA6B;AAC3B,SAAO,CAAC,GAAG,KAAK,OAAO;;;;;CAMzB,gBAAgB,WAAoC;AAClD,SAAO,KAAK,OAAO,QAAQ,MAAM,EAAE,UAAU,UAAU;;;;;CAMzD,QAAc;AACZ,OAAK,SAAS,EAAE;;;;;;AC5CpB,MAAM,kBAAkB;;;;;;;;AASxB,IAAa,iBAAb,MAAa,eAA2C;CACtD,AAAQ,cAAc;;;;;;;CAQtB,OAAO,YAAY,YAAoC;AACrD,MAAI,OAAO,WAAW,YACpB,OAAM,IAAI,MAAM,gEAAgE;EAGlF,MAAM,WAAW,OAAO;AACxB,MAAI,UAAU;AACZ,OAAI,SAAS,eAAe,WAC1B,OAAM,IAAI,MACR,uEAAuE,SAAS,WAAW,qDACtC,WAAW,GACjE;AAEH,UAAO,SAAS;;EAGlB,MAAM,WAAW,IAAI,eAAe,WAAW;AAC/C,SAAO,wBAAwB;GAAE;GAAU;GAAY;AACvD,SAAO;;CAGT,AAAQ,YAAY,YAAoB;AACtC,OAAK,YAAY,WAAW;;CAG9B,AAAQ,Y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irwallexApplePayShowResult, n as AirwallexApplePayConfig, o as MerchantValidationCallback, r as AirwallexApplePayMockScenario, t as AirwallexApplePayAdapter } from "../airwallex-apple-pay-adapter-
|
|
1
|
+
import { a as ApplePayEncryptedToken, i as AirwallexApplePayShowResult, n as AirwallexApplePayConfig, o as MerchantValidationCallback, r as AirwallexApplePayMockScenario, t as AirwallexApplePayAdapter } from "../airwallex-apple-pay-adapter-D8enI9Vq.mjs";
|
|
2
2
|
export { AirwallexApplePayAdapter, AirwallexApplePayConfig, AirwallexApplePayMockScenario, AirwallexApplePayShowResult, ApplePayEncryptedToken, MerchantValidationCallback };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { n as AirwallexApplePayMockScenario, t as AirwallexApplePayAdapter } from "../airwallex-apple-pay-adapter-
|
|
1
|
+
import { n as AirwallexApplePayMockScenario, t as AirwallexApplePayAdapter } from "../airwallex-apple-pay-adapter-Bs8AdQKY.mjs";
|
|
2
2
|
|
|
3
3
|
export { AirwallexApplePayAdapter, AirwallexApplePayMockScenario };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { r as PaymentMethod } from "../types-CQ8xbgoh.mjs";
|
|
2
2
|
import "../connect-card-Cra_F1cC.mjs";
|
|
3
3
|
import "../connect-tunnel-x-Wh-JG1O5.mjs";
|
|
4
|
-
import { a as ApplePayEncryptedToken } from "../airwallex-apple-pay-adapter-
|
|
4
|
+
import { a as ApplePayEncryptedToken } from "../airwallex-apple-pay-adapter-D8enI9Vq.mjs";
|
|
5
5
|
import { t as ApplePayMockScenario } from "../stripe-apple-pay-adapter-DcuGlQqQ.mjs";
|
|
6
6
|
|
|
7
7
|
//#region src/payment-methods/apple-pay.d.ts
|
|
@@ -48,6 +48,8 @@ type ApplePayConfirmRequest = {
|
|
|
48
48
|
[key: string]: unknown;
|
|
49
49
|
};
|
|
50
50
|
payer_email?: string;
|
|
51
|
+
payer_first_name?: string;
|
|
52
|
+
payer_last_name?: string;
|
|
51
53
|
mock_scenario?: string;
|
|
52
54
|
};
|
|
53
55
|
type ApplePayConfirmResponse = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apple-pay.d.mts","names":[],"sources":["../../src/payment-methods/apple-pay.ts"],"sourcesContent":[],"mappings":";;;;;;;KAYY,oBAAA;;;;;AAAA,KAMA,oBAAA,GANoB;EAMpB,YAAA,EAAA,MAAA;EAcA,aAAA,EAZK,oBAYgB;EAkBrB,cAAA,EAAA;IAKA,SAAA,CAAA,EAAA,MAAA;
|
|
1
|
+
{"version":3,"file":"apple-pay.d.mts","names":[],"sources":["../../src/payment-methods/apple-pay.ts"],"sourcesContent":[],"mappings":";;;;;;;KAYY,oBAAA;;;;;AAAA,KAMA,oBAAA,GANoB;EAMpB,YAAA,EAAA,MAAA;EAcA,aAAA,EAZK,oBAYgB;EAkBrB,cAAA,EAAA;IAKA,SAAA,CAAA,EAAA,MAAA;IAaA,WAAA,CAAA,EAAA;MAsBA,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,OAAqB;IAgQX,CAAA;IAmIN,kBAAkB,CAAA,EAAA;MAOlB,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,OAAqB;IAyf/B,CAAA;EA4G2B,CAAA;EAAqB,aAAA,CAAA,EAAA,MAAA;EA2BvB,cAAA,CAAA,EAAA,MAAA;EAAO,kBAAA,CAAA,EAAA,MAAA;CAvIX;KA77Bf,qBAAA;;;;;;;;;;;;KAkBA,sBAAA;;;;KAKA,sBAAA;;oBAIQ;;;;;;;;KASR,uBAAA;;;;;;;;gBAQI;;;;;;;;;;;;;KAcJ,qBAAA;;;;gBAII;iBACC;;;;;;;;;;;;;;;;;iBA2PK,eAAA,mDAGX,6CAER;;;;;;;;iBA8Ha,kBAAA,CAAA;;;;iBAOA,qBAAA,CAAA;cAyfV,uBAuIgC;;;;;6BA3BL,0BAAqB;;;;;;;;;;;;;;;;;;;;;;;;6BA2BvB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { i as definePaymentMethod, n as collectFraudMetadata, o as getOrCreateCheckoutRequestId } from "../utils-B70Y8YcZ.mjs";
|
|
2
|
-
import { n as AirwallexApplePayMockScenario, t as AirwallexApplePayAdapter } from "../airwallex-apple-pay-adapter-
|
|
2
|
+
import { n as AirwallexApplePayMockScenario, t as AirwallexApplePayAdapter } from "../airwallex-apple-pay-adapter-Bs8AdQKY.mjs";
|
|
3
3
|
import { t as handleNextAction } from "../next-action-handlers-BZs04hYb.mjs";
|
|
4
4
|
import { n as StripeApplePayAdapter, t as ApplePayMockScenario } from "../stripe-apple-pay-adapter-BLRjqgDf.mjs";
|
|
5
5
|
|
|
@@ -254,13 +254,15 @@ async function callAirwallexStartEndpoint(apiBaseUrl, secureToken, options, vali
|
|
|
254
254
|
})
|
|
255
255
|
}, checkoutRequestId);
|
|
256
256
|
}
|
|
257
|
-
async function callAirwallexConfirmEndpoint(apiBaseUrl, secureToken, applePayToken, mockScenarioStr, checkoutRequestId, payerEmail) {
|
|
257
|
+
async function callAirwallexConfirmEndpoint(apiBaseUrl, secureToken, applePayToken, mockScenarioStr, checkoutRequestId, payerEmail, payerFirstName, payerLastName) {
|
|
258
258
|
const result = await apiCall(`${apiBaseUrl}/api/checkout/${secureToken}/airwallex/apple-pay/confirm`, {
|
|
259
259
|
method: "POST",
|
|
260
260
|
headers: { "Content-Type": "application/json" },
|
|
261
261
|
body: JSON.stringify({
|
|
262
262
|
apple_pay_token: applePayToken,
|
|
263
263
|
payer_email: payerEmail,
|
|
264
|
+
payer_first_name: payerFirstName,
|
|
265
|
+
payer_last_name: payerLastName,
|
|
264
266
|
mock_scenario: mockScenarioStr
|
|
265
267
|
})
|
|
266
268
|
}, checkoutRequestId);
|
|
@@ -354,8 +356,8 @@ async function runAirwallexFlow(apiBaseUrl, secureToken, options, mockScenarioSt
|
|
|
354
356
|
if ("cancelled" in paymentResult && paymentResult.cancelled) return { errors: { apple_pay: "Apple Pay cancelled by user" } };
|
|
355
357
|
return { errors: { apple_pay: paymentResult.error || "Apple Pay failed" } };
|
|
356
358
|
}
|
|
357
|
-
const { token, complete, payerEmail } = paymentResult;
|
|
358
|
-
let response = await callAirwallexConfirmEndpoint(apiBaseUrl, secureToken, token, mockScenarioStr, checkoutRequestId, payerEmail);
|
|
359
|
+
const { token, complete, payerEmail, payerFirstName, payerLastName } = paymentResult;
|
|
360
|
+
let response = await callAirwallexConfirmEndpoint(apiBaseUrl, secureToken, token, mockScenarioStr, checkoutRequestId, payerEmail, payerFirstName, payerLastName);
|
|
359
361
|
let userActionCount = 0;
|
|
360
362
|
while (response.charge_status === "pending" && response.next_action && userActionCount < MAX_USER_ACTIONS) {
|
|
361
363
|
userActionCount++;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apple-pay.mjs","names":["preparedStripeState: PreparedStripeApplePayState | null","preparedAirwallexState: PreparedAirwallexState | null","applePayScriptLoadPromise: Promise<void> | null","airwallexMockScenario: AirwallexApplePayMockScenario","config: AirwallexApplePayConfig","submitPayment: TInternalFuncs[\"submitPayment\"]","paymentResult","confirmResult","paymentMethodId","billingDetails","autoPrepareConfig: AutoPrepareConfig | null","onApplePayReadyCallbacks: ((isReady: boolean) => void)[]","pendingAutoPrepareArgs: { apiBaseUrl: string; secureToken: string; environment: string } | null","pendingPrepareResolvers: (() => void)[]"],"sources":["../../src/payment-methods/apple-pay.ts"],"sourcesContent":["import type { PaymentKitErrors, PaymentKitStates, TInternalFuncs } from \"../types\";\nimport { collectFraudMetadata, definePaymentMethod, getOrCreateCheckoutRequestId } from \"../utils\";\nimport {\n AirwallexApplePayAdapter,\n type AirwallexApplePayConfig,\n AirwallexApplePayMockScenario,\n type ApplePayEncryptedToken,\n} from \"./airwallex-apple-pay-adapter\";\nimport { handleNextAction } from \"./next-action-handlers\";\nimport { ApplePayMockScenario, StripeApplePayAdapter } from \"./stripe-apple-pay-adapter\";\n\n// Apple Pay-specific types\nexport type ApplePayCustomerInfo = {\n first_name: string;\n last_name: string;\n email?: string;\n};\n\nexport type ApplePayStartRequest = {\n processor_id: string;\n customer_info: ApplePayCustomerInfo;\n fraud_metadata: {\n ipAddress?: string;\n browserInfo?: { [key: string]: unknown };\n processorFraudInfo?: { [key: string]: unknown };\n };\n mock_scenario?: string;\n // Airwallex-specific fields (sent on /start for merchant validation)\n validation_url?: string;\n initiative_context?: string;\n};\n\nexport type ApplePayStartResponse = {\n checkout_attempt_id: string;\n amount: number;\n currency: string;\n country: string;\n\n // Processor discriminator\n processor: \"stripe\" | \"airwallex\";\n\n // Stripe-specific fields (when processor=\"stripe\")\n client_secret?: string;\n stripe_pk?: string;\n\n // Airwallex-specific fields (when processor=\"airwallex\")\n merchant_session?: { [key: string]: unknown }; // Apple merchant session for completeMerchantValidation()\n};\n\n// Airwallex 3DS next action type (matches backend snake_case response)\nexport type Airwallex3dsNextAction = {\n type: \"airwallex_3ds\";\n url: string;\n};\n\nexport type ApplePayConfirmRequest = {\n // Stripe: Send PaymentMethod ID\n payment_method_id?: string;\n // Airwallex: Send encrypted Apple Pay token\n apple_pay_token?: ApplePayEncryptedToken | { [key: string]: unknown };\n // Email from payment sheet (Stripe Apple Pay) — used to fill missing customer email\n payer_email?: string;\n mock_scenario?: string;\n};\n\nexport type ApplePayConfirmResponse = {\n charge_status: \"success\" | \"fail\" | \"pending\";\n transaction_id?: string;\n error_code?: string;\n error_message_for_customer?: string;\n error_message_for_debug?: string;\n checkout_attempt_id: string;\n checkout_session_id?: string;\n next_action?: Airwallex3dsNextAction; // Present when charge_status=\"pending\" for 3DS\n payment_intent_id?: string;\n customer_id?: string;\n payment_method_id?: string;\n processor_used?: string;\n subscription_id?: string;\n invoice_id?: string;\n invoice_number?: number;\n card_brand?: string;\n card_last4?: string;\n card_exp_month?: number;\n card_exp_year?: number;\n};\n\nexport type ApplePaySubmitOptions = {\n /** Processor external ID — optional when using auto-prepare (SDK fetches from checkout session) */\n processorId?: string;\n processorType?: \"stripe\" | \"airwallex\";\n customerInfo: ApplePayCustomerInfo;\n mockScenario?: ApplePayMockScenario;\n amount?: number; // Amount in atomic units (cents) for Apple Pay sheet display\n currency?: string; // Currency code (e.g., \"usd\")\n /** Country code — optional when using auto-prepare (fetched from backend) */\n country?: string;\n merchantName?: string; // Merchant display name on Apple Pay sheet\n};\n\ntype ApplePayResult =\n | { data: { [key: string]: unknown }; errors?: undefined }\n | { data?: undefined; errors: PaymentKitErrors };\n\n// =============================================================================\n// Prepared Payment State (for Stripe pre-fetching before click)\n// =============================================================================\n\ntype PreparedStripeApplePayState = {\n processor: \"stripe\";\n adapter: StripeApplePayAdapter;\n startData: ApplePayStartResponse;\n mockScenarioStr?: string;\n checkoutRequestId: string;\n};\n\n// Global state to store prepared Apple Pay data (Stripe only - Airwallex uses native session)\nlet preparedStripeState: PreparedStripeApplePayState | null = null;\n\n// Prepared Airwallex state — persists options from prepare call for use at submit time\n// The amount field is fetched from the session via /session-info endpoint to fix\n// the $0.00 bug where options.amount was undefined and defaulted to 0.\ntype PreparedAirwallexState = {\n country: string;\n currency?: string;\n amount?: number;\n merchantName?: string;\n};\nlet preparedAirwallexState: PreparedAirwallexState | null = null;\n\n// Response type for Airwallex Apple Pay session-info endpoint\ntype AirwallexSessionInfoResponse = {\n amount: number;\n currency: string;\n country: string;\n};\n\n// =============================================================================\n// Auto-Prepare State\n// =============================================================================\n\ntype AutoPrepareConfig = {\n processorId: string;\n processorType: \"airwallex\" | \"stripe\" | undefined;\n};\n\ntype CheckoutSessionConfig = {\n express_checkout_processor_id?: string | null;\n express_checkout_processor_type?: string | null;\n};\n\n// =============================================================================\n// Apple Pay SDK Loader\n// =============================================================================\n\n// Loads Apple's JS SDK, which makes window.ApplePaySession available in non-Safari\n// browsers (Chrome, Firefox, etc.) and enables the \"Scan Code with iPhone\" QR flow.\nlet applePayScriptLoadPromise: Promise<void> | null = null;\nasync function loadApplePayScript(): Promise<void> {\n if (typeof window === \"undefined\") return;\n if (window.ApplePaySession) return;\n if (document.getElementById(\"__pk_apple_pay_sdk\")) {\n // Tag exists — if we have a cached promise, wait for it; otherwise the tag\n // already resolved (e.g. after a module reload) so return immediately.\n return applePayScriptLoadPromise ?? Promise.resolve();\n }\n applePayScriptLoadPromise = new Promise<void>((resolve, reject) => {\n const script = document.createElement(\"script\");\n script.id = \"__pk_apple_pay_sdk\";\n script.src = \"https://applepay.cdn-apple.com/jsapi/1.latest/apple-pay-sdk.js\";\n script.crossOrigin = \"anonymous\";\n // NOTE: Apple doesn't publish stable SRI hashes for the 1.latest pointer,\n // so integrity checking is omitted. CSP must allow script-src https://applepay.cdn-apple.com.\n script.onload = () => resolve();\n script.onerror = () => {\n // Clear cached state so the next prepareApplePay call can retry.\n applePayScriptLoadPromise = null;\n document.getElementById(\"__pk_apple_pay_sdk\")?.remove();\n reject(new Error(\"Failed to load Apple Pay SDK\"));\n };\n document.head.appendChild(script);\n });\n return applePayScriptLoadPromise;\n}\n\n// =============================================================================\n// Helper Functions\n// =============================================================================\n\nasync function apiCall<T>(\n url: string,\n options: RequestInit,\n checkoutRequestId?: string,\n): Promise<{ data?: T; error?: string }> {\n const headers = new Headers(options.headers);\n if (checkoutRequestId) {\n headers.set(\"x-request-id\", checkoutRequestId);\n }\n\n const response = await fetch(url, { ...options, headers });\n if (!response.ok) {\n let errorMessage = `Request failed (${response.status})`;\n try {\n const errorData = await response.json();\n errorMessage = errorData.detail || errorMessage;\n } catch {\n errorMessage = response.statusText || errorMessage;\n }\n return { error: errorMessage };\n }\n return { data: await response.json() };\n}\n\nfunction validateOptions(options: ApplePaySubmitOptions): PaymentKitErrors | null {\n if (!options?.processorId) {\n return { processor_id: \"Processor ID is required\" };\n }\n // For Airwallex, country is required (either directly or from prepared state)\n if (options.processorType === \"airwallex\" && !options.country && !preparedAirwallexState?.country) {\n return { country: \"Country is required for Airwallex Apple Pay\" };\n }\n return null;\n}\n\nfunction getMockScenarioStr(mockScenario?: ApplePayMockScenario): string | undefined {\n return mockScenario && mockScenario !== ApplePayMockScenario.None ? mockScenario : undefined;\n}\n\n// =============================================================================\n// Stripe Apple Pay Flow\n// =============================================================================\n\nasync function callStripeStartEndpoint(\n apiBaseUrl: string,\n secureToken: string,\n options: ApplePaySubmitOptions,\n mockScenarioStr?: string,\n checkoutRequestId?: string,\n): Promise<{ data?: ApplePayStartResponse; error?: string }> {\n return apiCall<ApplePayStartResponse>(\n `${apiBaseUrl}/api/checkout/${secureToken}/apple-pay/start`,\n {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({\n processor_id: options.processorId,\n customer_info: options.customerInfo,\n fraud_metadata: collectFraudMetadata(),\n mock_scenario: mockScenarioStr,\n } as ApplePayStartRequest),\n },\n checkoutRequestId,\n );\n}\n\nfunction initializeStripeAdapter(\n stripePk: string,\n mockScenario?: ApplePayMockScenario,\n): { adapter?: StripeApplePayAdapter; error?: string } {\n const adapter = new StripeApplePayAdapter(mockScenario);\n\n if (!adapter.initialize(stripePk)) {\n return { error: 'Stripe.js not loaded. Add <script src=\"https://js.stripe.com/v3/\"></script> to your page.' };\n }\n\n return { adapter };\n}\n\nasync function callStripeConfirmEndpoint(\n apiBaseUrl: string,\n secureToken: string,\n paymentMethodId: string,\n mockScenarioStr?: string,\n checkoutRequestId?: string,\n payerEmail?: string,\n): Promise<ApplePayResult> {\n const result = await apiCall<ApplePayConfirmResponse>(\n `${apiBaseUrl}/api/checkout/${secureToken}/apple-pay/confirm`,\n {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({\n payment_method_id: paymentMethodId,\n payer_email: payerEmail,\n mock_scenario: mockScenarioStr,\n } as ApplePayConfirmRequest),\n },\n checkoutRequestId,\n );\n\n if (result.error || !result.data) {\n return { errors: { apple_pay: result.error || \"Failed to confirm payment\" } };\n }\n\n const confirmData = result.data;\n\n if (confirmData.charge_status === \"success\") {\n return {\n data: {\n id: confirmData.transaction_id,\n checkoutAttemptId: confirmData.checkout_attempt_id,\n checkoutSessionId: confirmData.checkout_session_id ?? secureToken,\n state: \"checkout_succeeded\",\n paymentIntentId: confirmData.payment_intent_id,\n customerId: confirmData.customer_id,\n paymentMethodId: confirmData.payment_method_id,\n processorUsed: confirmData.processor_used,\n subscriptionId: confirmData.subscription_id,\n invoiceId: confirmData.invoice_id,\n invoiceNumber: confirmData.invoice_number,\n cardBrand: confirmData.card_brand,\n cardLast4: confirmData.card_last4,\n cardExpMonth: confirmData.card_exp_month,\n cardExpYear: confirmData.card_exp_year,\n errorCode: confirmData.error_code,\n errorMessageForCustomer: confirmData.error_message_for_customer,\n errorMessageForDebug: confirmData.error_message_for_debug,\n nextAction: confirmData.next_action,\n },\n };\n }\n\n return {\n errors: {\n apple_pay: confirmData.error_message_for_customer || confirmData.error_message_for_debug || \"Payment failed\",\n },\n };\n}\n\n// =============================================================================\n// Prepare Function (call BEFORE the click handler)\n// =============================================================================\n\n/**\n * Pre-fetch Apple Pay data and prepare the PaymentRequest.\n * Call this when the checkout page loads, NOT in the click handler.\n *\n * For Stripe: pre-fetches data and prepares PaymentRequest for immediate show.\n * For Airwallex: returns success with processor=\"airwallex\" so submit uses native ApplePaySession.\n *\n * @deprecated The SDK now calls this automatically on init. Use `onApplePayReady` to observe\n * readiness and `notifyAmountChanged` to trigger re-prepare after amount changes.\n */\nexport async function prepareApplePay(\n apiBaseUrl: string,\n secureToken: string,\n options: ApplePaySubmitOptions,\n environment: string,\n): Promise<{ success: boolean; error?: string; applePay?: boolean }> {\n console.log(\"[ApplePay] prepareApplePay called\");\n\n // Clear any previous state\n preparedStripeState = null;\n\n const mockScenarioStr = getMockScenarioStr(options.mockScenario);\n const checkoutRequestId = getOrCreateCheckoutRequestId(environment);\n console.log(`[ApplePay] Using checkout_request_id: ${checkoutRequestId}`);\n\n // Airwallex: Load Apple Pay SDK (enables QR code flow in non-Safari browsers),\n // then fetch session info to get the correct amount from line items.\n // This fixes the $0.00 bug where options.amount was undefined and defaulted to 0.\n if (options.processorType === \"airwallex\") {\n console.log(\"[ApplePay] Airwallex processor - loading Apple Pay SDK, fetching session info for amount\");\n\n // In mock mode, skip SDK loading and availability check (jsdom has no ApplePaySession)\n if (!mockScenarioStr) {\n try {\n await loadApplePayScript();\n } catch (err) {\n console.error(\"[ApplePay] Failed to load Apple Pay SDK:\", err);\n return { success: false, error: \"Failed to load Apple Pay\" };\n }\n\n let canMake = false;\n try {\n canMake = !!window.ApplePaySession?.canMakePayments();\n } catch {\n canMake = false;\n }\n if (!canMake) {\n console.log(\"[ApplePay] Apple Pay not available on this device/browser\");\n return { success: false, error: \"Apple Pay not available on this device\" };\n }\n }\n\n // Fetch session info (amount, currency, country) from backend\n const sessionInfoResult = await callAirwallexSessionInfoEndpoint(apiBaseUrl, secureToken, checkoutRequestId);\n\n if (sessionInfoResult.error || !sessionInfoResult.data) {\n console.error(\"[ApplePay] Failed to fetch session info:\", sessionInfoResult.error);\n return { success: false, error: sessionInfoResult.error || \"Failed to fetch Apple Pay session info\" };\n }\n\n console.log(\"[ApplePay] Got session info:\", sessionInfoResult.data);\n const resolvedAmount = sessionInfoResult.data.amount;\n const resolvedCurrency = sessionInfoResult.data.currency;\n const resolvedCountry = sessionInfoResult.data.country || options.country;\n\n preparedAirwallexState = {\n country: resolvedCountry,\n currency: resolvedCurrency,\n amount: resolvedAmount,\n merchantName: options.merchantName,\n };\n console.log(\"[ApplePay] Airwallex prepared state:\", preparedAirwallexState);\n return { success: true, applePay: true };\n }\n\n // Call Stripe start endpoint to get PaymentIntent details\n const startResult = await callStripeStartEndpoint(\n apiBaseUrl,\n secureToken,\n options,\n mockScenarioStr,\n checkoutRequestId,\n );\n if (startResult.error || !startResult.data) {\n return { success: false, error: startResult.error || \"Failed to start Apple Pay\" };\n }\n\n const startData = startResult.data;\n\n // Initialize Stripe adapter\n if (!startData.stripe_pk) {\n return { success: false, error: \"Stripe publishable key not provided\" };\n }\n\n const { adapter, error: adapterError } = initializeStripeAdapter(startData.stripe_pk, options.mockScenario);\n if (!adapter) {\n return { success: false, error: adapterError };\n }\n\n // Create PaymentRequest and call canMakePayment() (required before show())\n const prepareResult = await adapter.preparePaymentRequest({\n country: startData.country,\n currency: startData.currency.toLowerCase(),\n total: {\n label: \"Total\",\n amount: startData.amount,\n },\n });\n\n if (!prepareResult.success) {\n return {\n success: false,\n error: prepareResult.error || \"Failed to prepare payment request\",\n applePay: prepareResult.applePay,\n };\n }\n\n if (!prepareResult.applePay) {\n return {\n success: false,\n error: \"Apple Pay is not available on this device or Stripe account\",\n applePay: false,\n };\n }\n\n // Store the prepared state\n preparedStripeState = {\n processor: \"stripe\",\n adapter,\n startData,\n mockScenarioStr,\n checkoutRequestId,\n };\n\n console.log(\"[ApplePay] prepareApplePay success (Stripe), ready for click\");\n return { success: true, applePay: true };\n}\n\n/**\n * Check if Apple Pay has been prepared and is ready.\n */\nexport function isApplePayPrepared(): boolean {\n return (preparedStripeState?.adapter.isPrepared() ?? false) || preparedAirwallexState !== null;\n}\n\n/**\n * Clear prepared state (call on unmount or when checkout changes).\n */\nexport function clearPreparedApplePay(): void {\n if (preparedStripeState) {\n preparedStripeState.adapter.clearPrepared();\n }\n preparedStripeState = null;\n preparedAirwallexState = null;\n}\n\n// =============================================================================\n// Airwallex Apple Pay Flow\n// =============================================================================\n\n/**\n * Fetch session info (amount, currency, country) from the backend.\n * Called during prepareApplePay to get the correct amount BEFORE showing the payment sheet.\n * This fixes the $0.00 bug where options.amount was undefined and defaulted to 0.\n */\nasync function callAirwallexSessionInfoEndpoint(\n apiBaseUrl: string,\n secureToken: string,\n checkoutRequestId?: string,\n): Promise<{ data?: AirwallexSessionInfoResponse; error?: string }> {\n return apiCall<AirwallexSessionInfoResponse>(\n `${apiBaseUrl}/api/checkout/${secureToken}/airwallex/apple-pay/session-info`,\n {\n method: \"GET\",\n headers: { \"Content-Type\": \"application/json\" },\n },\n checkoutRequestId,\n );\n}\n\nasync function callAirwallexStartEndpoint(\n apiBaseUrl: string,\n secureToken: string,\n options: ApplePaySubmitOptions,\n validationUrl: string,\n initiativeContext: string,\n mockScenarioStr?: string,\n checkoutRequestId?: string,\n): Promise<{ data?: ApplePayStartResponse; error?: string }> {\n return apiCall<ApplePayStartResponse>(\n `${apiBaseUrl}/api/checkout/${secureToken}/airwallex/apple-pay/start`,\n {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({\n processor_id: options.processorId,\n customer_info: options.customerInfo,\n fraud_metadata: collectFraudMetadata(),\n validation_url: validationUrl,\n initiative_context: initiativeContext,\n mock_scenario: mockScenarioStr,\n } as ApplePayStartRequest),\n },\n checkoutRequestId,\n );\n}\n\nasync function callAirwallexConfirmEndpoint(\n apiBaseUrl: string,\n secureToken: string,\n applePayToken: ApplePayEncryptedToken,\n mockScenarioStr?: string,\n checkoutRequestId?: string,\n payerEmail?: string,\n): Promise<ApplePayConfirmResponse> {\n const result = await apiCall<ApplePayConfirmResponse>(\n `${apiBaseUrl}/api/checkout/${secureToken}/airwallex/apple-pay/confirm`,\n {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({\n apple_pay_token: applePayToken,\n payer_email: payerEmail,\n mock_scenario: mockScenarioStr,\n } as ApplePayConfirmRequest),\n },\n checkoutRequestId,\n );\n\n if (result.error || !result.data) {\n return {\n charge_status: \"fail\",\n error_message_for_debug: result.error || \"Failed to confirm payment\",\n checkout_attempt_id: \"\",\n };\n }\n\n return result.data;\n}\n\nasync function callAirwallexVerifyEndpoint(\n apiBaseUrl: string,\n secureToken: string,\n checkoutRequestId?: string,\n): Promise<ApplePayConfirmResponse> {\n const result = await apiCall<ApplePayConfirmResponse>(\n `${apiBaseUrl}/api/checkout/${secureToken}/airwallex/apple-pay/verify`,\n {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n },\n checkoutRequestId,\n );\n\n if (result.error || !result.data) {\n return {\n charge_status: \"fail\",\n error_message_for_debug: result.error || \"Failed to verify payment\",\n checkout_attempt_id: \"\",\n };\n }\n\n return result.data;\n}\n\nfunction toApplePayResult(response: ApplePayConfirmResponse, secureToken: string): ApplePayResult {\n if (response.charge_status === \"success\") {\n return {\n data: {\n id: response.transaction_id,\n checkoutAttemptId: response.checkout_attempt_id,\n checkoutSessionId: response.checkout_session_id ?? secureToken,\n state: \"checkout_succeeded\",\n paymentIntentId: response.payment_intent_id,\n customerId: response.customer_id,\n paymentMethodId: response.payment_method_id,\n processorUsed: response.processor_used,\n subscriptionId: response.subscription_id,\n invoiceId: response.invoice_id,\n invoiceNumber: response.invoice_number,\n cardBrand: response.card_brand,\n cardLast4: response.card_last4,\n cardExpMonth: response.card_exp_month,\n cardExpYear: response.card_exp_year,\n errorCode: response.error_code,\n errorMessageForCustomer: response.error_message_for_customer,\n errorMessageForDebug: response.error_message_for_debug,\n nextAction: response.next_action,\n },\n };\n }\n return {\n errors: {\n apple_pay: response.error_message_for_customer || response.error_message_for_debug || \"Payment failed\",\n },\n };\n}\n\nconst MAX_USER_ACTIONS = 5;\n\n/**\n * Run the Airwallex Apple Pay flow with 3DS loop support.\n *\n * Flow:\n * 1. Show native ApplePaySession, call /start for merchant validation\n * 2. On payment authorized, call /confirm with token\n * 3. If 3DS required, handle it and call /verify\n * 4. Loop if more 3DS needed\n */\nasync function runAirwallexFlow(\n apiBaseUrl: string,\n secureToken: string,\n options: ApplePaySubmitOptions,\n mockScenarioStr?: string,\n checkoutRequestId?: string,\n): Promise<ApplePayResult> {\n // Convert mock scenario\n let airwallexMockScenario: AirwallexApplePayMockScenario = AirwallexApplePayMockScenario.None;\n if (options.mockScenario === ApplePayMockScenario.Success) {\n airwallexMockScenario = AirwallexApplePayMockScenario.Success;\n } else if (options.mockScenario === ApplePayMockScenario.Cancelled) {\n airwallexMockScenario = AirwallexApplePayMockScenario.Cancelled;\n }\n\n // Defense-in-depth: validateOptions should catch this earlier, but guard against\n // future callers that bypass validation to avoid an unhandled TypeError on `.toUpperCase()`\n if (!options.country) {\n return { errors: { apple_pay: \"Country is required for Airwallex Apple Pay\" } };\n }\n\n // Auto-fetch amount from session if not provided (e.g. merchant didn't call prepareApplePay first)\n if (!options.amount) {\n const sessionInfoResult = await callAirwallexSessionInfoEndpoint(apiBaseUrl, secureToken, checkoutRequestId);\n if (sessionInfoResult.data) {\n options.amount = sessionInfoResult.data.amount;\n options.currency = options.currency || sessionInfoResult.data.currency;\n options.country = options.country || sessionInfoResult.data.country;\n }\n }\n\n // Initialize adapter\n const adapter = new AirwallexApplePayAdapter(airwallexMockScenario);\n\n const config: AirwallexApplePayConfig = {\n amount: options.amount || 0,\n currency: (options.currency || \"USD\").toUpperCase(),\n country: options.country.toUpperCase(),\n merchantName: options.merchantName || \"Total\",\n };\n\n if (!adapter.initialize(config)) {\n return { errors: { apple_pay: \"Apple Pay not available on this device\" } };\n }\n\n const canPay = await adapter.canMakePayment();\n if (!canPay) {\n return { errors: { apple_pay: \"Apple Pay not available on this device\" } };\n }\n\n // Get current domain for initiative_context\n const initiativeContext = typeof window !== \"undefined\" ? window.location.hostname : \"\";\n\n // In mock mode, call /start first to create checkout attempt (mock adapter skips merchant validation)\n if (airwallexMockScenario !== AirwallexApplePayMockScenario.None) {\n const startResult = await callAirwallexStartEndpoint(\n apiBaseUrl,\n secureToken,\n options,\n \"https://mock-apple-pay-gateway.example.com/paymentservices/startSession\",\n initiativeContext,\n mockScenarioStr,\n checkoutRequestId,\n );\n if (startResult.error || !startResult.data) {\n return { errors: { apple_pay: startResult.error || \"Failed to start mock checkout\" } };\n }\n }\n\n // Show Apple Pay sheet with merchant validation callback\n const paymentResult = await adapter.showPaymentSheet(async (validationUrl: string) => {\n // Call /start endpoint for merchant validation\n const startResult = await callAirwallexStartEndpoint(\n apiBaseUrl,\n secureToken,\n options,\n validationUrl,\n initiativeContext,\n mockScenarioStr,\n checkoutRequestId,\n );\n\n if (startResult.error || !startResult.data) {\n return { error: startResult.error || \"Merchant validation failed\" };\n }\n\n if (!startResult.data.merchant_session) {\n return { error: \"No merchant session returned from server\" };\n }\n\n return { merchantSession: startResult.data.merchant_session };\n });\n\n if (!paymentResult.success) {\n if (\"cancelled\" in paymentResult && paymentResult.cancelled) {\n return { errors: { apple_pay: \"Apple Pay cancelled by user\" } };\n }\n return { errors: { apple_pay: paymentResult.error || \"Apple Pay failed\" } };\n }\n\n const { token, complete, payerEmail } = paymentResult;\n\n // Call /confirm with the token\n let response = await callAirwallexConfirmEndpoint(\n apiBaseUrl,\n secureToken,\n token,\n mockScenarioStr,\n checkoutRequestId,\n payerEmail,\n );\n\n // Handle 3DS loop\n let userActionCount = 0;\n while (response.charge_status === \"pending\" && response.next_action && userActionCount < MAX_USER_ACTIONS) {\n userActionCount++;\n\n // Handle 3DS challenge — Airwallex3dsNextAction is structurally identical to SDK's Airwallex3dsAction\n const nextAction = response.next_action as Airwallex3dsNextAction;\n const actionResult = await handleNextAction({ type: nextAction.type, url: nextAction.url });\n\n // Call verify endpoint\n const verifyResponse = await callAirwallexVerifyEndpoint(apiBaseUrl, secureToken, checkoutRequestId);\n\n // Check if another 3DS action is required\n if (verifyResponse.charge_status === \"pending\" && verifyResponse.next_action) {\n if (!actionResult.success) {\n console.log(\"[ApplePay:Airwallex] 3DS failed but retry available, continuing loop...\");\n }\n response = verifyResponse;\n continue;\n }\n\n // No more actions - check final result\n if (!actionResult.success) {\n complete(\"fail\");\n return {\n errors: {\n apple_pay:\n verifyResponse.error_message_for_customer ||\n verifyResponse.error_message_for_debug ||\n actionResult.error ||\n \"3DS authentication failed\",\n },\n };\n }\n\n // 3DS succeeded\n complete(verifyResponse.charge_status === \"success\" ? \"success\" : \"fail\");\n return toApplePayResult(verifyResponse, secureToken);\n }\n\n // Check for max attempts exceeded\n if (userActionCount >= MAX_USER_ACTIONS) {\n complete(\"fail\");\n return { errors: { apple_pay: \"Too many authentication attempts. Please try again.\" } };\n }\n\n // No 3DS required - complete with result\n complete(response.charge_status === \"success\" ? \"success\" : \"fail\");\n return toApplePayResult(response, secureToken);\n}\n\n// =============================================================================\n// Main Submit Function\n// =============================================================================\n\nconst defSubmitPayment = (\n states: PaymentKitStates,\n getAutoPrepareConfig: () => AutoPrepareConfig | null,\n onClearPrepared: () => void,\n) => {\n const submitPayment: TInternalFuncs[\"submitPayment\"] = async (_fields, options) => {\n const { apiBaseUrl, secureToken, environment } = states;\n const applePayOptions = options as ApplePaySubmitOptions;\n\n // Fill in processorId/processorType from auto-prepare config if merchant didn't provide them\n const autoPrepareConfig = getAutoPrepareConfig();\n if (!applePayOptions.processorId && autoPrepareConfig?.processorId) {\n applePayOptions.processorId = autoPrepareConfig.processorId;\n }\n if (!applePayOptions.processorType && autoPrepareConfig?.processorType) {\n applePayOptions.processorType = autoPrepareConfig.processorType;\n }\n\n // Validate options\n const validationError = validateOptions(applePayOptions);\n if (validationError) {\n return { errors: validationError };\n }\n\n try {\n const mockScenarioStr = getMockScenarioStr(applePayOptions.mockScenario);\n const checkoutRequestId = getOrCreateCheckoutRequestId(environment);\n console.log(`[ApplePay] Using checkout_request_id: ${checkoutRequestId}`);\n\n // Check if we have prepared Stripe state\n if (preparedStripeState?.adapter.isPrepared()) {\n console.log(\"[ApplePay] Using prepared Stripe state for immediate sheet display\");\n\n // Show Apple Pay sheet IMMEDIATELY using the prepared PaymentRequest\n const paymentResultPromise = preparedStripeState.adapter.showPreparedPaymentSheet();\n const paymentResult = await paymentResultPromise;\n\n if (!paymentResult.success) {\n clearPreparedApplePay();\n\n if (\"cancelled\" in paymentResult && paymentResult.cancelled) {\n return { errors: { apple_pay: \"Apple Pay cancelled by user\" } };\n }\n const errorMessage = \"error\" in paymentResult ? paymentResult.error : \"Unknown error\";\n return { errors: { apple_pay: errorMessage } };\n }\n\n const { paymentMethodId, paymentMethodEvent, billingDetails } = paymentResult;\n\n // Confirm with backend using the PaymentMethod ID\n const confirmResult = await callStripeConfirmEndpoint(\n apiBaseUrl,\n secureToken,\n paymentMethodId,\n mockScenarioStr,\n preparedStripeState.checkoutRequestId,\n billingDetails?.email,\n );\n\n // Complete Apple Pay UI based on result\n if (confirmResult.data) {\n paymentMethodEvent.complete(\"success\");\n clearPreparedApplePay();\n return confirmResult;\n } else {\n paymentMethodEvent.complete(\"fail\");\n clearPreparedApplePay();\n return confirmResult;\n }\n }\n\n // Check if this is an Airwallex processor - route directly to Airwallex flow\n if (applePayOptions.processorType === \"airwallex\") {\n console.log(\"[ApplePay] Running Airwallex flow (processorType=airwallex)\");\n // Merge prepare-time state into submit options so country/currency/amount/merchantName\n // are available even if the merchant only provided them at prepare time\n if (preparedAirwallexState) {\n if (applePayOptions.country == null) applePayOptions.country = preparedAirwallexState.country;\n if (applePayOptions.currency == null) applePayOptions.currency = preparedAirwallexState.currency;\n if (applePayOptions.amount == null) applePayOptions.amount = preparedAirwallexState.amount;\n if (applePayOptions.merchantName == null) applePayOptions.merchantName = preparedAirwallexState.merchantName;\n }\n try {\n return await runAirwallexFlow(apiBaseUrl, secureToken, applePayOptions, mockScenarioStr, checkoutRequestId);\n } finally {\n preparedAirwallexState = null;\n }\n }\n\n // No prepared state for Stripe - call start endpoint\n const startResult = await callStripeStartEndpoint(\n apiBaseUrl,\n secureToken,\n applePayOptions,\n mockScenarioStr,\n checkoutRequestId,\n );\n\n if (startResult.error || !startResult.data) {\n return { errors: { apple_pay: startResult.error || \"Failed to start Apple Pay\" } };\n }\n\n const startData = startResult.data;\n\n // Stripe flow without prepared state - this may fail due to user gesture requirements\n console.warn(\"[ApplePay] Stripe flow without prepared state - may fail due to user gesture requirements\");\n\n if (!startData.stripe_pk) {\n return { errors: { apple_pay: \"Stripe publishable key not provided\" } };\n }\n\n const { adapter, error: adapterError } = initializeStripeAdapter(\n startData.stripe_pk,\n applePayOptions.mockScenario,\n );\n if (!adapter) {\n return { errors: { apple_pay: adapterError || \"Failed to initialize Stripe adapter\" } };\n }\n\n // This will likely fail due to user gesture requirements\n const prepareResult = await adapter.preparePaymentRequest({\n country: startData.country,\n currency: startData.currency.toLowerCase(),\n total: { label: \"Total\", amount: startData.amount },\n });\n\n if (!prepareResult.success || !prepareResult.applePay) {\n return { errors: { apple_pay: prepareResult.error || \"Apple Pay not available\" } };\n }\n\n const paymentResult = await adapter.showPreparedPaymentSheet();\n\n if (!paymentResult.success) {\n if (\"cancelled\" in paymentResult && paymentResult.cancelled) {\n return { errors: { apple_pay: \"Apple Pay cancelled by user\" } };\n }\n const errorMessage = \"error\" in paymentResult ? paymentResult.error : \"Unknown error\";\n return { errors: { apple_pay: errorMessage } };\n }\n\n const { paymentMethodId, paymentMethodEvent, billingDetails } = paymentResult;\n\n const confirmResult = await callStripeConfirmEndpoint(\n apiBaseUrl,\n secureToken,\n paymentMethodId,\n mockScenarioStr,\n checkoutRequestId,\n billingDetails?.email,\n );\n\n if (confirmResult.data) {\n paymentMethodEvent.complete(\"success\");\n return confirmResult;\n } else {\n paymentMethodEvent.complete(\"fail\");\n return confirmResult;\n }\n } catch (error) {\n onClearPrepared();\n return { errors: { apple_pay: `Apple Pay error: ${error}` } };\n }\n };\n\n return submitPayment;\n};\n\n// =============================================================================\n// Payment Method Definition\n// =============================================================================\n\nconst ApplePayPaymentMethod = definePaymentMethod((paymentKitStates) => {\n const { apiBaseUrl, secureToken, environment } = paymentKitStates;\n\n // Per-instance auto-prepare state — isolated from other ApplePayPaymentMethod instances.\n // Note: preparedStripeState and preparedAirwallexState remain module-level globals (pre-existing\n // architectural constraint shared across the file). Full per-instance isolation for those fields\n // would require threading them through prepareApplePay, runAirwallexFlow, validateOptions, and\n // defSubmitPayment — tracked as tech debt.\n let autoPrepareConfig: AutoPrepareConfig | null = null;\n let onApplePayReadyCallbacks: ((isReady: boolean) => void)[] = [];\n let isApplePayReadyState = false;\n let autoPrepareInProgress = false;\n // Incremented by cleanup() so that in-flight prepares can detect staleness and skip writes.\n let cleanupGeneration = 0;\n // Stores pending retry args when notifyAmountChanged is called while a prepare is in flight.\n let pendingAutoPrepareArgs: { apiBaseUrl: string; secureToken: string; environment: string } | null = null;\n // Resolvers waiting for a queued retry to complete (for correct notifyAmountChanged await).\n let pendingPrepareResolvers: (() => void)[] = [];\n\n function instanceSetApplePayReady(ready: boolean): void {\n isApplePayReadyState = ready;\n for (const cb of onApplePayReadyCallbacks) cb(ready);\n }\n\n function instanceClearPreparedApplePay(): void {\n clearPreparedApplePay();\n instanceSetApplePayReady(false);\n }\n\n async function instanceAutoPrepare(apBase: string, sToken: string, env: string): Promise<void> {\n if (autoPrepareInProgress) {\n // A prepare is already in flight; queue a retry and return a promise that resolves after it.\n pendingAutoPrepareArgs = { apiBaseUrl: apBase, secureToken: sToken, environment: env };\n return new Promise<void>((resolve) => pendingPrepareResolvers.push(resolve));\n }\n autoPrepareInProgress = true;\n // Capture generation so we can detect if cleanup() is called while we await.\n const capturedGeneration = cleanupGeneration;\n instanceSetApplePayReady(false);\n\n try {\n const result = await apiCall<CheckoutSessionConfig>(`${apBase}/api/checkout-sessions/token/${sToken}`, {\n method: \"GET\",\n });\n\n if (cleanupGeneration !== capturedGeneration) return;\n\n if (result.error || !result.data) {\n console.warn(\"[ApplePay] Auto-prepare: failed to fetch session config:\", result.error);\n return;\n }\n\n const processorId = result.data.express_checkout_processor_id;\n if (!processorId) {\n console.log(\"[ApplePay] Auto-prepare: no express checkout processor configured\");\n return;\n }\n\n const processorTypeRaw = result.data.express_checkout_processor_type;\n const processorType =\n processorTypeRaw === \"airwallex\" || processorTypeRaw === \"stripe\" ? processorTypeRaw : undefined;\n\n autoPrepareConfig = { processorId, processorType };\n\n const prepareResult = await prepareApplePay(\n apBase,\n sToken,\n { processorId, processorType, customerInfo: { first_name: \"\", last_name: \"\" } },\n env,\n );\n\n if (cleanupGeneration !== capturedGeneration) return;\n\n instanceSetApplePayReady(prepareResult.success && prepareResult.applePay === true);\n } catch (err) {\n if (cleanupGeneration === capturedGeneration) {\n console.error(\"[ApplePay] Auto-prepare failed:\", err);\n }\n } finally {\n autoPrepareInProgress = false;\n const stale = cleanupGeneration !== capturedGeneration;\n const pending = pendingAutoPrepareArgs;\n const resolvers = pendingPrepareResolvers.splice(0);\n if (!stale && pending) {\n pendingAutoPrepareArgs = null;\n // Run the retry and resolve all callers that were waiting for it.\n instanceAutoPrepare(pending.apiBaseUrl, pending.secureToken, pending.environment).then(() => {\n for (const r of resolvers) r();\n });\n } else {\n pendingAutoPrepareArgs = null;\n for (const r of resolvers) r();\n }\n }\n }\n\n // Start auto-prepare immediately so the button is ready before first user interaction.\n instanceAutoPrepare(apiBaseUrl, secureToken, environment).catch((err) =>\n console.error(\"[ApplePay] Unhandled init error:\", err),\n );\n\n return {\n name: \"apple_pay\",\n externalFuncs: {\n /**\n * @deprecated The SDK now prepares automatically on init. Use `onApplePayReady` to observe\n * readiness and `notifyAmountChanged` to trigger re-prepare after amount changes.\n */\n prepareApplePay: (options: ApplePaySubmitOptions) =>\n prepareApplePay(apiBaseUrl, secureToken, options, environment),\n\n /**\n * Register a callback to be notified when Apple Pay becomes ready (or stops being ready).\n * Fires immediately with the current state, then on every state change.\n * Multiple registrations are all notified — none are silently overwritten.\n *\n * @example\n * paymentKit.apple_pay.onApplePayReady((isReady) => {\n * applePayButton.disabled = !isReady;\n * });\n */\n onApplePayReady: (callback: (isReady: boolean) => void): void => {\n onApplePayReadyCallbacks.push(callback);\n // Fire immediately with current state so caller doesn't miss a completed prepare.\n callback(isApplePayReadyState);\n },\n\n /**\n * Notify the SDK that the payment amount has changed (e.g. coupon applied).\n * The SDK will disable Apple Pay, re-prepare with the new amount, then re-enable.\n * The returned promise resolves when re-prepare is fully complete.\n *\n * @example\n * await paymentKit.apple_pay.notifyAmountChanged();\n */\n notifyAmountChanged: (): Promise<void> => {\n instanceClearPreparedApplePay();\n autoPrepareConfig = null;\n return instanceAutoPrepare(apiBaseUrl, secureToken, environment);\n },\n },\n internalFuncs: {\n submitPayment: defSubmitPayment(paymentKitStates, () => autoPrepareConfig, instanceClearPreparedApplePay),\n cleanup: () => {\n cleanupGeneration++;\n onApplePayReadyCallbacks = [];\n isApplePayReadyState = false;\n autoPrepareConfig = null;\n pendingAutoPrepareArgs = null;\n pendingPrepareResolvers = [];\n autoPrepareInProgress = false;\n clearPreparedApplePay();\n },\n },\n };\n});\n\nex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AM;EACN,eAAe;GAKb,kBAAkB,YAChB,gBAAgB,YAAY,aAAa,SAAS,YAAY;GAYhE,kBAAkB,aAA+C;AAC/D,6BAAyB,KAAK,SAAS;AAEvC,aAAS,qBAAqB;;GAWhC,2BAA0C;AACxC,mCAA+B;AAC/B,wBAAoB;AACpB,WAAO,oBAAoB,YAAY,aAAa,YAAY;;GAEnE;EACD,eAAe;GACb,eAAe,iBAAiB,wBAAwB,mBAAmB,8BAA8B;GACzG,eAAe;AACb;AACA,+BAA2B,EAAE;AAC7B,2BAAuB;AACvB,wBAAoB;AACpB,6BAAyB;AACzB,8BAA0B,EAAE;AAC5B,4BAAwB;AACxB,2BAAuB;;GAE1B;EACF;EACD;AAIF,wBAAe"}
|
|
1
|
+
{"version":3,"file":"apple-pay.mjs","names":["preparedStripeState: PreparedStripeApplePayState | null","preparedAirwallexState: PreparedAirwallexState | null","applePayScriptLoadPromise: Promise<void> | null","airwallexMockScenario: AirwallexApplePayMockScenario","config: AirwallexApplePayConfig","submitPayment: TInternalFuncs[\"submitPayment\"]","paymentResult","confirmResult","paymentMethodId","billingDetails","autoPrepareConfig: AutoPrepareConfig | null","onApplePayReadyCallbacks: ((isReady: boolean) => void)[]","pendingAutoPrepareArgs: { apiBaseUrl: string; secureToken: string; environment: string } | null","pendingPrepareResolvers: (() => void)[]"],"sources":["../../src/payment-methods/apple-pay.ts"],"sourcesContent":["import type { PaymentKitErrors, PaymentKitStates, TInternalFuncs } from \"../types\";\nimport { collectFraudMetadata, definePaymentMethod, getOrCreateCheckoutRequestId } from \"../utils\";\nimport {\n AirwallexApplePayAdapter,\n type AirwallexApplePayConfig,\n AirwallexApplePayMockScenario,\n type ApplePayEncryptedToken,\n} from \"./airwallex-apple-pay-adapter\";\nimport { handleNextAction } from \"./next-action-handlers\";\nimport { ApplePayMockScenario, StripeApplePayAdapter } from \"./stripe-apple-pay-adapter\";\n\n// Apple Pay-specific types\nexport type ApplePayCustomerInfo = {\n first_name: string;\n last_name: string;\n email?: string;\n};\n\nexport type ApplePayStartRequest = {\n processor_id: string;\n customer_info: ApplePayCustomerInfo;\n fraud_metadata: {\n ipAddress?: string;\n browserInfo?: { [key: string]: unknown };\n processorFraudInfo?: { [key: string]: unknown };\n };\n mock_scenario?: string;\n // Airwallex-specific fields (sent on /start for merchant validation)\n validation_url?: string;\n initiative_context?: string;\n};\n\nexport type ApplePayStartResponse = {\n checkout_attempt_id: string;\n amount: number;\n currency: string;\n country: string;\n\n // Processor discriminator\n processor: \"stripe\" | \"airwallex\";\n\n // Stripe-specific fields (when processor=\"stripe\")\n client_secret?: string;\n stripe_pk?: string;\n\n // Airwallex-specific fields (when processor=\"airwallex\")\n merchant_session?: { [key: string]: unknown }; // Apple merchant session for completeMerchantValidation()\n};\n\n// Airwallex 3DS next action type (matches backend snake_case response)\nexport type Airwallex3dsNextAction = {\n type: \"airwallex_3ds\";\n url: string;\n};\n\nexport type ApplePayConfirmRequest = {\n // Stripe: Send PaymentMethod ID\n payment_method_id?: string;\n // Airwallex: Send encrypted Apple Pay token\n apple_pay_token?: ApplePayEncryptedToken | { [key: string]: unknown };\n // Email from payment sheet (Stripe Apple Pay) — used to fill missing customer email\n payer_email?: string;\n // Name from payment sheet — used to fill missing customer name\n payer_first_name?: string;\n payer_last_name?: string;\n mock_scenario?: string;\n};\n\nexport type ApplePayConfirmResponse = {\n charge_status: \"success\" | \"fail\" | \"pending\";\n transaction_id?: string;\n error_code?: string;\n error_message_for_customer?: string;\n error_message_for_debug?: string;\n checkout_attempt_id: string;\n checkout_session_id?: string;\n next_action?: Airwallex3dsNextAction; // Present when charge_status=\"pending\" for 3DS\n payment_intent_id?: string;\n customer_id?: string;\n payment_method_id?: string;\n processor_used?: string;\n subscription_id?: string;\n invoice_id?: string;\n invoice_number?: number;\n card_brand?: string;\n card_last4?: string;\n card_exp_month?: number;\n card_exp_year?: number;\n};\n\nexport type ApplePaySubmitOptions = {\n /** Processor external ID — optional when using auto-prepare (SDK fetches from checkout session) */\n processorId?: string;\n processorType?: \"stripe\" | \"airwallex\";\n customerInfo: ApplePayCustomerInfo;\n mockScenario?: ApplePayMockScenario;\n amount?: number; // Amount in atomic units (cents) for Apple Pay sheet display\n currency?: string; // Currency code (e.g., \"usd\")\n /** Country code — optional when using auto-prepare (fetched from backend) */\n country?: string;\n merchantName?: string; // Merchant display name on Apple Pay sheet\n};\n\ntype ApplePayResult =\n | { data: { [key: string]: unknown }; errors?: undefined }\n | { data?: undefined; errors: PaymentKitErrors };\n\n// =============================================================================\n// Prepared Payment State (for Stripe pre-fetching before click)\n// =============================================================================\n\ntype PreparedStripeApplePayState = {\n processor: \"stripe\";\n adapter: StripeApplePayAdapter;\n startData: ApplePayStartResponse;\n mockScenarioStr?: string;\n checkoutRequestId: string;\n};\n\n// Global state to store prepared Apple Pay data (Stripe only - Airwallex uses native session)\nlet preparedStripeState: PreparedStripeApplePayState | null = null;\n\n// Prepared Airwallex state — persists options from prepare call for use at submit time\n// The amount field is fetched from the session via /session-info endpoint to fix\n// the $0.00 bug where options.amount was undefined and defaulted to 0.\ntype PreparedAirwallexState = {\n country: string;\n currency?: string;\n amount?: number;\n merchantName?: string;\n};\nlet preparedAirwallexState: PreparedAirwallexState | null = null;\n\n// Response type for Airwallex Apple Pay session-info endpoint\ntype AirwallexSessionInfoResponse = {\n amount: number;\n currency: string;\n country: string;\n};\n\n// =============================================================================\n// Auto-Prepare State\n// =============================================================================\n\ntype AutoPrepareConfig = {\n processorId: string;\n processorType: \"airwallex\" | \"stripe\" | undefined;\n};\n\ntype CheckoutSessionConfig = {\n express_checkout_processor_id?: string | null;\n express_checkout_processor_type?: string | null;\n};\n\n// =============================================================================\n// Apple Pay SDK Loader\n// =============================================================================\n\n// Loads Apple's JS SDK, which makes window.ApplePaySession available in non-Safari\n// browsers (Chrome, Firefox, etc.) and enables the \"Scan Code with iPhone\" QR flow.\nlet applePayScriptLoadPromise: Promise<void> | null = null;\nasync function loadApplePayScript(): Promise<void> {\n if (typeof window === \"undefined\") return;\n if (window.ApplePaySession) return;\n if (document.getElementById(\"__pk_apple_pay_sdk\")) {\n // Tag exists — if we have a cached promise, wait for it; otherwise the tag\n // already resolved (e.g. after a module reload) so return immediately.\n return applePayScriptLoadPromise ?? Promise.resolve();\n }\n applePayScriptLoadPromise = new Promise<void>((resolve, reject) => {\n const script = document.createElement(\"script\");\n script.id = \"__pk_apple_pay_sdk\";\n script.src = \"https://applepay.cdn-apple.com/jsapi/1.latest/apple-pay-sdk.js\";\n script.crossOrigin = \"anonymous\";\n // NOTE: Apple doesn't publish stable SRI hashes for the 1.latest pointer,\n // so integrity checking is omitted. CSP must allow script-src https://applepay.cdn-apple.com.\n script.onload = () => resolve();\n script.onerror = () => {\n // Clear cached state so the next prepareApplePay call can retry.\n applePayScriptLoadPromise = null;\n document.getElementById(\"__pk_apple_pay_sdk\")?.remove();\n reject(new Error(\"Failed to load Apple Pay SDK\"));\n };\n document.head.appendChild(script);\n });\n return applePayScriptLoadPromise;\n}\n\n// =============================================================================\n// Helper Functions\n// =============================================================================\n\nasync function apiCall<T>(\n url: string,\n options: RequestInit,\n checkoutRequestId?: string,\n): Promise<{ data?: T; error?: string }> {\n const headers = new Headers(options.headers);\n if (checkoutRequestId) {\n headers.set(\"x-request-id\", checkoutRequestId);\n }\n\n const response = await fetch(url, { ...options, headers });\n if (!response.ok) {\n let errorMessage = `Request failed (${response.status})`;\n try {\n const errorData = await response.json();\n errorMessage = errorData.detail || errorMessage;\n } catch {\n errorMessage = response.statusText || errorMessage;\n }\n return { error: errorMessage };\n }\n return { data: await response.json() };\n}\n\nfunction validateOptions(options: ApplePaySubmitOptions): PaymentKitErrors | null {\n if (!options?.processorId) {\n return { processor_id: \"Processor ID is required\" };\n }\n // For Airwallex, country is required (either directly or from prepared state)\n if (options.processorType === \"airwallex\" && !options.country && !preparedAirwallexState?.country) {\n return { country: \"Country is required for Airwallex Apple Pay\" };\n }\n return null;\n}\n\nfunction getMockScenarioStr(mockScenario?: ApplePayMockScenario): string | undefined {\n return mockScenario && mockScenario !== ApplePayMockScenario.None ? mockScenario : undefined;\n}\n\n// =============================================================================\n// Stripe Apple Pay Flow\n// =============================================================================\n\nasync function callStripeStartEndpoint(\n apiBaseUrl: string,\n secureToken: string,\n options: ApplePaySubmitOptions,\n mockScenarioStr?: string,\n checkoutRequestId?: string,\n): Promise<{ data?: ApplePayStartResponse; error?: string }> {\n return apiCall<ApplePayStartResponse>(\n `${apiBaseUrl}/api/checkout/${secureToken}/apple-pay/start`,\n {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({\n processor_id: options.processorId,\n customer_info: options.customerInfo,\n fraud_metadata: collectFraudMetadata(),\n mock_scenario: mockScenarioStr,\n } as ApplePayStartRequest),\n },\n checkoutRequestId,\n );\n}\n\nfunction initializeStripeAdapter(\n stripePk: string,\n mockScenario?: ApplePayMockScenario,\n): { adapter?: StripeApplePayAdapter; error?: string } {\n const adapter = new StripeApplePayAdapter(mockScenario);\n\n if (!adapter.initialize(stripePk)) {\n return { error: 'Stripe.js not loaded. Add <script src=\"https://js.stripe.com/v3/\"></script> to your page.' };\n }\n\n return { adapter };\n}\n\nasync function callStripeConfirmEndpoint(\n apiBaseUrl: string,\n secureToken: string,\n paymentMethodId: string,\n mockScenarioStr?: string,\n checkoutRequestId?: string,\n payerEmail?: string,\n): Promise<ApplePayResult> {\n const result = await apiCall<ApplePayConfirmResponse>(\n `${apiBaseUrl}/api/checkout/${secureToken}/apple-pay/confirm`,\n {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({\n payment_method_id: paymentMethodId,\n payer_email: payerEmail,\n mock_scenario: mockScenarioStr,\n } as ApplePayConfirmRequest),\n },\n checkoutRequestId,\n );\n\n if (result.error || !result.data) {\n return { errors: { apple_pay: result.error || \"Failed to confirm payment\" } };\n }\n\n const confirmData = result.data;\n\n if (confirmData.charge_status === \"success\") {\n return {\n data: {\n id: confirmData.transaction_id,\n checkoutAttemptId: confirmData.checkout_attempt_id,\n checkoutSessionId: confirmData.checkout_session_id ?? secureToken,\n state: \"checkout_succeeded\",\n paymentIntentId: confirmData.payment_intent_id,\n customerId: confirmData.customer_id,\n paymentMethodId: confirmData.payment_method_id,\n processorUsed: confirmData.processor_used,\n subscriptionId: confirmData.subscription_id,\n invoiceId: confirmData.invoice_id,\n invoiceNumber: confirmData.invoice_number,\n cardBrand: confirmData.card_brand,\n cardLast4: confirmData.card_last4,\n cardExpMonth: confirmData.card_exp_month,\n cardExpYear: confirmData.card_exp_year,\n errorCode: confirmData.error_code,\n errorMessageForCustomer: confirmData.error_message_for_customer,\n errorMessageForDebug: confirmData.error_message_for_debug,\n nextAction: confirmData.next_action,\n },\n };\n }\n\n return {\n errors: {\n apple_pay: confirmData.error_message_for_customer || confirmData.error_message_for_debug || \"Payment failed\",\n },\n };\n}\n\n// =============================================================================\n// Prepare Function (call BEFORE the click handler)\n// =============================================================================\n\n/**\n * Pre-fetch Apple Pay data and prepare the PaymentRequest.\n * Call this when the checkout page loads, NOT in the click handler.\n *\n * For Stripe: pre-fetches data and prepares PaymentRequest for immediate show.\n * For Airwallex: returns success with processor=\"airwallex\" so submit uses native ApplePaySession.\n *\n * @deprecated The SDK now calls this automatically on init. Use `onApplePayReady` to observe\n * readiness and `notifyAmountChanged` to trigger re-prepare after amount changes.\n */\nexport async function prepareApplePay(\n apiBaseUrl: string,\n secureToken: string,\n options: ApplePaySubmitOptions,\n environment: string,\n): Promise<{ success: boolean; error?: string; applePay?: boolean }> {\n console.log(\"[ApplePay] prepareApplePay called\");\n\n // Clear any previous state\n preparedStripeState = null;\n\n const mockScenarioStr = getMockScenarioStr(options.mockScenario);\n const checkoutRequestId = getOrCreateCheckoutRequestId(environment);\n console.log(`[ApplePay] Using checkout_request_id: ${checkoutRequestId}`);\n\n // Airwallex: Load Apple Pay SDK (enables QR code flow in non-Safari browsers),\n // then fetch session info to get the correct amount from line items.\n // This fixes the $0.00 bug where options.amount was undefined and defaulted to 0.\n if (options.processorType === \"airwallex\") {\n console.log(\"[ApplePay] Airwallex processor - loading Apple Pay SDK, fetching session info for amount\");\n\n // In mock mode, skip SDK loading and availability check (jsdom has no ApplePaySession)\n if (!mockScenarioStr) {\n try {\n await loadApplePayScript();\n } catch (err) {\n console.error(\"[ApplePay] Failed to load Apple Pay SDK:\", err);\n return { success: false, error: \"Failed to load Apple Pay\" };\n }\n\n let canMake = false;\n try {\n canMake = !!window.ApplePaySession?.canMakePayments();\n } catch {\n canMake = false;\n }\n if (!canMake) {\n console.log(\"[ApplePay] Apple Pay not available on this device/browser\");\n return { success: false, error: \"Apple Pay not available on this device\" };\n }\n }\n\n // Fetch session info (amount, currency, country) from backend\n const sessionInfoResult = await callAirwallexSessionInfoEndpoint(apiBaseUrl, secureToken, checkoutRequestId);\n\n if (sessionInfoResult.error || !sessionInfoResult.data) {\n console.error(\"[ApplePay] Failed to fetch session info:\", sessionInfoResult.error);\n return { success: false, error: sessionInfoResult.error || \"Failed to fetch Apple Pay session info\" };\n }\n\n console.log(\"[ApplePay] Got session info:\", sessionInfoResult.data);\n const resolvedAmount = sessionInfoResult.data.amount;\n const resolvedCurrency = sessionInfoResult.data.currency;\n const resolvedCountry = sessionInfoResult.data.country || options.country;\n\n preparedAirwallexState = {\n country: resolvedCountry,\n currency: resolvedCurrency,\n amount: resolvedAmount,\n merchantName: options.merchantName,\n };\n console.log(\"[ApplePay] Airwallex prepared state:\", preparedAirwallexState);\n return { success: true, applePay: true };\n }\n\n // Call Stripe start endpoint to get PaymentIntent details\n const startResult = await callStripeStartEndpoint(\n apiBaseUrl,\n secureToken,\n options,\n mockScenarioStr,\n checkoutRequestId,\n );\n if (startResult.error || !startResult.data) {\n return { success: false, error: startResult.error || \"Failed to start Apple Pay\" };\n }\n\n const startData = startResult.data;\n\n // Initialize Stripe adapter\n if (!startData.stripe_pk) {\n return { success: false, error: \"Stripe publishable key not provided\" };\n }\n\n const { adapter, error: adapterError } = initializeStripeAdapter(startData.stripe_pk, options.mockScenario);\n if (!adapter) {\n return { success: false, error: adapterError };\n }\n\n // Create PaymentRequest and call canMakePayment() (required before show())\n const prepareResult = await adapter.preparePaymentRequest({\n country: startData.country,\n currency: startData.currency.toLowerCase(),\n total: {\n label: \"Total\",\n amount: startData.amount,\n },\n });\n\n if (!prepareResult.success) {\n return {\n success: false,\n error: prepareResult.error || \"Failed to prepare payment request\",\n applePay: prepareResult.applePay,\n };\n }\n\n if (!prepareResult.applePay) {\n return {\n success: false,\n error: \"Apple Pay is not available on this device or Stripe account\",\n applePay: false,\n };\n }\n\n // Store the prepared state\n preparedStripeState = {\n processor: \"stripe\",\n adapter,\n startData,\n mockScenarioStr,\n checkoutRequestId,\n };\n\n console.log(\"[ApplePay] prepareApplePay success (Stripe), ready for click\");\n return { success: true, applePay: true };\n}\n\n/**\n * Check if Apple Pay has been prepared and is ready.\n */\nexport function isApplePayPrepared(): boolean {\n return (preparedStripeState?.adapter.isPrepared() ?? false) || preparedAirwallexState !== null;\n}\n\n/**\n * Clear prepared state (call on unmount or when checkout changes).\n */\nexport function clearPreparedApplePay(): void {\n if (preparedStripeState) {\n preparedStripeState.adapter.clearPrepared();\n }\n preparedStripeState = null;\n preparedAirwallexState = null;\n}\n\n// =============================================================================\n// Airwallex Apple Pay Flow\n// =============================================================================\n\n/**\n * Fetch session info (amount, currency, country) from the backend.\n * Called during prepareApplePay to get the correct amount BEFORE showing the payment sheet.\n * This fixes the $0.00 bug where options.amount was undefined and defaulted to 0.\n */\nasync function callAirwallexSessionInfoEndpoint(\n apiBaseUrl: string,\n secureToken: string,\n checkoutRequestId?: string,\n): Promise<{ data?: AirwallexSessionInfoResponse; error?: string }> {\n return apiCall<AirwallexSessionInfoResponse>(\n `${apiBaseUrl}/api/checkout/${secureToken}/airwallex/apple-pay/session-info`,\n {\n method: \"GET\",\n headers: { \"Content-Type\": \"application/json\" },\n },\n checkoutRequestId,\n );\n}\n\nasync function callAirwallexStartEndpoint(\n apiBaseUrl: string,\n secureToken: string,\n options: ApplePaySubmitOptions,\n validationUrl: string,\n initiativeContext: string,\n mockScenarioStr?: string,\n checkoutRequestId?: string,\n): Promise<{ data?: ApplePayStartResponse; error?: string }> {\n return apiCall<ApplePayStartResponse>(\n `${apiBaseUrl}/api/checkout/${secureToken}/airwallex/apple-pay/start`,\n {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({\n processor_id: options.processorId,\n customer_info: options.customerInfo,\n fraud_metadata: collectFraudMetadata(),\n validation_url: validationUrl,\n initiative_context: initiativeContext,\n mock_scenario: mockScenarioStr,\n } as ApplePayStartRequest),\n },\n checkoutRequestId,\n );\n}\n\nasync function callAirwallexConfirmEndpoint(\n apiBaseUrl: string,\n secureToken: string,\n applePayToken: ApplePayEncryptedToken,\n mockScenarioStr?: string,\n checkoutRequestId?: string,\n payerEmail?: string,\n payerFirstName?: string,\n payerLastName?: string,\n): Promise<ApplePayConfirmResponse> {\n const result = await apiCall<ApplePayConfirmResponse>(\n `${apiBaseUrl}/api/checkout/${secureToken}/airwallex/apple-pay/confirm`,\n {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({\n apple_pay_token: applePayToken,\n payer_email: payerEmail,\n payer_first_name: payerFirstName,\n payer_last_name: payerLastName,\n mock_scenario: mockScenarioStr,\n } as ApplePayConfirmRequest),\n },\n checkoutRequestId,\n );\n\n if (result.error || !result.data) {\n return {\n charge_status: \"fail\",\n error_message_for_debug: result.error || \"Failed to confirm payment\",\n checkout_attempt_id: \"\",\n };\n }\n\n return result.data;\n}\n\nasync function callAirwallexVerifyEndpoint(\n apiBaseUrl: string,\n secureToken: string,\n checkoutRequestId?: string,\n): Promise<ApplePayConfirmResponse> {\n const result = await apiCall<ApplePayConfirmResponse>(\n `${apiBaseUrl}/api/checkout/${secureToken}/airwallex/apple-pay/verify`,\n {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n },\n checkoutRequestId,\n );\n\n if (result.error || !result.data) {\n return {\n charge_status: \"fail\",\n error_message_for_debug: result.error || \"Failed to verify payment\",\n checkout_attempt_id: \"\",\n };\n }\n\n return result.data;\n}\n\nfunction toApplePayResult(response: ApplePayConfirmResponse, secureToken: string): ApplePayResult {\n if (response.charge_status === \"success\") {\n return {\n data: {\n id: response.transaction_id,\n checkoutAttemptId: response.checkout_attempt_id,\n checkoutSessionId: response.checkout_session_id ?? secureToken,\n state: \"checkout_succeeded\",\n paymentIntentId: response.payment_intent_id,\n customerId: response.customer_id,\n paymentMethodId: response.payment_method_id,\n processorUsed: response.processor_used,\n subscriptionId: response.subscription_id,\n invoiceId: response.invoice_id,\n invoiceNumber: response.invoice_number,\n cardBrand: response.card_brand,\n cardLast4: response.card_last4,\n cardExpMonth: response.card_exp_month,\n cardExpYear: response.card_exp_year,\n errorCode: response.error_code,\n errorMessageForCustomer: response.error_message_for_customer,\n errorMessageForDebug: response.error_message_for_debug,\n nextAction: response.next_action,\n },\n };\n }\n return {\n errors: {\n apple_pay: response.error_message_for_customer || response.error_message_for_debug || \"Payment failed\",\n },\n };\n}\n\nconst MAX_USER_ACTIONS = 5;\n\n/**\n * Run the Airwallex Apple Pay flow with 3DS loop support.\n *\n * Flow:\n * 1. Show native ApplePaySession, call /start for merchant validation\n * 2. On payment authorized, call /confirm with token\n * 3. If 3DS required, handle it and call /verify\n * 4. Loop if more 3DS needed\n */\nasync function runAirwallexFlow(\n apiBaseUrl: string,\n secureToken: string,\n options: ApplePaySubmitOptions,\n mockScenarioStr?: string,\n checkoutRequestId?: string,\n): Promise<ApplePayResult> {\n // Convert mock scenario\n let airwallexMockScenario: AirwallexApplePayMockScenario = AirwallexApplePayMockScenario.None;\n if (options.mockScenario === ApplePayMockScenario.Success) {\n airwallexMockScenario = AirwallexApplePayMockScenario.Success;\n } else if (options.mockScenario === ApplePayMockScenario.Cancelled) {\n airwallexMockScenario = AirwallexApplePayMockScenario.Cancelled;\n }\n\n // Defense-in-depth: validateOptions should catch this earlier, but guard against\n // future callers that bypass validation to avoid an unhandled TypeError on `.toUpperCase()`\n if (!options.country) {\n return { errors: { apple_pay: \"Country is required for Airwallex Apple Pay\" } };\n }\n\n // Auto-fetch amount from session if not provided (e.g. merchant didn't call prepareApplePay first)\n if (!options.amount) {\n const sessionInfoResult = await callAirwallexSessionInfoEndpoint(apiBaseUrl, secureToken, checkoutRequestId);\n if (sessionInfoResult.data) {\n options.amount = sessionInfoResult.data.amount;\n options.currency = options.currency || sessionInfoResult.data.currency;\n options.country = options.country || sessionInfoResult.data.country;\n }\n }\n\n // Initialize adapter\n const adapter = new AirwallexApplePayAdapter(airwallexMockScenario);\n\n const config: AirwallexApplePayConfig = {\n amount: options.amount || 0,\n currency: (options.currency || \"USD\").toUpperCase(),\n country: options.country.toUpperCase(),\n merchantName: options.merchantName || \"Total\",\n };\n\n if (!adapter.initialize(config)) {\n return { errors: { apple_pay: \"Apple Pay not available on this device\" } };\n }\n\n const canPay = await adapter.canMakePayment();\n if (!canPay) {\n return { errors: { apple_pay: \"Apple Pay not available on this device\" } };\n }\n\n // Get current domain for initiative_context\n const initiativeContext = typeof window !== \"undefined\" ? window.location.hostname : \"\";\n\n // In mock mode, call /start first to create checkout attempt (mock adapter skips merchant validation)\n if (airwallexMockScenario !== AirwallexApplePayMockScenario.None) {\n const startResult = await callAirwallexStartEndpoint(\n apiBaseUrl,\n secureToken,\n options,\n \"https://mock-apple-pay-gateway.example.com/paymentservices/startSession\",\n initiativeContext,\n mockScenarioStr,\n checkoutRequestId,\n );\n if (startResult.error || !startResult.data) {\n return { errors: { apple_pay: startResult.error || \"Failed to start mock checkout\" } };\n }\n }\n\n // Show Apple Pay sheet with merchant validation callback\n const paymentResult = await adapter.showPaymentSheet(async (validationUrl: string) => {\n // Call /start endpoint for merchant validation\n const startResult = await callAirwallexStartEndpoint(\n apiBaseUrl,\n secureToken,\n options,\n validationUrl,\n initiativeContext,\n mockScenarioStr,\n checkoutRequestId,\n );\n\n if (startResult.error || !startResult.data) {\n return { error: startResult.error || \"Merchant validation failed\" };\n }\n\n if (!startResult.data.merchant_session) {\n return { error: \"No merchant session returned from server\" };\n }\n\n return { merchantSession: startResult.data.merchant_session };\n });\n\n if (!paymentResult.success) {\n if (\"cancelled\" in paymentResult && paymentResult.cancelled) {\n return { errors: { apple_pay: \"Apple Pay cancelled by user\" } };\n }\n return { errors: { apple_pay: paymentResult.error || \"Apple Pay failed\" } };\n }\n\n const { token, complete, payerEmail, payerFirstName, payerLastName } = paymentResult;\n\n // Call /confirm with the token\n let response = await callAirwallexConfirmEndpoint(\n apiBaseUrl,\n secureToken,\n token,\n mockScenarioStr,\n checkoutRequestId,\n payerEmail,\n payerFirstName,\n payerLastName,\n );\n\n // Handle 3DS loop\n let userActionCount = 0;\n while (response.charge_status === \"pending\" && response.next_action && userActionCount < MAX_USER_ACTIONS) {\n userActionCount++;\n\n // Handle 3DS challenge — Airwallex3dsNextAction is structurally identical to SDK's Airwallex3dsAction\n const nextAction = response.next_action as Airwallex3dsNextAction;\n const actionResult = await handleNextAction({ type: nextAction.type, url: nextAction.url });\n\n // Call verify endpoint\n const verifyResponse = await callAirwallexVerifyEndpoint(apiBaseUrl, secureToken, checkoutRequestId);\n\n // Check if another 3DS action is required\n if (verifyResponse.charge_status === \"pending\" && verifyResponse.next_action) {\n if (!actionResult.success) {\n console.log(\"[ApplePay:Airwallex] 3DS failed but retry available, continuing loop...\");\n }\n response = verifyResponse;\n continue;\n }\n\n // No more actions - check final result\n if (!actionResult.success) {\n complete(\"fail\");\n return {\n errors: {\n apple_pay:\n verifyResponse.error_message_for_customer ||\n verifyResponse.error_message_for_debug ||\n actionResult.error ||\n \"3DS authentication failed\",\n },\n };\n }\n\n // 3DS succeeded\n complete(verifyResponse.charge_status === \"success\" ? \"success\" : \"fail\");\n return toApplePayResult(verifyResponse, secureToken);\n }\n\n // Check for max attempts exceeded\n if (userActionCount >= MAX_USER_ACTIONS) {\n complete(\"fail\");\n return { errors: { apple_pay: \"Too many authentication attempts. Please try again.\" } };\n }\n\n // No 3DS required - complete with result\n complete(response.charge_status === \"success\" ? \"success\" : \"fail\");\n return toApplePayResult(response, secureToken);\n}\n\n// =============================================================================\n// Main Submit Function\n// =============================================================================\n\nconst defSubmitPayment = (\n states: PaymentKitStates,\n getAutoPrepareConfig: () => AutoPrepareConfig | null,\n onClearPrepared: () => void,\n) => {\n const submitPayment: TInternalFuncs[\"submitPayment\"] = async (_fields, options) => {\n const { apiBaseUrl, secureToken, environment } = states;\n const applePayOptions = options as ApplePaySubmitOptions;\n\n // Fill in processorId/processorType from auto-prepare config if merchant didn't provide them\n const autoPrepareConfig = getAutoPrepareConfig();\n if (!applePayOptions.processorId && autoPrepareConfig?.processorId) {\n applePayOptions.processorId = autoPrepareConfig.processorId;\n }\n if (!applePayOptions.processorType && autoPrepareConfig?.processorType) {\n applePayOptions.processorType = autoPrepareConfig.processorType;\n }\n\n // Validate options\n const validationError = validateOptions(applePayOptions);\n if (validationError) {\n return { errors: validationError };\n }\n\n try {\n const mockScenarioStr = getMockScenarioStr(applePayOptions.mockScenario);\n const checkoutRequestId = getOrCreateCheckoutRequestId(environment);\n console.log(`[ApplePay] Using checkout_request_id: ${checkoutRequestId}`);\n\n // Check if we have prepared Stripe state\n if (preparedStripeState?.adapter.isPrepared()) {\n console.log(\"[ApplePay] Using prepared Stripe state for immediate sheet display\");\n\n // Show Apple Pay sheet IMMEDIATELY using the prepared PaymentRequest\n const paymentResultPromise = preparedStripeState.adapter.showPreparedPaymentSheet();\n const paymentResult = await paymentResultPromise;\n\n if (!paymentResult.success) {\n clearPreparedApplePay();\n\n if (\"cancelled\" in paymentResult && paymentResult.cancelled) {\n return { errors: { apple_pay: \"Apple Pay cancelled by user\" } };\n }\n const errorMessage = \"error\" in paymentResult ? paymentResult.error : \"Unknown error\";\n return { errors: { apple_pay: errorMessage } };\n }\n\n const { paymentMethodId, paymentMethodEvent, billingDetails } = paymentResult;\n\n // Confirm with backend using the PaymentMethod ID\n const confirmResult = await callStripeConfirmEndpoint(\n apiBaseUrl,\n secureToken,\n paymentMethodId,\n mockScenarioStr,\n preparedStripeState.checkoutRequestId,\n billingDetails?.email,\n );\n\n // Complete Apple Pay UI based on result\n if (confirmResult.data) {\n paymentMethodEvent.complete(\"success\");\n clearPreparedApplePay();\n return confirmResult;\n } else {\n paymentMethodEvent.complete(\"fail\");\n clearPreparedApplePay();\n return confirmResult;\n }\n }\n\n // Check if this is an Airwallex processor - route directly to Airwallex flow\n if (applePayOptions.processorType === \"airwallex\") {\n console.log(\"[ApplePay] Running Airwallex flow (processorType=airwallex)\");\n // Merge prepare-time state into submit options so country/currency/amount/merchantName\n // are available even if the merchant only provided them at prepare time\n if (preparedAirwallexState) {\n if (applePayOptions.country == null) applePayOptions.country = preparedAirwallexState.country;\n if (applePayOptions.currency == null) applePayOptions.currency = preparedAirwallexState.currency;\n if (applePayOptions.amount == null) applePayOptions.amount = preparedAirwallexState.amount;\n if (applePayOptions.merchantName == null) applePayOptions.merchantName = preparedAirwallexState.merchantName;\n }\n try {\n return await runAirwallexFlow(apiBaseUrl, secureToken, applePayOptions, mockScenarioStr, checkoutRequestId);\n } finally {\n preparedAirwallexState = null;\n }\n }\n\n // No prepared state for Stripe - call start endpoint\n const startResult = await callStripeStartEndpoint(\n apiBaseUrl,\n secureToken,\n applePayOptions,\n mockScenarioStr,\n checkoutRequestId,\n );\n\n if (startResult.error || !startResult.data) {\n return { errors: { apple_pay: startResult.error || \"Failed to start Apple Pay\" } };\n }\n\n const startData = startResult.data;\n\n // Stripe flow without prepared state - this may fail due to user gesture requirements\n console.warn(\"[ApplePay] Stripe flow without prepared state - may fail due to user gesture requirements\");\n\n if (!startData.stripe_pk) {\n return { errors: { apple_pay: \"Stripe publishable key not provided\" } };\n }\n\n const { adapter, error: adapterError } = initializeStripeAdapter(\n startData.stripe_pk,\n applePayOptions.mockScenario,\n );\n if (!adapter) {\n return { errors: { apple_pay: adapterError || \"Failed to initialize Stripe adapter\" } };\n }\n\n // This will likely fail due to user gesture requirements\n const prepareResult = await adapter.preparePaymentRequest({\n country: startData.country,\n currency: startData.currency.toLowerCase(),\n total: { label: \"Total\", amount: startData.amount },\n });\n\n if (!prepareResult.success || !prepareResult.applePay) {\n return { errors: { apple_pay: prepareResult.error || \"Apple Pay not available\" } };\n }\n\n const paymentResult = await adapter.showPreparedPaymentSheet();\n\n if (!paymentResult.success) {\n if (\"cancelled\" in paymentResult && paymentResult.cancelled) {\n return { errors: { apple_pay: \"Apple Pay cancelled by user\" } };\n }\n const errorMessage = \"error\" in paymentResult ? paymentResult.error : \"Unknown error\";\n return { errors: { apple_pay: errorMessage } };\n }\n\n const { paymentMethodId, paymentMethodEvent, billingDetails } = paymentResult;\n\n const confirmResult = await callStripeConfirmEndpoint(\n apiBaseUrl,\n secureToken,\n paymentMethodId,\n mockScenarioStr,\n checkoutRequestId,\n billingDetails?.email,\n );\n\n if (confirmResult.data) {\n paymentMethodEvent.complete(\"success\");\n return confirmResult;\n } else {\n paymentMethodEvent.complete(\"fail\");\n return confirmResult;\n }\n } catch (error) {\n onClearPrepared();\n return { errors: { apple_pay: `Apple Pay error: ${error}` } };\n }\n };\n\n return submitPayment;\n};\n\n// =============================================================================\n// Payment Method Definition\n// =============================================================================\n\nconst ApplePayPaymentMethod = definePaymentMethod((paymentKitStates) => {\n const { apiBaseUrl, secureToken, environment } = paymentKitStates;\n\n // Per-instance auto-prepare state — isolated from other ApplePayPaymentMethod instances.\n // Note: preparedStripeState and preparedAirwallexState remain module-level globals (pre-existing\n // architectural constraint shared across the file). Full per-instance isolation for those fields\n // would require threading them through prepareApplePay, runAirwallexFlow, validateOptions, and\n // defSubmitPayment — tracked as tech debt.\n let autoPrepareConfig: AutoPrepareConfig | null = null;\n let onApplePayReadyCallbacks: ((isReady: boolean) => void)[] = [];\n let isApplePayReadyState = false;\n let autoPrepareInProgress = false;\n // Incremented by cleanup() so that in-flight prepares can detect staleness and skip writes.\n let cleanupGeneration = 0;\n // Stores pending retry args when notifyAmountChanged is called while a prepare is in flight.\n let pendingAutoPrepareArgs: { apiBaseUrl: string; secureToken: string; environment: string } | null = null;\n // Resolvers waiting for a queued retry to complete (for correct notifyAmountChanged await).\n let pendingPrepareResolvers: (() => void)[] = [];\n\n function instanceSetApplePayReady(ready: boolean): void {\n isApplePayReadyState = ready;\n for (const cb of onApplePayReadyCallbacks) cb(ready);\n }\n\n function instanceClearPreparedApplePay(): void {\n clearPreparedApplePay();\n instanceSetApplePayReady(false);\n }\n\n async function instanceAutoPrepare(apBase: string, sToken: string, env: string): Promise<void> {\n if (autoPrepareInProgress) {\n // A prepare is already in flight; queue a retry and return a promise that resolves after it.\n pendingAutoPrepareArgs = { apiBaseUrl: apBase, secureToken: sToken, environment: env };\n return new Promise<void>((resolve) => pendingPrepareResolvers.push(resolve));\n }\n autoPrepareInProgress = true;\n // Capture generation so we can detect if cleanup() is called while we await.\n const capturedGeneration = cleanupGeneration;\n instanceSetApplePayReady(false);\n\n try {\n const result = await apiCall<CheckoutSessionConfig>(`${apBase}/api/checkout-sessions/token/${sToken}`, {\n method: \"GET\",\n });\n\n if (cleanupGeneration !== capturedGeneration) return;\n\n if (result.error || !result.data) {\n console.warn(\"[ApplePay] Auto-prepare: failed to fetch session config:\", result.error);\n return;\n }\n\n const processorId = result.data.express_checkout_processor_id;\n if (!processorId) {\n console.log(\"[ApplePay] Auto-prepare: no express checkout processor configured\");\n return;\n }\n\n const processorTypeRaw = result.data.express_checkout_processor_type;\n const processorType =\n processorTypeRaw === \"airwallex\" || processorTypeRaw === \"stripe\" ? processorTypeRaw : undefined;\n\n autoPrepareConfig = { processorId, processorType };\n\n const prepareResult = await prepareApplePay(\n apBase,\n sToken,\n { processorId, processorType, customerInfo: { first_name: \"\", last_name: \"\" } },\n env,\n );\n\n if (cleanupGeneration !== capturedGeneration) return;\n\n instanceSetApplePayReady(prepareResult.success && prepareResult.applePay === true);\n } catch (err) {\n if (cleanupGeneration === capturedGeneration) {\n console.error(\"[ApplePay] Auto-prepare failed:\", err);\n }\n } finally {\n autoPrepareInProgress = false;\n const stale = cleanupGeneration !== capturedGeneration;\n const pending = pendingAutoPrepareArgs;\n const resolvers = pendingPrepareResolvers.splice(0);\n if (!stale && pending) {\n pendingAutoPrepareArgs = null;\n // Run the retry and resolve all callers that were waiting for it.\n instanceAutoPrepare(pending.apiBaseUrl, pending.secureToken, pending.environment).then(() => {\n for (const r of resolvers) r();\n });\n } else {\n pendingAutoPrepareArgs = null;\n for (const r of resolvers) r();\n }\n }\n }\n\n // Start auto-prepare immediately so the button is ready before first user interaction.\n instanceAutoPrepare(apiBaseUrl, secureToken, environment).catch((err) =>\n console.error(\"[ApplePay] Unhandled init error:\", err),\n );\n\n return {\n name: \"apple_pay\",\n externalFuncs: {\n /**\n * @deprecated The SDK now prepares automatically on init. Use `onApplePayReady` to observe\n * readiness and `notifyAmountChanged` to trigger re-prepare after amount changes.\n */\n prepareApplePay: (options: ApplePaySubmitOptions) =>\n prepareApplePay(apiBaseUrl, secureToken, options, environment),\n\n /**\n * Register a callback to be notified when Apple Pay becomes ready (or stops being ready).\n * Fires immediately with the current state, then on every state change.\n * Multiple registrations are all notified — none are silently overwritten.\n *\n * @example\n * paymentKit.apple_pay.onApplePayReady((isReady) => {\n * applePayButton.disabled = !isReady;\n * });\n */\n onApplePayReady: (callback: (isReady: boolean) => void): void => {\n onApplePayReadyCallbacks.push(callback);\n // Fire immediately with current state so caller doesn't miss a completed prepare.\n callback(isApplePayReadyState);\n },\n\n /**\n * Notify the SDK that the payment amount has changed (e.g. coupon applied).\n * The SDK will disable Apple Pay, re-prepare with the new amount, then re-enable.\n * The returned promise resolves when re-prepare is fully complete.\n *\n * @example\n * await paymentKit.apple_pay.notifyAmountChanged();\n */\n notifyAmountChanged: (): Promise<void> => {\n instanceClearPreparedApplePay();\n autoPrepareConfig = null;\n return instanceAutoPrepare(apiBaseUrl, secureToken, environment);\n },\n },\n internalFuncs: {\n submitPayment: defSubmitPayment(paymentKitStates, () => autoPrepareConfig, instanceClearPreparedApplePay),\n cleanup: () => {\n cleanupGeneration++;\n onApplePayReadyCallbacks = [];\n isApplePayReadyState = false;\n autoPrepareConfig = null;\n pendingAutoPrepareArgs = null;\n pendingPrepareResolvers = [];\n autoPrepareInProgress = false;\n clearPreparedApplePay();\n },\n },\n };\n});\n\nexport { ApplePayMockScenario };\n\nexport default ApplePayPaymentMethod;\n"],"mappings":";;;;;;AAwHA,IAAIA,sBAA0D;AAW9D,IAAIC,yBAAwD;AA6B5D,IAAIC,4BAAkD;AACtD,eAAe,qBAAoC;AACjD,KAAI,OAAO,WAAW,YAAa;AACnC,KAAI,OAAO,gBAAiB;AAC5B,KAAI,SAAS,eAAe,qBAAqB,CAG/C,QAAO,6BAA6B,QAAQ,SAAS;AAEvD,6BAA4B,IAAI,SAAe,SAAS,WAAW;EACjE,MAAM,SAAS,SAAS,cAAc,SAAS;AAC/C,SAAO,KAAK;AACZ,SAAO,MAAM;AACb,SAAO,cAAc;AAGrB,SAAO,eAAe,SAAS;AAC/B,SAAO,gBAAgB;AAErB,+BAA4B;AAC5B,YAAS,eAAe,qBAAqB,EAAE,QAAQ;AACvD,0BAAO,IAAI,MAAM,+BAA+B,CAAC;;AAEnD,WAAS,KAAK,YAAY,OAAO;GACjC;AACF,QAAO;;AAOT,eAAe,QACb,KACA,SACA,mBACuC;CACvC,MAAM,UAAU,IAAI,QAAQ,QAAQ,QAAQ;AAC5C,KAAI,kBACF,SAAQ,IAAI,gBAAgB,kBAAkB;CAGhD,MAAM,WAAW,MAAM,MAAM,KAAK;EAAE,GAAG;EAAS;EAAS,CAAC;AAC1D,KAAI,CAAC,SAAS,IAAI;EAChB,IAAI,eAAe,mBAAmB,SAAS,OAAO;AACtD,MAAI;AAEF,mBADkB,MAAM,SAAS,MAAM,EACd,UAAU;UAC7B;AACN,kBAAe,SAAS,cAAc;;AAExC,SAAO,EAAE,OAAO,cAAc;;AAEhC,QAAO,EAAE,MAAM,MAAM,SAAS,MAAM,EAAE;;AAGxC,SAAS,gBAAgB,SAAyD;AAChF,KAAI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AQ,YAAY,CAAC,WAAW;AAC3F,UAAK,MAAM,KAAK,UAAW,IAAG;MAC9B;UACG;AACL,6BAAyB;AACzB,SAAK,MAAM,KAAK,UAAW,IAAG;;;;AAMpC,qBAAoB,YAAY,aAAa,YAAY,CAAC,OAAO,QAC/D,QAAQ,MAAM,oCAAoC,IAAI,CACvD;AAED,QAAO;EACL,MAAM;EACN,eAAe;GAKb,kBAAkB,YAChB,gBAAgB,YAAY,aAAa,SAAS,YAAY;GAYhE,kBAAkB,aAA+C;AAC/D,6BAAyB,KAAK,SAAS;AAEvC,aAAS,qBAAqB;;GAWhC,2BAA0C;AACxC,mCAA+B;AAC/B,wBAAoB;AACpB,WAAO,oBAAoB,YAAY,aAAa,YAAY;;GAEnE;EACD,eAAe;GACb,eAAe,iBAAiB,wBAAwB,mBAAmB,8BAA8B;GACzG,eAAe;AACb;AACA,+BAA2B,EAAE;AAC7B,2BAAuB;AACvB,wBAAoB;AACpB,6BAAyB;AACzB,8BAA0B,EAAE;AAC5B,4BAAwB;AACxB,2BAAuB;;GAE1B;EACF;EACD;AAIF,wBAAe"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payment-kit-js/vanilla",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.15",
|
|
4
4
|
"main": "./dist/index.mjs",
|
|
5
5
|
"types": "./dist/index.d.mts",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -52,5 +52,5 @@
|
|
|
52
52
|
"tsdown": "^0.15.10",
|
|
53
53
|
"typescript": "^5.9.3"
|
|
54
54
|
},
|
|
55
|
-
"stableVersion": "0.5.
|
|
55
|
+
"stableVersion": "0.5.15"
|
|
56
56
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"airwallex-apple-pay-adapter-Bb0CreWN.d.mts","names":[],"sources":["../src/payment-methods/airwallex-apple-pay-adapter.ts"],"sourcesContent":[],"mappings":";;AAoBA;AAOA;AAmBA;AAOA;AAWA;AAIA;;;;;;;;;;;aAhDY,6BAAA;;;;;UAOK,sBAAA;;;;;;;;;;;;;;;;;;UAmBA,uBAAA;;;;;;KAOL,2BAAA;;SAGC;;;;;;;;;;KAQD,0BAAA,8BAEP;;;;cAEQ,wBAAA;;;6BAIgB;;;;;qBAQR;;;;;oBAsBK;;;;;;;;;;;;;;;;;;yCAkEqB,6BAA6B,QAAQ"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"airwallex-apple-pay-adapter-CHAnLP_x.mjs","names":["paymentRequest: ApplePayJS.ApplePayPaymentRequest","token: ApplePayEncryptedToken"],"sources":["../src/payment-methods/airwallex-apple-pay-adapter.ts"],"sourcesContent":["/**\n * Airwallex Apple Pay Adapter\n *\n * Uses native ApplePaySession API directly (not Stripe.js) to show the payment sheet\n * and extract the encrypted payment token for Airwallex processing.\n *\n * Flow:\n * 1. initialize() - Checks if ApplePaySession is available\n * 2. canMakePayment() - Checks if user can pay with Apple Pay\n * 3. showPaymentSheet() - Shows Apple Pay sheet with merchant validation callback\n *\n * The key difference from Stripe's approach is that we use the native\n * ApplePaySession API and handle merchant validation via Airwallex's\n * payment_session/start API which validates with Apple.\n *\n * Mock scenarios are supported for E2E testing without real Apple Pay.\n */\n\n/// <reference types=\"@types/applepayjs\" />\n\nexport enum AirwallexApplePayMockScenario {\n None = \"none\",\n Success = \"success\",\n Cancelled = \"cancelled\",\n}\n\n// Apple Pay token structure from payment.token\nexport interface ApplePayEncryptedToken {\n paymentData: {\n version?: string;\n data: string;\n signature: string;\n header: {\n ephemeralPublicKey: string;\n publicKeyHash: string;\n transactionId: string;\n };\n };\n paymentMethod: {\n displayName: string;\n network: string;\n type: string;\n };\n transactionIdentifier: string;\n}\n\nexport interface AirwallexApplePayConfig {\n amount: number; // Amount in atomic units (cents)\n currency: string; // e.g., \"USD\"\n country: string; // e.g., \"US\"\n merchantName?: string; // Display name on Apple Pay sheet\n}\n\nexport type AirwallexApplePayShowResult =\n | {\n success: true;\n token: ApplePayEncryptedToken;\n payerEmail?: string;\n complete: (status: \"success\" | \"fail\") => void;\n }\n | { success: false; cancelled: true }\n | { success: false; error: string };\n\n// Merchant validation callback type - called during onvalidatemerchant\nexport type MerchantValidationCallback = (\n validationUrl: string,\n) => Promise<{ merchantSession?: object; error?: string }>;\n\nexport class AirwallexApplePayAdapter {\n private config: AirwallexApplePayConfig | null = null;\n private mockScenario: AirwallexApplePayMockScenario;\n\n constructor(mockScenario?: AirwallexApplePayMockScenario) {\n this.mockScenario = mockScenario ?? AirwallexApplePayMockScenario.None;\n }\n\n /**\n * Initialize the adapter with payment configuration.\n * Returns true if Apple Pay is available on this device.\n */\n initialize(config: AirwallexApplePayConfig): boolean {\n // Mock scenarios bypass real Apple Pay initialization\n if (this.mockScenario !== AirwallexApplePayMockScenario.None) {\n console.log(\"[MockApplePay:Airwallex] initialize called (mock mode)\");\n this.config = config;\n return true;\n }\n\n // Check if ApplePaySession is available (Safari on iOS/macOS only)\n if (typeof window === \"undefined\" || !window.ApplePaySession) {\n console.error(\"[ApplePay:Airwallex] ApplePaySession not available (not Safari or unsupported device)\");\n return false;\n }\n\n this.config = config;\n return true;\n }\n\n /**\n * Check if the user can make a payment with Apple Pay.\n * Returns true if Apple Pay is set up on this device.\n */\n async canMakePayment(): Promise<boolean> {\n // Mock scenarios always return true\n if (this.mockScenario !== AirwallexApplePayMockScenario.None) {\n console.log(\"[MockApplePay:Airwallex] canMakePayment: true (mock mode)\");\n return true;\n }\n\n if (!window.ApplePaySession) {\n return false;\n }\n\n try {\n const canMake = window.ApplePaySession.canMakePayments();\n console.log(\"[ApplePay:Airwallex] canMakePayments:\", canMake);\n return canMake;\n } catch (error) {\n console.error(\"[ApplePay:Airwallex] canMakePayment error:\", error);\n return false;\n }\n }\n\n /**\n * Convert atomic units (cents) to display format for Apple Pay.\n * Apple Pay expects a string like \"10.00\" for $10.00.\n */\n private formatAmount(amountAtom: number, currency: string): string {\n // Most currencies use 2 decimal places, some like JPY use 0\n const zeroDecimalCurrencies = [\n \"BIF\",\n \"CLP\",\n \"DJF\",\n \"GNF\",\n \"ISK\",\n \"JPY\",\n \"KMF\",\n \"KRW\",\n \"PYG\",\n \"RWF\",\n \"UGX\",\n \"VND\",\n \"VUV\",\n \"XAF\",\n \"XOF\",\n \"XPF\",\n ];\n const isZeroDecimal = zeroDecimalCurrencies.includes(currency.toUpperCase());\n\n if (isZeroDecimal) {\n return amountAtom.toString();\n }\n\n return (amountAtom / 100).toFixed(2);\n }\n\n /**\n * Show the Apple Pay payment sheet and return the encrypted token.\n *\n * This uses the native ApplePaySession API with a two-step flow:\n * 1. onvalidatemerchant - Called by Apple to validate the merchant\n * The callback should call our backend which calls Airwallex's payment_session/start\n * 2. onpaymentauthorized - Called when user authorizes with Face ID/Touch ID\n * Returns the encrypted token for processing\n *\n * @param onMerchantValidation - Callback to validate merchant with backend\n * @returns Promise with token or error\n */\n async showPaymentSheet(onMerchantValidation: MerchantValidationCallback): Promise<AirwallexApplePayShowResult> {\n // Handle mock scenarios\n if (this.mockScenario === AirwallexApplePayMockScenario.Success) {\n console.log(\"[MockApplePay:Airwallex] showPaymentSheet: returning mock success token\");\n const mockToken: ApplePayEncryptedToken = {\n paymentData: {\n version: \"EC_v1\",\n data: \"MOCK_ENCRYPTED_DATA\",\n signature: \"MOCK_SIGNATURE\",\n header: {\n ephemeralPublicKey: \"MOCK_PUBLIC_KEY\",\n publicKeyHash: \"MOCK_HASH\",\n transactionId: \"mock_txn_123\",\n },\n },\n paymentMethod: {\n displayName: \"Visa 4242\",\n network: \"Visa\",\n type: \"debit\",\n },\n transactionIdentifier: \"MOCK_TXN_ID\",\n };\n return {\n success: true,\n token: mockToken,\n payerEmail: \"mock@example.com\",\n complete: (status) => console.log(`[MockApplePay:Airwallex] complete: ${status}`),\n };\n }\n\n if (this.mockScenario === AirwallexApplePayMockScenario.Cancelled) {\n console.log(\"[MockApplePay:Airwallex] showPaymentSheet: returning mock cancelled\");\n return { success: false, cancelled: true };\n }\n\n // Real Apple Pay flow\n if (!this.config) {\n return { success: false, error: \"Apple Pay not initialized\" };\n }\n\n if (!window.ApplePaySession) {\n return { success: false, error: \"ApplePaySession not available\" };\n }\n\n const config = this.config;\n const amount = this.formatAmount(config.amount, config.currency);\n\n // Create the Apple Pay payment request\n const paymentRequest: ApplePayJS.ApplePayPaymentRequest = {\n countryCode: config.country.toUpperCase(),\n currencyCode: config.currency.toUpperCase(),\n supportedNetworks: [\"visa\", \"masterCard\", \"amex\", \"discover\"],\n merchantCapabilities: [\"supports3DS\"],\n total: {\n label: config.merchantName || \"Total\",\n amount: amount,\n type: \"final\",\n },\n // Surface the payer's email so the backend can fill missing customer email.\n requiredBillingContactFields: [\"email\"],\n };\n\n return new Promise((resolve) => {\n try {\n // Create ApplePaySession (version 3 is widely supported)\n // biome-ignore lint/style/noNonNullAssertion: ApplePaySession is guaranteed to exist (checked in canMakePayments)\n const session = new window.ApplePaySession!(3, paymentRequest);\n\n // Capture status constants at creation time (before optional chaining could yield undefined)\n // biome-ignore lint/style/noNonNullAssertion: ApplePaySession existence verified above\n const STATUS_SUCCESS = window.ApplePaySession!.STATUS_SUCCESS;\n // biome-ignore lint/style/noNonNullAssertion: ApplePaySession existence verified above\n const STATUS_FAILURE = window.ApplePaySession!.STATUS_FAILURE;\n\n // Handle merchant validation\n session.onvalidatemerchant = async (event: ApplePayJS.ApplePayValidateMerchantEvent) => {\n console.log(\"[ApplePay:Airwallex] onvalidatemerchant called\", event.validationURL);\n\n try {\n const validationResult = await onMerchantValidation(event.validationURL);\n\n if (validationResult.error || !validationResult.merchantSession) {\n console.error(\"[ApplePay:Airwallex] Merchant validation failed:\", validationResult.error);\n session.abort();\n resolve({\n success: false,\n error: validationResult.error || \"Merchant validation failed\",\n });\n return;\n }\n\n // Complete merchant validation with the session from Apple\n session.completeMerchantValidation(validationResult.merchantSession);\n console.log(\"[ApplePay:Airwallex] Merchant validation completed\");\n } catch (error) {\n console.error(\"[ApplePay:Airwallex] Merchant validation error:\", error);\n session.abort();\n resolve({\n success: false,\n error: `Merchant validation error: ${error}`,\n });\n }\n };\n\n // Handle payment authorization (user approved with Face ID/Touch ID)\n session.onpaymentauthorized = (event: ApplePayJS.ApplePayPaymentAuthorizedEvent) => {\n console.log(\"[ApplePay:Airwallex] onpaymentauthorized called\");\n\n // Extract the encrypted token\n const payment = event.payment;\n const token: ApplePayEncryptedToken = {\n paymentData: payment.token.paymentData as ApplePayEncryptedToken[\"paymentData\"],\n paymentMethod: {\n displayName: payment.token.paymentMethod.displayName,\n network: payment.token.paymentMethod.network,\n type: payment.token.paymentMethod.type,\n },\n transactionIdentifier: payment.token.transactionIdentifier,\n };\n\n const payerEmail = payment.billingContact?.emailAddress ?? payment.shippingContact?.emailAddress ?? undefined;\n\n // Create the complete callback that the caller will use after confirming\n const complete = (status: \"success\" | \"fail\") => {\n console.log(\"[ApplePay:Airwallex] completing session with status:\", status);\n const appleStatus = status === \"success\" ? STATUS_SUCCESS : STATUS_FAILURE;\n session.completePayment({ status: appleStatus });\n };\n\n resolve({\n success: true,\n token,\n payerEmail,\n complete,\n });\n };\n\n // Handle cancellation\n session.oncancel = () => {\n console.log(\"[ApplePay:Airwallex] oncancel - user cancelled\");\n resolve({ success: false, cancelled: true });\n };\n\n // Start the session - this shows the Apple Pay sheet\n console.log(\"[ApplePay:Airwallex] Starting Apple Pay session\");\n session.begin();\n } catch (error) {\n console.error(\"[ApplePay:Airwallex] Session creation error:\", error);\n resolve({\n success: false,\n error: `Failed to create Apple Pay session: ${error}`,\n });\n }\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAoBA,IAAY,0FAAL;AACL;AACA;AACA;;;AA6CF,IAAa,2BAAb,MAAsC;CACpC,AAAQ,SAAyC;CACjD,AAAQ;CAER,YAAY,cAA8C;AACxD,OAAK,eAAe,gBAAgB,8BAA8B;;;;;;CAOpE,WAAW,QAA0C;AAEnD,MAAI,KAAK,iBAAiB,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