Implementing Klarna Checkout service not working

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
DmS
Posts: 56
Joined: 09 Aug 2021, 08:59

Implementing Klarna Checkout service not working

Post by DmS » 31 Aug 2021, 12:59

Hi.
I'm having issues with understanding how to setup Klarna KCO from https://github.com/MyOnlineStore/omnipa ... a-checkout

I've installed ai-payments
I installed Klarna Checkout via composer require myonlinestore/omnipay-klarna-checkout

When setting up the new service, I've set
Type : Payment
Provider : OmniPay

Then I got the default opt/values for OmniPay

Then I added username & secret from our sandbox account at klarna
and type: KlarnaCheckout (tried with Klarna.Checkout as well)

The service shows as available in checkout, but when I go to pay I get "Class '\Omnipay\KlarnaCheckout\Gateway' not found"
In the https://github.com/MyOnlineStore/omnipa ... ateway.php I can see that the namespace is MyOnlineStore\Omnipay\KlarnaCheckout

So either I don't understand what to write in option/value "Type", or I've missed something else, perhaps in config files?

Never set up things like this via OmniPay/Aimeos before, just in pure code. So all help is appreciated.

Cheers/Dan
_____________
Laravel 8 with Aimeos 2021.10.7 + Marketplace. Setup via composer. Mac with Valet and MySql 8.0.25

User avatar
aimeos
Administrator
Posts: 8057
Joined: 01 Jan 1970, 00:00

Re: Implementing Klarna Checkout service not working

Post by aimeos » 01 Sep 2021, 09:54

They use a different namespace like you mention (which isn't according to the Omnipay standards) and thus, the gateway class won't be found. The only option now is to create a custom Aimeos service payment provider that extends from the Omnipay provider class and overwrites the getProvider() method:
https://github.com/aimeoscom/ai-payment ... #L702-L712
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

DmS
Posts: 56
Joined: 09 Aug 2021, 08:59

Re: Implementing Klarna Checkout service not working

Post by DmS » 01 Sep 2021, 13:36

Ok, thank you.
That's unfortunate of them, especially since they are on Omnipay list of plugins.
I'll take a look at creating a custom provider as you suggest.

But in theory, should I change the namespace in their code, then it should work with your Omnipay privider?
I won't do that since it's breaking all their updates of course, but only so I understand.
/Dan
_____________
Laravel 8 with Aimeos 2021.10.7 + Marketplace. Setup via composer. Mac with Valet and MySql 8.0.25

User avatar
aimeos
Administrator
Posts: 8057
Joined: 01 Jan 1970, 00:00

Re: Implementing Klarna Checkout service not working

Post by aimeos » 02 Sep 2021, 06:01

DmS wrote: 01 Sep 2021, 13:36 That's unfortunate of them, especially since they are on Omnipay list of plugins.
Unfortunately, this doesn't mean that the driver fully complies to the specs as there's no test suite to verify that.
DmS wrote: 01 Sep 2021, 13:36 But in theory, should I change the namespace in their code, then it should work with your Omnipay privider?
I won't do that since it's breaking all their updates of course, but only so I understand.
Yes and you should create a PR or an issue telling them that their driver doesn't comply to Omnipay standards because this doesn't work:

Code: Select all

Omnipay\Omnipay::create( 'KlarnaCheckout' );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

DmS
Posts: 56
Joined: 09 Aug 2021, 08:59

Re: Implementing Klarna Checkout service not working

Post by DmS » 02 Sep 2021, 11:40

I'll post an issue.
For now I'm slowly getting closer.
I've created a new provider in my extension that overrides the getProvider method from OmniPay

Code: Select all

<?php
namespace Aimeos\MShop\Service\Provider\Payment;

class KlarnaKco 
    extends \Aimeos\MShop\Service\Provider\Payment\OmniPay
    implements \Aimeos\MShop\Service\Provider\Payment\Iface
{
    /**
     * Tries to get an authorization or captures the money immediately for the given
     * order if capturing isn't supported or not configured by the shop owner.
     *
     * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object
     * @param array $params Request parameter if available
     * @return \Aimeos\MShop\Common\Helper\Form\Standard Form object with URL, action
     *  and parameters to redirect to    (e.g. to an external server of the payment
     *  provider or to a local success page)
     */
    public function process( \Aimeos\MShop\Order\Item\Iface $order, array $params = [] ) : ?\Aimeos\MShop\Common\Helper\Form\Iface
    {
        // perform your actions
        return parent::process( $order, $params );
    }

    /**
     * Returns the KlarnaKCO gateway provider object.
     *
     * @return \MyOnlineStore\Omnipay\KlarnaCheckout\GatewayInterface Gateway provider object
     */
    protected function getProvider() : \MyOnlineStore\Omnipay\KlarnaCheckout\GatewayInterface
    {
        if( !isset( $this->provider ) )
        {
            $this->provider = OPay::create( $this->getValue( 'type' ) );
            $this->provider->setTestMode( (bool) $this->getValue( 'testmode', false ) );
            $this->provider->initialize( $this->getServiceItem()->getConfig() );
        }

        return $this->provider;
    }
}
The provider is now visible under payment provider, and to init it successfully, I need to add "type":

Code: Select all

\MyOnlineStore\Omnipay\KlarnaCheckout\Gateway
when I set it up instead of KlarnaCheckout, it seems to work, up to pressing pay, but then complains that the items array is empty, so now I'm getting to the point of figuring out all parameters it needs.

So thank you for the help so far :)
_____________
Laravel 8 with Aimeos 2021.10.7 + Marketplace. Setup via composer. Mac with Valet and MySql 8.0.25

