Problem/Motivation
Klarna API key passwords may be up to 255 characters in length. The default maxlength of Drupal text fields is 128 characters. This has not been overridden in the password field in the config form, resulting in being unable to store a full Klarna API key password.
Steps to reproduce
Install module and set up a payment gateway. Attempt to copy and paste a Klarna API key password longer than 128 characters into the password field.
Proposed resolution
Add'#maxlength' => 255 into the password field definition in /src/Plugin/Commerce/PaymentGateway/Klarna.php
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | 2025-02-04_12-51.png | 24.5 KB | tuutti |
Issue fork commerce_klarna_payments-3499488
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
cruze72 commentedI have tested the latest version of the module beta-3.0.0 and get the following error message:
An error occurred for order #698: [Klarna\ApiException]: [401] Client error: `POST https://api.klarna.com/payments/v1/sessions` resulted in a `401 Unauthorized` response: @"error_code": "PERMISSION_DENIED", "error_message": "Authentication failed or permission not granted"
$form['password'] = [
'#type' => 'textfield',
'#title' => $this->t('Password'),
'#default_value' => $this->configuration['password'],
'#maxlength' => 255,
];
I added suggested fix but still get the error.
Comment #5
tuutti commentedCan you check if overriding the API key in settings.php works?
Add something like this to your settings.php file, just replace the
klarna_paymentswith the actual ID of your Klarna payment gateway:$config['commerce_payment.commerce_payment_gateway.klarna_payments']['configuration']['password'] = '123';You can check if the new value is set with drush:
drush cget commerce_payment.commerce_payment_gateway.klarna_payments --include-overriddenComment #6
cruze72 commentedHi,
Unfortunately not.
Error when processing a transaction:
An error occurred for order #709: [Klarna\ApiException]: [401] Client error: `POST https://api.klarna.com/payments/v1/sessions` resulted in a `401 Unauthorized` response: @"error_code": "PERMISSION_DENIED", "error_message": "Authentication failed or permission not granted"
Error when running drush command:
[error] Config commerce_payment.commerce_payment_gateway.klarna_payments does not exist
I have tested with password key in the following formats:
Comment #7
tuutti commentedYou need to change the
klarna_paymentspart to match whatever your payment gateway ID is.You can find it by going to Commerce -> Configuration -> Payment gateways.
It should be the value of "ID" field. In my case it's

klarna_payments:So if your ID were called
klarna_something, then the drush command should be:drush cget commerce_payment.commerce_payment_gateway.klarna_something --include-overriddenAnd the $config:
Comment #8
cruze72 commentedGot it. Here is the result:
uuid: f7d15d54-b589-4a9c-bfdf-55716c0716d7
langcode: en
status: true
dependencies:
module:
- commerce_klarna_payments
id: klarna
label: Klarna
weight: null
plugin: klarna_payments
configuration:
display_label: 'Klarna Payments'
mode: live
payment_method_types:
- 'Credit card'
collect_billing_information: true
username: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
password: bnlNIW8pL0NXUGw_TTJLKjY4akpqRXB0SVdmSmxrVU0sYzQyZDg0YzktYjYxMy00NzJlLThlYjYtYjM0ZmYwNDQ4Y2VhLDEsNWJIQTU2ajFBa3Ro
region: eu
cancel_fraudulent_orders: false
options:
color_border: ''
color_border_selected: ''
color_text: ''
color_details: ''
radius_border: ''
conditions: { }
conditionOperator: AND
I am correct in taking the client API keys as the username and password from the Klarna settings and not SMTP or client ID?
Comment #9
cruze72 commentedUpon further testing it appears the same error message is shown with the similar Commerce Klarna Checkout module.
"PERMISSION_DENIED", "error_message": "Authentication failed or permission not granted"
Would this indicate that either Klarna has changed their API or i am using the wrong login credentials.
Within the Klarna merchant portal there are three options for integrating:
1.Klarna API keys
2. Client ID with password
3. SMTP authentication
I've tried with all three variants and in the case of #1 - with and without the 'klarna_live_api_' prefix in the password.
I am assuming i should use #1 ...?
Comment #10
tuutti commentedGo to Settings -> Klarna API Keys -> click "Generate new Klarna API Key" button and it should give a downloadable text file with "username" and "password".
The password should include the
klarna_live_api_prefixComment #12
tuutti commented