Confirmation routing
A gateway answers on up to two roads. The browser road carries the customer back to a screen. The server road does not depend on the customer's browser. What either one posts is a claim: before an order is marked paid, villaPayment3 has the gateway's own word (a charge answer, an inquiry, or a checksummed or signed notification).
| Browser road | Server road | villaPayment3 asks | |
|---|---|---|---|
| K Bank card | 3-D Secure posts the browser to /callback | K Bank posts /notify (checksum) | K Bank charge inquiry |
| K Bank Thai QR | K Bank's QR form posts the browser to /qrcallback | K Bank posts /thaiqr/notify | K Bank QR inquiry |
| MPGS (Amex) | MPGS returns the browser to whoever opened it | villaWalletBackend2 posts /amex/callback (HMAC signature checked) | MPGS RETRIEVE ORDER |
| Stripe (Apple Pay) | None in the normal case: the page gets JSON | Stripe posts /stripe/webhook (signed) | Stripe PaymentIntent |
Either road can be the one that starts process-paid, and process-paid skips an order that is already paid. The browser road also moves the person. The server road is the recovery path when the customer closes the tab after paying; it can fail too (a refused checksum, an error), so it is not a guarantee. Your channel must read "paid" from its server, never from the return URL. Each section below says what that method does.
K Bank card: POST /cardpayment
The one direct flow that returns the customer to a channel of your choosing.
Channel page villaPayment3 K Bank
──────────── ───────────── ──────
form + K Bank script ───────────────────────────────▶ card form, token
POST /cardpayment ──▶ price order (calculate-cost)
callback=<base> store callback on the card record
charge ─────────────────────────▶ charge
◀─────────────────────────────── 3-D Secure URL
◀── page: go to 3-D Secure
customer passes 3-D Secure ──────────────────────────▶
◀─────────────────── browser POSTs /callback ◀──────── (fixed body: K Bank adds no field of ours)
inquiry ────────────────────────▶ "Authorized, THB, this order, this amount"
confirmed: process-paid starts (a claim on the charge stops a repeat)
read callback from the card record
◀── page: go to {callback}/thankyou?orderId&status&amount&chargeId&token
◀── POST /notify (checksum) ◀─── server road, with or without the browser
can start process-paid when the browser road did not
What to know:
- Where the return address lives. K Bank's callback body is fixed, so villaPayment3 keeps your
callbackon the card payment record (debugInfo.callback) atPOST /cardpaymentand reads it back at/callback. A charge made withoutcallbackreturns tohttps://shop.villamarket.com. - No 3-D Secure?
POST /cardpaymentanswers the return page itself, with K Bank's status, and starts process-paid for a successful charge. bypass_ordercharges never mark a Villa order paid or unpaid, on either road.- The answer is normally HTTP 200 with an HTML page that redirects (a meta refresh and a link); success or
failure is in
status. Two answers do not redirect:POST /cardpaymentanswers500with JSON ({"error": "…"}) when it refuses the request (a missing field, an order it cannot price), and/callbackanswers a500HTML error page when it cannot read the charge at all. - Failure returns too. A declined card comes back to
{callback}/thankyou?…&status=failure. So does a charge K Bank could not be asked about:failuremeans not confirmed, not always declined. - K Bank not reachable for the inquiry: nothing is marked paid or unpaid, and the customer returns with
status=failure./notifyand the queued status check normally settle it. Nothing guarantees that, so an order that stays unpaid after the customer says they paid needs a person. - Code:
functions/cardtoken/app.py,functions/callback/callback.py,functions/callback/src/credit_card_callback_processor.py,functions/callback/src/payment_redirect.py.
K Bank Thai QR: POST /kbank/order
Channel page villaPayment3 K Bank
POST /kbank/order ──▶ price order, create QR order ──▶ order_…
◀── K Bank order id
K Bank QR form (data-payment-methods="qr", data-order-id) ─▶ shows QR
customer scans and pays in a banking app
◀─────────────────── form POSTs /qrcallback ◀───────── orderId, status, amount, chargeId
QR inquiry ─────────────────────▶ paid? exact amount?
◀── page: go to https://shop.villamarket.com/thankyou?orderId&status&amount&chargeId
◀── POST /thaiqr/notify ◀─────── server road
- The return address is fixed to the shop on every stack. There is no
callbackfield on this flow. - The page redirects automatically only on success. Otherwise it shows a link, or a "we could not confirm this payment" page (HTTP 409) when K Bank does not confirm.
- A channel that is not the shop should take Thai QR through the hosted checkout.
- Code:
functions/kbankcreateorder/createorder.py,functions/callback/qrcallback.py,functions/callback/qrnotify.py.
American Express: MPGS
The storefront's Amex flow is run by villaWalletBackend2. It opens MPGS, receives the customer back, and then tells villaPayment3 server to server:
villaWalletBackend2 ── POST /amex/callback ──▶ villaPayment3
X-Villa-Timestamp: <unix seconds>
X-Villa-Signature: v1=<hex HMAC-SHA256(secret, "<timestamp>.<raw body>")>
MPGS RETRIEVE ORDER ──▶ AUTHORIZED / CAPTURED, THB
order marked paid once
| Answer | Meaning |
|---|---|
200 | Recorded (a reported decline is recorded as not paid, without asking MPGS) |
400 | The body cannot be read |
401 | Not signed, or signed wrongly, in block mode only (see below) |
409 | MPGS does not know the payment, or does not confirm it |
503 | The signing secret cannot be read, or MPGS has not answered yet: send it again |
The X-Villa-… headers are the contract villaPayment3 checks; the wallet backend does not send them yet. So the
template sets AMEX_SIGNATURE_MODE: flag today: an unsigned or badly signed callback is accepted and logged, and
an order is marked paid only if MPGS confirms it. The AmexSignatureFlagged metric is emitted for a newly recorded
successful callback whose signature is missing or invalid (not for a decline or a replay), before process-paid is
invoked. block (the code's default) answers 401 instead.
villaPayment3 never redirects a browser on this flow, so it has no say in which channel the customer returns to.
A new channel takes Amex through the hosted checkout, where MPGS returns to /checkout/return/{sessionId}.
Code: functions/callback/amexcallback.py, functions/callback/src/callback_signature.py.
Apple Pay: Stripe
Stripe takes Apple Pay and nothing else. The flow below is for Villa orders and is live on the Villa shop. For a site that owns its orders, use site Apple Pay and its signed server callback. The site route is on the current dev stack with test keys only.
Channel page villaPayment3 Stripe
POST /stripe/applepay {orderId} ─▶ price order
◀── {amount, publishableKey}
Apple Pay sheet ─────────────────────────────────────▶ confirmation token
POST /stripe/applepay {orderId, confirmationTokenId}
must be an Apple Pay card
PaymentIntent, confirm ─────────▶
◀── 200 {status: "succeeded" | "processing" | "requires_action"} or 402 declined
your page goes to its own confirmation screen
◀── POST /stripe/webhook ◀────── signed; order marked paid once
- The customer never leaves your page, so there is nothing to route: your own script moves to your confirmation screen.
- Stripe is also given a
return_urlfor the rare redirect-based step. It is{origin}/thankyou?orderId=…, where the origin is your page'sOriginheader only if it is an https Villa web origin (villamarket.com,villamarket.net,villamarket.ai,villa.kitchenand their subdomains). Any other origin falls back tohttps://shop.villamarket.com(master) orhttps://villa.kitchen(other stacks). It is never taken from the body. - A
200is not "paid". The webhook marks the order paid after it checks the signature and that the money came from an Apple Pay card; anything else is refunded. - Code:
functions/stripe/confirm.py,functions/stripe/webhook.py,functions/stripe/src/stripe_client.py.
Hosted checkout: all four
Channel villaPayment3 checkout Gateways
POST /checkout/sessions ─▶ price, lock orders
◀── {sessionId, url}
open url ───────────▶ /checkout/pay/<token>: card · Thai QR · Amex · Apple Pay
card / QR ─────────────────────▶ K Bank
Amex ──────────────────────────▶ MPGS
Apple Pay ─────────────────────▶ Stripe
◀── returns to /checkout/return/<sessionId>
(a declined Apple Pay stays on the payment page)
verify_session asks the gateway; session paid once
customer sees "Confirming your payment…" then "Payment received"
channel polls GET /checkout/status/<sessionId>
- A payment that leaves the payment page returns to one page,
/checkout/return/{sessionId}: K Bank's 3-D Secure return is handed over from/callback, K Bank's QR form posts to the payment link, MPGS is given that page as its return URL, and for Apple Pay the payment page moves there itself after a200answer and, when that answer isrequires_action, once the extra step has succeeded. A declined Apple Pay (402), a failed extra step or another error is shown on the payment page, which stays. Whatever is posted to the page is ignored. - K Bank's
/callback,/notifyand/thaiqr/notifyand the Stripe webhook recognise a checkout payment (functions/callback/src/checkout_bridge.py) and hand it to the checkout. They never mark it paid themselves. verify_sessionis what asks a gateway. It runs when the result page opens; when the payment page opens or the status is polled while the session isprocessing; after a card charge that K Bank answers with a charge id and without a refusal, an HTTP 5xx or a 3-D Secure redirect; after an Apple Pay confirmation that Stripe reports assucceededorprocessing; when K Bank's QR form posts back; when a gateway notification is handed over; before a session is cancelled or replaced; and from a reconciler every 5 minutes. It may ask nothing: the page opens, the status poll and a QR post without a charge id are throttled (once every 4 seconds per session), and it asks only about attempts that can still change and have something to ask about (a card charge id, a PaymentIntent, the MPGS order).- A Thai QR attempt gets its charge id only from K Bank's form post or
/thaiqr/notify; if both are lost there is nothing to ask, and it needs a person. - The result page continues to the channel only where the channel registry is on (the current dev stack, since
1 Dec 2026): there a registered channel id in
sourcedecides where the customer is returned. Everywhere elsesource(weborapp) is stored and shown to staff; it does not route. See Return to the channel and Gaps. - Code:
functions/checkout/app.py,functions/checkout/src/service.py,functions/checkout/src/pages.py.