Skip to main content

ProcessPaidOrderInternal Lambda Function

Quick Reference​

  • Function Name: payment3-process-paid-order-internal-${BRANCH}
  • Handler: process_paid_internal.lambda_handler
  • Runtime: Python 3.13 (Docker Image)
  • Trigger: Lambda invocation (asynchronous)
  • Timeout: 200 seconds
  • Template.yaml: resource ProcessPaidOrderInternal

Function Overview​

ProcessPaidOrderInternal processes Thai QR reports. Two callback functions invoke it, asynchronously, through functions/callback/src/qrprocessor.py, after K Bank reports a QR payment: /qrcallback (QRCallback) and /thaiqr/notify (QRNotify). It asks K Bank about the QR itself, writes the order's payment header, records the run and notifies the customer.

Card charges are not decided here. An event whose paymentType is not "qr" is refused with statusCode 400 before anything is claimed or read. The card branch this function used to have took any "success" row of payment3-check-status-<branch> (one written before the charge-for-order rule too) or the first card record the index returned as the order's answer; card charges are decided by process_paid.py alone (every charge of the order, asked with K Bank through check-status).

Event​

Format: JSON string or dict

{
"orderId": "002500048744",
"paymentType": "qr",
"success": true,
"chargeId": "chrg_test_EXAMPLE3"
}
  • orderId (or order_id), chargeId (or charge_id): required.
  • paymentType (or payment_type): must be "qr".
  • success: K Bank's report. true for a paid QR; false for one that failed or expired (/thaiqr/notify). The run asks K Bank either way and writes what K Bank answers.

At most once per order​

Every report holds the order's claim in payment3-process-paid-once-<branch> (src/paid_once.py) while it runs, shared with every other process-paid entry point (card, Amex, wallet, Apple Pay, checkout), so no two runs of one order overlap and a paid run happens at most once per order.

  • A run that leaves the order unpaid (a QR that failed or expired) releases its claim when it ends, so the order's later payment is still processed.
  • A report that meets another run's live claim of the order fails with OrderRunInProgress, and Lambda's asynchronous retry delivers it again. So does a report that meets a done claim of an unpaid order while the order cannot be read: the claim may be stale (left by a run that did not mark the order paid), and it is taken over on the retry.
  • A report that fails (any answer but 200) keeps its claim failed for a person (docs/RUNBOOK_PROCESS_PAID_NEEDS_PERSON.md). An "unpaid" report that fails while the order is still unpaid (K Bank's inquiry did not answer) ends as an unpaid run instead: its claim is released, so it never holds the order's next payment. One that fails after K Bank's own answer said paid and the order was marked paid keeps its failure.

Processing Flow​

  1. Refuse anything but a Thai QR report (statusCode 400).
  2. Claim the order (above).
  3. Parse and validate the event.
  4. Skip when the order is already paid.
  5. Ask K Bank about the QR: GET {BASE_URL}/qr/v2/qr/{charge_id} (x-api-key header), for its status and amount.
  6. Write the payment header through the Pay Lambda (savePaidOrder): paymentId the QR charge id, isPaid K Bank's answer, totalPaid K Bank's amount.
  7. Record the run in payment3-process-paid-order-<branch>.
  8. Notify the customer (success or failure) and send the e-mail.
  9. Answer statusCode 200.

Error Handling​

  • Another paymentType: refused with 400 before the claim; nothing is read or written.
  • A "qr" report missing a field (orderId aside), K Bank, the order table or the Pay Lambda failing: the run answers 500 under the claim and writes an error row (is_error = true) to payment3-process-paid-order-<branch>. A paid report's claim is then kept failed for a person (the alarms); an unpaid one's is released while the order is unpaid (above).

Tables​

  • order-table-dev (shared): the order, read by orderId.
  • payment3-process-paid-order-<branch>: one row per run (orderId, created_at, data, is_error).
  • payment3-process-paid-once-<branch>: the per-order claim.

Environment​

  • BRANCH: deployment branch (default dev).
  • K Bank credentials from Secrets Manager (kbank-prod on master, else kbank-dev): KBANK_API_KEY, KBANK_SECRET_KEY, BASE_URL, MID.

Testing​

Offline, on moto, with K Bank's QR inquiry faked: functions/processPaidOrder/tests/test_decline_then_pay.py (the QR cases), test_paid_once.py, test_retry_or_ask_a_person.py.

  • ProcessPaidOrder (process_paid.py): K Bank card charges.
  • ProcessPaidAmexOrderInternal, ProcessPaidWalletOrderInternal, ProcessPaidStripeInternal (Apple Pay), ProcessPaidCheckoutInternal (the hosted checkout).
  • QRCallback (/qrcallback) and QRNotify (/thaiqr/notify): invoke this function through qrprocessor.py. (The template also lets Callback, Notify and DelayedNotify invoke it; they do not.)