# Shopify (headless): Integration steps

> STEP 2

These are the steps for integrating Checkout+ SDK into a headless Shopify store. You implement a `CartConnector` using your existing cart logic and pass it to the SDK. `CartConnector` runs in the browser.

## Who this page is for

This page is for Shopify stores that meet both of the following.

- A headless Shopify store that handles the cart with the Storefront API (for example, one built with Next.js or Hydrogen)
- Purchases go through Shopify checkout

If you use a cart or checkout other than Shopify's, talk to your Recustomer contact.

## Integration flow

The integration has the following five steps. For each step, the table lists how to check that it worked and the `kind` delivered to `onError` on failure (for what each `kind` means, see [`onError`](./shopify-headless-reference-on-error.html)).

| Step | What to do | How to check it worked | `kind` delivered to `onError` on failure |
| --- | --- | --- | --- |
| 1 | [Load the SDK](./shopify-headless-load-sdk.html) | In the browser console, `window.rcmr?.checkoutPlus` is defined | — |
| 2 | [Implement `CartConnector`](./shopify-headless-cart-connector.html) | Calling `getCart()` of the `CartConnector` you implemented, from your development code, returns every line in the cart | `invalid-cart`, `unavailable` |
| 3 | [Initialize the SDK](./shopify-headless-init.html) | `init()` returns a `CheckoutPlusSession`, and `invalid-options` is not delivered to `onError` | `invalid-options` |
| 4 | [Show the banner](./shopify-headless-banner.html) | When you open the cart, the banner appears, and in the Network tab, `metafield` in the cart response isn't `null` | `not-published`, `invalid-benefit-set`, `acceptance-suspended`, `currency-not-offered`, `ui-failure` |
| 5 | [Replace the checkout button](./shopify-headless-checkout-button.html) | When you check the box and proceed to checkout, checkout contains one Checkout+ product line | `add-failed`, `remove-failed` |

`init()` makes no network requests. The only case where `init()` notifies `onError` is when the `storeId` or `cart` you pass is invalid (`invalid-options`). Failures for the benefit set (the commitment charge and benefits for each range) or the cart are delivered when the SDK starts fetching (on the first call to `mount()`, `refresh()`, or `prepareCheckout()`). In the step 3 example, fetching starts with the `refresh()` right after initialization.

- If the SDK can't be loaded in step 1, `window.rcmr.checkoutPlus` doesn't exist, so you can't call `init()`, and `onError` is not called either.
- `invalid-cart` in step 2 arrives when the SDK fetches the cart and the value returned by `CartConnector` has the wrong shape. This isn't limited to the first fetch.
- `unavailable` in step 2 arrives when `getCart` throws an exception.
- `ui-failure` in step 4 arrives when an unexpected failure occurs inside the banner or the details dialog.
- `add-failed` in step 5 arrives when adding the Checkout+ product is rejected. `remove-failed` arrives when removing the Checkout+ product is rejected and the SDK could not confirm that it was removed.
- `callback-failure` arrives in any step when one of your callbacks (`onCheckoutPlusLinesChange`, etc.) throws an exception.

After Steps 1–5, complete the tasks in [Shopify store settings and before going live](./shopify-headless-go-live.html).

The code for each step is an example of how to integrate. What the SDK needs is the method calls described in each step and in the [Reference](./shopify-headless-reference.html); you don't need to write the code or split the files the same way as the examples. The examples are written in React (19 or later). With other frameworks, you call the same methods and follow the same rules. In the code, `cartConnector`, `reloadCart()`, `reportError()`, and `useCart()` are placeholder names for your logic. Also replace `getCheckoutUrl()` in step 5 ([Step 5: Replace the checkout button](./shopify-headless-checkout-button.html)) with your own logic. The full text of the examples is in [4. Sample code](./shopify-headless-sample-code.html).

## Preparation

### Before you start

| Task | Who | Details |
| --- | --- | --- |
| Set up returns and exchanges, and the send back method (Sagawa Express pickup, receiver-pay) | Your store's staff | ["Prerequisites" in What is Checkout+?](../checkout-plus/what-is-checkout-plus.html#prerequisites) |
| Set up the Checkout+ policy (benefits and the conditions for excluded products) | Your store's staff and Recustomer | [Checkout+ policy](../checkout-plus/policy.html) |
| Activate Checkout+ | Your store's staff | [Getting started](../checkout-plus/activation.html) |
| Publish the Checkout+ product to your sales channel (such as the Headless channel) | Recustomer | — |
| Make the Checkout+ metafield readable through the Storefront API | Recustomer | — |

