Developer notes
Technical reference for developers who customize Klarna Payments.
Add-on identity
Section titled “Add-on identity”| Item | Value |
|---|---|
| Add-on id | kv_klarna_payments |
| Function prefix | fn_kv_klarna_payments_ |
| Language variables | kv_klarna_payments.* |
| CSS scope | .kv-klarna (pay page), .kv-klarna-payments (admin) |
| PHP namespace | Tygh\Addons\KvKlarnaPayments |
| Processor | kv_klarna_payments.php, name “Klarna Payments (Kavio Labs)” |
Sources (API versions)
Section titled “Sources (API versions)”Every endpoint and field comes from Klarna’s OpenAPI specifications, read 2026-11-24 from a public copy of Klarna’s own files, plus the docs.klarna.com pages named below. The OpenCart edition of this product (2026-11-07) left some points unverified; they are verified here against the specifications.
| What | Where used | Source |
|---|---|---|
Hosts per region and environment: api.klarna.com / api.playground.klarna.com, api-na.klarna.com / api-na.playground.klarna.com, api-oc.klarna.com / api-oc.playground.klarna.com |
Config::HOSTS |
docs.klarna.com/api/api-urls |
| HTTP Basic with the API credentials (username : password) | Client\BaseClient::authHeaders() |
Payments API “Authentication” |
POST /payments/v1/sessions (createCreditSession); required purchase_country, purchase_currency, order_amount, order_lines; answer merchant_session (session_id, client_token, payment_method_categories[] with identifier, name, asset_urls.standard) |
Api::createSession, PaymentService::prepare |
Klarna Payments API V1 1.0.0 |
GET /payments/v1/sessions/{session_id} (readCreditSession) |
Api::readSession (reused session) |
same |
POST /payments/v1/authorizations/{authorizationToken}/order (createOrder): create_order_request (same fields + auto_capture, billing_address, shipping_address, merchant_reference1, merchant_urls); 200 order (order_id, redirect_url, fraud_status, authorized_payment_method); 400 constraint, 403, 404 authorization does not exist, 409 data does not match the session |
Api::createOrder, PaymentService::place |
same |
DELETE /payments/v1/authorizations/{authorizationToken} (cancelAuthorization, 204) |
changed order, lost answer | same |
merchant_urls: confirmation, notification (“URL for notifications on pending orders”, max 2000) |
PaymentService::payload |
same, schema merchant_urls |
address: given_name, family_name, email, street_address, street_address2, postal_code (max 10), city, region, country, phone; text max 99 |
PaymentService::address |
same, schema address |
order_line: type (physical, discount, shipping_fee, sales_tax, digital, gift_card, store_credit, surcharge), name (max 255), reference (max 255), quantity, unit_price, tax_rate, total_amount, total_discount_amount, total_tax_amount, product_url |
Transactions::shareMath |
Multi-Vendor split: each child order’s share = floor(amount x child total / parent total); the rounding rest goes only to the last open child order; a cancelled child’s share is released, never captured by another child |
Lines |
same, schema order_line; tax rules: docs.klarna.com “tax handling” (total_tax_amount = total_amount - total_amount * 10000 / (10000 + tax_rate), +-1; US: sales_tax line, no tax keys) |
|
JS SDK https://x.klarnacdn.net/kp/lib/v1/api.js, window.klarnaAsyncCallback, Klarna.Payments.init({client_token}), load({container, payment_method_category}, data, cb) -> show_form, authorize({payment_method_category}, data, cb) -> approved, authorization_token, show_form; customer data in authorize(); order within 60 minutes |
js/addons/kv_klarna_payments/pay.js |
docs.klarna.com Klarna Payments “Step 2: check out” + SDK reference |
GET /ordermanagement/v1/orders/{order_id} (getOrder): status (AUTHORIZED, PART_CAPTURED, CAPTURED, CANCELLED, EXPIRED, CLOSED), fraud_status (ACCEPTED, PENDING, REJECTED), order_amount, original_order_amount, captured_amount, refunded_amount, remaining_authorized_amount, expires_at (ISO 8601, “can only be captured until this time”), merchant_reference1, purchase_currency, captures[] (capture_id, reference, captured_amount), refunds[] (refund_id, reference, refunded_amount) |
PaymentService::syncLocked, adoptForeign, attach |
Klarna Order Management API 1.0, schemas Order, Capture, Refund |
POST .../captures (captureOrder): CaptureObject (captured_amount required, description, order_lines, reference, shipping_info[] with shipping_company, tracking_number, max 100); 201 with headers Capture-Id, Location |
Api::capture |
same |
POST .../refunds (refundOrder): RefundObject (refunded_amount required, description, order_lines, reference); 201 with Refund-Id, Location; 403 REFUND_NOT_ALLOWED |
Api::refund |
same + docs.klarna.com “refund orders and manage authorizations” |
POST .../cancel (cancelOrder, 204 / 403), POST .../release-remaining-authorization (204 / 403 NOT_ALLOWED), POST .../extend-authorization-time (extendAuthorizationTime, 204 / 403) |
Api::cancel, release, extend |
same |
Header Klarna-Idempotency-Key on Order Management POSTs: “unique and is recommended to be a UUID version 4 … only the key matters”; protects retries within 24 hours |
Api::uuid, PaymentService::operate, retryOp |
same (parameter description) + docs.klarna.com “escalation and retry policy” |
Error body: correlation_id, error_code, error_messages[] |
BaseClient::errorFrom |
ErrorV2 / ErrorMessageDto |
Pending orders: Klarna POSTs order_id and event_type (FRAUD_RISK_ACCEPTED, FRAUD_RISK_REJECTED, FRAUD_RISK_STOPPED) to the notification URL and retries until HTTP 200 |
PaymentService::notify (the body is a hint; the order is read back) |
docs.klarna.com “pending orders” |
Not verified against a live Klarna account: everything above is proven against the local stand-in tests/mock-server/server.js, which copies the shapes of the specifications and applies Klarna’s documented line and tax validation. No playground credentials were available while building 1.0.0. Run one playground order per region you use before going live.
Structure
Section titled “Structure”| Class | Role |
|---|---|
Client\BaseClient, Client\Api |
cURL with TLS verification, JSON, Basic auth, Klarna error mapping, masked logging; every Klarna endpoint |
Client |
Client of the current mode, client of a payment’s own mode (forTxn), the connection test |
Accounts, Secret |
API username / password per environment and the region; password sealed with AES-256-GCM |
Config, Access |
Settings, hosts, markets, locales; who may see what; pay page key (HMAC of order id, time and e-mail) |
Lines |
CS-Cart order -> Klarna order lines (both tax calculation methods, prices with / without tax, order discounts, gift certificates, currency conversion, US sales tax, rounding) |
Transition |
Local states and the order status each one leads to (never backwards) |
Transactions |
Payment attempts and Order Management operations, Multi-Vendor shares |
PaymentService |
Pay page session, place, unknown result + attach, read-back, notification, capture / refund / cancel / release / extend, status hook |
Queue, Jobs |
Read-backs (sync) and repeats of unconfirmed operations (op) |
Log |
Log with masking, filters, CSV, retention |
Flow and safety
Section titled “Flow and safety”- The processor script checks market, currency and connection and redirects to
kv_klarna_payments.pay?order_id=&key=(key = HMAC, never stored). - The pay page creates (or reuses for 30 minutes, same lines) a session and shows the categories;
Cache-Control: no-store; never a cacheable dispatch of the full page cache. kv_klarna_payments.authorize(POST, security hash) takes a per-order MySQL lock, rebuilds the lines from the order and compares them with the session’s (changed:DELETEthe authorization, answer “changed”), claimssession -> creatingatomically, calls createOrder withauto_capture: false.- A lost createOrder answer (timeout, 5xx): the exact createOrder body and the token are stored on the attempt BEFORE the request (Klarna “Retry order creation”). The identical request is repeated once at once, then by the queue job
placewhile the token lives (60 minutes); Klarna answers a repeat of the same body with the same order_id (a different body gets 409). The attempt stayscreatingmeanwhile (it blocks a second payment). Only when the token’s life is over without an answer does it becomeunknown(the order goes to the review status, the tab offers “Attach Klarna order”, checked against merchant reference, amount and currency). The authorization is released (DELETE) only after a definitive rejection (4xx). - Order Management: the op row with its UUID v4 key and the request body is written before the request; a lost answer leaves it
unknown, the queue repeats the same key and body for 24 hours (the reference = the key, so a read-back finds it), then marks it failed. Only one money operation per payment is open at a time. - Totals (
captured_minor,refunded_minor) are read from Klarna; confirmed operations are a floor, never added up. - The order status hook runs only for changes the add-on did not make itself (
PaymentService::$busy).
Hooks used
Section titled “Hooks used”| Hook | Handler |
|---|---|
change_order_status_post |
capture / cancel / refund on the configured statuses |
prepare_checkout_payment_methods |
hides the method (inactive, not connected, market / currency); “Never the default payment method” moves it behind the other methods |
init_templater_post |
upgrade synchronizer (only while pending) |
Template orders:tabs_extra (admin) |
Klarna tab |
Template index:styles (storefront) |
pay page styles |
Database tables
Section titled “Database tables”| Table | Purpose |
|---|---|
?:kv_klarna_payments_accounts |
Credentials (password sealed), region, last connection check |
?:kv_klarna_payments_transactions |
Attempts: session, Klarna order id, status, fraud status, amounts, lines, expiry |
?:kv_klarna_payments_ops |
Capture, refund, cancel, release, extend with idempotency key, request body, Klarna id |
?:kv_klarna_payments_log, ?:kv_klarna_payments_jobs |
Log, queue |
php81 tests/unit.php # also php83; fixtures from the spec examplesnode tests/mock-server/server.js 11994node tests/e2e/e2e.mjs --store http://localhost:8171 --db cscart_cs417 --dir dev-env/stores/cs417Test overrides in config.local.php (loopback hosts only, ignored otherwise):
$config['kv_klarna_payments_base_url'] = 'http://127.0.0.1:11994';$config['kv_klarna_payments_sdk_url'] = 'http://127.0.0.1:11994/sdk.js'; // browser test: SDK stub with the documented call shapesNot in 1.0.0
Section titled “Not in 1.0.0”On-site Messaging, Klarna Checkout / Express Checkout, customer tokens and recurring payments, finalize() for multi-step checkouts, updating the authorization amount (PATCH authorization) after an order edit, extend payment due date, hosted payment page.