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

CommentFileSizeAuthor
#7 2025-02-04_12-51.png24.5 KBtuutti
Command icon 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

gillmead created an issue. See original summary.

cruze72’s picture

I 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.

fernando_silva made their first commit to this issue’s fork.

tuutti’s picture

I have tested the latest version of the module beta-3.0.0 and get the following error message

Can you check if overriding the API key in settings.php works?

Add something like this to your settings.php file, just replace the klarna_payments with 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-overridden

cruze72’s picture

Hi,
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:

$config['commerce_payment.commerce_payment_gateway.klarna_payments']['configuration']['password']
= '<my passkey without 'klarna_live_api'_>';
$config['commerce_payment.commerce_payment_gateway.klarna_payments']['configuration']['password']
= 'klarna_live_api_<my passkey>';
tuutti’s picture

StatusFileSize
new24.5 KB

You need to change the klarna_payments part 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:
screenshot of payment gateway id

So if your ID were called klarna_something, then the drush command should be:

drush cget commerce_payment.commerce_payment_gateway.klarna_something --include-overridden

And the $config:

$config['commerce_payment.commerce_payment_gateway.klarna_something']['configuration']['password']
= 'apikey';
cruze72’s picture

Got 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?

cruze72’s picture

Upon 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 ...?

tuutti’s picture

Go 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_ prefix

tuutti’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.