Cart and checkout

A cart addressed by its token, and checkout as a conversation with it: address, delivery, payment, coupon, gift card, paying on the gateway’s page.

The cart and the checkout belong to the storefront half: no API key is needed. A cart is protected by its own token — whoever holds it owns the cart.

The cart and the order go through the same code as the site’s pages: stock limits, quantity prices, weight, coupons, taxes, statuses, automations and the confirmation email all behave the same. An order placed through the API is marked with the source api.

The cart

POST   /api/v2/shop/carts                    create an empty one → 201
GET    /api/v2/shop/carts/{token}            read it
PATCH  /api/v2/shop/carts/{token}/items      set a product's quantity
DELETE /api/v2/shop/carts/{token}            empty it

Create

curl -s -X POST https://shop.example/api/v2/shop/carts
{ "data": { "token": "hW2wzXbz…UCLgGOUDBCJr", "currency": "USD",
            "items": [], "count": 0, "total": "0.00", "coupon": null } }

Keep the token — it is both the cart’s address and its password. An unknown token is 404 No such cart. (the API does not tell a stranger whether a cart exists). The cart is made on the storefront from X-Storefront.

Add, change, remove

One call for all of it: “how many of this product do you want”.

curl -s -X PATCH https://shop.example/api/v2/shop/carts/$TOKEN/items \
     -H 'Content-Type: application/json' -H 'Accept: application/json' \
     -d '{"product": "ST-1001", "quantity": 1}'
FieldWhat it is
productour id, the sku or the external_id of the product
quantityhow many, 0–9999; 0 removes the line
lineinstead of product: the line key from the answer — for a product that is in the cart configured two different ways
configthe line’s configuration (e.g. options: {"options": {…}}) — for a product with options; a plain product needs none

A hidden product can be added (it sells by a direct link); a disabled one cannot: 404 No such product.

The answer is the whole cart:

{
  "data": {
    "token": "hW2wzXbz…",
    "currency": "USD",
    "items": [
      {
        "line": "1",
        "product": { "id": 1, "sku": "ST-1001", "external_id": null,
                     "name": "Phone Alpha 256GB",
                     "url": "https://shop.example/api/v2/shop/products/1" },
        "quantity": 1,
        "unit_price": "999.00",
        "line_total": "999.00"
      }
    ],
    "count": 1,
    "total": "999.00",
    "coupon": null
  }
}
  • line is the line’s key; for a plain product it is the product’s id.
  • unit_price already reflects a quantity break the line has reached.
  • options (only on a configured product) — what is inside the line and already in its price: label, quantity, per_item, amount, product_id.
  • problem (only on a line with one) — why the line cannot be ordered as it stands: the product ran out, or less of it is left. Until that is put right the checkout will not go through.

You cannot add more than there is:

{ "message": "Only 1 of Phone Alpha 256GB is left in stock — the basket cannot hold more.",
  "errors": { "product": ["Only 1 of Phone Alpha 256GB is left in stock — the basket cannot hold more."] } }

Empty

DELETE /carts/{token} removes the products, the checkout answers and the coupon. The cart itself and its token stay.

Checkout

A browser answers every question on one page. A client cannot: to learn what delivery costs it first needs an address, and to learn the total it needs the chosen delivery. So checkout is a conversation with the cart: each answer is kept on it, and a client can stop and come back.

GET    /api/v2/shop/carts/{token}/checkout             what has been answered and what is on offer
PATCH  /api/v2/shop/carts/{token}/checkout             answer (merged with the earlier answers)
POST   /api/v2/shop/carts/{token}/checkout/complete    place the order

The order of things: customer and address → GET (delivery and payment on offer) → pick delivery and payment → GET (the total) → complete.

Answers

FieldWhat it is
customer_namethe recipient’s name
emailemail
phonephone, best in international form: +380675550128
countrycountry, two letters (UA)
citythe town’s name — printed on the order
place_idthe locality’s id from /places/suggest — delivery is priced by it (see Localities)
address_linestreet, house, flat
postal_codepostcode
notesa comment on the order
shipping_method_ida delivery option’s id from GET …/checkout (a string)
shipping_fieldswhat a delivery option asks on top of the address (a carrier’s branch and so on) — an object
payment_method_ida payment method’s id from GET …/checkout (a number)
codea coupon or a gift card code — one box, as on the site
remove_coupontrue — take the coupon off
remove_prepaida card_id from prepaid — take that gift card off

PATCH merges with the answers already given: the step that picks a delivery does not unanswer the address. A field sent as null is erased. Nothing is checked against what is on offer at this step — the answers only have to add up at complete.

curl -s -X PATCH https://shop.example/api/v2/shop/carts/$TOKEN/checkout \
     -H 'Content-Type: application/json' -H 'Accept: application/json' \
     -d '{"customer_name": "Ivan Petrenko", "email": "ivan@example.com",
          "phone": "+380675550128", "country": "UA", "city": "Kyiv",
          "place_id": 14008, "address_line": "1 Example St, flat 5"}'

