Return to the channel
The hosted checkout is the one place where K Bank (card, Thai QR), MPGS (American Express) and Stripe (Apple Pay)
payments are confirmed on the same page, /checkout/return/{sessionId}. The channel registry lets that page
send the customer on to the sales channel the session was made for.
Merged to the dev branch on 1 Dec 2026 (pull request #231). It sits behind the stack parameter
CheckoutChannelRegistry, whose default is off; on the current dev stack it has been on since 1 Dec 2026.
On 1 Dec 2026 two test channels (outlet-a, outlet-b, each with its own Villa-hosted return site) were registered there, one staff link was made for each, and both were paid with the K Bank sandbox card through 3-D Secure. After 3-D Secure the browser was on the older stack's error page (gap 9), so /checkout/return/{sessionId} was opened on the current stack by hand. From there it went by itself: the session's status was paid, the result page said "Taking you back to Test outlet A…" (or B) and moved to that outlet's own /thankyou?sessionId=…&status=success&state=paid, and the outlet's page read /checkout/status and showed "Payment confirmed".
One thing does not work there yet: after 3-D Secure, K Bank's sandbox sends the browser to the older dev stack,
which shows an error page (gap 9). The payment is still confirmed (the checkout asks K Bank itself), but
the customer has to open /checkout/return/{sessionId} to be taken back. master (production) has neither the
hosted checkout nor the registry. Putting them there is a decision for the payments owner.
On 1 Dec 2026 the villa.kitchen address in the examples reaches an older dev stack that has no hosted
checkout (/checkout/... answers 403 there). Use the current dev stack's MainApi address in its place: see
Base URLs.
How it works
- A person with access to the AWS account registers the channel: an id, a label and a return base.
- A session names the channel:
sourceonPOST /checkout/sessions, orchannelonPOST /checkout/links. The registry entry is copied into the session. - The result page shows Return to label. When the payment is confirmed (
paid) it also shows "Taking you back to label…" and moves there by itself after 2.5 seconds. - Your channel's page asks
GET /checkout/status/{sessionId}before it shows anything as paid.
The return address is never taken from a request. It comes from the registry only, so no caller can turn the result page into an open redirect.
The return URL
{returnBase}/thankyou?sessionId={sessionId}&status={status}&state={state}
/thankyou is appended unless the return base already ends with it.
| Parameter | Value | Notes |
|---|---|---|
sessionId | CHK and 12 characters of A-Z, 2-7 | Not a secret. There is no order id in the URL |
status | success, pending or failure | From state: paid is success; processing and needs_attention are pending; everything else is failure |
state | The session's state when the URL was built | paid, processing, open, expired, cancelled, needs_attention |
failure means not confirmed. It never means that nothing was charged.
The customer is moved automatically only when the state is paid, and only once in a browser session: opening the
result page again shows the link and stays. In every other state the link is there and the customer decides. The 2.5
seconds start when the page shows the payment as received; if the page has not caught up within 4 seconds they
start anyway.
Register a channel
One SSM parameter per channel, in the account of the stack:
/payment3/<branch>/checkout/channels/<id>
{"label": "Hotel X shop", "returnBase": "https://shop.hotelx.example", "enabled": true}
The channel kit writes and checks it for you:
python3 channel-kit/channel.py --branch dev register --id hotel-x --label 'Hotel X shop' \
--return-base https://shop.hotelx.example # a plan: prints the entry and the three URLs
python3 channel-kit/channel.py --branch dev register --id hotel-x --label 'Hotel X shop' \
--return-base https://shop.hotelx.example --apply # writes it
| Field | Rule |
|---|---|
id | A lower-case letter, then 1 to 31 of a-z, 0-9, -. link, web and app are reserved: they are labels, never channels |
label | 1 to 60 characters. Shown to the customer |
returnBase | https://host with an optional port and path, or an app's own scheme://…. The host is a DNS name (letters, digits, hyphens and dots; an international name in its xn-- form) or an IPv4 address. http:// only for localhost and 127.0.0.1, and never on master. At most 512 characters |
enabled | Must be true. Anything else: the entry is not used |
Refused in a return base: a user name or password, a port that is not a number from 1 to 65535, an IPv6 address
or a percent-escape as the host of an https base, ?, #, whitespace, a control character, a quote, <, >, a
backslash, a backtick, and the schemes a browser would run or read locally (javascript, data, file, blob
and the like), and the other schemes a browser handles itself (ftp, ws, wss, mailto, tel, sms). An entry
that breaks a rule is treated as if it were not there. The stored base is checked again each time a return URL is
built.
Each running copy of the checkout function remembers an entry for 60 seconds, so a change reaches new sessions within about a minute.
Name the channel on a session
- Signed-in customer
- Staff link
curl -X POST https://villa.kitchen/api/payment3devapi/checkout/sessions \
-H "Authorization: $COGNITO_ID_TOKEN" -H "Content-Type: application/json" \
-d '{"orderIds": ["002500000301"], "source": "hotel-x"}'
web and app are labels, never channels: a session that sends them is made as it always was, and the registry is
not asked. Villa's own shop and app register their own ids (for example villa-web).
A source that is not a registered, enabled channel is no error: the session is made without a channel and its
source is stored as web. One case is refused: the source could be a channel and the registry cannot be read.
The answer is 503 channel_registry_unavailable, nothing is created, and the same request can be sent again.
curl -X POST https://villa.kitchen/api/payment3devapi/checkout/links \
-H "x-api-key: $CHECKOUT_STAFF_KEY" -H "Content-Type: application/json" \
-d '{"orderIds": ["002500000101"], "channel": "hotel-x"}'
Staff name a channel on purpose, so a mistake is an error and not a link that returns nowhere:
| Answer | Meaning |
|---|---|
400 channel_unknown | Not a registered, enabled channel (also: an address in place of an id) |
503 channel_registry_unavailable | The registry could not be read. Try again, or create the link without channel |
A session with a channel answers with one more field:
{
"sessionId": "CHKQ2M4K7X3ZPAB",
"url": "https://villa.kitchen/api/payment3devapi/checkout/pay/Fn0b…",
"channel": {"id": "hotel-x", "label": "Hotel X shop", "returnTo": "https://shop.hotelx.example/thankyou"},
"expiresAt": 1793664000, "amount": 20050, "amountDisplay": "200.50", "currency": "THB",
"orders": [{"orderId": "002500000301", "amount": 20050}],
"methods": ["card", "thai_qr", "amex", "apple_pay"]
}
The entry is copied into the session. Changing or disabling a channel later affects new sessions only: a customer who is already paying still returns to the address their session was made with.
What your channel's page must do
The return URL is a hint. Anyone can type it.
- Read
sessionIdfrom the query. If it is notCHKand 12 characters ofA-Z,2-7, show nothing about a payment. - Ask
GET {api}/checkout/status/{sessionId}. - Show "paid" only when the answer says
"state": "paid". Ignorestatusandstatein the URL for that decision. - While the answer is
processingorneeds_attention, show "We are checking your payment" and ask again. When you stop asking, say so: never leave "checking" on the screen after the checking has stopped. - For
open,expiredandcancelled, say "not completed" only whenunconfirmedisfalse(a status without the field is not proof) andattempt(the latest attempt) is absent,declined,refused,failedorrefunded. Otherwise a gateway may have taken a payment (anunresolvedattempt, the latest or an earlier one): say that it is not confirmed yet and that the customer should not pay again. - If the answer cannot be read, say so and tell the customer not to pay again.
curl https://villa.kitchen/api/payment3devapi/checkout/status/CHKQ2M4K7X3ZPAB
{"sessionId": "CHKQ2M4K7X3ZPAB", "state": "paid", "method": "card", "attempt": "paid",
"channel": {"id": "hotel-x", "label": "Hotel X shop"},
"returnUrl": "https://shop.hotelx.example/thankyou?sessionId=CHKQ2M4K7X3ZPAB&status=success&state=paid",
"unconfirmed": false}
With the registry on, this one route answers with Access-Control-Allow-Origin: * (its 404 too), so a static page
on your own domain can read it from the browser. It needs no key: a session id is not a secret, and the answer
holds no order id, amount or customer detail. No other route is opened this way.
A working page is in the repository: channel-kit/thankyou/ (index.html, thankyou.js, thankyou.css). It asks
every 3 seconds for at most 2 minutes and never shows "paid" from the URL. If the payment is still not final then,
it says "We could not confirm your payment yet. Please do not pay again." and shows Check again and the way
back to the shop.
For an app, register the app's scheme as the return base (hotelx://pay) and do the same check in the app when
the deep link opens.
What the switch does
CheckoutChannelRegistry is a stack parameter (on or off, default off).
off (the default) | on | |
|---|---|---|
source on a customer session | web or app, a label for staff | A registered id also sets the channel |
channel on a staff link | Not read | Looked up; unknown is 400 |
| Answers | Exactly as before: no channel, no returnUrl, no unconfirmed | Sessions with a channel carry them |
| Result page | Stays | Links back, and returns by itself when paid |
GET /checkout/status from another origin | No CORS header | Access-Control-Allow-Origin: * |
Switching it off later also stops the return of sessions made while it was on: from then on their pages and
status answers carry no link. A result page that is already open and still waiting for the payment (processing)
puts its link away at its next status answer, and shows it again if the registry is switched back on while it
waits. A page that is open and not waiting keeps what it was given until it is opened again, and a page that has
already started the 2.5-second return completes it.
The way back is a script of its own, static/channel-return.js. Only a result page with a way back loads it, and
the route serves it only while the registry is on. checkout.js is not changed by this work, so with the registry
off nothing a browser receives differs from before. While a page waits, a status answer that cannot be read (an error, a network
fault) leaves the link as it is, and the page asks again every 5 seconds until it stops waiting, 5 minutes after it
was opened.
A deploy does not change the parameter (a deploy passes four parameters only); someone sets it on purpose.
When the registry cannot be read
The checkout reads the registry with one short attempt (about two seconds at most). If that fails, it does not ask again for 15 seconds.
- A request that names a channel gets
503 channel_registry_unavailable: a customer session whosesourceis a well-formed id other thanwebandapp, and a staff link withchannel. Nothing is created and nothing is charged. Send the same request again: a session made without its channel could never return the customer. - A customer session with
sourceweborapp, or with none, is created as always. So is a staff link withoutchannel. The registry is not asked for them. - An id the same copy of the function read in the last 60 seconds is still known, so it keeps working.
Tests
| What | Where |
|---|---|
| Return base rules, the registry, sessions, the result page, the status route, staff links, the switch | functions/checkout/tests/test_channels.py |
| A real browser: pay by card, the result page, the automatic return, the channel's page asking the status, a forged "paid" URL, the switch turned off while a customer waits | functions/checkout/tests/e2e/checkout.e2e.mjs, checks 12a to 12m |
| The channel's reference page | channel-kit/tests/thankyou.test.mjs |
cd functions/checkout && python -m pytest tests/test_channels.py -q
node functions/checkout/tests/e2e/checkout.e2e.mjs e2e-out # needs Chromium (npx playwright install chromium)
What it does not do
- It covers the hosted checkout only. Direct card (
callback), direct Thai QR and direct Amex return as before: see Confirmation routing. - A declined hosted Apple Pay, a failed extra Apple Pay step and an unpaid QR never reach the result page, so nothing returns the customer: they are still on the payment page.
- It does not tell your server. Your server asks the status route, or reads the order.
- A partner cannot create a session by itself: a customer session needs a Villa sign-in and a staff link needs a Villa staff key. See Launch a shop.