Skip to content

Checkout

Take a customer from a cart to a paid order, confirming the payment with Stripe.js in your own page.

Updated

A purchase is a cart the customer fills, presents for payment and pays in your page, with Stripe.js. Every step needs a signed-in customer whose token holds checkout:write (Authentication); reading carts needs read. The examples use the client from Your own front end.

  1. Open a cart

    POST /v1/stores/{store}/checkouts opens one in a currency, the currency of the offers you show.

    curl --request POST 'https://api.sherpo.io/customer/v1/stores/yourstore.sherpo.io/checkouts' \
      --header 'Authorization: Bearer ctk_...' \
      --header 'Content-Type: application/json' \
      --data '{ "currency": "eur" }'
  2. Put prices in it

    PUT /v1/stores/{store}/checkouts/{checkout}/items sets a line: the offer’s id as the price, and a quantity (1 to 99; 0 takes the line out). Each line is priced at the offer the customer gets now.

    curl --request PUT 'https://api.sherpo.io/customer/v1/stores/yourstore.sherpo.io/checkouts/<checkout id>/items' \
      --header 'Authorization: Bearer ctk_...' \
      --header 'Content-Type: application/json' \
      --data '{ "price": { "id": "<price id>" }, "quantity": 1 }'

    A tip or a donation also takes the display_name it’s left under (up to 20 characters) and a message (up to 255), which null clears; the order’s line keeps them.

  3. Apply a coupon

    PUT /v1/stores/{store}/checkouts/{checkout}/coupon names one as its code: a discount’s code, or its id, as an affiliate link carries it. null takes it off.

    curl --request PUT 'https://api.sherpo.io/customer/v1/stores/yourstore.sherpo.io/checkouts/<checkout id>/coupon' \
      --header 'Authorization: Bearer ctk_...' \
      --header 'Content-Type: application/json' \
      --data '{ "code": "WELCOME10" }'
  4. Present it for payment

    POST /v1/stores/{store}/checkouts/{checkout}/freeze checks every line still prices as it did, and the cart stops changing.

  5. Start paying

    POST /v1/stores/{store}/checkouts/{checkout}/pay answers what your page confirms with Stripe, or the order when nothing is due.

  6. Confirm in your page, then here

    Confirm the payment with Stripe.js, then call POST /v1/stores/{store}/checkouts/{checkout}/confirm, which reads it back from Stripe and answers the order once it’s paid.

Opening a cart, setting a line or a coupon, freezing it and abandoning it each answer the cart as it stands; pay and confirm answer the payment’s state instead (below):

setCheckoutItem 200json
{
  "id": "<checkout id>",
  "status": "open",
  "currency": "eur",
  "exponent": 2,
  "items": [
    {
      "id": "<line id>",
      "product_id": "<product id>",
      "price_id": "<price id>",
      "product": { "id": "<product id>", "slug": "intro-course", "title": "Intro course", "category": "course", "deleted": false },
      "quantity": 1,
      "amount": 4900,
      "discount": 490,
      "discount_id": "<discount id>",
      "total": 4410,
      "mode": "purchase",
      "autorenew": null,
      "display_name": null,
      "message": null,
      "refund_policy": "30d",
      "refund_until": "2026-12-03T10:00:00.000Z"
    }
  ],
  "totals": { "amount": 4900, "discount": 490, "total": 4410 },
  "coupon": "WELCOME10",
  "refund_consent": null,
  "refund_waiver": null,
  "card_required": null,
  "under_minimum": null,
  "order": null,
  "expires_at": "2026-11-03T11:00:00.000Z"
}

A cart is open while it changes, frozen once presented for payment, paid once its order exists (order, { id, number }), and abandoned or expired otherwise. A line’s mode is purchase, upgrade (a higher tier of what the customer holds) or renewal. A line’s refund_policy is the refund window it sells under as the store sets it now, a frozen cart included (none, 24h, 72h, 7d, 14d or 30d; the price’s own, else its product’s; null when neither sets one), and refund_until, while the cart is open or frozen, is until when a purchase made now may be refunded (null for none, no policy, or a cart that can no longer be paid). Show it as is: it moves on each time you read the cart. A frozen cart keeps the policy the customer consented to in refund_waiver.

When a line’s refund policy is shorter than the customer’s statutory window, the customer must consent to it in your page. An open cart says so ahead in refund_consent, the policy to consent to (none, 24h, 72h or 7d; null when none is asked), so your page asks before freezing. Send their consent as refund_waiver, the policy they consented to (true is taken too):

