Checkout Browser SDK

Checkout Browser SDK lets you integrate Checkout both on regular websites and JavaScript web applications, either implemented in libraries or frameworks like React, Vue, or Angular.

Ask about this Page
Copy for LLM
View as Markdown
With the Checkout Browser SDK, you can integrate Checkout either as a browser script or as an npm module.
You can initialize Checkout in three ways depending on the Checkout mode you use:
  • For the Complete Checkout mode, call the checkoutFlow method.
  • For the Payment Only mode, call the paymentFlow method.
  • For Express Payment buttons, use the expressPayment object.
Except for specific cases, the following code samples use the npm module and the checkoutFlow method.

Versioning

Configuration properties

Following are the configuration properties that you can provide with the checkoutFlow or paymentFlow methods.
The following table shows the configuration properties and their types that you can provide with either the checkoutFlow or paymentFlow method:
PropertyTypeRequiredDescription
projectKeystringYesIdentifier of your Checkout entity and the project_key of your Project.
sessionIdstringYesIdentifier of the Checkout Session.
regionstringYesRegion where your Checkout application is hosted. For example, europe-west1.gcp.
localestringNo (default: en)Your customer’s locale. If the provided locale is not found, then English will be used.
onInfofunctionNoFor more information, see onInfo, onWarn, and onError.
onWarnfunctionNoFor more information, see onInfo, onWarn, and onError.
onErrorfunctionNoFor more information, see onInfo, onWarn, and onError.
logInfobooleanNo (default: false)For more information, see logInfo, logWarn, and logError.
logWarnbooleanNo (default: false)For more information, see logInfo, logWarn, and logError.
logErrorbooleanNo (default: false)For more information, see logInfo, logWarn, and logError.
stylesobjectNoObject that contains CSS variables to customize the style of Checkout.
languageOverridesobjectNoObject that contains properties to customize the texts and labels of Checkout.
formsobjectNoObject that contains properties to customize the address form fields of Checkout. It applies only to the checkoutFlow method.
taxobjectNoIf set as {display: true}, then the tax information from Composable Commerce is displayed in the price summary of your checkout page. It applies only to the checkoutFlow method.
currencyLocalestringNoLocale for which you can set the currency formatting for prices. When it is not set, locale determines the price formatting as a default. If set, the locale for price formatting is overridden. For example, if locale is set as fr, then Swiss prices are displayed as 10,00 CHF. However, if currencyLocale is set as de-CH, then Swiss prices are displayed as CHF 10.00.
skipPaymentSuccessPagebooleanNoIf set as true, then the payment success page is not displayed. It applies only to the checkoutFlow method.
skipPaymentErrorPagebooleanNoIf set as true, then the payment error page is not displayed. It applies only to the checkoutFlow method.
showExtensionErrorMessagesobjectNoObject that contains the address property. If set as true, then address-related extension error messages are shown in the UI. Otherwise, a generic error message will be shown.

Browser script

To integrate Checkout as a browser script, add the following code between the opening and closing <head> tags of each page on which you want to activate the start of the checkout process.
When integrating Checkout as a browser script, @latest serves as an alias to load the latest version of the SDK.
Integrate Checkout as a browser scriptHTML
<script>
  (function (w, d, s) {
    if (w.ctc) {
      return;
    }
    var js,
      fjs = d.getElementsByTagName(s)[0];
    var q = [];
    w.ctc =
      w.ctc ||
      function () {
        q.push(arguments);
      };
    w.ctc.q = q;
    js = d.createElement(s);
    js.type = 'text/javascript';
    js.async = true;
    js.src =
      'https://unpkg.com/@commercetools/checkout-browser-sdk@latest/browser/sdk.js';
    fjs.parentNode.insertBefore(js, fjs);
  })(window, document, 'script');
  ctc('checkoutFlow', {
    projectKey: '{projectKey}',
    region: '{region}',
    sessionId: '{sessionId}',
    logInfo: true, // Recommended for debugging during development.
    logWarn: true, // Recommended for debugging during development.
    logError: true, // Recommended for debugging during development.
  });
</script>
We recommend subscribing to the info, warn, and error Messages for debugging purposes during development. For further information, see Message subscription and logging.

npm module

Prerequisites

  • Node v12.22.7 (or later)
  • Either npm v6 (or later) or yarn v1.22.17 (or later)

Install the SDK package

To install the SDK package, run one of the following commands:

  • npm install @commercetools/checkout-browser-sdk
  • yarn add @commercetools/checkout-browser-sdk

npm script

Integrate Checkout as an npm modulejavascript
import { checkoutFlow } from '@commercetools/checkout-browser-sdk';

checkoutFlow({
  projectKey: '{projectKey}',
  region: '{region}',
  sessionId: '{sessionId}',
  logInfo: true, // Recommended for debugging during development.
  logWarn: true, // Recommended for debugging during development.
  logError: true, // Recommended for debugging during development.
});
We recommend subscribing to the info, warn, and error Messages for debugging purposes during development. For further information, see Message subscription and logging.

checkoutFlow method

