# prepareCheckout: Check before proceeding to checkout

> STEP 3-1

```ts
prepareCheckout(): Promise<CheckoutPreparation>
```

Call this when the checkout button is pressed. The SDK adds (or removes) the Checkout+ product in the cart to match the shopper's selection (whether to add the Checkout+ product). When it completes, fetch the cart's `checkoutUrl` at that moment and navigate to it ([Step 5: Replace the checkout button](./shopify-headless-checkout-button.html)). **It does not return a decision on whether to proceed to checkout.**

![Diagram with Japanese labels. Flow of prepareCheckout(): 1 fetch, 2 decide, 3 update, 4 verify, 5 complete](images/checkout-plus-sdk_reference-1.svg)

Flow of `prepareCheckout()`. Even if verification fails, it does not stop and moves on to 5.

## Exceptions and failures

- Exceptions: The returned Promise never rejects. In every case, it completes with `{ attached }` within 10 seconds.
- When the Checkout+ product can't be added, the result can't be verified, or 10 seconds pass: It completes with `{ attached: false }`. Checkout is not blocked, and you do not need to show the shopper any special message.
- After `destroy()`: It completes immediately with `{ attached: false }`.

No reason (`reason`) is returned. Recustomer investigates the cause. Failure notifications are described in [`onError`](./shopify-headless-reference-on-error.html). For the cases where the SDK does not offer Checkout+, see [What the SDK does for each trigger](./shopify-headless-behavior.html#what-the-sdk-does-for-each-trigger).

## Return value (CheckoutPreparation)

| Field | Type | Description |
| --- | --- | --- |
| `attached` | `boolean` | `true` means the SDK confirmed that the Checkout+ product was added. `false` means it was not added, or could not be confirmed |

- Even when `attached` is `false`, the product may actually have been added, because cart updates in progress are not aborted after 10 seconds.
- You can use `attached` for logging, but you do not have to. Recustomer calculates the share of checkouts with Checkout+ from orders.

## Specification

- The SDK fetches the cart and the benefit set again at the moment the checkout button is pressed and determines the range and the price at that moment. If the range is the same as the Checkout+ product already in the cart, that line stays as is. If the range changed (for example, the cart was changed in another tab, or the benefit set was updated), it removes any leftover Checkout+ product and, as selected, adds the Checkout+ product for the current range. It requests the removal and the addition in a single call to [`updateCheckoutPlusLines`](./shopify-headless-reference-update-checkout-plus-lines.html). This is the same as the theme app embed.
- If the cart is no longer offered Checkout+ at that moment (for example, a product was removed in another tab, or only excluded products remain), the SDK removes any leftover Checkout+ product and completes with `{ attached: false }` without adding one.
- What the SDK does when `updateCheckoutPlusLines` returns `'rejected'` (whether it retries, and what it reports through `onError`) is described in [When it returns `'rejected'`](./shopify-headless-reference-update-checkout-plus-lines.html#when-it-returns-rejected).
- When 10 seconds pass, `prepareCheckout()` stops waiting and completes. After the timeout, no new cart updates start. A Checkout+ product added afterward is removed when the shopper comes back from checkout (⑩ and ⑫ in [What the SDK does for each trigger](./shopify-headless-behavior.html#what-the-sdk-does-for-each-trigger)).
- After adding, the SDK does not check again whether the range changed. Even if adding the Checkout+ product triggers an automatic discount and changes the range, the added line stays and checkout proceeds. This is the same as the theme app embed. In Shopify Plus stores with the Checkout+ block in checkout, the block adjusts the range.
- If it is called a second time before it completes, it returns the same Promise as the first call. The same cart update is never run twice.
- If there is no cart, it completes immediately with `{ attached: false }`. If the benefit set (the commitment charge and benefits for each range) cannot be fetched, it completes without adding the Checkout+ product, and removes any Checkout+ product left in the cart.