curl --request POST 'https://api.sherpo.io/customer/v1/stores/yourstore.sherpo.io/checkouts/<checkout id>/freeze' \
  --header 'Authorization: Bearer ctk_...' \
  --header 'Content-Type: application/json' \
  --data '{ "refund_waiver": "24h" }'

Without it, or with a policy other than the one asked now, the freeze answers 422 invalid at ["args", "refund_waiver"], naming the policy: read the cart and ask again. A frozen cart keeps the consent as refund_waiver, with when it was given.

An app the store hasn’t verified is refused a cart that asks for this consent, and any refund_waiver, as 403 not_permitted (principal): abandon the cart and send the customer to the product’s page on the store’s own site, where they consent themselves.

Paying

pay answers one of these, by status:

status Do
paid Nothing is due: show the order.
requires_payment Confirm the payment with Stripe.js, then call confirm.
processing The payment is settling: call confirm again later.
requires_setup Nothing is charged now, but a plan in the cart will charge its first renewal: save a card with Stripe.js’s confirmSetup, then call confirm.
setup_processing Stripe is checking the card: call confirm again later.
cancelled The payment was called off.
refunded, refund_pending, refund_failed The payment was given back, is being given back, or couldn’t be (the store settles it with the customer).
payCheckout 200json
{
  "status": "requires_payment",
  "payment": {
    "processor": "stripe",
    "client_secret": "pi_..._secret_...",
    "publishable_key": "pk_live_...",
    "account": "acct_...",
    "instrument": null,
    "confirm_by": "2026-11-03T11:15:00.000Z",
    "amount": 4410,
    "currency": "eur",
    "exponent": 2
  }
}

Load Stripe.js with the payment’s publishable_key, its account as the connected account, and its client_secret. showOrder, showWaiting, showProblem and the other show functions stand for your page’s own:

import { loadStripe, type Stripe, type StripeElements } from '@stripe/stripe-js';
import { Problem, sherpo } from './sherpo';

type Payment = {
  client_secret: string;
  publishable_key: string;
  account: string;
  instrument: string | null;
  confirm_by: string;
};
type Pay =
  | { status: 'paid'; order: { id: string; number: number } }
  | { status: 'requires_payment' | 'processing'; payment: Payment & { amount: number; currency: string; exponent: number | null } }
  | { status: 'requires_setup' | 'setup_processing'; setup: Payment }
  | { status: 'cancelled' | 'refunded' | 'refund_pending' | 'refund_failed' };

/** Starts paying a frozen cart and mounts the payment form when there is something to confirm. */
export async function startPaying(checkout: string, token: string) {
  const answer = await sherpo<Pay>(`/checkouts/${checkout}/pay`, { token, method: 'POST', body: {} });
  if (answer.status !== 'requires_payment' && answer.status !== 'requires_setup') return show(answer);
  const { publishable_key, account, client_secret, confirm_by } = 'payment' in answer ? answer.payment : answer.setup;
  const stripe = await loadStripe(publishable_key, { stripeAccount: account });
  if (stripe === null) throw new Error('Stripe.js could not load.');
  const elements = stripe.elements({ clientSecret: client_secret });
  elements.create('payment').mount('#payment');
  return { stripe, elements, setup: answer.status === 'requires_setup', confirm_by };
}

type Form = { stripe: Stripe; elements: StripeElements; setup: boolean; confirm_by: string };

/** When the customer presses Pay: confirms with Stripe, then reads the outcome back. */
export async function submitPayment(checkout: string, token: string, { stripe, elements, setup, confirm_by }: Form) {
  const confirmParams = { return_url: 'https://www.example.com/checkout/return' };
  const { error } = setup
    ? await stripe.confirmSetup({ elements, redirect: 'if_required', confirmParams })
    : await stripe.confirmPayment({ elements, redirect: 'if_required', confirmParams });
  // A card the customer must fix stays here; any other error may hide a payment that went through.
  if (error?.type === 'card_error' || error?.type === 'validation_error') return showProblem(error.message);
  let answer: Pay;
  try {
    answer = await sherpo<Pay>(`/checkouts/${checkout}/confirm`, { token, method: 'POST', body: {} });
  } catch (failed) {
    return recover(checkout, token, failed, confirm_by);
  }
  return show(answer);
}