When using the Complete Checkout mode, use the checkoutFlow method to initialize the full checkout process with the related configuration.
Initialize checkout process with 'checkoutFlow' passing also optional propertiesjavascript
import { checkoutFlow } from '@commercetools/checkout-browser-sdk';

checkoutFlow({
  projectKey: '{projectKey}',
  region: '{region}',
  sessionId: '{sessionId}',
  locale: `{locale}`,
  logInfo: true,
  logWarn: true,
  logError: true,
  ... // Other configuration properties.
});
When calling the checkoutFlow method, the Checkout UI component for the specified sessionId opens. If the customer quits the checkout process before completing it but the Checkout Session is still valid, you can call the method again with the same sessionId. If the session has expired, you can refresh it and open the component again by getting a new sessionId.
By default, when calling the checkoutFlow method, the component appears full screen in an absolutely positioned <div> element that hides other content on your page. The component renders a header with your preconfigured logo, the input fields, and the Leave checkout button. By clicking the Leave checkout button, the component disappears and the content of your page becomes visible again.
Alternatively, you can display the component inline with your content by using a <div> or <span> element with the data-ctc attribute in it (for example, <div data-ctc />). By doing so, the header and the Leave checkout button do not appear, and you can render any other content around the component, such as a custom header or footer.
Additionally, the checkoutFlow method displays success and error pages after a payment attempt, providing customers with clear feedback about the payment outcome. You can avoid displaying these pages by passing the skipPaymentSuccessPage and skipPaymentErrorPage properties and setting them to true. The properties are independent of each other, so you can pass just one of them, or both.

paymentFlow method

When using the Payment Only mode, use the paymentFlow method to initialize the payment process.
Initialize payment process with 'paymentFlow' passing also optional propertiesjavascript
import { paymentFlow } from '@commercetools/checkout-browser-sdk';

paymentFlow({
  projectKey: '{projectKey}',
  region: '{region}',
  sessionId: '{sessionId}',
  locale: `{locale}`,
  logInfo: true,
  logWarn: true,
  logError: true,
  ... // Other configuration properties.
});
When you call the paymentFlow method, the Payment UI component for the specified sessionId opens. It creates payment sessions for all active payment methods that match your payment predicates configuration, using the current Cart total amount.
If the Cart amount changes after calling this method, for example, when a buyer adds a discount code, the existing payment methods might no longer satisfy your predicates configuration, and the previously created payment sessions become invalid due to changes in the Cart total amount. In that case, you must call reloadPaymentMethods() to create new payment sessions based on the updated amount.
Call the 'reloadPaymentMethods' method to create a new Checkout Sessionjavascript
import { paymentFlow, reloadPaymentMethods } from '@commercetools/checkout-browser-sdk';

reloadPaymentMethods();
By default, when calling the paymentFlow method, the component appears full screen in an absolutely positioned <div> element that hides other content on your page. It is your responsibility to implement a way to close it and show the content on the page.
Alternatively, you can display the component inline with your content by using a <div> or <span> element with the data-ctc attribute in it (for example, <div data-ctc />). By doing so, you can render other content around the component, such as the shipping and billing address forms.

paymentFlow method in multi-step checkouts

If your checkout process consists of multiple steps that your customers must complete, you can use the paymentFlow method to render the Payment UI component on a specific step of the process.

Payment as the last step

The easiest way to add the Payment UI component to your checkout process is to render it as the last step of the process.

You can call the paymentFlow method after the customer has entered other checkout information. The Payment UI component will display the list of available payment integrations and a payment button.
You can display a custom payment button or require the customer to accept the terms and conditions before proceeding with the payment.

Payment as intermediate step

You can render the Payment UI component as an intermediate step of your checkout process, for example, to display an order review step before the payment.

To do so, you must provide an HTML element with a data-ctc-selector="confirmMethod" attribute in it, together with a custom payment button, which must be deactivated or hidden.
When the customer clicks the element to select the payment integration, a Payment Integration Selection Confirmation Message generates and the Payment UI component displays a summary of the payment and the selected payment integration, but the payment process will not be triggered. Therefore, to let the customer proceed to the payment, you must react to the Message by activating or displaying the custom payment button.
At this stage, the customer can edit the information entered in the previous steps or proceed to the payment. If the user selects a different payment integration or if the payment amount changes, you must call the paymentFlow method again with the same sessionId.
You can also require the customer to accept the terms and conditions before proceeding with the payment.

Display custom and vendor payment buttons

