Skip to main content

Credit Card Payment Flows - Complete Documentation

This document describes all possible flows for credit card/KBank payments, from checkout through callback to order processing and card tokenization.

Overview​

The credit card payment system involves these Lambda functions:

FunctionPurpose
payment3-cardtoken-${BRANCH}Creates charge, stores CardPaymentRecord, redirects to KBank 3DS
payment3-callback-${BRANCH}Receives KBank callback, updates status, invokes ProcessPaidOrder
payment3-process-paid-order-${BRANCH}Processes order (success/failure), saves card token via KBankCreateUser
payment3-kbank-create-user-${BRANCH}Creates KBank user OR invokes addcard for existing users
payment3-kbank-add-card-${BRANCH}Adds new card to existing KBank customer

Callback Body Parameters​

Source: CreditCardCallbackProcessor in functions/callback/src/credit_card_callback_processor.py

Body format: URL-encoded string or JSON (JSON tried first, then urllib.parse.parse_qsl)

FieldRequiredUsed by BackendPurpose
objectId or idYesYesCharge ID; lookup/update CardPaymentRecordTable
tokenYesYesToken ID; identifies card payment record (hash key)
statusYesYes"true"/"success" or "false"/"failure"
messageNoNoNot used
saveCardNoNoNot used — echoed from KBank, not acted upon

Redirect URL: KBank controls the callback body. Pass callback when calling POST /cardpayment; we store it in the payment record and the callback reads it from there. See Callback URL App Integration.


Charge Amount​

CardToken asks K Bank for the order's calculated grandTotal (never the posted one; ฿1 for ADDCARD, the posted one only for a bypass_order test charge) rounded half-up to satang, two decimals. The cost calculator works in baht floats and can give half a satang (74.005). Every check of a K Bank charge (/callback, CheckStatus, a pre-charged chargeId) compares K Bank's amount with the order's grandTotal in satang, rounded half-up (74.005 is 7401 satang), so the charge is for 74.01. Sent as 74.005, K Bank could hold 74.00, and the paid charge would never match the order. A charge the caller makes itself (Flow 4b) must be for that amount too.


Flow 1: New User Pays (First-Time Card)​

Scenario: User has no KBank customer record. Pays for order with card.

┌─────────────┐     ┌──────────────┐     ┌─────────────┐     ┌──────────────┐
│ Frontend │────▶│ CardToken │────▶│ KBank │────▶│ 3DS Page │
│ Checkout │ │ Lambda │ │ Charge │ │ (if 3DS) │
└─────────────┘ └──────────────┘ └─────────────┘ └──────────────┘
│ │ │
│ │ create CardPaymentRecord │
│ │ (tokenId, orderId, status=false) │
│ │ │
│ │◀────────────────────────────────────────┘
│ │ User completes 3DS
│ │
▼ ▼
┌─────────────────────────────────────────────────────────────────────────┐
│ KBank Callback (POST /callback) │
│ body: objectId=chrg_xxx&token=tokn_xxx&status=true&saveCard=false (KBank fixed) │
└─────────────────────────────────────────────────────────────────────────┘
│
▼
┌──────────────────────┐
│ callback.py │
│ CreditCardCallback │
│ Processor │
└──────────────────────┘
│
│ 1. Parse body (objectId, token, status)
│ 2. Query CardPaymentRecordTable by chargeId (objectId) via GSI
│ 3. Update record: chargeId, status
│ 4. Save to CreditCardNotifyTable
│ 5. Invoke ProcessPaidOrder (orderId from card_record)
│
▼
┌──────────────────────┐
│ ProcessPaidOrder │
└──────────────────────┘
│
│ 1. Query OrderTable by orderId
│ 2. Query CardPaymentRecordTable by orderId
│ 3. save_paid_order() → Pay Lambda
│ 4. send_notification() → success
│ 5. save_card_token() → KBankCreateUser
│
▼
┌──────────────────────┐
│ KBankCreateUser │
│ (createuser.py) │
└──────────────────────┘
│
│ Query KBankUserMappingTable: user (ownerId) exists?
│ → NO
│
│ Call KBank API: POST /card/v2/customer
│ Save mapping: ownerId → kbank_id in KBankUserMappingTable
│
▼
Success: New KBank user created, card token saved

Key files:

  • functions/cardtoken/app.py — creates charge, CardPaymentRecord
  • functions/callback/callback.py — updates status, invokes ProcessPaidOrder
  • functions/processPaidOrder/process_paid.py — save_card_token()
  • functions/kbankuser/createuser.py — creates new KBank user

Flow 2: Existing User Pays with New Card (Add Card)​

Scenario: User already has KBank customer record. Pays with a different card.

┌─────────────┐     ┌──────────────┐     ┌─────────────┐
│ Frontend │────▶│ CardToken │────▶│ KBank │
└─────────────┘ └──────────────┘ └─────────────┘
│ │ │
│ │ │ 3DS callback
▼ ▼ ▼
┌─────────────────────────────────────────────────────────────────────────┐
│ KBank Callback (POST /callback) │
└─────────────────────────────────────────────────────────────────────────┘
│
▼
┌──────────────────────┐ ┌──────────────────────┐
│ callback.py │────▶│ ProcessPaidOrder │
└──────────────────────┘ └──────────────────────┘
│ │
│ │ save_card_token()
│ │ payload: { ownerId, token }
│ ▼
│ ┌──────────────────────┐
│ │ KBankCreateUser │
│ │ (createuser.py) │
│ └──────────────────────┘
│ │
│ │ Query KBankUserMappingTable
│ │ → User EXISTS (has kbank_id)
│ │
│ │ Invoke addcard Lambda
│ │ payload: { kbank_customer_id, token }
│ ▼
│ ┌──────────────────────┐
│ │ KBank Add Card │
│ │ (addcard.py) │
│ └──────────────────────┘
│ │
│ │ POST {kbank_url}/card/v2/customer/{id}/card
│ │ body: { mode: "token", token: token }
│ │
│ ▼
│ Success: Card added to existing KBank customer

