Klarna Docs - Advanced configuration

Advanced configuration

Additional configuration can be made by a Magento developer. These topics assume advanced knowledge of Magento development. A custom module will need to be created to accomplish these tasks.

Custom external payment methods

A custom external payment method can be added as an option by creating a custom module to include it. The external payment method must be approved by Klarna. Please consult your Klarna Representative for a list of approved external payment methods.  

The external payment method must provide its own checkout page. If the customer selects this payment method, Klarna will redirect the customer to this page using the “redirect URL” and that page will need to collect the customers information along with shipping and payment details.

The adding of a method can be accomplished by adding xml to a klarna.xml file in your new module. See the example code below:

MARKUP
<?xml version="1.0"?>
<klarna xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Klarna_Base:etc/klarna.xsd">
    <external_payment_methods>
        <your_method_code translate="name" ifconfig="payment/your_method_code/active">
            <label>Your Method</label>
            <name>Your Method</name>
            <redirect_url><![CDATA[{{secure_base_url}}method/start]]></redirect_url>
            <image_url><![CDATA[{{secure_base_url}}yourmethod.png]]></image_url>
            <description>Checkout using my method.</description>

Custom merchant checkbox

A custom checkbox can be added to the Klarna Checkout by creating a custom module to include it as an option. 

You will need to create two observers. One will validate whether the checkbox should be shown. The other will trigger the events on order placement based on the status of the checkbox.  Each event will have a prefix of kco_ added automatically to them when they are called.  You should account for this in your events.xml file

The adding of the checkbox can be accomplished by adding xml to a custom klarna.xml file in your new module’s etc directory. 

Example code:

MARKUP
<?xml version="1.0"?>
<klarna xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Klarna_Base:etc/klarna.xsd">
    <merchant_checkbox id=”your_checkbox” translate="label text">
        <label>Your Checkbox</label>
        <text>Click our checkbox</text>
        <validation_event>merchant_checkbox_your_checkbox_validation</validation_method>
        <save_event>merchant_checkbox_your_checkbox_save</save_method>
    </merchant_checkbox>
</klarna>

Below are examples of both observers:

PHP
class MerchantCheckboxNewsletterSignup implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        if ($observer->getChecked()) {
            // Do something...
        }
    }
}

Multiple Checkbox Support

If you need to programmatically enable/disable a checkbox or change it’s required or “checked by default” flag, you can create a plugin for \Klarna\Kco\Model\Checkout\Checkbox::getAdditionalCheckboxes($quote)

On order placement, a custom event for each individual checkbox is fired. The naming convention for these events will be kco_[checkbox ID]_save.

Custom themes

When using a custom theme not based on Magento’s Luma theme, it may be necessary to customize the Klarna checkout pages.  What you will need to do is add an override for klarnacheckout.xml to your theme (in the directory structure Klarna_Kco/page_layout under your theme's design directory (potentially app/design/frontend/<namespace>/<modulename>).  You will probably want to use the same layout file from your theme being used in Magento_Checkout/page_layout/checkout.xml. This will add your site's header and footer back into the page at the correct places.  If you instead want to remove your site's header & footer from the checkout (as some cart abandonment experts advise) then you will need to add code to that same klarnacheckout.xml file to remove your header container/blocks.

A/B testing

If A/B testing is necessary, it’s recommended to use a third party service to accomplish testing. Example services are Google or Optimizely.

If you wish to create your own test programmatically, the Klarna Checkout Module has an observer event that can be observed to determine if Klarna checkout should load. You will need to create a new module to add the observer method.

Example -  observer observing the event ‘kco_override_load_checkout’:

PHP
public function execute(\Magento\Framework\Event\Observer $observer)
{
    if ($this->getForceUseKlarna()) {
        // Force user to use Klarna
        $observer->getOverrideObject()->setForceEnabled(true);
        return;
    }
    if ($this->getForceUseDefaultCheckout()) {
        // Force customer to use default checkout
        $observer->getOverrideObject()->setForceDisabled(true);

Adding EMD data

Developers can add EMD (extra merchant data) by creating their own custom module by following the guidelines below:

1. In etc/di.xml file, add a section like below to register a plugin class to generate and update EMD data

MARKUP
<type name="Klarna\Base\Model\Api\Parameter">
        <plugin name="CUSTOM_PLUGIN" type="Vendor\Module\Plugin\CustomPlugin" sortOrder="10"/>
 </type>

2. This plugin class will provide a before method for setRequest function as example below:

PHP
public function beforeSetRequest (\Klarna\Base\Model\Api\Parameter $subject, array $request)
    {
        if ($this->attachmentExists($request)) {
            $request['attachment'] = $this->updateAttachment($request);
        } else {
            $createAttachment = $this->createAttachment();
            if ($createAttachment) {
                $request['attachment'] = $this->createAttachment();
            }
        }

The format of attachment (as returned from function createAttachment() or updateAttachment()) will be like below, where variable $attachmentBody is an array

PHP
[
        'content_type' => 'application/vnd.klarna.internal.emd-v2+json',
        'body' => json_encode($attachmentBody)
 ];