To display a custom payment button when using the paymentFlow method, you must provide a <button> element with a data-ctc-selector="paymentButton" attribute in it. This way, the Payment UI component will hide the default payment button and listen for clicks on the provided element.
To activate payment integrations that have a branded vendor button (for example, PayPal), you must provide a <div> element with a data-ctc-selector="vendorButton" attribute in it. When the customer selects a payment integration that has a vendor button, this will be displayed in the <div> element.
It is your responsibility to display or hide the custom payment button by reacting to the Payment Integration Selection Confirmation Message. If the Message hasVendorButton property is set to false, you must display the custom payment button; if it is set to true, you must hide the custom payment button.
You must provide only one <div> element with a data-ctc-selector="vendorButton" attribute because some vendor buttons do not work correctly if multiple <div> elements are present.
You can display custom and vendor buttons only for payment integrations rendered via the web components Payment Integration Type.
When rendering the Payment UI component as an intermediate step of the checkout process, it is mandatory to display a custom payment button.
Display payment and vendor buttons and react to Message to hide payment buttonjavascript
  <button data-ctc-selector="paymentButton">Proceed with payment</button>
  <div data-ctc-selector="vendorPaymentButton"></div>

  <script>
    paymentFlow({
      projectKey: '{projectKey}',
      region: '{region}',
      sessionId: '{sessionId}',
      onInfo: (message) => {
        if (message.code === 'payment_method_selection_confirmation') {
          const {
            method: { hasVendorButton, type },
          } = message.payload as {
            method: { type: string; hasVendorButton: boolean };
          };
          const paymentButton = document.querySelector('[data-ctc-selector="paymentButton"]');
          if (paymentButton) {
            paymentButton.style.display = hasVendorButton ? 'none' : 'block';
          }
        }
      },
    });
  </script>

Accept terms and conditions before payment

When using the paymentFlow method, you can request the customer to accept terms and conditions before proceeding with the payment. To do so, you must provide an HTML element with a data-ctc-selector="termsAndConditionsPending" attribute in it until the customer accepts the required terms and conditions.
An External Terms And Conditions Pending Message generates if the customer tries to proceed with the payment while the element is present.
HTML element for pending acceptance of terms and conditionsjavascript
{
  areTermsAndConditionsPending ? (
    <span data-ctc-selector="termsAndConditionsPending" />
  ) : null;
}

React to Messages

When using the paymentFlow method, it is important to react to the Messages generated by Checkout to provide feedback to customers and handle different scenarios that may occur during the payment process.
For example, if the Cart contains Discount Codes that don't apply to it, Checkout removes the codes to avoid errors when converting the Cart to an Order. When this happens, Checkout generates the NotApplicableDiscountCodeRemoved Message, which you can react to, for example to provide feedback to your customer or to update the cart's information on your checkout flow.

Following is an example with some Messages that you should consider reacting to.

It is your responsibility to react to the payment_failed and payment_cancelled Messages and to give feedback to the customer about the failure. To allow the customer to retry the payment, while also ensuring the Cart has the latest version and correct total amount, call the paymentFlow method with the same sessionId again.
React to Messages using the 'paymentFlow' methodjavascript
import { paymentFlow } from '@commercetools/checkout-browser-sdk';

paymentFlow({
  projectKey: '{projectKey}',
  region: '{region}',
  sessionId: '{sessionId}',
  locale: `{locale}`,
  logInfo: true,
  logWarn: true,
  logError: true,
  onError: (error) => {
    switch (error.code) {
      case 'payment_method_loading_error':
        // Store the number of failed payment integrations on the local state.
        // If the number of failed payment integrations is equal to the total number of payment integrations, display a generic error message.
        break;
      case 'payment_failed':
        // Show a payment failed message to the customer.
        // Call paymentFlow method again to restart the payment flow.
        break;
    }
  },
  onInfo: (message) => {
    switch (message.code) {
      case 'checkout_completed':
        const {
          order: { id },
        } = message.payload as {
          order: { id: string };
        };
        // Redirect to confirmation page with Order ID.
        break;
      case 'payment_method_selection_confirmation_failed':
        // Show message to customer and allow user to try again.
        break;
      case 'payment_method_selection_confirmation': {
        const {
          method: { hasVendorButton, type },
        } = message.payload as {
          method: { type: string; hasVendorButton: boolean };
        };
        // Hide custom payment button if hasVendorButton is 'true'.
        break;
      }
      case 'payment_methods_received': {
        const { paymentMethods } = message.payload as { paymentMethods: string[] };
        // Store total number of payment integrations on local state.
        break;
      }
      case 'payment_method_loaded':
        // Increment loaded payment integrations.
        break;
      case 'payment_started':
        // Show blocking overlay with spinner to avoid user interaction with the page.
        // Initialize local state errors to 'false'.
        break;
      case 'payment_completed':
        // Hide blocking overlay.
        break;
      case 'payment_method_selected':
        const {
          method: { hasVendorButton, type },
        } = message.payload as {
          method: { type: string; hasVendorButton: boolean };
        };
        // Store selected payment integration on local state and remove local payment error state.
        break;
      case 'payment_cancelled':
        // Hide blocking overlay.
        break;
      case 'external_terms_and_conditions_pending':
        // Show message to the customer to accept terms and conditions.
        break;
    }
  },
});

Express Payments BETA

Express Payments let you render payment buttons, such as Apple Pay and Google Pay, anywhere on your website to complete an express checkout that bypasses the multi-step checkout process.

Express Payments use a two-step integration pattern:

  1. Initialize once: call expressPayment.init() to set up the configuration and Message handlers.
  2. Mount multiple times: call expressPayment.mount() for each location where you want to display express payment buttons.

Express Payment configuration properties

