Quickstart
This page takes you from a token to a placed test order with @shoppable/checkout, the
Shoppable checkout SDK (software development kit). Your site shows the cart and the checkout
form; Shoppable is the merchant of record, so it prices the cart, takes payment and places the
order with each retailer.
-
Get a test token
Copy the test publishable token from the Developers page of the Shoppable customer portal. Test tokens start with
pk_test_and work onlocalhost, so you can start right away. Get your keys covers where tokens come from and which domains each one works on. -
Install
Terminal window npm install https://developer.shoppable.com/sdk/shoppable-checkout-0.2.1.tgz @stripe/stripe-js@^7 -
Create a client
import { ShoppableCheckout, cheapestOffer } from "@shoppable/checkout";const client = new ShoppableCheckout({ token: "pk_test_..." });const config = await client.getCartConfiguration();getCartConfigurationchecks the token against your page’s domain, starts the session, and returns the cart’s settings:currency,country,stripePublishableKey,isTestModeand your Cart Builder branding. -
Look up products
const [product] = await client.lookupProducts(["00012345678905"]);Each product lists
availability: one offer per retailer that can sell it on your cart, with that retailer’s price and shipping terms. Products are keyed by UPC (Universal Product Code); useproduct.upcexactly as it comes back when you add the product to the cart. -
Pick an offer
const offer = cheapestOffer(product);if (!offer) throw new Error("No retailer sells this product on your cart.");cheapestOfferpicks the lowest sale price. You can let shoppers choose a retailer instead; see Choosing a retailer. -
Price the cart
const priced = await client.prepareCheckout({items: [{ upc: product.upc, merchantId: offer.merchantId, quantity: 1 }],});You send what the shopper chose. The server returns a
PricedCheckoutwith every amount computed from its own data. -
Show the server’s totals
const money = new Intl.NumberFormat(config.language, {style: "currency",currency: config.currency,});console.log("Subtotal", money.format(priced.subTotal));console.log("Shipping", money.format(priced.shippingTotal));console.log("Discounts", money.format(priced.discountTotal));console.log("Estimated tax", money.format(priced.estimatedTax));console.log("Total", money.format(priced.total));Display these numbers as they are. Never add up prices in the browser; when the cart changes, price it again. Cart and pricing explains every field.
-
Take payment and place the order
Mount Stripe’s Address Element and Payment Element with the cart’s Stripe key. Your page needs an email field and two empty containers:
<input id="email">,<div id="address">and<div id="payment">.import { loadStripe } from "@stripe/stripe-js";import { toStripeShipping } from "@shoppable/checkout";if (!config.stripePublishableKey) throw new Error("This cart has no payment key yet.");const stripe = await loadStripe(config.stripePublishableKey);if (!stripe) throw new Error("Stripe.js failed to load.");const elements = stripe.elements({mode: "payment",amount: Math.round(priced.total * 100),currency: config.currency,captureMethod: "manual",});const addressElement = elements.create("address", {mode: "shipping",fields: { phone: "always" },validation: { phone: { required: "always" } },});addressElement.mount("#address");elements.create("payment").mount("#payment");When the shopper clicks Place order, save their email and place the order in one call:
const { error } = await elements.submit();if (error) return; // Stripe shows the problem next to the fieldconst { value } = await addressElement.getValue();const shipping = toStripeShipping(value);const email = (document.querySelector("#email") as HTMLInputElement).value;await client.updateCheckout(priced._id, { email });const order = await client.placeOrder(priced._id, {confirmPayment: (clientSecret) =>stripe.confirmPayment({elements,clientSecret,confirmParams: {return_url: window.location.href,shipping,payment_method_data: {billing_details: { name: shipping.name, email, phone: shipping.phone, address: shipping.address },},},redirect: "if_required",}),});console.log("Order placed:", order.orderNumber);toStripeShippingturns the Address Element value into theshippingStripe expects; the order is refused without it. Payments adds address validation, wallets and error handling.Pay with the test card
4242 4242 4242 4242, any future expiry date and any CVC (card verification code). Test mode places no real orders.