Docs
STEP 1-1

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

WhoWhat they doMethods and functions
SDK user's codeInitializes the SDK, once per pageinit()
SDK user's codePasses the element to place the banner inmount()
SDK user's codeTells 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 initializationrefresh()
SDK user's codeWhen the checkout button is pressed, asks the SDK to prepare for checkoutprepareCheckout()
SDK user's codeShuts down the SDKdestroy()
The SDKFetches the cart and the benefit set, and decides whether to show the bannerCartConnector's getCart
The SDKAdds and removes the Checkout+ productCartConnector's updateCheckoutPlusLines
The SDKNotices when the previous screen is shown as is, fetches the cart again, and removes the leftover Checkout+ product—
The shopperChecks or unchecks the box. Presses the checkout button. Comes back from checkout with Back—

How the benefit set reaches the SDK

Diagram with Japanese labels. Diagram: the benefit set set in the Recustomer admin dashboard is placed in the Shopify store's Checkout+ metafield, the SDK user's getCart fetches it with the cart and passes it to the SDK, and the SDK calculates the price and shows it in the banner

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.

Diagram with Japanese labels. Diagram of how the SDK changes (after init(), fetching the cart and checking it against the benefit set, a price can be offered, no price is offered, after a failed fetch, preparing checkout, and Shopify checkout) and the triggers for each change. When the shopper comes back from checkout with Back, it returns to the check if the previous screen is shown as is, or to after init() if the page reloads

How the SDK changes, and what triggers each change.

What the SDK does for each trigger

State diagramTriggerConditionCartBanneronErrorprepareCheckout() result
—init()storeId or cart is invalid (conditions)Not changedNot showninvalid-optionsCompletes immediately with { attached: false }
②Fetching the cart and the benefit setA cart that can be offered Checkout+On the first fetch, removes any leftover Checkout+ productCalculates the price. Draws it if there is a target (the element passed to mount())——
③Fetching the cart and the benefit setAcceptance is suspended, or the cart currency differs from the benefit set's currencyOn the first fetch, removes any leftover Checkout+ productNot shownacceptance-suspended, currency-not-offered—
③Fetching the cart and the benefit setA 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+ productNot shown——
④Fetching the cart and the benefit setThe cart was fetched, but there is no benefit set, or it can't be readOn the first fetch, removes any leftover Checkout+ productNot shown. Removed if shownnot-published, invalid-benefit-set—
④Fetching the cart and the benefit setThe cart cannot be fetched, or the cart values cannot be readUnchanged. The cart is unknown, so leftover Checkout+ products are not removed eitherNot shown. Removed if shownunavailable, 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 boxA 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 fetchedDoesn'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 isThe benefit set and the cart had been fetchedFetches the cart again and removes the leftover Checkout+ productReturns 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 isAfter 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-failureIf 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 getCart returns (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(), or prepareCheckout() (① in the diagram), on each refresh() after that (⑤ and ⑥), on each prepareCheckout() after that (⑦), and when the previous screen is shown as is (⑩). The SDK user's code calls refresh() 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). When refresh() 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 fresh init().
  • Whenever the SDK adds or removes the Checkout+ product, in any row of the table, onCheckoutPlusLinesChange is 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 initialSelection is true, 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 gets checkoutUrl and navigates, the shopper moves to Shopify checkout. If it can't get checkoutUrl, the shopper stays on the current page and can press the checkout button again.

Behavior in each situation

Diagram with Japanese labels. Shopper actions and SDK behavior: 1 open the cart, 2 check the box, 3 change the cart, 4 proceed to checkout, 5 come back from checkout with Back

  • On every page load, the checkbox returns to its initial state. For stores that set initialSelection to true where the shopper unchecked the box, see initialSelection: Initial state of the checkbox. On SPA navigation, the CheckoutPlusSession stays, 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.

© Recustomer, Inc.