The following table shows the configuration properties to provide with the expressPayment.init() method:
PropertyTypeRequiredDescription
projectKeystringYesIdentifier of your Checkout entity and the project_key of your Project.
sessionIdstringYesIdentifier of the Checkout Session.
regionstringYesRegion where your Checkout application is hosted. For example, europe-west1.gcp.
countryCodestringYesThe country code of your store (ISO 3166-1 alpha-2). For example, DE.
localestringNo (default: en)Your customer's locale. If the provided locale is not found, then English will be used.
onInfofunctionNoFor more information, see onInfo, onWarn, and onError.
onWarnfunctionNoFor more information, see onInfo, onWarn, and onError.
onErrorfunctionNoFor more information, see onInfo, onWarn, and onError.
logInfobooleanNo (default: false)For more information, see logInfo, logWarn, and logError.
logWarnbooleanNo (default: false)For more information, see logInfo, logWarn, and logError.
logErrorbooleanNo (default: false)For more information, see logInfo, logWarn, and logError.

Initialize the Express Payment

Before mounting Express Payments buttons, you must initialize the Express Payment once with your configuration. This is typically done when the page loads or when the session is created.

Initialize Express Paymentjavascript
import { expressPayment } from '@commercetools/checkout-browser-sdk';

expressPayment.init({
  projectKey: '{projectKey}',
  region: '{region}',
  sessionId: '{sessionId}',
  countryCode: '{countryCode}',
  locale: 'en', // Optional: defaults to 'en'
  logInfo: true,
  logWarn: true,
  logError: true,
  onInfo: (message) => {
    switch (message.code) {
      case 'express_payment_started':
        // Show blocking overlay with spinner.
        break;
      case 'express_payment_cancelled':
        // Hide blocking overlay.
        break;
      case 'express_payment_completed':
        // Redirect to confirmation page with Order ID.
        const { order } = message.payload as { order: { id: string } };
        window.location.href = '/thank-you?orderId=' + order.id;
        break;
    }
  },
  onError: (error) => {
    switch (error.code) {
      case 'express_payment_failed':
        // Show error message to customer.
        break;
    }
  },
});
The sessionId used for Express Payments does not need to have a Cart associated with it initially. You can create or update the Cart in the onPayButtonClick callback when the customer clicks the Express Payments button.

Use Express Payments with a browser script

When integrating as a browser script, use the ctc function to initialize and mount the Express Payments:
Initialize and mount Express Payment with browser scriptHTML
<!-- Single container: use data-ctc-express without a value -->
<div data-ctc-express></div>

<script>
  (function (w, d, s) {
    if (w.ctc) {
      return;
    }
    var js,
      fjs = d.getElementsByTagName(s)[0];
    var q = [];
    w.ctc =
      w.ctc ||
      function () {
        q.push(arguments);
      };
    w.ctc.q = q;
    js = d.createElement(s);
    js.type = 'text/javascript';
    js.async = true;
    js.src =
      'https://unpkg.com/@commercetools/checkout-browser-sdk@latest/browser/sdk.js';
    fjs.parentNode.insertBefore(js, fjs);
  })(window, document, 'script');

  // Initialize Express Payment
  ctc('expressPaymentInit', {
    projectKey: '{projectKey}',
    region: '{region}',
    sessionId: '{sessionId}',
    countryCode: '{countryCode}',
    locale: 'en', // Optional: defaults to 'en'
    logInfo: true,
    logWarn: true,
    logError: true,
    onInfo: function (message) {
      if (message.code === 'express_payment_completed') {
        var order = message.payload.order;
        window.location.href = '/thank-you?orderId=' + order.id;
      }
    },
    onError: function (error) {
      if (error.code === 'express_payment_failed') {
        console.error('Express payment failed');
      }
    },
  });

  // Mount Express Payment buttons (no expressId needed for single container)
  ctc('expressPaymentMount', {
    initialAmount: {
      type: 'centPrecision',
      currencyCode: 'EUR',
      centAmount: 2999,
      fractionDigits: 2,
    },
    onPayButtonClick: function () {
      // Create a Cart with the product.
      // This must return a Promise.
      return createCartWithProduct(productId);
    },
  });
</script>
When using the browser script, use expressPaymentInit and expressPaymentMount as function names instead of expressPayment.init and expressPayment.mount.

Mount Express Payments buttons

After initialization, you can mount Express Payments buttons to one or more containers on your page. You can choose between two approaches:

  1. Mount all buttons at once: use expressPayment.mount() to render all available Express Payments buttons in a single container.
  2. Mount individual buttons: use expressPayment.getAvailableMethods() to discover available methods, then expressPayment.mountMethod() to mount each button individually for custom layouts.

Mount all buttons in a single container

The mount() method renders all available express payment buttons in an element with the data-ctc-express attribute:
HTML container for express buttonshtml
<!-- Single container: no expressId needed -->
<div data-ctc-express></div>
Mount all Express Payment buttonsjavascript
import { expressPayment } from '@commercetools/checkout-browser-sdk';

// No expressId needed - SDK finds the element with data-ctc-express attribute
expressPayment.mount({
  initialAmount: {
    type: 'centPrecision',
    currencyCode: 'EUR',
    centAmount: 2999,
    fractionDigits: 2,
  },
});

Mount individual buttons with custom layout

