Braintree

Core Service Functions

1. initialize()

  • Purpose: Loads all necessary Braintree JS SDKs dynamically.
  • Steps:
    • Verifies if braintree exists on window.
    • Loads multiple Braintree SDKs sequentially (client, hosted fields, 3DS, PayPal, etc.).
  • Error Handling: Uses customClientErrorHandler to report script load issues.

2. createClient()

  • Purpose: Instantiates the Braintree client.
  • Parameters:
    • Calls initialize() to ensure scripts are loaded.
    • Creates and returns a client using the auth key.
  • Returns: Braintree client instance.

3. hostFieldHandle()

  • Purpose: Initializes and styles Braintree Hosted Fields.
  • Details:
    • Mounts card inputs in DOM containers.
    • Custom placeholders and localized strings via useTranslation().
    • Provides real-time styling and validation feedback.

4. threeDSecureHandle()

  • Purpose: Sets up 3DS for additional fraud verification.
  • Returns: A 3DS instance for verifying card payments securely.

5. dataCollectorHandle()

  • Purpose: Collects device & PayPal data for risk analysis.
  • Returns: Data collector instance tied to the client.

6. googlePaymentHandle()

  • Purpose: Initializes Google Pay via Braintree.
  • Returns: Google Payment instance used in later payment processing.

7. localPaymentHandle(amount, device_data, type, trolleyDetail)

  • Purpose: Manages bank-specific payment methods like iDEAL.
  • Flow:
    • Generates redirect URL with guest and trolley data.
    • Encodes redirect for fallback.
    • Initializes local payment with Braintree.
    • Determines whether to use fallback or direct startPayment()
  • Handles: Fallback or direct tokenization path.

8. handleLocalPaymentFallback(...)

  • Purpose: Processes fallback for local payments when auto-start fails.
  • Logic:
    • Tokenizes payment instance if fallback is invoked.
    • Sends data to checkoutStore.createTransaction().

9. createLocalPayment(...)

  • Purpose: Initiates the local payment via a button click.
  • Steps:
    • Builds params from current checkout state (addresses, totals).
    • Sends POST request to /trolleys/checkout/braintree.
    • Calls startPayment() with user metadata and URLs.
  • Handles: Google Pay-like interaction and result processing.

10. googlePaymentProcess(...)

  • Purpose: Prepares Google Pay transaction info and initializes PaymentsClient.
  • Config:
    • Currency, amount, billing address required.
    • Uses useRuntimeConfig() for environment settings.

11. googlePaymentReady(handlers, domId)

  • Purpose: Renders Google Pay button if eligible.
  • Handler:
    • onClick: Initiates transaction or 3DS verification.
    • onError: Fallback handler for payment failures.
  • Integration:
    • Injects button into provided DOM node.

12. paypalCheckoutHandle(options)

  • Purpose: Creates and loads PayPal Checkout instance.
  • Returns: Object with paypal instance and loaded sdk.

13. applePayHandle()

  • Purpose: Initializes Apple Pay with Braintree.
  • Returns: Apple Pay payment instance.

14. loadBraintreeScript(src)

  • Purpose: Dynamically appends a <script> element for the Braintree SDK
  • Logic:
    • Checks if script already exists.
    • Loads from CDN and resolves or rejects on load/error.

Copyright © 2026