Skip to main content

Checkout API

The hosted checkout (functions/checkout): payment links, website/app sessions and bulk payments, with card, Thai QR, American Express and Apple Pay. How it fits with everything else: PAYMENTS.md.

Base URL​

Checkout runs on MainApi on the current dev stack. Production is off. See Hosted checkout for outlets for the production rollout gate.

StackBase
Current dev stackhttps://rj1mf23lu0.execute-api.ap-southeast-1.amazonaws.com/Prod
Older dev and staging addressesHosted checkout is not available
ProductionHosted checkout is not enabled

On an enabled stack, links and gateway return URLs use its configured CheckoutPublicBaseUrl. If it is empty, they use the stack's default base.

For dashboard monitoring, see DEV checkout health.

Amounts are integers in satang (7400 = ฿74.00) unless a field says Display. Times are Unix seconds.

POST /checkout/links
x-api-key: <staff key>
Content-Type: application/json

{
"orderIds": ["002500000101", "002500000102"],
"expiresInMinutes": 1440,
"reference": {"saleId": "store-12", "customerId": "C-889", "note": "phone order"}
}
FieldRequiredRules
orderIdsyes1 to 50 existing, unpaid Villa order ids (letters, digits, -, _); duplicates count once. Several = one payment
expiresInMinutesnoWhole minutes, 5 to 10080 (7 days). Default 1440 (24 hours)
referencenosaleId, customerId, note: text up to 160 characters each; for staff only

201 Created:

{
"sessionId": "CHK7Q2M4K9X3ZPAB",
"url": "https://shop.villamarket.com/api/payment3api/checkout/pay/Fn0b...32 characters...",
"expiresAt": 1793664000,
"amount": 20050,
"amountDisplay": "200.50",
"currency": "THB",
"orders": [{"orderId": "002500000101", "amount": 12050}, {"orderId": "002500000102", "amount": 8000}],
"methods": ["card", "thai_qr", "amex", "apple_pay"]
}

Both link and session creation return the enabled methods in display order. The payment page uses the same list. An enabled method may still be unavailable when its gateway is down or not configured.

Send url to the customer. It is the only way to see and pay the link: keep it out of logs and tickets. sessionId is not secret; use it for staff calls and when talking to the payments team.

The staff key goes in x-api-key. Keys are made and revoked with scripts/create-checkout-staff-key.sh {dev|prod} <key-id> [--revoke]; only their SHA-256 is stored, in the secret payment3-checkout-{dev|prod}.

GET    /checkout/links/{sessionId}     x-api-key: <staff key>
DELETE /checkout/links/{sessionId} x-api-key: <staff key>

GET answers the session's state, orders, amount, reference, every payment attempt (method, amount, state, reason, gateway reference) and, once paid, the payment and what happened to each order (fulfilment). It never answers the link token.

Each order's fulfilment state: invoked (handed to the order processor), invoke_failed and waiting (handed on again every 5 minutes: the hand-over failed, or another run of the order is still going or waits for a person, functions/processPaidOrder/docs/RUNBOOK_PROCESS_PAID_NEEDS_PERSON.md), done (marked paid), already_paid (another payment had paid it: a second payment, escalated) and amount_mismatch (not marked paid, escalated).

DELETE cancels an open or expired link and frees its orders for a new link. It is refused (409 payment_in_progress) only while a payment on the link is being confirmed (15 minutes at most); try again a few minutes later. Repeating it on a cancelled link answers 200 and frees its orders again (a cancel that failed half way can be finished). A QR still on the customer's screen or an open American Express page does not block it; they are listed in the answer, and a payment made through them later is flagged to staff (paid_closed_session):

{"sessionId": "CHK7Q2M4K9X3ZPAB", "state": "cancelled",
"stillPayable": [{"attemptId": "CHK7Q2M4K9X3ZPABA02", "method": "thai_qr", "payableUntil": 1793671200}]}

Pay your own order (website / app)​

POST /checkout/sessions
Authorization: <Cognito ID token>
Content-Type: application/json

{"orderIds": ["002500000301"], "source": "app"}