For more control over the layout and placement of express buttons, you can mount them individually. Use the expressId option to target specific containers with data-ctc-express="expressId":
HTML containers for individual express buttonshtml
<div data-ctc-express="googlepay"></div>
<div data-ctc-express="applepay"></div>
<div data-ctc-express="paypal"></div>
Mount individual Express Payment buttonsjavascript
import { expressPayment, type ExpressPaymentMethod } from '@commercetools/checkout-browser-sdk';

// Get available express payment methods
const methods = await expressPayment.getAvailableMethods();
// Returns: [{ type: 'googlepay', paymentIntegrationId: '...', connectorId: '...' }, ...]

// Mount each method to its own container
methods.forEach((method) => {
  expressPayment.mountMethod({
    expressId: method.type, // Targets <div data-ctc-express="googlepay">
    method,
    initialAmount: {
      type: 'centPrecision',
      currencyCode: 'EUR',
      centAmount: 2999,
      fractionDigits: 2,
    },
    onPayButtonClick: async () => {
      await createCartWithProduct(productId);
    },
  });
});

This approach lets you:

  • Place buttons in different locations on the page
  • Control the order in which buttons appear
  • Wrap buttons in custom decorator components
  • Conditionally show or hide specific payment methods
The getAvailableMethods() method returns the list of Express Payments methods configured for your project. The available methods depend on your payment connector configuration and may vary at runtime.

Use the onPayButtonClick hook

The onPayButtonClick callback is an optional hook. It is called when the customer clicks an Express Payments button. Use it to perform async operations before the payment starts, such as creating or updating a Cart. You can also use it for any other async operations you need.
When the Express Payments flow starts, the Checkout Session must have a Cart associated with it. If no Cart is present at this point, the Express Payments flow will fail.

The following examples show some possible implementations:

Example: Create a Cart when the express button is clickedjavascript
import { expressPayment } from '@commercetools/checkout-browser-sdk';

expressPayment.mount({
  initialAmount: {
    type: 'centPrecision',
    currencyCode: 'EUR',
    centAmount: 2999,
    fractionDigits: 2,
  },
  onPayButtonClick: async () => {
    // Create a Cart with the product and update the session.
    const cart = await createCart();
    await addProductToCart(cart.id, productId);
    await updateSessionWithCart(sessionId, cart.id);
  },
});
Example: Perform other async operationsjavascript
import { expressPayment } from '@commercetools/checkout-browser-sdk';

expressPayment.mount({
  initialAmount: {
    type: 'centPrecision',
    currencyCode: 'EUR',
    centAmount: 2999,
    fractionDigits: 2,
  },
  onPayButtonClick: async () => {
    // Perform any async operations before the express payment starts.
    await trackAnalyticsEvent('express_payment_started');
    await validateInventory(productId);
  },
});

Mount options

The following table shows the options to provide with the expressPayment.mount() and expressPayment.mountMethod() methods:
PropertyTypeRequiredDescription
expressIdStringNoIdentifier to find a specific container with data-ctc-express="expressId". If not provided, the SDK looks for an element with just the data-ctc-express attribute.
initialAmountObjectYesThe initial amount to display on the Express Payments buttons. Contains currencyCode, centAmount, fractionDigits, and optionally type (must be 'centPrecision' if provided).
methodObjectOnly for mountMethod()The payment method to mount. Obtained from getAvailableMethods(). Contains type, paymentIntegrationId, and connectorId.
onPayButtonClickFunctionNoOptional hook called when the customer clicks an Express Payments button. Use this to perform async operations before the checkout starts, such as creating or updating a Cart. Must return a Promise.
onShippingAddressSelectedFunctionNoCallback function called when the customer selects a shipping address in the payment sheet. Receives a partial address (country, postalCode, city, state). If not provided, the Cart is automatically updated with the partial address.
getShippingMethodsFunctionNoCallback function to provide custom shipping methods. Must return a Promise that resolves to an array of shipping options. If not provided, shipping methods are fetched automatically from Composable Commerce.
onShippingMethodSelectedFunctionNoCallback function called when the customer selects a shipping method. If not provided, the Cart is automatically updated with the selected shipping method.
onPaymentSubmitFunctionNoCallback function called before the payment is submitted with the complete shipping and billing addresses and customer email. If not provided, the Cart is automatically updated with the addresses and email.

Handle shipping with Express Payment

Express Payment supports two approaches for handling shipping:

Automatic shipping handling

By default, Express Payments automatically handles shipping methods based on your Composable Commerce configuration. The shipping methods matching the customer's address are displayed in the payment sheet.

Custom shipping methods

If you need more control over shipping methods, you can provide custom shipping methods using the getShippingMethods and onShippingMethodSelected callbacks.
Custom shipping methodsjavascript
import { expressPayment } from '@commercetools/checkout-browser-sdk';

