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.
| Stack | Base |
|---|---|
| Current dev stack | https://rj1mf23lu0.execute-api.ap-southeast-1.amazonaws.com/Prod |
| Older dev and staging addresses | Hosted checkout is not available |
| Production | Hosted 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.
Create a payment link (staff)
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"}
}
| Field | Required | Rules |
|---|---|---|
orderIds | yes | 1 to 50 existing, unpaid Villa order ids (letters, digits, -, _); duplicates count once. Several = one payment |
expiresInMinutes | no | Whole minutes, 5 to 10080 (7 days). Default 1440 (24 hours) |
reference | no | saleId, 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}.
See or cancel a link (staff)
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.
| Where | What changes for a session with a channel |
|---|---|
The 201 answer | One more field: "channel": {"id", "label", "returnTo"} |
| The result page | A 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.
| Route | What it does |
|---|---|
GET /checkout/pay/{token} | The page: orders, total (priced again), and enabled methods |
GET /checkout/pay/{token}/card | K Bank's card form for the current total |
POST /checkout/pay/{token}/card | K 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}/qr | Shows 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}/qr | K 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}/amex | Creates the MPGS session and opens MPGS's payment page |
POST /checkout/pay/{token}/applepay | Apple 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.css | The page's script and style |
GET /checkout/assets/channel-return.js | The way back to a sales channel; served only while the channel registry is on |
States
state | Meaning |
|---|---|
open | Payable (a QR on screen or an open MPGS page is not a payment yet) |
processing | A 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 |
paid | A gateway confirmed the full amount; the orders are being marked paid |
expired | The link ran out (a payment already in flight is still checked for an hour) |
cancelled | Staff cancelled it |
needs_attention | Money 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.
| Status | code | When |
|---|---|---|
| 400 | order_ids_required, order_id_invalid, too_many_orders, reference_invalid, expiry_invalid, json_required | The request itself |
| 400 | card_token_missing, token_invalid, not_apple_pay | A payment step without what it needs; not Apple Pay through Stripe |
| 400 | channel_unknown | A staff link names a channel that is not registered and enabled (channel registry on) |
| 401 | staff_key_required, sign_in_required | No staff key / no Cognito session |
| 402 | declined | The gateway declined (the message is the gateway's, for the customer) |
| 403 | staff_key_invalid | Wrong staff key |
| 404 | order_not_found, link_not_found, session_not_found, not_found | not_found: no such route |
| 408 | card_timeout | K Bank's card form timed out |
| 409 | order_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_attempts | The 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) |
| 410 | link_expired, link_cancelled | |
| 413 | request_too_large | Body over 16 KB |
| 502 | apple_pay_unanswered | Stripe did not answer a confirmation |
| 503 | method_unavailable | The 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 |
| 503 | channel_registry_unavailable | A 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 |
| 503 | card_unavailable, qr_unavailable, amex_unavailable, apple_pay_unavailable, order_store_unavailable, staff_auth_unavailable | A gateway or a store is not reachable or not configured on this stack |
| 500 | internal_error | Logged 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.