Skip to content

fix(beam): confirm card payments, allow payment retries, stop cancelling on a failed charge - #222

Merged
thunpisit merged 1 commit into
mainfrom
fix/beam-provider
Oct 25, 2026
Merged

thunpisit merged 1 commit into
mainfrom
fix/beam-provider

Conversation

@thunpisit

Copy link
Copy Markdown
Contributor

Phase 2.2 of the bactrack.in.th Beam work. Closes #219; groundwork for #218.

Bugs fixed

  1. Card / hosted-page payments never confirmed.
    • createCharge read paymentLinkId; POST /payment-links returns {id, url} (OpenAPI CreatePaymentLinkResponse), so no link id was ever stored. The test fixture used the wrong shape too, which is why tests passed.
    • payment_link.paid ({paymentLinkId, status:"PAID", order:{netAmount, referenceId}}) normalized to pending and was ignored. It now normalizes to succeeded with the nested reference/amount.
  2. Second payment link / QR for an order always failed (PromptPay QR can't be regenerated: fixed idempotency key + new expiryTime makes Beam reject the retry #219). The key was fixed per order while every body carries a fresh expiry, so Beam returned 412 for the key's 12h retention. The order page's "Complete payment" retry could never succeed. ChargeInput.attempt scopes the key (<orderId>:link:<a>, <orderId>:qr:<a>; first tries unchanged). /checkout/pay validates ^[A-Za-z0-9-]{1,40}$; the order page sends a fresh attempt per press.
  3. charge.failed cancelled the order (found while porting, not in the issues). A declined card retried on Beam's page, or a new QR after the first expired, then landed on a cancelled order, which markPaid ignores: customer charged, order cancelled, stock released. Failure is now logged only; the reservation sweep releases unpaid orders.
  4. Shared settlement. The webhook's succeeded block moved verbatim into settleBeamPayment (reused by the status check in the next PR), behind checkSettlement (amount + reference must match). The purchase analytics event is now winner-only like the receipt; it used to be recorded once per redelivery. Payment on a cancelled/refunded order logs an error for manual refund/reinstate.

Also adds PaymentProvider.getChargeStatus (Beam: GET /charges/{id}, 404 → GET /payment-links/{id}, PAID → GET /charges?sourceId= for the real charge id). Never throws; an error is ok:false, never failed.

Tests

  • beam-v460.test.ts (12): keys with/without attempt, wire headers, payment_link.paid shape, charge.failed, getChargeStatus (charge / link PAID→charge / EXPIRED·DISABLED·VOIDED / ACTIVE / 500 / network / empty id), checkSettlement.
  • Route-level integration through the real webhook handler (4): failed leaves order pending and a later success settles it; payment_link.paid settles; amount mismatch → 200 settled:false, still pending; redelivered success → one purchase event.
  • lint 0 errors (55 warnings, = main) · check 0 · 1146/1146 · build · contract-guard · css-inventory +0

Not yet verified

Real Beam Playground: the payment_link.paid payload is from Beam's docs, not a captured delivery. The Playground smoke in the test plan covers it.

🤖 Generated with Claude Code

…ing on a failed charge

Four Beam integration bugs, found on bactrack.in.th (#217–#219):

- Card / hosted-page payments were never confirmed. createCharge read
  `paymentLinkId` but POST /payment-links returns `{id, url}`, so no
  link id was stored; and `payment_link.paid` ({status: "PAID",
  order: {referenceId, netAmount}}) normalized to "pending" and was
  ignored. Both fixed; the test fixture now uses Beam's real shape.
- Any second payment link or QR for an order failed (#219). The key
  was fixed per order while the body carries a fresh expiry, so Beam
  answered 412 for the key's 12h retention — the order page's
  "complete payment" button could never work. ChargeInput.attempt
  scopes the key (`<orderId>:link:<attempt>`, `<orderId>:qr:<attempt>`);
  first tries keep their old keys. /checkout/pay validates and passes
  it; the order page sends a fresh one per press.
- charge.failed cancelled the order. A declined card retried on the
  hosted page, or a new QR after the first expired, then paid an order
  that markPaid ignores once cancelled: customer charged, order
  cancelled, stock released. A failure is now logged only; unpaid
  orders are released by the reservation sweep.
- Payment side effects are extracted into settleBeamPayment (shared
  with the upcoming status check, #218) behind checkSettlement, which
  refuses an amount or reference that doesn't match the order. The
  purchase analytics event is now winner-only like the receipt (it was
  recorded once per webhook redelivery). A payment landing on a
  cancelled/refunded order is logged as an error for manual action.

Adds PaymentProvider.getChargeStatus (Beam: charge, falling back to
payment link → its SUCCEEDED charge), used by the next PR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thunpisit
thunpisit merged commit 8a58e6b into main Oct 25, 2026
2 checks passed
@thunpisit thunpisit mentioned this pull request Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PromptPay QR can't be regenerated: fixed idempotency key + new expiryTime makes Beam reject the retry

1 participant