Key difference from Flow 1: createuser.py checks kbank_user_exists. If true, it invokes payment3-kbank-add-card-${BRANCH} instead of calling the KBank create-user API.

Addcard Lambda (functions/kbankuser/addcard.py):

  • Input: { "kbank_customer_id": "...", "token": "tokn_xxx" }
  • Calls: POST {url}/card/v2/customer/{kbank_customer_id}/card
  • Returns: customer info including updated cards array

Flow 3: Payment Failed (Declined / 3DS Failed)​

Scenario: Card declined or 3DS authentication failed.

  1. Callback receives status=false
  2. Updates CardPaymentRecordTable with status=False
  3. Still invokes ProcessPaidOrder (same as success)
  4. ProcessPaidOrder determines failure from order_status, sends failure notification

Note: ProcessPaidOrder is invoked for both success and failure. It infers success from order_status (checksum or CheckStatusTable).


Flow 4: ADDCARD (Card Verification Only)​

Scenario: User adds card without a real order (1 THB verification charge).

CardToken: When orderId=ADDCARD:

  • Charges 1 THB
  • Skips order validation
  • Creates CardPaymentRecord with orderId="ADDCARD"
  • Skips invoke_process_paid_order from cardtoken (does not invoke ProcessPaidOrder)

Callback: When KBank sends callback for ADDCARD charge:

  • callback.py receives objectId, token, status
  • Updates CardPaymentRecord
  • Invokes ProcessPaidOrder with orderId="ADDCARD"
  • ProcessPaidOrder: OrderTable.query("ADDCARD") → OrderNotFound (no such order)
  • Flow fails at ProcessPaidOrder

Current state: ADDCARD callback flow is not fully wired. ProcessPaidOrder expects a real orderId.


Flow 4b: Pre-charged request (chargeId posted to POST /cardpayment)​

Scenario: The caller already charged the card with K Bank and posts the chargeId instead of having CardToken charge the token.

POST /cardpayment has no authorizer, so a posted chargeId, status or redirect_url is a claim. For a real order (not ADDCARD, not bypass_order), CardToken reads the charge back from K Bank (GET /card/v2/charge/{chargeId}) and uses K Bank's answer only, with the browser callback's rules (src/payment_verification.py). It asks before it writes anything:

  • The charge must name this order (description = orderId, or reference_order ending _<orderId>, as charges made by CardToken do), be in THB and be for exactly the order's calculated grandTotal (compared in satang, rounded half-up; never the posted grandTotal). Otherwise, or when K Bank has no such charge or cannot be asked, the request is refused and nothing is written: the redirect says status=failure, no CardPaymentRecord is saved for the order, the order is not saved (its payment header is not reset to PENDING), no status check is queued, ProcessPaidOrder is not invoked, and Sentry is told the charge id, the order and the reason.
  • Paid (success, Authorized or Settled): as a charge made here without 3-D Secure (status check queued, ProcessPaidOrder invoked).
  • Waiting for 3-D Secure (Pre-Authorized): the browser goes to K Bank's own redirect_url (never a posted one); /callback verifies the result, or K Bank's notify reports it. The CardPaymentRecord holds the charge as accepted (status true), as for a 3-D Secure charge made by CardToken (K Bank's create reply says success), although K Bank's inquiry says pending until 3-D Secure is finished: ProcessPaidOrder reads a record whose notify checksum is verified as paid only with status true, and as a failed payment with status false.
  • Declined or failed: status=failure, not processed.

ADDCARD and bypass_order pre-charged requests are not looked up: neither is ever processed for an order.

CheckStatus applies the same rules to every card record: a K Bank success counts (CheckStatusTable success, status "success") only for a charge that pays the record's order; otherwise the row's status is unverified and Sentry is told. ProcessPaidOrder marks an order paid only from a "success" row (or the notify checksum path).


Flow 5: Table Lookup Order (CardPaymentRecordTable)​

Callback finds the card record by chargeId (objectId):

  1. Query CardPaymentRecordTable.chargeId_index (branch table)
  2. If not found: query CardPaymentRecordTableMaster.chargeId_index
  3. If AttributeDeserializationError: fallback to boto3 direct query

orderId comes from the card payment record, not from the callback body.


Summary Table​

FlowCallback statusProcessPaidOrderKBankCreateUserAddcard
New user, successstatus=trueInvokedCreates new user—
Existing user, successstatus=trueInvokedInvokes addcardAdds card
Payment failedstatus=falseInvokedInvoked—
ADDCARD—Would fail (no order)——

saveCard Field​

Status: Not used by the backend.

  • Present in KBank callback body
  • Not read by CreditCardCallbackProcessor
  • Not stored in CardPaymentRecordTable
  • Not passed to ProcessPaidOrder
  • save_card_token() is always called for every processed order