expressPayment.mount({
  initialAmount: {
    type: 'centPrecision',
    currencyCode: 'EUR',
    centAmount: 2999,
    fractionDigits: 2,
  },
  onPayButtonClick: async () => {
    // Create a Cart when the customer clicks the express button.
    await createCartWithProduct(productId);
  },
  getShippingMethods: async () => {
    // Return custom shipping methods.
    return [
      {
        id: 'standard',
        name: 'Standard Shipping',
        description: '5-7 business days',
        amount: {
          centAmount: 499,
          currencyCode: 'EUR',
        },
      },
      {
        id: 'express',
        name: 'Express Shipping',
        description: '1-2 business days',
        isSelected: true,
        amount: {
          centAmount: 999,
          currencyCode: 'EUR',
        },
      },
    ];
  },
  onShippingMethodSelected: async ({ shippingMethod }) => {
    // Update the Cart with the selected shipping method.
    await updateCartShippingMethod(shippingMethod.id);
  },
});
When using custom shipping methods, you must provide both getShippingMethods and onShippingMethodSelected callbacks. The getShippingMethods callback is called when the payment sheet is displayed, and onShippingMethodSelected is called when the customer selects a shipping method.

Handle addresses with Express Payments

Express Payments provides callbacks to handle shipping and billing addresses:

Partial address selection

The onShippingAddressSelected callback is called when the customer selects a shipping address in the payment sheet. At this stage, only a partial address is available (country, postalCode, city, and state) for privacy reasons.
Handle partial address selectionjavascript
import { expressPayment } from '@commercetools/checkout-browser-sdk';

expressPayment.mount({
  initialAmount: {
    type: 'centPrecision',
    currencyCode: 'EUR',
    centAmount: 2999,
    fractionDigits: 2,
  },
  onShippingAddressSelected: async ({ address }) => {
    // Use the partial address to calculate shipping costs or validate delivery.
    console.log('Partial address:', address.country, address.postalCode);
  },
});

Complete address handling

The onPaymentSubmit callback is called before the payment is submitted and provides the complete shipping and billing addresses along with the customer email. Use this callback to perform final Cart updates or validations.
Handle complete addresses and customer emailjavascript
import { expressPayment } from '@commercetools/checkout-browser-sdk';

expressPayment.mount({
  initialAmount: {
    type: 'centPrecision',
    currencyCode: 'EUR',
    centAmount: 2999,
    fractionDigits: 2,
  },
  onPaymentSubmit: async ({ shippingAddress, billingAddress, customerEmail }) => {
    // Update the Cart with the complete addresses and customer email.
    await updateCartAddresses(shippingAddress, billingAddress);
    await updateCartEmail(customerEmail);
  },
});
If you do not provide the onPaymentSubmit callback, the Cart is automatically updated with the shipping and billing addresses and customer email received from the payment provider.

React to Express Payments Messages

When using Express Payments, it is important to react to the Messages generated by Checkout to provide feedback to customers and handle different scenarios.
React to Express Payment Messagesjavascript
import { expressPayment } from '@commercetools/checkout-browser-sdk';

expressPayment.init({
  projectKey: '{projectKey}',
  region: '{region}',
  sessionId: '{sessionId}',
  countryCode: '{countryCode}',
  onInfo: (message) => {
    switch (message.code) {
      case 'express_payment_started':
        // Customer clicked an express payment button.
        // Show blocking overlay with spinner.
        break;
      case 'express_payment_cancelled':
        // Customer cancelled the express payment.
        // Hide blocking overlay.
        break;
      case 'express_payment_interrupted':
        // Express payment was interrupted by the seller's callback.
        // Hide blocking overlay.
        break;
      case 'express_payment_completed':
        // Express payment completed successfully.
        const { order } = message.payload as { order: { id: string } };
        window.location.href = '/thank-you?orderId=' + order.id;
        break;
    }
  },
  onError: (error) => {
    switch (error.code) {
      case 'express_payment_failed':
        // Express payment failed.
        // Show error message to customer.
        break;
      case 'no_express_payment_integrations':
        // No express payment integrations are configured.
        // Hide express buttons container.
        break;
      case 'express_container_not_found':
        // The container element was not found.
        break;
      case 'express_multiple_containers_found':
        // Multiple elements with data-ctc-express attribute found.
        // Use expressId to specify which container to use.
        break;
      case 'express_payment_integration_not_available':
        // The requested payment integration is not available.
        // This can happen when using mountMethod() with an unavailable method.
        break;
    }
  },
});
It is your responsibility to react to the express_payment_failed and express_payment_cancelled Messages and to give feedback to the customer. Unlike the checkoutFlow and paymentFlow methods, Express Payment does not display error pages automatically.

Complete Express Payments example

The following example shows a complete integration of Express Payments on a product page using mount() to render all buttons in a single container:
HTML structurehtml
<!-- Single container for all express buttons -->
<div data-ctc-express></div>
Complete Express Payment integrationjavascript
import { expressPayment } from '@commercetools/checkout-browser-sdk';

