Skip to content

Developer notes

Technical reference for developers who customize TrueLayer Pay by Bank. Every endpoint, header, field and status below is taken from the official TrueLayer documentation (https://docs.truelayer.com), read on 2026-11-11; the page is named next to each item. Fixtures in tests/fixtures are byte copies of the documentation examples (tests/fixtures/MANIFEST.tsv lists the URL of each file).

Item Value
Add-on id kv_truelayer
Function prefix fn_kv_truelayer_
Language variables kv_truelayer.*
CSS scope .kv-truelayer
PHP namespace Tygh\Addons\KvTruelayer
Preset payment (generated by kv new --kind integration)
Item Detail Documentation
Base URLs sandbox https://api.truelayer-sandbox.com, live https://api.truelayer.com Test payments in sandbox
Token POST https://auth.truelayer.com/connect/token (sandbox: auth.truelayer-sandbox.com), form fields grant_type=client_credentials, client_id, client_secret, scope=payments; answer {access_token, expires_in 3600, token_type Bearer, scope}; cached until shortly before it expires Generate an access token
Request signing header Tl-Signature = JWS with detached payload, alg ES512, kid = key id shown in Console, tl_version “2”, tl_headers including Idempotency-Key; signed string POST /path\nIdempotency-Key: <value>\n<body>; signature = raw R and S of ECDSA P-521 / SHA-512 (132 bytes). Keys: openssl ecparam -genkey -name secp521r1 -noout; public key uploaded in Console > Payments > Settings. Implemented in Signing, verified against the official vectors (tests/fixtures, github.com/TrueLayer/truelayer-signing test-resources) Sign your requests and make them idempotent; request-signing-v2.md
Idempotency header Idempotency-Key (UUID v4, valid 30 days); same key + same body = identical answer; same key + other body = 422 Idempotency-Key Reuse; 409 = request still in flight; Tl-Should-Retry Sign your requests and make them idempotent
Signature test POST /test-signature signed like any POST, answer 204 Sign your requests and make them idempotent, step 5
Merchant accounts GET /v3/merchant-accounts -> items[] {id, currency, account_identifiers, available_balance_in_minor, current_balance_in_minor, account_holder_name}; one GBP and one EUR per client id Get data about your merchant accounts; Merchant accounts
Create payment POST /v3/payments (signed, idempotent). Body: amount_in_minor, currency (GBP, EUR), payment_method {type bank_transfer, provider_selection {type user_selected, filter {countries, release_channel}, scheme_selection {type}}, beneficiary {type merchant_account, merchant_account_id, account_holder_name (max 40), statement_reference (max 18, GBP only)}}, user {name, email, phone, address}, metadata (max 10 string pairs), hosted_page {return_uri, country_code, language_code, max_wait_for_result}. Answer 201 {id, user.id, resource_token (15 min), status, hosted_page.uri} Create a payment; API reference “Create payment” (OpenAPI CreatePayment)
Get payment GET /v3/payments/{id} (bearer token only) -> payment with status, and per status executed_at, settled_at, failed_at, failure_stage, failure_reason, reversed_at, disputed_at, metadata Get data about a payment
Refund POST /v3/payments/{id}/refunds (signed, idempotent) body {amount_in_minor (omitted = full), reference (required, max 18)}; 202 {id}, 200 when the payment has not settled yet; GET /v3/payments/{id}/refunds -> items[]; GET /v3/payments/{payment_id}/refunds/{refund_id} Refund a payment; API reference “Create payment refund”
Hosted page Redirect to hosted_page.uri; the user returns to return_uri with payment_id appended (and error=tl_hpp_abandoned when abandoned); return_uri must be registered in Console and start with https:// Hosted page; Application settings
Webhooks one webhook URI per Console app; headers Tl-Signature, X-TL-Webhook-Timestamp; body {type, event_version, event_id, payment_id ...}; types used: payment_authorized, payment_executed, payment_settled, payment_failed, payment_creditable, payment_reversed, refund_executed, refund_failed; 2xx = delivered, retried for 72 hours; duplicates and out-of-order delivery possible Payment webhooks; Refund webhooks; Configure webhooks for your integration
Webhook signature Tl-Signature signed by TrueLayer, header jku must be https://webhooks.truelayer.com/.well-known/jwks (live) or https://webhooks.truelayer-sandbox.com/.well-known/jwks (sandbox); key chosen by kid (EC P-521 ES512, RS512 supported); signed string = POST + path of the registered URI + signed headers (X-Tl-Webhook-Timestamp, Content-Type) + raw body Configure webhooks for your integration
Statuses authorization_required, authorizing, authorized, executed, settled (merchant account only), failed (+ failure_stage, failure_reason); refunds pending, authorized, executed, failed Payment statuses; Payout and refund statuses
Errors RFC 7807 Problem Details: type, title, status, detail, trace_id, errors{path: [..]}; 401, 403, 400 (invalid parameters / invalid state), 404, 409, 422, 429, 502, 500 Payments API errors

Where the documentation is not consistent (and what the add-on does)

Section titled “Where the documentation is not consistent (and what the add-on does)”
  • The example requests on the “Create a payment” page place hosted_page inside payment_method; the OpenAPI schema (CreatePayment) and the page’s section “2.5 Hosted_page object” (“This object doesn’t exist inside any other object”) put it at the top level. The add-on sends it at the top level.
  • The payment_failed and refund_executed webhook examples contain a trailing comma and are not valid JSON as printed; the unit tests use the examples with only that comma removed.
  • The documentation states that GET endpoints take the bearer token; the Insomnia text of the refund page shows a Tl-Signature on GET. The OpenAPI reference lists Tl-Signature only on POST operations; the add-on signs POST requests only.
  • A return URI with a query string (as every CS-Cart address has) is not described. The add-on reads payment_id from the query and, if it was appended with a second ?, from the raw query string.
Class Role
Signing ES512 / JWS detached payload: sign requests, verify request and webhook signatures, JWK to PEM, key inspection and generation (OpenSSL only)
Client\BaseClient HTTP (cURL, TLS verification, timeouts), JSON or a signed string body, OAuth2 client-credentials with an encrypted token cache per mode, error mapping, masked logging
Client\Api The TrueLayer calls and the Problem Details parser
Client api() = client of the account’s current mode (new payments only), forTxn() = client of the mode and account a payment was created with (every call about an existing payment), the connection test
Accounts, Secret Credentials per mode (client secret, signing key sealed with AES-256-GCM), merchant account ids
PaymentService Start (hosted page), read-back sync with the merchant reference check, order status, refunds (rows under lock, reconcile through the refund list)
Transition Pure status logic (payment -> local status -> order status, never backwards)
Transactions Payment rows, open attempts, refunded amount, Multi-Vendor child shares
Webhook Signature check against the JWKS (cached one hour, one refresh per minute on an unknown kid), event ids
Config, Access Settings, who may see and manage what
Queue, Jobs, JobException, BusyException Job queue: back-off 5 min / 15 min / 1 h / 3 h / 12 h, per-job isolation, overlap lock; handlers in Jobs::handle() (webhook, sync, refund by row id), recurring work in Jobs::schedule()
Log Event log with masking, filters, CSV, retention
  • Several attempts of one order are separate rows; PaymentService::initialize() reads open attempts first and refuses while one is authorized / executed.
  • mode, account_id, merchant_account_id and a random UUID v4 idem_key are stored on the row at creation and used for every later call.
  • Every read-back is compared with the row by PaymentService::mismatch() (payment id, amount, currency, merchant account, metadata kv_txn / kv_shop).
  • A refund row (amount, order share, key, unique reference) is written under the per-payment GET_LOCK before the request; a busy lock writes and queues nothing; the queue resumes a row by id; the amount is checked against payment amount minus non-failed refunds under the lock.
  • Transition::merge() never moves a payment backwards; settled is the only paid state; reversed_at marks a payment reversed.
Table Purpose
?:kv_truelayer_accounts Credentials per company and mode (secret and key sealed), merchant ids (extra), last connection check, token cache
?:kv_truelayer_jwks Cached TrueLayer webhook key set per mode
?:kv_truelayer_log Log
?:kv_truelayer_jobs Queue
?:kv_truelayer_transactions, ?:kv_truelayer_refunds Payments (one row per attempt) and refunds
Hook Purpose
change_order_status_post Queue a full refund when the order reaches the configured status
prepare_checkout_payment_methods Hide the method when the add-on is disabled / unlicensed / not connected, the order currency is not GBP or EUR, or no merchant account is set
init_templater_post Upgrade synchronizer (only while an upgrade is pending)
Template hooks orders:tabs_extra (payment tab), addons:manage / tabs_content (licence), index:styles

The base URL can be overridden for tests in config.local.php (development only; the token endpoint and the webhook key set then also come from that address):

$config['kv_truelayer_base_url'] = 'http://127.0.0.1:11940';

node tests/mock-server/server.js 11940 starts a local stand-in built from the documentation examples; it verifies every Tl-Signature with the official public test key, enforces the CreatePayment schema and idempotency, and signs webhooks with the official test private key. node tests/e2e/e2e.mjs runs the HTTP end-to-end test, php tests/unit.php the unit tests (signing vectors, webhook signature, status mapping, idempotency, request builder).

Admin templates live in design/backend/templates/addons/kv_truelayer/. Do not edit them in a store; they are replaced on update.