User avatar
aimeos
Administrator
Posts: 8057
Joined: 01 Jan 1970, 00:00

Re: Implementing Klarna Checkout service not working

Post by aimeos » 03 Sep 2021, 06:12

Your implementation of getProvider() is wrong. It must be:

Code: Select all

	protected function getProvider() : \Omnipay\Common\GatewayInterface
	{
		if( !isset( $this->provider ) )
		{
			$this->provider = new \MyOnlineStore\Omnipay\KlarnaCheckout\Gateway();
			$this->provider->setTestMode( (bool) $this->getValue( 'testmode', false ) );
			$this->provider->initialize( $this->getServiceItem()->getConfig() );
		}

		return $this->provider;
	}
Alternatively, you may not need to overwrite this method and use the Omnipay service provider if you add "\MyOnlineStore\Omnipay\KlarnaCheckout\Gateway" as type.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

DmS
Posts: 56
Joined: 09 Aug 2021, 08:59

Re: Implementing Klarna Checkout service not working

Post by DmS » 06 Sep 2021, 10:10

Ah, ok.
Fixed the provider override where I was wrong, thanx.

Still getting the "The items parameter is required" when trying to pay

Also tried with the standard OmniPay provider and the type = \MyOnlineStore\Omnipay\KlarnaCheckout\Gateway

I'm getting the exact same result, so I think I'm missing something else here.
_____________
Laravel 8 with Aimeos 2021.10.7 + Marketplace. Setup via composer. Mac with Valet and MySql 8.0.25

DmS
Posts: 56
Joined: 09 Aug 2021, 08:59

Re: Implementing Klarna Checkout service not working

Post by DmS » 06 Sep 2021, 15:53

After a lot of debugging, everything points to the OmniPay gateway not getting the items from the basket plus having different parameter names than expected in the Klarna service.
When I'm dumping the object here, there is no info on what products are in the basket: $data = $this->getData( $base, $order->getId(), $params );
Then when processOrder tries to send $this->sendRequest( $order, $data ) , the validation of what the klarna service needs acc to their specification for authorisation fails.

So either I'm totally lost here, or there is something odd going on.
_____________
Laravel 8 with Aimeos 2021.10.7 + Marketplace. Setup via composer. Mac with Valet and MySql 8.0.25

User avatar
aimeos
Administrator
Posts: 8057
Joined: 01 Jan 1970, 00:00

Re: Implementing Klarna Checkout service not working

Post by aimeos » 07 Sep 2021, 08:22

The Omnipay driver requires the products which are not passed by the Omnipay service provider.
Like you already noticed, the driver uses non-standard names for URLs and maybe there are other things that are non-standard too. The only options are now:
1.) fork the repository and make the code standard compliant
2.) create a custom Aimeos service provider
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

DmS
Posts: 56
Joined: 09 Aug 2021, 08:59

Re: Implementing Klarna Checkout service not working

Post by DmS » 07 Sep 2021, 09:54

Right, thank you,
It will have to be a custom service provider then.
_____________
Laravel 8 with Aimeos 2021.10.7 + Marketplace. Setup via composer. Mac with Valet and MySql 8.0.25

Post Reply