// Initialize Express Payment once when the page loads.
const initializeExpressPayment = (sessionId: string) => {
  expressPayment.init({
    projectKey: '{projectKey}',
    region: '{region}',
    sessionId: sessionId,
    countryCode: 'DE',
    locale: 'de-DE', // Optional: defaults to 'en'
    logInfo: true,
    logWarn: true,
    logError: true,
    onInfo: (message) => {
      switch (message.code) {
        case 'express_payment_started':
          showBlockingOverlay();
          break;
        case 'express_payment_cancelled':
          hideBlockingOverlay();
          break;
        case 'express_payment_completed':
          hideBlockingOverlay();
          const { order } = message.payload as { order: { id: string } };
          window.location.href = '/thank-you?orderId=' + order.id;
          break;
      }
    },
    onError: (error) => {
      hideBlockingOverlay();
      if (error.code === 'express_payment_failed') {
        showErrorMessage('Express payment failed. Please try again.');
      }
    },
  });
};

// Mount express buttons (no expressId needed for single container).
const mountExpressButtons = (product: { id: string; price: number; currency: string }) => {
  expressPayment.mount({
    initialAmount: {
      type: 'centPrecision',
      currencyCode: product.currency,
      centAmount: product.price,
      fractionDigits: 2,
    },
    onPayButtonClick: async () => {
      // Create a Cart with the product.
      await createCartWithProduct(product.id);
    },
    getShippingMethods: async () => {
      return getAvailableShippingMethods(product.currency);
    },
    onShippingMethodSelected: async ({ shippingMethod }) => {
      await setCartShippingMethod(shippingMethod.id);
    },
  });
};

Complete Express Payments example with individual buttons

The following example shows how to mount buttons individually for custom layouts:

HTML structure for individual buttonshtml
<!-- Containers for each payment method -->
<div class="express-buttons-row">
  <div data-ctc-express="googlepay"></div>
  <div data-ctc-express="applepay"></div>
  <div data-ctc-express="paypal"></div>
</div>
Express Payments with individual buttonsjavascript
import { expressPayment, type ExpressPaymentMethod } from '@commercetools/checkout-browser-sdk';

// Initialize Express Payments once when the page loads.
const initializeExpressPayment = (sessionId: string) => {
  expressPayment.init({
    projectKey: '{projectKey}',
    region: '{region}',
    sessionId: sessionId,
    countryCode: 'DE',
    locale: 'de-DE', // Optional: defaults to 'en'
    logInfo: true,
    logWarn: true,
    logError: true,
    onInfo: (message) => {
      if (message.code === 'express_payment_completed') {
        const { order } = message.payload as { order: { id: string } };
        window.location.href = '/thank-you?orderId=' + order.id;
      }
    },
  });
};

// Mount express buttons individually for a product.
const mountIndividualExpressButtons = async (product: { id: string; price: number; currency: string }) => {
  // Get available Express Payments methods
  const methods = await expressPayment.getAvailableMethods();

  // Common mount options
  const commonOptions = {
    initialAmount: {
      type: 'centPrecision' as const,
      currencyCode: product.currency,
      centAmount: product.price,
      fractionDigits: 2,
    },
    onPayButtonClick: async () => {
      await createCartWithProduct(product.id);
    },
  };

  // Mount each method to its own container
  methods.forEach((method) => {
    expressPayment.mountMethod({
      expressId: method.type, // Targets <div data-ctc-express="googlepay">, etc.
      method,
      ...commonOptions,
    });
  });
};

You can also dynamically create containers if the available methods are not known at build time:

Dynamically creating containersjavascript
// Mount each method to its own dynamically created container
methods.forEach((method) => {
  const expressId = method.type;

  // Create container element if it doesn't exist
  if (!document.querySelector(`[data-ctc-express="${expressId}"]`)) {
    const container = document.createElement('div');
    container.setAttribute('data-ctc-express', expressId);
    container.className = 'express-button-wrapper';
    document.querySelector('.express-buttons-row')?.appendChild(container);
  }

  expressPayment.mountMethod({
    expressId,
    method,
    ...commonOptions,
  });
});

close method

Use the close method to close the Checkout or the Payment UI component when it is not needed. Calling the close method is recommended over unmounting the component, as it ensures that the necessary cleanup is performed.
An example of use is a single-page application (SPA) implementing a multi-step checkout process with the paymentFlow method, where the customer decides to edit the information entered in a previous step. In such cases, you can use the close method to close the payment UI component.
Once the customer moves the payment step again, you can call the paymentFlow method with the same sessionId to render the Payment UI component again.
Another example of a use case is to implement a custom button to leave the checkout when using the checkoutFlow method.
Call the 'close' methodjavascript
import { close } from '@commercetools/checkout-browser-sdk';

close();

Return URL

Some payment integrations may require a redirection from your website to one of the payment service providers (PSPs) to get information about the customer. In such cases, when you install a Connector, you must set a return URL to take the customer back to your website after the information has been retrieved. We recommend using the same URL as the one where the checkout process started; however, this is not mandatory.

You can set only one return URL per Connector. If you want to use the same Connector with multiple domains, you must create different Applications in the Merchant Center and override the return URL.

When information has been retrieved, the paymentReference parameter is added to the return URL with the id of the Payment as its value, and the customer returns to your website. Now, you must call the checkoutFlow or paymentFlow method again by providing the same configuration parameters as before along with the paymentReference.
Call the 'checkoutFlow' method with the 'paymentReference' parameterjavascript
import { checkoutFlow } from '@commercetools/checkout-browser-sdk';

