Klarna Docs - Additional purchase validations

Additional purchase validations


In this post you will learn where to hook into the module to extend the validations for Klarna Checkout before the purchase happens.


It is very important to know when the real purchase creation should happen in the Klarna System. For the Klarna Checkout it happens when the customer clicks the purchase button. Before Klarna creates the order in the Klarna system they will send a request to the shop to ensure that the data are all correct. This request is the validation callback. The Klarna Checkout module has some default validations for example:

  • Checking if order amount between Klarna and the shop are the same
  • Checking if the selected shipping method is the same between Klarna und the shop

Besides the default validation other validations can be easily added.

Why this can be useful

There can be always cases which needs to be tested since they are to specific. Since the Klarna Checkout module is a very generic module which is used in different markets there can be market specific cases which must be checked. Moreover when using 3rd party libraries which are extended some classes or database tables (for example added more fields to a customer account) these things can not be evaluated from the Klarna Checkout module. For these cases own validations should be added.

Steps to extend the Klarna Checkout validation logic

1. Checkout how Klarnas validation logic is working:The core validation logic is written down in vendor/Klarna/Kco/etc/di.xml

<type name="Klarna\Kco\Model\Checkout\Validation">
     <argument name="validations" xsi:type="array">
        <item name="shipping_amount" xsi:type="object">Klarna\Kco\Model\Checkout\Validations\ShippingAmount</item>
        <item name="shipping_method" xsi:type="object">Klarna\Kco\Model\Checkout\Validations\ShippingMethod</item>
        <item name="totals" xsi:type="object">Klarna\Kco\Model\Checkout\Validations\OrderTotal</item>

You can see that some default validation classes are already used and that the module makes use of an array. All the validation classes are implementing the interface Klarna/Kco/Api/CheckoutValidationInterface.php. When checking out the class "Klarna\Kco\Model\Checkout\Validation" you will see that it will iterate over the array.

2. Create the validation class
An example class for a own validation can look like the following:

namespace A\B\C;

use Klarna\Kco\Api\CheckoutValidationInterface;

class MyValidation implements CheckoutValidationInterface
     * {@inheritDoc}
    public function validate(DataObject $request, CartInterface $quote): void

3. Extend Klarna's validation configuration

Now you just need to update your etc/di.xml file the respective module directory. For adding the class to the array the content must look like:

<type name="Klarna\Kco\Model\Checkout\Validation">
     <argument name="validations" xsi:type="array">
        <item name="my_validation" xsi:type="object">A\B\C\MyValidation</item>

4. Run the Magento cli commands so that Magento knows about this new entry.

After these steps your own validation will now be used inside the Klarna Checkout workflow.