SDK states and transitions
This page explains when and what the SDK does in response to shopper actions and cart changes. All of this happens inside the SDK, and the SDK user's code stays as described in the integration steps. Terms such as benefit set (the commitment charge and benefits for each range), range, and price are in the Glossary of How it works.
Who drives what
| Who | What they do | Methods and functions |
|---|---|---|
| SDK user's code | Initializes the SDK, once per page | init() |
| SDK user's code | Passes the element to place the banner in | mount() |
| SDK user's code | Tells the SDK right after initialization and after changing the cart. Coming back from checkout with Back and the page reloading also counts as right after initialization, because it starts over from initialization | refresh() |
| SDK user's code | When the checkout button is pressed, asks the SDK to prepare for checkout | prepareCheckout() |
| SDK user's code | Shuts down the SDK | destroy() |
| The SDK | Fetches the cart and the benefit set, and decides whether to show the banner | CartConnector's getCart |
| The SDK | Adds and removes the Checkout+ product | CartConnector's updateCheckoutPlusLines |
| The SDK | Notices when the previous screen is shown as is, fetches the cart again, and removes the leftover Checkout+ product | — |
| The shopper | Checks or unchecks the box. Presses the checkout button. Comes back from checkout with Back | — |
How the benefit set reaches the SDK
How the benefit set reaches the SDK. The SDK calculates the price and decides whether to add or remove the Checkout+ product.
When you set the benefit set in the Recustomer admin dashboard, it is written to the Shopify store's Checkout+ metafield. Each time the SDK fetches the cart, it also receives this metafield with the cart through getCart (getCart return value). The SDK user's code only passes the metafield as fetched, without reading its contents.
State diagram
A CheckoutPlusSession changes as follows. Each box in the diagram is what the SDK is doing at that time. There is no method to read the SDK's state.
How the SDK changes, and what triggers each change.
What the SDK does for each trigger
| State diagram | Trigger | Condition | Cart | Banner | onError | prepareCheckout() result |
|---|---|---|---|---|---|---|
| — | init() | storeId or cart is invalid (conditions) | Not changed | Not shown | invalid-options | Completes immediately with { attached: false } |
| ② | Fetching the cart and the benefit set | A cart that can be offered Checkout+ | On the first fetch, removes any leftover Checkout+ product | Calculates the price. Draws it if there is a target (the element passed to mount()) | — | — |
| ③ | Fetching the cart and the benefit set | Acceptance is suspended, or the cart currency differs from the benefit set's currency | On the first fetch, removes any leftover Checkout+ product | Not shown | acceptance-suspended, currency-not-offered | — |
| ③ | Fetching the cart and the benefit set | A cart with only excluded products, the amount excluding excluded products is 0, no matching range, or no cart (getCart returns null) | On the first fetch, removes any leftover Checkout+ product | Not shown | — | — |
| ④ | Fetching the cart and the benefit set | The cart was fetched, but there is no benefit set, or it can't be read | On the first fetch, removes any leftover Checkout+ product | Not shown. Removed if shown | not-published, invalid-benefit-set | — |
| ④ | Fetching the cart and the benefit set | The cart cannot be fetched, or the cart values cannot be read | Unchanged. The cart is unknown, so leftover Checkout+ products are not removed either | Not shown. Removed if shown | unavailable, invalid-cart | — |
| — | mount() | A cart that can be offered Checkout+ | — | Draws the price | — | — |
| — | mount() or refresh() | After the benefit set or the cart could not be fetched | — | Not shown | — | — |
| — | The shopper checks or unchecks the box | A cart that can be offered Checkout+ | Unchanged. Only records the shopper's selection (whether to add the Checkout+ product) | Toggles the checkbox | — | — |
| ⑦⑧ | The checkout button (prepareCheckout()) | A cart that can be offered Checkout+ | Determines the range again from the cart at that moment, adds or removes the Checkout+ product as selected, then fetches the cart again to verify | — | add-failed, remove-failed (When it returns 'rejected') | Completes with { attached } when verification finishes or 10 seconds pass |
| ⑦⑧ | The checkout button (prepareCheckout()) | When the button was pressed, the cart was no longer offered Checkout+ (a product was removed in the other tab, only excluded products remain, and so on) | Removes any leftover Checkout+ product and adds nothing | — | remove-failed (When it returns 'rejected') | Completes with { attached: false } |
| ⑨ | The checkout button (prepareCheckout()) | A cart that is not offered Checkout+, or after the benefit set or the cart could not be fetched | Doesn't add the Checkout+ product. If the cart was fetched, removes any leftover Checkout+ product | — | remove-failed (When it returns 'rejected') | Completes with { attached: false } |
| ⑩ | The shopper comes back from checkout with the browser's Back button, and the previous screen is shown as is | The benefit set and the cart had been fetched | Fetches the cart again and removes the leftover Checkout+ product | Returns the checkbox to its initial state. If the shopper had unchecked it, it stays unchecked | — | — |
| ⑪ | The shopper comes back from checkout with the browser's Back button, and the previous screen is shown as is | After the benefit set or the cart could not be fetched | — | Not shown | — | — |
| ⑫ | The shopper comes back from checkout with the browser's Back button, and the page reloads | — | Starts over from initialization on the new page. Removes the leftover Checkout+ product on the first mount() or refresh() that the SDK user's code calls | — | — | — |
| — | An unexpected failure occurs inside the UI components (the banner or the details dialog) | — | — | Unmounts all of them, the same as destroy() | ui-failure | If in progress, completes immediately with { attached: false } |
| — | destroy() | — | — | Removes all of them | — | If in progress, completes immediately with { attached: false } |
- Whether the SDK goes to ② or ③ in the diagram depends on whether any condition in the two ③ rows of the table applies. If one does, it goes to ③ and does not offer a price.
- The SDK decides which products are excluded products by comparing the product tags that
getCartreturns (tags) with the exclusion tags. - A cart with both eligible and excluded products goes to ②, and the banner's notice shows the excluded product names. How the range is determined is described under "Range" in the Glossary.
- The SDK fetches the cart and the benefit set on the first
mount(),refresh(), orprepareCheckout()(① in the diagram), on eachrefresh()after that (⑤ and ⑥), on eachprepareCheckout()after that (⑦), and when the previous screen is shown as is (⑩). The SDK user's code callsrefresh()right after initialization and after changing the cart. It receives the benefit set again together with the cart (How the benefit set reaches the SDK). Whenrefresh()changes the price, the selection follows Selection when the displayed price changes. refresh()can be called any time, any number of times (refresh: Call it any time, any number of times).- After the benefit set or the cart could not be fetched, the banner is shown again only when the page is reloaded, or after
destroy()and a freshinit(). - Whenever the SDK adds or removes the Checkout+ product, in any row of the table,
onCheckoutPlusLinesChangeis called. - The checkout button adds the Checkout+ product when the box is checked. A box the shopper checked stays checked after the banner is removed (for example, after the cart drawer closes). If the box is checked because
initialSelectionistrue, the product is added only after the banner has been drawn (initialSelection: Initial state of the checkbox). - After
prepareCheckout()completes, if the SDK user's code getscheckoutUrland navigates, the shopper moves to Shopify checkout. If it can't getcheckoutUrl, the shopper stays on the current page and can press the checkout button again.
Behavior in each situation
- On every page load, the checkbox returns to its initial state. For stores that set
initialSelectiontotruewhere the shopper unchecked the box, seeinitialSelection: Initial state of the checkbox. On SPA navigation, theCheckoutPlusSessionstays, so the checkbox stays too. - In the cart, the SDK changes only the Checkout+ product line. It doesn't change regular product lines.
- Even if verification fails in step 4 of the diagram above (for example, the cart changed midway), checkout is not blocked.
- When the same cart is open in two tabs, the SDK also decides based on the cart at the moment the checkout button is pressed (⑦⑧ in the table). Checkout is not blocked.
What the SDK does in each situation is in the table in What the SDK does for each trigger. How Checkout+ is handled inside checkout is in How it works on the checkout page.