Checkout Guide
The checkout flow moves a customer from browsing to a submitted P21 order. The augur packages handle cart state, pricing, shipping, address validation, payment and order submission; your site wires them to its server actions.
Flow Overview
Add to Cart → Cart Page → Checkout → Order Submitted → Confirmation- Cart: lines live in augur-hooks' Zustand cart store, kept in step with the commerce service.
- Shipping: the customer enters a shipping address (checked with SmartyStreets) and picks a method from live rates.
- Billing and payment: a card through the hosted payment iframe, or invoice for approved accounts.
- Order submission: the order (lines, addresses, shipping, payment) goes to P21 through the commerce service.
- Confirmation: the customer sees the order on a thank-you page.
Cart Management
Initialization
useCartInitialization (augur-hooks) finds or creates the cart header in the commerce service and loads its lines into the store. Call it once in the root layout:
import { useCartInitialization } from "@simpleapps-com/augur-hooks";
import { useSession } from "next-auth/react";
import { cartHdrLookup, getCartLines } from "@/lib/actions/commerce";
const { data: session, status } = useSession();
useCartInitialization(
{ status, userId: session?.user?.id, cartHdrUid: session?.user?.cartHdrUid },
{ cartHdrLookup, getCartLines },
);cartHdrLookup(userId, cartToken) and getCartLines(cartHdrUid) are your server actions (over getSiteActions().commerce). A guest's cart token is a UUID kept in localStorage by default; getCartToken, saveCartToken and generateCartToken replace that.
Once initialized, the cart is available anywhere:
import { useCartHdrUid, useCartLines } from "@simpleapps-com/augur-hooks";
const cartHdrUid = useCartHdrUid();
const cartLines = useCartLines();Cart Actions
useCartActions changes the store at once and rolls back if the server action fails. Each server action resolves true on success and false on failure (Recipes):
import { useCartActions } from "@simpleapps-com/augur-hooks";
import { addToCart, deleteItemsFromCart, updateCartLines } from "@/lib/actions/commerce";
const { addToCart: add, updateQuantity, removeFromCart, clearCart } = useCartActions({
addToCart,
updateCartLines,
deleteItemsFromCart,
toast: { success: toast.success, error: toast.error },
});The checkout provider waits for any cart change in flight and refetches the cart before it builds the order, so the order holds what the server's cart holds.
Debounce quantity edits (useDebounce): update the field at once and send the change after a pause, so rapid clicks don't flood the server.
Cart Pricing
useCartPricing prices every line at its cart quantity, so quantity breaks apply. It is the one source of prices for the cart and checkout pages:
const { getUnitPrice, getItemTotal, subtotal, isLoading } = useCartPricing();
const unitPrice = getUnitPrice(line.itemId);The Checkout Provider
createCheckoutProvider (from @simpleapps-com/augur-hooks/checkout) holds the checkout's state (steps, addresses, carrier, payment, readiness) and submits the order. Create it once per site:
// lib/checkout.ts
import { createCheckoutProvider } from "@simpleapps-com/augur-hooks/checkout";
import { submitOrder, transactionSetup, cardInfo, validatePayment, accountQuery, billingUpdate } from "@/lib/actions/checkout";
export const { CheckoutProvider, useCheckout } = createCheckoutProvider({
shipping: { carriers: { "1": "Ground", "2": "Next Day" }, defaultCarrierId: "1" },
payments: { callbacks: { transactionSetup, cardInfo, validatePayment, accountQuery, billingUpdate } },
order: { successRedirect: "/checkout/thank-you", submitOrder },
});shipping: the carriers to offer, the default, and optional free-shipping thresholds by customer class.payments(optional; B2B sites without card processing leave it out): the server actionsusePaymentFlowcalls, invoice availability, allowed iframe origins.order: where to go after success,submitOrder(orderData, cartHdrUid), and optionalcalculateTaxandfetchClientIp.identity,ui.onError,customStateand lifecyclehooks(the hooks) cover the rest.
The provider builds the order payload with augur-utils' buildCheckoutPayload (oe_hdr and oe_line) unless hooks.assembleOrder replaces it, and gates the Process Order button with determineOrderReadiness unless hooks.isOrderReady overrides it.
Shipping
Address Validation
const result = await getSiteActions().smartyStreets.validateAddress({ address1, address2, city, state, postalCode });
// result.status: "success" | "not_found" | "error", with the corrected fieldsValidation is never cached. Validate on blur or after a short debounce, and offer the correction when augur-utils' isAddressDifferent says the result differs from what was entered.
Shipping Rates
const rates = await getSiteActions().shipping.getShippingRates(toAddress, weight, serviceCodes);The origin is the site config's shipping.fromAddress, and shipping.defaultServiceCodes filters the services when serviceCodes is left out. Your site decides which carriers to offer and any free-shipping rules.
Billing & Payment
Card payments run through the hosted iframe, so card data never touches your server. usePaymentFlow (inside the checkout provider) runs:
- Setup:
transactionSetupreturns the session and iframe URL; augur-web'sPaymentModalshows it. - Capture: the customer enters the card in the iframe.
- Card info and validation:
cardInfo, thenvalidatePaymentconfirm the card. - Account:
accountQueryreturns the payment account for the order, andbillingUpdaterecords the billing address.
Each step is a server action over getSiteActions().payments (transactionSetup, cardInfo, validatePayment, accountQuery, billingUpdate, getSurcharge). For invoice customers, billing is an address and PO number, with no payment processing.
Order Submission
submitOrder is your server action:
"use server";
import { getSiteActions, safeAction } from "@simpleapps-com/augur-server";
export async function submitOrder(orderData: Record<string, unknown>, cartHdrUid: number) {
return safeAction(() => getSiteActions().commerce.checkoutOrder(cartHdrUid, orderData));
}On success the provider clears the cart state, saves the confirmation in sessionStorage (ORDER_CONFIRMATION_KEY) and redirects to successRedirect. The commerce service creates the order in P21.
Server Actions
All checkout operations go through the site actions from augur-server:
const actions = getSiteActions();
actions.commerce; // cart lookups and lines, order submission
actions.shipping; // rate quotes
actions.payments; // card processing
actions.smartyStreets; // address validation
actions.pricing; // prices and tax estimatesClient Hooks
| Hook | Purpose |
|---|---|
useCartInitialization |
Find or create the cart and load its lines |
useCartActions |
Add, update, remove and clear, optimistically |
useCartHdrUid |
The cart header UID from the store |
useCartLines |
The cart lines from the store |
useCartPricing |
Prices for the cart with quantity breaks |
usePaymentFlow |
The card payment sequence |
useOrderReadiness |
Whether the order can be submitted, debounced |
useCheckoutPersistence |
Keeps checkout fields across reloads |
Typical Architecture
Client (React) Server (Next.js) Augur Services
───────────────── ──────────────── ──────────────
Zustand cart store ──actions──► commerce actions ──SDK calls──► Commerce API
useCartPricing ──proxy────► queryAction ──SDK calls──► Pricing API
Shipping step ──actions──► shipping actions ──SDK calls──► Logistics API
Address form ──actions──► smartyStreets ──SDK calls──► SmartyStreets API
PaymentModal ──actions──► payments actions ──SDK calls──► Payments API
Process Order ──actions──► checkoutOrder ──SDK calls──► Commerce API → P21