Same answer as a link (201). Every order must belong to the signed-in customer (ownerId = the token's sub); another person's order, or an order without an owner, answers 404 order_not_found. The session lives 2 hours. Calling it again for the same orders (the page, and with it the link, was closed) replaces the customer's own earlier session; it answers 409 payment_in_progress while a payment on that one is being confirmed, and 409 order_has_open_link when the order is on a staff link. Open url in the browser or a web view; after payment the customer lands on the result page.

Sales channels (channel registry)​

Off unless the stack parameter CheckoutChannelRegistry is on (its default is off). With it off nothing in this section applies, channel on a link is not read, and every answer is as the rest of this page describes.

A session can be made for a registered sales channel: "source": "<channel id>" on POST /checkout/sessions, or "channel": "<channel id>" on POST /checkout/links. Channels are registered in SSM (/payment3/<branch>/checkout/channels/<id>: a label and a return base); a request names a channel by its id and can never give an address.

WhereWhat changes for a session with a channel
The 201 answerOne more field: "channel": {"id", "label", "returnTo"}
The result pageA link "Return to label"; once the state is paid the page continues there by itself, once per browser session
The return URL{returnBase}/thankyou?sessionId=…&status=…&state=…, where status is success, pending or failure (no order id)
GET /checkout/status/{sessionId}Adds channel ({"id", "label"}), returnUrl and unconfirmed (true when the session is not paid and an attempt, the latest or an earlier one, may be a payment a gateway took); answers with Access-Control-Allow-Origin: *, so the channel's own page can ask

web and app are labels, never channels (and so is link). A source that is not a registered, enabled channel does not fail a customer session: it is made without a channel. A staff link that names one answers 400 channel_unknown. When the registry cannot be read, a customer session or a staff link that names what could be a channel answers 503 channel_registry_unavailable: nothing is created, send it again. The return URL is a hint; the channel shows "paid" only when the status route says so. More: Return to the channel.

Checkout methods​

Set the stack's CheckoutMethods parameter to a comma-separated list with no spaces. The default is card,thai_qr,amex,apple_pay. CodeBuild accepts an optional CHECKOUT_METHODS override. Omit it to keep the stack's value.

An unset or empty Lambda CHECKOUT_METHODS uses the default. A nonempty value with an unknown entry enables no methods. Checkout logs a configuration error, returns an empty methods list and refuses all payment starts.

Turning off a method stops new starts. It does not stop verification, returns, status checks, reconciliation or cancellation for existing attempts. A QR charge can still report back to POST /checkout/pay/{token}/qr.

The payment page and its routes​

These are used by the page itself; a client app only opens url.

RouteWhat it does
GET /checkout/pay/{token}The page: orders, total (priced again), and enabled methods
GET /checkout/pay/{token}/cardK Bank's card form for the current total
POST /checkout/pay/{token}/cardK Bank's form posts the card token here; the checkout charges it and answers 303 to K Bank's 3-D Secure page (or the result page)
GET /checkout/pay/{token}/qrShows K Bank's QR for the current total (the same QR again while it is fresh and the total has not changed)
POST /checkout/pay/{token}/qrK Bank's QR form posts the charge id here; it is matched to its attempt by asking K Bank, then verified; answers 303 to the result page
GET /checkout/pay/{token}/amexCreates the MPGS session and opens MPGS's payment page
POST /checkout/pay/{token}/applepayApple Pay. {} quotes (publishableKey, amount, currency); {"confirmationTokenId": "ctoken_..."} confirms on the server (see STRIPE_APPLE_PAY.md)
GET/POST /checkout/return/{sessionId}Where the gateways send the customer back; shows the verified result. The session id is not a secret: anything posted here is ignored, and the gateways are asked at most every 4 seconds
GET /checkout/status/{sessionId}{"sessionId", "state", "method", "attempt"} for the result page's poll
GET /checkout/assets/checkout.js, checkout.cssThe page's script and style
GET /checkout/assets/channel-return.jsThe way back to a sales channel; served only while the channel registry is on

States​

stateMeaning
openPayable (a QR on screen or an open MPGS page is not a payment yet)
processingA payment is being confirmed by its gateway (the page shows "Confirming your payment…"); shown for 15 minutes at most, while the gateway is still asked afterwards
paidA gateway confirmed the full amount; the orders are being marked paid
expiredThe link ran out (a payment already in flight is still checked for an hour)
cancelledStaff cancelled it
needs_attentionMoney arrived that does not fit; staff decide (see PAYMENTS.md)

Errors​

Every error is {"error": "<message for the person>", "code": "<code>"} (JSON routes) or an HTML page with the message (page routes). Messages never include a stack trace, a gateway answer or a key.

StatuscodeWhen
400order_ids_required, order_id_invalid, too_many_orders, reference_invalid, expiry_invalid, json_requiredThe request itself
400card_token_missing, token_invalid, not_apple_payA payment step without what it needs; not Apple Pay through Stripe
400channel_unknownA staff link names a channel that is not registered and enabled (channel registry on)
401staff_key_required, sign_in_requiredNo staff key / no Cognito session
402declinedThe gateway declined (the message is the gateway's, for the customer)
403staff_key_invalidWrong staff key
404order_not_found, link_not_found, session_not_found, not_foundnot_found: no such route
408card_timeoutK Bank's card form timed out
409order_paid, order_has_open_link, order_unpriceable, order_unreadable, order_nothing_to_pay, order_currency, already_paid, session_changed, payment_in_progress, not_cancellable, needs_attention, id_collision, too_many_attemptsThe orders or the link are not in a state for this (id_collision: try again; too_many_attempts: 50 tries on one link, make a new link)
410link_expired, link_cancelled
413request_too_largeBody over 16 KB
502apple_pay_unansweredStripe did not answer a confirmation
503method_unavailableThe stack does not allow this payment start. No gateway is called and no attempt is added. HTML responses also carry X-Checkout-Error-Code: method_unavailable
503channel_registry_unavailableA customer session or a staff link names what could be a channel and the registry cannot be read (channel registry on). Nothing was created: send it again
503card_unavailable, qr_unavailable, amex_unavailable, apple_pay_unavailable, order_store_unavailable, staff_auth_unavailableA gateway or a store is not reachable or not configured on this stack
500internal_errorLogged and sent to Sentry. The message never says no payment was taken (the error may come after a gateway took it): it asks the customer not to pay again if they already paid

Limits​

  • 50 orders per payment; they are priced in parallel (8 at a time).
  • 50 payment attempts per link (opening the QR or Amex page again within a few minutes reuses its attempt).
  • Gateways are asked at most once every 4 seconds per session by the page's poll; the reconciler asks every 5 minutes.