checkoutFlow({
  region: '{region}',
  projectKey: '{projectKey}',
  sessionId: '{sessionId}',
  locale: '{locale}',
  logInfo: true,
  logWarn: true,
  logError: true,
  paymentReference: '{paymentReference}',
});
When calling the checkoutFlow or paymentFlow method again, the UI component appears and displays a loading status while the payment is verified. If the payment is successful, the checkout process is complete. Otherwise, the customer is prompted to either quit the checkout process or try again by editing the entered information.

Checkout completion

Once the customer completes the checkout process successfully, the SDK closes the Checkout or the payment UI component and the Checkout Completed Message generates. After this happens, you can determine how the checkout process should continue by reacting to the Message, both when using the checkoutFlow and the paymentFlow method.

For example, you can react to the Message to display feedback for the customer on your website, such as a message or a dialog. Or, you can use the Message to redirect the customer to a different page, such as a Thank You page.

React to Checkout Completed Message and redirect to Thank you pagejavascript
import { checkoutFlow } from '@commercetools/checkout-browser-sdk';

checkoutFlow({
  projectKey: '{projectKey}',
  region: '{region}',
  sessionId: '{sessionId}',
  onInfo: (message) => {
    if (message.code === 'checkout_completed') {
      const {
          order: { id },
        } = message.payload as {
          order: { id: string };
        };
      window.location.href = '/thank-you?orderId=' + id;
    }
  },
});

Retry order verification

Sometimes a checkout cannot complete verification on time and times out before the Order is created. When this happens, Checkout generates the Order Verification Timeout Message. To retry the verification, use the retryOrderVerification method.
If the retry attempt fails, then Checkout generates the Order Verification Retry Error Message with details about why the retry failed.
Call the 'retryOrderVerification method' to retry order verificationjavascript
import { retryOrderVerification } from '@commercetools/checkout-browser-sdk';

retryOrderVerification();

Message subscription and logging

The Messages from Checkout can be of three severity levels: info, warn, or error.

You can handle Messages by using:

  • The onInfo, onWarn, and onError handlers to subscribe to Messages.
  • The logInfo, logWarn, and logError methods to log Messages.

onInfo, onWarn, and onError

You can subscribe to the info, warn, or error Messages by passing the optional onInfo, onWarn, or onError message handlers with the checkoutFlow or paymentFlow methods. The handlers will receive the Message as a parameter.
In the following examples, we subscribe to all info Messages and log a Received note followed by the Message code on the browser's developer console.
Use 'onInfo' with the npm module to subscribe to info Messagesjavascript
import { checkoutFlow } from '@commercetools/checkout-browser-sdk';

checkoutFlow({
  projectKey: '{projectKey}',
  region: '{region}',
  sessionId: '{sessionId}',
  onInfo: (message) => {
    console.error('Received: ' + message.code);
  },
});
Use 'onInfo' with the browser script to subscribe to info MessagesHTML
<script>
  (function (w, d, s) {
    if (w.ctc) {
      return;
    }
    var js,
      fjs = d.getElementsByTagName(s)[0];
    var q = [];
    w.ctc =
      w.ctc ||
      function () {
        q.push(arguments);
      };
    w.ctc.q = q;
    js = d.createElement(s);
    js.type = 'text/javascript';
    js.async = true;
    js.src =
      'https://unpkg.com/@commercetools/checkout-browser-sdk@latest/browser/sdk.js';
    fjs.parentNode.insertBefore(js, fjs);
  })(window, document, 'script');

  ctc('checkoutFlow', {
    projectKey: '{projectKey}',
    region: '{region}',
    sessionId: '{sessionId}',
    onInfo: function (message) {
      console.error('Received: ' + message.code);
    },
  });
</script>

logInfo, logWarn, and logError

You can use the logInfo, logWarn, and logError methods to log Messages in the console for debugging purposes. We recommend not activating them in production since, usually, these logs are not useful for end users.
In the following examples, we log all the info and error Messages, but not the warn ones.
Use 'logInfo' and 'logError' with the npm module to log info and error Messagesjavascript
import { checkoutFlow } from '@commercetools/checkout-browser-sdk';

checkoutFlow({
  projectKey: '{projectKey}',
  region: '{region}',
  sessionId: '{sessionId}',
  logInfo: true,
  logWarn: false,
  logError: true,
});
Use 'logInfo' and 'logError' with the browser script to log info and error MessagesHTML
<script>
  (function (w, d, s) {
    if (w.ctc) {
      return;
    }
    var js,
      fjs = d.getElementsByTagName(s)[0];
    var q = [];
    w.ctc =
      w.ctc ||
      function () {
        q.push(arguments);
      };
    w.ctc.q = q;
    js = d.createElement(s);
    js.type = 'text/javascript';
    js.async = true;
    js.src =
      'https://unpkg.com/@commercetools/checkout-browser-sdk@latest/browser/sdk.js';
    fjs.parentNode.insertBefore(js, fjs);
  })(window, document, 'script');

  ctc('checkoutFlow', {
    projectKey: '{projectKey}',
    region: '{region}',
    sessionId: '{sessionId}',
    logInfo: true,
    logWarn: false,
    logError: true,
  });
</script>