/** After a failed `confirm`: the payment may have gone through, so read the cart back before offering to pay again. */
async function recover(checkout: string, token: string, failed: unknown, confirmBy: string) {
  const cart = await sherpo<{ status: string; order: { id: string; number: number } | null }>(
    `/checkouts/${checkout}`,
    { token },
  ).catch((readBack: unknown) => {
    const said = failed instanceof Error ? failed.message : String(failed);
    throw new AggregateError([failed, readBack], `The payment and the cart could not be read: ${said}`);
  });
  if (cart.order !== null) return showOrder(cart.order);
  // A refusal stays refused, as does a fault of the API's own; anything else may pass.
  const fault = failed instanceof Problem && String(failed.body.type).endsWith('#client-fault');
  const passing =
    !(failed instanceof Problem) || (!fault && (failed.status < 400 || failed.status === 429 || failed.status >= 500));
  // `confirm` still answers on an expired cart until `confirm_by`, so a settling payment is worth waiting on until then.
  const open = cart.status === 'frozen' || (cart.status === 'expired' && Date.now() < Date.parse(confirmBy));
  if (open && passing) return showWaiting(confirmBy);
  throw failed;
}

/** Shows what `pay` or `confirm` answered. */
function show(answer: Pay) {
  switch (answer.status) {
    case 'paid':
      return showOrder(answer.order);
    case 'processing':
    case 'setup_processing':
      // Settling: call `confirm` again in a while.
      return showWaiting('payment' in answer ? answer.payment.confirm_by : answer.setup.confirm_by);
    case 'requires_payment':
    case 'requires_setup':
      return showProblem('Your payment didn\'t go through. Try again, or use another method.');
    default:
      // Called off or given back: the cart can't be paid any more.
      return showClosed(answer.status);
  }
}

confirm reads the payment back from Stripe, never from your page, and answers paid with the order once it went through, processing while it settles, or requires_payment when the customer has to try again. A method that redirects (a bank’s page) comes back to your return_url: call confirm there.

For requires_setup, the snippet confirms with stripe.confirmSetup and the setup’s publishable_key, account and client_secret instead. Tell the customer nothing is charged now and the card pays the first renewal. confirm then answers paid once the card is saved, setup_processing while Stripe checks it, or requires_setup to try again; the checkout can’t complete without a card. An open or frozen cart that will ask for one reads card_required: { "reason": "renewal" } ahead.

Time to confirm

The payment or setup carries confirm_by, the latest time confirm takes it: the cart’s expires_at plus 15 minutes, or 15 minutes after the payment started on a cart that never expires. Tell the customer this time when the method leaves them instructions to follow (a voucher, a QR code, an approval in their bank’s app). Past expires_at, pay answers 409 conflict (expired), though confirm still answers until confirm_by. A payment not confirmed by then is called off, unless it’s already processing, and the cart may close sooner. Asking to pay again answers the same payment.

Saved cards

To pay with a card the customer saved, send its id from GET /v1/stores/{store}/billing_profile as instrument:

{ "instrument": { "id": "<instrument id>" } }

It counts only when the payment starts: naming another card while one is still to confirm answers 409 conflict (paying), a card that isn’t the customer’s answers 404 not_found, and a cart that saves a card for its renewals (requires_setup) takes a saved card alone, any other saved method answering 422 invalid on instrument. The payment or setup names the saved card it runs on as instrument (null for a new card), so after a reload your page confirms it with no card form.

An app the store hasn’t verified is refused instrument, and any payment that runs on a saved card (one the customer started on the store’s own site included) while it’s still to confirm, as 403 not_permitted (principal): abandon that cart and open another, which the customer pays with a card entered in your page.

Totals under the minimum charge

A total above zero but under the payment processor’s minimum charge is granted free, since the processor refuses to take it. The cart reads under_minimum, with the amount waived and the processor’s minimum, so your page can say why the customer pays nothing. The freeze discounts its lines whole, so totals.discount includes the waived amount and totals.total is zero; it asks no refund_waiver, and the order carries the same statements.under_minimum for its receipt. A total the processor still refuses as too small when paid is granted free then: pay answers paid (or requires_setup, when a plan in it will charge its first renewal), and under_minimum.minimum is null.

The store’s Meta pixel

Where the store sends purchase events to its Meta pixel, pay also takes attribution: the _fbp and _fbc cookies (fbp, fbc), the customer’s client_ip_address, client_user_agent, country, city, region, postal_code and the source_url the payment started from. They’re kept for that event alone.

Giving up, and failures

POST /v1/stores/{store}/checkouts/{checkout}/abandon gives a cart up; a frozen one hands its coupons back.

No write takes an idempotency key. After a call that failed or never answered, read the cart back (GET /v1/stores/{store}/checkouts/{checkout}, or the customer’s open and frozen carts with GET /checkouts) before trying again, rather than opening another.

A store that pauses new purchases answers 403 not_permitted (paused), while renewing what the customer already holds still goes through.

After the purchase

A paid cart reads its order. What the customer bought then shows in their library: GET /entitlements, and owned and access on the product.

Type to search…

↑↓ navigate↵ selectEsc close