DOCS

Custom integration

Custom integration

Build an end-to-end Checkout integration into your custom site.

This guide covers the technical aspects of completing a full integration of Zonos Checkout into your custom site or platform. It is intended for developers who are comfortable working with JavaScript and have experience with frontend development. All steps are required unless otherwise noted.

Prerequisites


Install the Zonos JS script 

Your custom integration will need to include the Zonos JavaScript snippet. This script is responsible for rendering the Checkout UI, Zonos Hello, handling payment processing, and handling visitor geo-IP detection.

1

Install the Zonos JS snippet

The Zonos Elements script is available at the following URL:

Zonos JS snippet

1
<script src="https://cdn.jsdelivr.net/npm/@zonos/elements/dist/scripts/loadZonos.js" />

Handling browser caching

We also typically recommend appending a timestamp or other unique identifier to the URL to ensure that the script is not cached by the browser. This will ensure that the latest version of the script is always loaded. For example:

Zonos JS snippet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<script>
  (async function () {
    const timestamp = new Date().getTime();
    const zonosScript = document.querySelector(
      `script[src*="https://cdn.jsdelivr.net/npm/@zonos/elements/dist/scripts/loadZonos.js"]`,
    );

    if (!zonosScript) {
      const script = document.createElement("script");
      script.src = `https://cdn.jsdelivr.net/npm/@zonos/elements/dist/scripts/loadZonos.js?timestamp=${timestamp}`;
      script.addEventListener(
        "load",
        () => {
          // Initialize the script here (instructions in next section)
        },
        false,
      );
      document.head.appendChild(script);
    }
  })();
</script>
2

Authenticate the Zonos JS snippet

Once you have loaded the Zonos JS script, you need to authenticate it by passing a Zonos API key and store ID into Zonos.init function. The API keys used to authenticate Checkout are designed to be publishable, meaning they can be safely used in frontend code without exposing any sensitive information.

Zonos JS snippet

1
2
3
4
5
6
Zonos.init({
  // ... other fields
  zonosApiKey: "API KEY", // Replace with your actual API key (found in Dashboard)
  storeId: "STORE ID", // Replace with your actual store ID (found in Dashboard)
  // ... other fields
});
3

Set allowed domains

In order to ensure that the Zonos JS script is only loaded on allowed sites, we filter requests based on a list of "allowed domains". This list is configured in Dashboard. Without this configuration, the Zonos JS script will return permission errors instead of loading properly.

To update your allowed domains:

  1. Go to Dashboard -> Settings -> Checkout settings.
  2. Under the Allowed domains section, add the domain(s) where you will be integrating Checkout.
  3. Click Save.

Set up Zonos Hello 

Once you have set up the Zonos JS script, you need to configure Hello to work with your site. Hello is responsible for detecting the visitor's location, language, and currency, and displaying the appropriate information to them. Hello is required when using Checkout.

You can configure all Hello settings in both Dashboard and the Zonos JS script. If you have already configured Hello in Dashboard, the script will load those settings and use them. If you specify any values in the helloSettings property of the Zonos.init function, the script will use those values instead.

1

Configure currency conversion

Hello uses CSS selectors to identify elements on your site that display currency information. You will need to pass these selectors to the helloSettings.currencyElementSelector property of the Zonos.init function.

You can use any valid CSS selector here, for example #price, .price to select multiple different elements.

Zonos JS snippet

1
2
3
4
5
6
7
Zonos.init({
  // ... other fields
  helloSettings: {
    // ... other fields
    currencyElementSelector: "#price, .price", // Replace with your actual selector
  },
});

Optional — Automatically open Hello on page load

By default, Hello will only open when the visitor clicks on the flag button. If you would like to automatically open Hello when the page loads, you can call the Zonos.openHelloDialog() function once the Zonos script has loaded.

Zonos JS snippet

1
2
3
4
5
Zonos.init({
  // ... other fields
});

Zonos.openHelloDialog();

Set up Zonos Checkout 

Once Hello has been configured, you need to set up Checkout to work with your site. Checkout is responsible for allowing the customer to enter their shipping information, calculate landed cost, and complete the order.

Checkout will share contextual data with Hello, such as the visitor's location, language, and currency. This ensures that the customer's experience is consistent across the whole shopping process.

You can configure all Checkout settings in both Dashboard and the Zonos JS script. If you have already configured Checkout in Dashboard, the script will load those settings and use them. If you specify any values in the checkoutSettings property of the Zonos.init function, the script will use those values instead.

1

Configure the 'place order' button

The Zonos JS script will automatically recognize international shoppers and direct them to the Checkout flow. However, you will need to configure the 'place order' button on your platform to open Checkout when clicked. This can be done by passing a CSS selector to the checkoutSettings.placeOrderButtonSelector property of the Zonos.init function.

If you have multiple buttons that can be used to place an order, make sure to pass in a selector that will match all of them. For example, #placeOrder, .place-order will match both #placeOrder and .place-order.

Zonos JS snippet

1
2
3
4
5
6
7
Zonos.init({
  // ... other fields
  checkoutSettings: {
    // ... other fields
    placeOrderButtonSelector: "#placeOrder", // Replace with your actual selector(s)
  },
});
2

Securely create cart details on server-side

In order to display the cart details to the customer, you need to create a server-side function that will call the Zonos API to create a cart, and then pass that cart ID back to your frontend. This will ensure that cart details are not exposed to the customer in a way that can be manipulated.

Your backend API call will use a secret GraphQL credential token, which is different than the public token which you use to authenticate the Zonos JS script. This token can be retrieved from Dashboard, and needs to be passed as a header in your API call.

The createCart mutation accepts a list of items, which should be formatted according to the cart item schema.

Example server-side cart creation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Example cart creation with Node.js
async function createCart(someCartDetails) {
  const response = await fetch("https://api.zonos.com/graphql", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      credentialToken: "credential_live_xxxxxxxxxx", // Replace with your actual credential token (found in Dashboard)
    },
    body: `
      mutation {
        createCart(input: {
          cart: {
            items: ${JSON.stringify(someCartDetails)}
          }
        }) {
          id
        }
      }
    `,
  });

  const data = await response.json();
  return data.createCart.id; // Return the cart ID to your frontend
}

We suggest creating an API endpoint on your server-side and then calling that endpoint from your frontend JS integration, which is detailed in the next step.

3

Pass cart ID to Checkout via frontend

Once you have created a cart on your server-side, you need to pass the cart ID to the Zonos JS script. This can be done by using the createCartId callback which is part of the Zonos.init function. Checkout will then securely retrieve the cart details from Zonos when opened, preventing any tampering with the cart.

Zonos JS snippet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Zonos.init({
  // ... other fields
  checkoutSettings: {
    // ... other fields
    placeOrderButtonSelector: "#placeOrder", // Replace with your actual selector(s)
    createCartId: async () => {
      const response = await fetch("https://api.merchant.com/api/get-cart", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
      });
      const json = await response.json();

      return json.id; // Only need to return the cart ID
    },
  },
});

Was this page helpful?