{"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: PaymentKitEnvironment } | null","pendingPrepareResolvers: (() => void)[]"],"sources":["../../src/payment-methods/apple-pay.ts"],"sourcesContent":["import type { PaymentKitEnvironment, 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  // Airwallex-only fields. Stripe /apple-pay/start is a pure read and\n  // ignores these — customer_info/fraud_metadata are sent to /apple-pay/confirm\n  // instead, where the customer + checkout attempt + PaymentIntent are created.\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  amount: number;\n  currency: string;\n  country: string;\n\n  // Stripe-specific (when calling /apple-pay/start)\n  stripe_pk?: string;\n\n  // Airwallex-specific (when calling /airwallex/apple-pay/start). Stripe's\n  // /start does not create a checkout attempt — it's deferred to /confirm.\n  checkout_attempt_id?: string;\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-only: moved from /start (where they used to drive customer/PI\n  // creation). At confirm time, the Stripe path creates the customer, checkout\n  // attempt, and PaymentIntent in a single call.\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  // 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  checkoutRequestId?: string,\n): Promise<{ data?: ApplePayStartResponse; error?: string }> {\n  // /apple-pay/start is now a pure read: it returns stripe_pk + sheet data\n  // (amount/currency/country). Customer/CheckoutAttempt/PaymentIntent are\n  // created in /apple-pay/confirm once the user authorizes the sheet.\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      } 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  options: ApplePaySubmitOptions,\n  paymentMethodId: string,\n  mockScenarioStr?: string,\n  checkoutRequestId?: string,\n  payerEmail?: string,\n): Promise<ApplePayResult> {\n  // /apple-pay/confirm now carries the fields that used to live on /start —\n  // processor_id, customer_info, fraud_metadata — because the customer,\n  // checkout attempt, and Stripe PaymentIntent are all created here.\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        processor_id: options.processorId,\n        customer_info: options.customerInfo,\n        fraud_metadata: collectFraudMetadata(),\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: PaymentKitEnvironment,\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 fetch stripe_pk + sheet display data\n  const startResult = await callStripeStartEndpoint(apiBaseUrl, secureToken, options, checkoutRequestId);\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          applePayOptions,\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(apiBaseUrl, secureToken, applePayOptions, checkoutRequestId);\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        applePayOptions,\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: PaymentKitEnvironment } | null =\n    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: PaymentKitEnvironment): 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":";;;;;;AAkIA,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,CAAC,SAAS,YACZ,QAAO,EAAE,cAAc,4BAA4B;AAGrD,KAAI,QAAQ,kBAAkB,eAAe,CAAC,QAAQ,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