Skip to content

Commit

Permalink
NTR: PISHPW-215: Klarna one (#421)
Browse files Browse the repository at this point in the history
Co-authored-by: Vitalij Mik <[email protected]>
  • Loading branch information
BlackScorp and Vitalij Mik committed Sep 9, 2024
1 parent d51ecce commit b15fe1e
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Components/Constants/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class PaymentMethod
const KLARNA_PAY_LATER = "klarnapaylater";
const KLARNA_PAY_NOW = "klarnapaynow";
const KLARNA_SLICE_IT = "klarnasliceit";

const KLARNA_ONE = "klarna";
const P24 = "przelewy24";
const APPLE_PAY = "applepay";
const APPLEPAY_DIRECT = "applepaydirect";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public static function getSupportedPaymentMethods()
PaymentMethod::TWINT,
PaymentMethod::BANCOMAT_PAY,
PaymentMethod::BLIK,
PaymentMethod::KLARNA_ONE
];
}

Expand Down
4 changes: 4 additions & 0 deletions Services/Mollie/Payments/PaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use MollieShopware\Services\Mollie\Payments\Requests\IDeal;
use MollieShopware\Services\Mollie\Payments\Requests\In3;
use MollieShopware\Services\Mollie\Payments\Requests\KBC;
use MollieShopware\Services\Mollie\Payments\Requests\Klarna;
use MollieShopware\Services\Mollie\Payments\Requests\PayLater;
use MollieShopware\Services\Mollie\Payments\Requests\PayNow;
use MollieShopware\Services\Mollie\Payments\Requests\PayPal;
Expand Down Expand Up @@ -111,6 +112,9 @@ public function createByPaymentName($paymentMethod)

case PaymentMethod::BLIK:
return new Blik();

case PaymentMethod::KLARNA_ONE:
return new Klarna();
}

throw new \Exception('Payment handler not found for: ' . $paymentMethod);
Expand Down
34 changes: 34 additions & 0 deletions Services/Mollie/Payments/Requests/Klarna.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace MollieShopware\Services\Mollie\Payments\Requests;

use MollieShopware\Components\Constants\PaymentMethod;
use MollieShopware\Services\Mollie\Payments\AbstractPayment;
use MollieShopware\Services\Mollie\Payments\Converters\AddressConverter;
use MollieShopware\Services\Mollie\Payments\Converters\LineItemConverter;
use MollieShopware\Services\Mollie\Payments\Exceptions\ApiNotSupportedException;
use MollieShopware\Services\Mollie\Payments\PaymentInterface;

class Klarna extends AbstractPayment implements PaymentInterface
{

/**
*/
public function __construct()
{
parent::__construct(
new AddressConverter(),
new LineItemConverter(),
PaymentMethod::KLARNA_ONE
);
}

/**
* @throws ApiNotSupportedException
* @return mixed[]|void
*/
public function buildBodyPaymentsAPI()
{
throw new ApiNotSupportedException('Klarna One does not support the Payments API!');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function testSupportedPaymentMethods()
PaymentMethod::IN3,
PaymentMethod::TWINT,
PaymentMethod::BANCOMAT_PAY,
PaymentMethod::BLIK
PaymentMethod::BLIK,
PaymentMethod::KLARNA_ONE,
];

$this->assertEquals($expected, PaymentMethodsInstaller::getSupportedPaymentMethods());
Expand Down
128 changes: 128 additions & 0 deletions Tests/PHPUnit/Services/Mollie/Payments/Requests/KlarnaOneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace MollieShopware\Tests\PHPUnit\Services\Mollie\Payments\Requests;

use MollieShopware\Components\Constants\PaymentMethod;
use MollieShopware\Services\Mollie\Payments\Exceptions\ApiNotSupportedException;
use MollieShopware\Services\Mollie\Payments\Models\Payment;
use MollieShopware\Services\Mollie\Payments\Models\PaymentAddress;
use MollieShopware\Services\Mollie\Payments\Models\PaymentLineItem;
use MollieShopware\Services\Mollie\Payments\Requests\Klarna;
use MollieShopware\Services\Mollie\Payments\Requests\PayLater;
use MollieShopware\Services\Mollie\Payments\Requests\PayNow;
use MollieShopware\Tests\Utils\Traits\PaymentTestTrait;
use PHPUnit\Framework\TestCase;

class KlarnaOneTest extends TestCase
{
use PaymentTestTrait;


/**
* @var PayLater
*/
private $payment;

/**
* @var PaymentAddress
*/
private $addressInvoice;

/**
* @var PaymentAddress
*/
private $addressShipping;

/**
* @var PaymentLineItem
*/
private $lineItem;

/**
*
*/
public function setUp(): void
{
$this->payment = new Klarna();

$this->addressInvoice = $this->getAddressFixture1();
$this->addressShipping = $this->getAddressFixture2();
$this->lineItem = $this->getLineItemFixture();

$this->payment->setPayment(
new Payment(
'UUID-123',
'Order UUID-123',
'20004',
$this->addressInvoice,
$this->addressShipping,
49.98,
[$this->lineItem],
'USD',
'de_DE',
'https://local/redirect',
'https://local/notify'
)
);
}

/**
* This test verifies that the Payments-API request
* for our payment is correct.
* Please note, Klarna does not work with the payments API
* so this is just an empty array
*/
public function testPaymentsAPI()
{
$this->expectException(ApiNotSupportedException::class);

$this->payment->buildBodyPaymentsAPI();
}

/**
* This test verifies that the Orders-API request
* for our payment is correct.
*/
public function testOrdersAPI()
{
$expected = [
'method' => PaymentMethod::KLARNA_ONE,
'amount' => [
'currency' => 'USD',
'value' => '49.98',
],
'redirectUrl' => 'https://local/redirect',
'webhookUrl' => 'https://local/notify',
'locale' => 'de_DE',
'orderNumber' => '20004',
'payment' => [
'webhookUrl' => 'https://local/notify',
],
'billingAddress' => $this->getExpectedAddressStructure($this->addressInvoice),
'shippingAddress' => $this->getExpectedAddressStructure($this->addressShipping),
'lines' => [
$this->getExpectedLineItemStructure($this->lineItem),
],
'metadata' => [],
];

$requestBody = $this->payment->buildBodyOrdersAPI();

$this->assertSame($expected, $requestBody);
}

/**
* This test verifies that we can set a custom expiration date
* for our Orders API request.
*/
public function testExpirationDate()
{
$dueInDays = 5;
$expectedDueDate = date('Y-m-d', strtotime(' + ' . $dueInDays . ' day'));

$this->payment->setExpirationDays($dueInDays);
$request = $this->payment->buildBodyOrdersAPI();

$this->assertEquals($expectedDueDate, $request['expiresAt']);
}
}

0 comments on commit b15fe1e

Please sign in to comment.