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:
Besides the default validation other validations can be easily added.
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.
1. Checkout how Klarnas validation logic is working:The core validation logic is written down in vendor/Klarna/Kco/etc/di.xml
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:
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:
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.