What is on offer

Both PATCH and GET …/checkout answer with the checkout’s state for the answers given:

{
  "data": {
    "answers": { "customer_name": "Ivan Petrenko", "email": "ivan@example.com",
                 "city": "Kyiv", "place_id": 14008, "…": "…" },
    "delivery": [
      { "id": "1", "label": "Courier", "cost": "50.00", "delivery_time": "1–2 days",
        "needs_address": true, "needs_fields": false, "meta": {} },
      { "id": "2", "label": "Pickup", "cost": "0.00", "delivery_time": null,
        "needs_address": false, "needs_fields": false, "meta": {} },
      { "id": "7", "label": "Nova Poshta, branch", "cost": null, "delivery_time": "1–3 days",
        "needs_address": false, "needs_fields": true, "meta": {} }
    ],
    "payment": [
      { "id": 1, "label": "Card online", "charged_in": "UAH" },
      { "id": 2, "label": "Cash on delivery", "charged_in": "UAH" }
    ],
    "currency": "UAH",
    "subtotal": "999.00",
    "discount": "0.00",
    "discounts": [],
    "total": "1049.00",
    "prepaid": [],
    "to_pay": "1049.00"
  }
}
FieldWhat it is
delivery[]delivery options for the address given: without a place_id / country the list may be empty or incomplete
delivery[].costthe cost, a decimal string; null — the carrier named no price, the shop will work it out. Do not tell the customer “free”
delivery[].needs_addressfalse — collected in person, address_line is not required
delivery[].needs_fieldsthe option needs something more (a branch, a pickup point) — send it in shipping_fields
payment[]the payment methods available for this cart; charged_in — the currency the method will ask the bank for
subtotalthe goods
discountthe coupon’s discount
discounts[]every discount line by line: code (coupon or a module’s code, e.g. points), label, amount
totalthe total: goods after discounts + delivery + what modules add (a payment method’s surcharge and the like)
prepaid[]what is paid before any bank — gift cards: label, amount, card_id
to_paywhat is left to pay: total minus prepaid

Coupon and gift card

curl -s -X PATCH …/checkout -H 'Content-Type: application/json' -d '{"code": "WELCOME10"}'

The answer then carries code beside data — what the code box said:

{ "data": { "…": "…" }, "code": { "ok": true, "message": "Coupon applied." } }

ok: false — the code did not fit, the reason is in message. A gift card shows up in prepaid, a coupon in discounts.

Place the order

curl -s -X POST https://shop.example/api/v2/shop/carts/$TOKEN/checkout/complete \
     -H 'Accept: application/json'

Everything is derived again from the stored answers and nothing is trusted from the client: delivery is recomputed for the stored address, so an option that does not exist in that zone cannot be bought however the cart was patched.

Answers missing — 422 with the fields:

{ "message": "The checkout is not complete.",
  "errors": {
    "address_line": ["The address line field is required."],
    "shipping_method_id": ["The shipping method id field is required."],
    "payment_method_id": ["The payment method id field is required."]
  } }

An empty cart — 422 The cart is empty.

What is usually required (it depends on the shop’s settings):

  • customer_name — when the delivery goes to an address; for a pickup a phone is enough.
  • email and/or phone — which contact is required is the shop’s choice (“email”, “phone”, “either” or “both”). A phone is checked by its country’s rules.
  • address_line — when the chosen delivery option goes to an address.
  • city — when the goods have to be delivered (digital goods need none).
  • postal_code — when the shop asks for it.
  • shipping_method_id — one of those offered for this address.
  • payment_method_id — one of those offered.

Order placed — 201:

{
  "data": {
    "order": {
      "number": "TG-000123",
      "status": "awaiting_payment",
      "total": "1049.00",
      "currency": "UAH",
      "payment": {
        "state": "pending",
        "currency": "UAH",
        "amount": "1049.00",
        "prepaid": "0.00",
        "url": "https://pay.example/checkout/…"
      }
    }
  }
}
FieldWhat it is
numberthe order number — tell it to the customer; the admin finds the order by it, and so does /admin/orders/{number}
statusthe order’s status; an unpaid online order waits for payment and holds no stock
payment.statethe payment state (pending, authorized, paid, …) — an axis separate from the status
payment.amountwhat the bank will be asked for (after a gift card), in payment.currency
payment.prepaidwhat a gift card paid
payment.urlthe gateway’s page a browser would have been sent to; null — the method takes no money online

A card is paid only on the gateway’s page. Open payment.url in a browser or a web view; the gateway tells the shop about the payment itself. Learn the result from the order’s state (admin half), or show the customer the shop’s page the gateway brings them back to.

After complete the cart is emptied, but its token still points at it — it can be used for the next order.

What the API cannot place yet

  • An order on behalf of a company (the company fields exist only in the site’s form).
  • Answers to the shop’s own checkout questions (the form’s extra fields).
  • An order signed in to a customer’s account: the API places orders as a guest.

Such orders are placed on the site, or by a manager in the admin.