PaybytokenDocs
SDK · Browser

Browser SDK and events

Embedded checkout initialization, appearance, lifecycle events and cleanup.

Use initEmbeddedCheckout when the browser should lazily ask your server to create a session. Use createEmbeddedCheckout only when your application has already fetched that session.

Install

pnpm add @paybytoken/checkout

Initialization

import {
  createEmbeddedCheckout,
  initEmbeddedCheckout,
} from '@paybytoken/checkout'

const recommended = await initEmbeddedCheckout({
  fetchCheckoutSession: createSessionOnMerchantServer,
})

const alreadyFetched = createEmbeddedCheckout({
  clientSecret: session.clientSecret,
  checkoutUrl: session.checkoutUrl,
})

Initialize only after the customer opens the payment step. If initialization fails before an instance is returned, show a retry that creates a new session. Do not loop on a rejected or expired client secret.

Lifecycle

checkout
  .on('ready', handleReady)
  .on('change', handleChange)
  .on('complete', handleComplete)
  .on('return', handleReturn)
  .on('cancel', handleCancel)
  .on('loaderror', handleLoadError)
  .on('error', handleError)
EventDataUse it for
readyamountTotal, currency, merchantNameRemove a local skeleton or loading state.
changestep, optional paymentIntentIdReflect payment, confirming, completed or failed presentation state.
completeoptional paymentIntentId, transactionHashShow “payment submitted”; do not fulfill from this event.
returnoptional payment IDs and successUrlNavigate within the merchant experience.
canceloptional cancelUrlRestore the cart or close the payment step.
loaderrorreason, messageReplace an iframe load failure with a retry action.
errorreason, message, optional paymentIntentIdExplain a wallet, request or payment failure.

Every event also contains type and sessionId.

Mount and cleanup

checkout.mount('#paybytoken-checkout')

// Temporarily remove the iframe while keeping the session and handlers.
checkout.unmount()
checkout.mount('#paybytoken-checkout')

// Permanently release the in-memory credential and all listeners.
checkout.destroy()

An instance can have only one mounted checkout. Destroy it before replacing the container, switching providers or initializing another session.

Appearance

Account-level checkout branding remains the source of the merchant logo and display name. The browser SDK provides a constrained presentation layer:

const checkout = await initEmbeddedCheckout({
  fetchCheckoutSession: createSessionOnMerchantServer,
  locale: 'auto',
  appearance: {
    disableAnimations: false,
    variables: {
      colorPrimary: '#9FE870',
      colorPrimaryText: '#0E0F0C',
      colorBackground: '#F4F5F0',
      colorSurface: '#FFFFFF',
      colorText: '#0E0F0C',
      colorMutedText: '#626760',
      colorBorder: '#DFE3DC',
      borderRadius: 24,
    },
  },
})

Colors must be six-digit hex values. borderRadius accepts an integer from 0 through 32. Checkout respects prefers-reduced-motion; disableAnimations: true disables its animation for every customer. Safety, warning, error and success semantics cannot be overridden.

Update presentation without creating another session:

checkout.update({
  appearance: {
    disableAnimations: true,
    variables: { colorBackground: '#FFFFFF' },
  },
})

On this page

API Workbench

Full Explorer

Open in new tab