- Charges begin on activation. If you don't want test orders to be charged, ask support to enable test mode before activation.

### Development Shopify store and store ID

Use a development Shopify store to check your integration. Install the Recustomer app on the development Shopify store and set up the Checkout+ benefit set. Recustomer sets the commitment charges according to your contract, and the benefits and exclusion tags are set in the Checkout+ policy in the Recustomer admin dashboard.

The store ID is an ID that Recustomer issues for each store (a UUID, in the form `0193f0c2-7a4e-7c1d-9b2a-5e8f3d6c1a90`), and one value identifies one store. It is shown in the Recustomer admin dashboard. You pass it as `storeId` to `init`. It isn't a secret, so you can put it in browser code. The development Shopify store and the production Shopify store are separate stores in Recustomer, so their store IDs differ too. Pass it from per-environment configuration (such as environment variables), depending on which store the build targets ([Step 3: Initialize the SDK](./shopify-headless-init.html)).

Recustomer writes the benefit set to the Checkout+ metafield ([Glossary](./overview.html#glossary)). The SDK user fetches it together with the cart from the Storefront API in `CartConnector`'s `getCart`, and passes it to the SDK ([Step 2: Implement `CartConnector`](./shopify-headless-cart-connector.html)). Recustomer will tell you the metafield namespace and key.

Your Recustomer contact will tell you how to prepare the development Shopify store.

Before you start the integration, open the SDK URL (`https://script.rcmr.io/checkout-plus/sdk/shopify-headless/v1/checkout-plus.js`) in a browser and confirm that its contents are displayed. You check the benefit set in `metafield` of the cart response in the Network tab of the developer tools after showing the banner in Step 4.

### Requirements

| Item | Details |
| --- | --- |
| Content Security Policy (CSP) | `https://script.rcmr.io` in `script-src`. If you show your store's images in the details dialog, the image origin in `img-src` |
| Supported currency | Carts in the same currency as the benefit set only (benefit sets are currently in Japanese yen only) |
| Storefront API permissions | Query with an access token that has the permission to read product tags (`unauthenticated_read_product_tags`) and the permission to read products (`unauthenticated_read_product_listings`, used to fetch the Checkout+ metafield) |
| Sales channel | The Checkout+ product is published to your sales channel (such as the Headless channel). Recustomer handles publishing |
| Storefront API version | A version Shopify currently supports |

- CSP: Needed only if you set a CSP. With a CSP that uses `'strict-dynamic'`, scripts without a nonce aren't loaded, so add a nonce attribute to the SDK script tag (with Next.js `Script`, pass `nonce`). For how to configure CSP, see [MDN's documentation](https://developer.mozilla.org/ja/docs/Web/HTTP/Guides/CSP).
- Supported currency: When the cart currency differs from the currency of the benefit set (currently, carts not in Japanese yen), the banner isn't shown. Shoppers can still purchase without Checkout+.
- Storefront API permissions: Besides fetching and updating the cart, your `CartConnector` implementation reads the handles and tags of products in the cart through the Storefront API. The cart and product permissions (`unauthenticated_read_checkouts`, `unauthenticated_write_checkouts`, `unauthenticated_read_product_listings`) are usually already granted for Shopify stores that implement a cart. The permission to read product tags (`unauthenticated_read_product_tags`) may not be granted, so check it. Without it, `getCart` fails and `unavailable` is delivered to `onError`. If your store uses the Headless channel, check the Storefront API access scopes in the Headless channel in the Shopify admin. In Shopify, the cart can be read and written without an access token, but product tags require one ([Storefront API authentication](https://shopify.dev/docs/api/storefront/latest#authentication)). Fetching the Checkout+ metafield also requires an access token and the product permission (`unauthenticated_read_product_listings`) (Shopify's [Shop](https://shopify.dev/docs/api/storefront/latest/objects/Shop)). If you handle the cart without an access token, make the Storefront API calls in your `CartConnector` implementation with an access token that has these permissions (`unauthenticated_read_product_tags` and `unauthenticated_read_product_listings`).
- Storefront API version: Requests to a version that is no longer supported are processed with the oldest supported version. You can check the version actually used in the `X-Shopify-API-Version` header of the Storefront API response ([Shopify API versioning](https://shopify.dev/docs/api/usage/versioning)). In supported versions, the Checkout+ product does not appear in product lists or search results.
