Skip to content

737 — Where a REST klarna_payments order is confirmed ​

Problem ​

A REST-placed klarna_payments order was charged at Klarna and then cancelled by this deployment. KlarnaAdapter returned PENDING and designated the merchant_urls.notification webhook as "the single confirmation point". The handler for that webhook, Adv_checkout::klarnaPaymentsNotification(), had an empty body. The order never left PENDING, and the 180-minute abandonment sweep (AdvCancelIncompleteOrders) reversed it: stock, gift stock, points and the coupon. The defect is latent: klarna_payments is not in getExternalPayWays() (#673). Fixing it is a prerequisite for adding it there (#464, epic #738).

The webhook alone would not have fixed it ​

Klarna's pending-orders documentation says the following. This was read from web search results; the documentation page itself returned 404 when fetched, so it was not read first-hand.

  • merchant_urls.notification is called only for an order Klarna created with fraud_status: PENDING. Klarna posts FRAUD_RISK_ACCEPTED / REJECTED / STOPPED once its review decides, typically within 4–8 hours and at most 24. It retries every 10 minutes for 24 hours until it gets a 200.
  • That pending-review process is offered to US and UK merchants only, and is off by default.

For this platform's merchants, createOrder returns fraud_status: ACCEPTED (or REJECTED) synchronously, and the webhook never fires. The adapter already had that value in hand and discarded it. Legacy klarnaCreateOrderSuccess() writes PAID at exactly this point.

Decision ​

D1 — Synchronous arm (the common case).

  • When createOrder returns fraud_status: ACCEPTED, KlarnaAdapter returns PaymentInitResult(status: 'PENDING', confirmed: true).
  • PlaceOrderService then calls PaymentConfirmationService::confirmPayment() after placement and stock reservation. That call does PENDING → PAID, then dispatches OrderPaid. The place-order response reports PAID.
  • The adapter still never writes PAID itself, so confirmation keeps a single seam.
  • PENDING and REJECTED are never confirmed here.
  • A Rejected transition (the order is no longer PENDING) keeps the response at PENDING, so the response never reports a write that did not happen.

D2 — Notification arm (the pending-review case).

  • klarnaPaymentsNotification() delegates to Domains\Checkout\Payment\KlarnaNotificationHandler.
  • The handler reads order_id from the push body and resolves the order with Order\Repository::findByTranTicketAndPayway() (tran_ticket, payway = klarna_payments). An order in any status except PENDING and CANCELED (PAID, PENDING_ACCEPTED, shipped, refunded ...) is acknowledged without a Klarna call, because place-order hands the Klarna id to the customer and the URL can be replayed. CANCELED still reaches Klarna so that "accepted too late" is logged. Otherwise it re-reads the order from Klarna's Order Management API (Klarna::getOrder(), GET /ordermanagement/v1/orders/{id}).
  • It confirms only when Klarna itself reports fraud_status: ACCEPTED and the Klarna order status is not CANCELLED or EXPIRED.
  • A Klarna order id is a 36-character UUID and shop_order.tran_ticket was varchar(32), which cut it silently (or failed the write in strict mode) and made the lookup miss. Migration 20261110120000_widen_and_index_shop_order_tran_ticket widens it to 64 and indexes it, since this lookup is unauthenticated.
  • A transient failure (Klarna unreachable) answers 503 so that Klarna retries. Every terminal outcome answers 200.

D3 — Authentication. The caller of the notification URL is never trusted. A forged POST can at most cause one read of a Klarna order that this deployment already holds, and only Klarna's answer can confirm it. This makes the arm:

  • independent of #444, which concerns the secret on a different callback (merchant_urls.authorization) and is not used here;
  • independent of #742, because this route carries no order serial.

PaymentCallbackUrlBuilder::notificationUrl() keeps emitting the same route.

Not chosen:

  • merchant_urls.authorization: it is not sent on the create-order leg, and #444 blocks it.
  • Extending Rest\Checkout::verifyWithGateway(): that path is pulled by the customer and does not help a shopper who closed the tab. It can still be added later on top of Klarna::getOrder().

Deviations from the issue's acceptance criteria ​

  • AC 1 is_paid. It is not written. The criterion predates the product-owner ruling of 2026-09-04 on #754 (docs/decisions/747-payment-correctness-shared-write-paths.md), under which REST confirmation never stamps is_paid. The test asserts status = PAID and is_paid unchanged.
  • AC 1/2 harness. tests/Integration/Domains/Checkout/KlarnaRestConfirmationFlowTest.php is written as specified: CiDatabaseTestCase, a real Adv_checkout, and FakeInput. It needs the test database. The decision logic is also covered without one, by tests/Unit/Checkout/Payment/KlarnaNotificationHandlerTest.php and tests/Legacy/Checkout/AdvCheckoutKlarnaNotificationTest.php.
  • AC 3(b). The sweep now skips a debris row that is no longer PENDING. getDebrisOrders() selects status so the row carries it. In production the query already filters on that column, so this is defence in depth for forks that override the query. It also makes the AC observable.

Known limit (follow-up, not in this change) ​

An order that Klarna genuinely holds at PENDING can be cancelled by the 180-minute sweep before Klarna decides. This only happens for US/UK merchants with pending review enabled. A later ACCEPTED notification then finds the order CANCELED: confirmPayment() returns Rejected, which is logged and acknowledged. Closing this gap means making the sweep probe Klarna (Klarna::getOrder()) before it cancels a klarna_payments order.

A second gap has the same remedy. If confirmPayment() throws during place-order after Klarna accepted and charged the order, PlaceOrderService logs it (log_message, not the structured logger) and the order stays PENDING. Klarna never notifies an order it accepted synchronously, so nothing follows and the sweep cancels it. A sweep that asks Klarna first would cover both.

Follow-ups, filed: #1043 (the sweep asks Klarna before cancelling, which also covers the gap where confirmation threw) and #1044 (a Klarna arm in REST verifyWithGateway(), the "can be added later" above).

Last updated: