diff --git a/Components/Constants/PaymentMethod.php b/Components/Constants/PaymentMethod.php index 8dd85871..aeb0b5c6 100644 --- a/Components/Constants/PaymentMethod.php +++ b/Components/Constants/PaymentMethod.php @@ -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"; diff --git a/Components/Installer/PaymentMethods/PaymentMethodsInstaller.php b/Components/Installer/PaymentMethods/PaymentMethodsInstaller.php index 0829072b..8987e19d 100644 --- a/Components/Installer/PaymentMethods/PaymentMethodsInstaller.php +++ b/Components/Installer/PaymentMethods/PaymentMethodsInstaller.php @@ -122,6 +122,7 @@ public static function getSupportedPaymentMethods() PaymentMethod::TWINT, PaymentMethod::BANCOMAT_PAY, PaymentMethod::BLIK, + PaymentMethod::KLARNA_ONE ]; } diff --git a/Services/Mollie/Payments/PaymentFactory.php b/Services/Mollie/Payments/PaymentFactory.php index 3c7dda48..b78441d7 100644 --- a/Services/Mollie/Payments/PaymentFactory.php +++ b/Services/Mollie/Payments/PaymentFactory.php @@ -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; @@ -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); diff --git a/Services/Mollie/Payments/Requests/Klarna.php b/Services/Mollie/Payments/Requests/Klarna.php new file mode 100644 index 00000000..092eddc5 --- /dev/null +++ b/Services/Mollie/Payments/Requests/Klarna.php @@ -0,0 +1,34 @@ +assertEquals($expected, PaymentMethodsInstaller::getSupportedPaymentMethods()); diff --git a/Tests/PHPUnit/Services/Mollie/Payments/Requests/KlarnaOneTest.php b/Tests/PHPUnit/Services/Mollie/Payments/Requests/KlarnaOneTest.php new file mode 100644 index 00000000..d9b97d51 --- /dev/null +++ b/Tests/PHPUnit/Services/Mollie/Payments/Requests/KlarnaOneTest.php @@ -0,0 +1,128 @@ +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']); + } +}