Skip to content

A Checkout That Can't Charge the Wrong Price

Every shop has the same three bugs waiting in its cart. A price changes while a shopper browses, and the order is placed at the old one. Someone edits the price in a request from their browser. A slow network makes the shopper press "Pay" twice, and two orders arrive. This post shows a checkout where none of them can happen, with ObjectsGrid Carts.

A cart holds intentions, not prices

A cart line names an Offer and a quantity. Nothing else:

{
  "seller": {"at_id": "https://apis.objectsgrid.com/organizations/<shop>"},
  "cartItem": [
    {"offer": {"at_id": "https://apis.objectsgrid.com/offers/<trail-shoe>"}, "quantity": 2},
    {"offer": {"at_id": "https://apis.objectsgrid.com/offers/<socks>"}}
  ]
}

When you read one cart, ObjectsGrid reads each line's Offer and adds what it costs now:

"cartItem": [
  {"offer": {"at_id": "…/offers/<trail-shoe>"}, "quantity": 2, "price": 89.90, "priceCurrency": "EUR"},
  {"offer": {"at_id": "…/offers/<socks>"}, "price": 9.90, "priceCurrency": "EUR"}
],
"totalPrice": [{"at_type": "MonetaryAmount", "currency": "EUR", "value": 189.70}]

The prices are computed on every read and never stored. A price a browser sends back is dropped. A cart can't go stale and can't be edited into a discount: it always shows today's catalog. Totals are summed per currency, exactly, so 3 × 19.99 is 59.97 and not 59.970000000000006.

The checkout keeps the price the shopper saw

Your server checks the cart out, adding what the order needs that a cart doesn't have:

curl -X POST https://apis.objectsgrid.com/carts/<id>/checkout \
  -H 'X-ObjectsGrid-AccessPoint: EU' -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
  -d '{"orderNumber": "A-1001",
       "billingAddress": {"streetAddress": "Teststr. 1", "postalCode": "10115", "addressCountry": "DE"}}'

The answer is the new Order, 201. Its lines are the cart's, and its acceptedOffer are the Offers at the version it read:

"acceptedOffer": [{"at_id": "https://apis.objectsgrid.com/offers/<trail-shoe>?version=1764600112233445"}]

That version of the Offer stays readable, with its price, after the Offer changes or is deleted. Months later, "what did this customer pay per item?" is one read, not a reconstruction from logs. See an order that remembers the customer as they were for how pinned versions work.

Lines that can't be sold are refused

Before it writes anything, the checkout checks every line, and refuses the whole checkout with 409 when one can't be ordered:

  • the Offer is gone;
  • it's past its validThrough;
  • its availability is OutOfStock, Discontinued or SoldOut;
  • the Product's Inventories hold less than the quantity.

The message names each line, such as /cartItem/1: only 1 in stock, so your page can point at it. Nothing is ordered, and the cart can still change. A product without an Inventory isn't checked, so you add stock tracking when you need it.

A retried checkout is safe

The order's id is the cart's id. After a successful checkout the cart is marked checkedOut, points to its order, and can't change. Checking it out again answers the same order with 200. So a retry after a timeout, a double click or a crashed worker finds the order instead of creating a second one.

Who does what

  • The browser keeps the cart. A new Organization Account's default policy lets a public API key create carts and read and change one by its id. Keep the id in the visitor's browser. Anonymous callers can't list carts or check them out.
  • Your server checks out, with its service account, once the payment provider confirms the payment, then records the payment.
  • A cart expires 30 days after its last change, unless expires says otherwise.

Discount codes are passed from the cart to the order, not applied: working out a promotion is still your app's job. The Carts and checkout guide has the full API.