From 856072036728e07e488c46d53163373e106afe3c Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Fri, 9 Feb 2024 10:01:31 +0100 Subject: [PATCH] NTR: PIHPS-205 - add new payment method blik --- .../Service/MollieLimitsRemover.php | 15 ++++- src/Handler/Method/BlikPayment.php | 32 +++++++++ src/Resources/config/services/handlers.xml | 8 +++ .../Provider/ActivePaymentMethodsProvider.php | 8 ++- .../ActivePaymentMethodsProviderInterface.php | 2 +- src/Service/PaymentMethodService.php | 2 + .../e2e/storefront/payment-methods/blik.cy.js | 43 ++++++++++++ .../Builder/Payments/BlikOrderBuilderTest.php | 67 +++++++++++++++++++ .../Payments/TwintOrderBuilderTest.php | 2 +- .../Service/PaymentMethodServiceTest.php | 2 + 10 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 src/Handler/Method/BlikPayment.php create mode 100644 tests/Cypress/cypress/e2e/storefront/payment-methods/blik.cy.js create mode 100644 tests/PHPUnit/Service/MollieApi/Builder/Payments/BlikOrderBuilderTest.php diff --git a/src/Components/MollieLimits/Service/MollieLimitsRemover.php b/src/Components/MollieLimits/Service/MollieLimitsRemover.php index 622dcc4e2..7e5b5e957 100644 --- a/src/Components/MollieLimits/Service/MollieLimitsRemover.php +++ b/src/Components/MollieLimits/Service/MollieLimitsRemover.php @@ -78,6 +78,7 @@ public function removePaymentMethods(PaymentMethodRouteResponse $originalData, S $price = $cart->getPrice()->getTotalPrice(); } + $countryIsoCode = null; if ($this->isOrderRoute()) { try { @@ -90,18 +91,30 @@ public function removePaymentMethods(PaymentMethodRouteResponse $originalData, S } $price = $order->getAmountTotal(); + + $billingAddress = $order->getBillingAddress(); + if ($billingAddress === null) { + return $originalData; + } + $billingCountry = $billingAddress->getCountry(); + if ($billingCountry === null) { + return $originalData; + } + $countryIsoCode = $billingCountry->getIso(); } if (!isset($price)) { return $originalData; } + $availableMolliePayments = $this->paymentMethodsProvider->getActivePaymentMethodsForAmount( $price, $context->getCurrency()->getIsoCode(), [ $context->getSalesChannel()->getId(), - ] + ], + $countryIsoCode ); /** @var PaymentMethodEntity $paymentMethod */ diff --git a/src/Handler/Method/BlikPayment.php b/src/Handler/Method/BlikPayment.php new file mode 100644 index 000000000..f4914474d --- /dev/null +++ b/src/Handler/Method/BlikPayment.php @@ -0,0 +1,32 @@ + $orderData + * @param OrderEntity $orderEntity + * @param SalesChannelContext $salesChannelContext + * @param CustomerEntity $customer + * @return array + */ + public function processPaymentMethodSpecificParameters(array $orderData, OrderEntity $orderEntity, SalesChannelContext $salesChannelContext, CustomerEntity $customer): array + { + return $orderData; + } +} diff --git a/src/Resources/config/services/handlers.xml b/src/Resources/config/services/handlers.xml index 0cb06f79d..5ffa6d2d6 100644 --- a/src/Resources/config/services/handlers.xml +++ b/src/Resources/config/services/handlers.xml @@ -183,5 +183,13 @@ + + + + + + + + diff --git a/src/Service/Payment/Provider/ActivePaymentMethodsProvider.php b/src/Service/Payment/Provider/ActivePaymentMethodsProvider.php index aacbf2358..8359d41c6 100644 --- a/src/Service/Payment/Provider/ActivePaymentMethodsProvider.php +++ b/src/Service/Payment/Provider/ActivePaymentMethodsProvider.php @@ -51,7 +51,7 @@ public function __construct(MollieApiFactory $mollieApiFactory, MollieOrderPrice * @param array $salesChannelIDs * @return Method[] */ - public function getActivePaymentMethodsForAmount(float $price, string $currency, array $salesChannelIDs): array + public function getActivePaymentMethodsForAmount(float $price, string $currency, array $salesChannelIDs, ?string $billingCountryCode): array { if ($price < 0.01) { return []; @@ -61,9 +61,13 @@ public function getActivePaymentMethodsForAmount(float $price, string $currency, 'amount' => [ 'value' => $this->priceFormatter->formatValue($price), 'currency' => strtoupper($currency), - ] + ], ]; + if ($billingCountryCode) { + $params['billingCountry'] = $billingCountryCode; + } + return $this->getActivePaymentMethods($params, $salesChannelIDs); } diff --git a/src/Service/Payment/Provider/ActivePaymentMethodsProviderInterface.php b/src/Service/Payment/Provider/ActivePaymentMethodsProviderInterface.php index b22230074..ab1e8a691 100644 --- a/src/Service/Payment/Provider/ActivePaymentMethodsProviderInterface.php +++ b/src/Service/Payment/Provider/ActivePaymentMethodsProviderInterface.php @@ -14,5 +14,5 @@ interface ActivePaymentMethodsProviderInterface * @param array $salesChannelIDs * @return array */ - public function getActivePaymentMethodsForAmount(float $price, string $currency, array $salesChannelIDs): array; + public function getActivePaymentMethodsForAmount(float $price, string $currency, array $salesChannelIDs, ?string $billingCountryCode): array; } diff --git a/src/Service/PaymentMethodService.php b/src/Service/PaymentMethodService.php index 8a746a0f0..5d9c1e85d 100644 --- a/src/Service/PaymentMethodService.php +++ b/src/Service/PaymentMethodService.php @@ -8,6 +8,7 @@ use Kiener\MolliePayments\Handler\Method\BankTransferPayment; use Kiener\MolliePayments\Handler\Method\BelfiusPayment; use Kiener\MolliePayments\Handler\Method\BilliePayment; +use Kiener\MolliePayments\Handler\Method\BlikPayment; use Kiener\MolliePayments\Handler\Method\CreditCardPayment; use Kiener\MolliePayments\Handler\Method\EpsPayment; use Kiener\MolliePayments\Handler\Method\GiftCardPayment; @@ -417,6 +418,7 @@ public function getPaymentHandlers(): array In3Payment::class, PosPayment::class, TwintPayment::class, + BlikPayment::class, // IngHomePayPayment::class, // not allowed anymore // DirectDebitPayment::class, // only allowed when updating subsriptions, aka => not allowed anymore ]; diff --git a/tests/Cypress/cypress/e2e/storefront/payment-methods/blik.cy.js b/tests/Cypress/cypress/e2e/storefront/payment-methods/blik.cy.js new file mode 100644 index 000000000..1d28a1a0b --- /dev/null +++ b/tests/Cypress/cypress/e2e/storefront/payment-methods/blik.cy.js @@ -0,0 +1,43 @@ +import Devices from "Services/utils/Devices"; +import Session from "Services/utils/Session" +// ------------------------------------------------------ +import PaymentAction from "Actions/storefront/checkout/PaymentAction"; +import DummyBasketScenario from "Scenarios/DummyBasketScenario"; +// ------------------------------------------------------ + + +const devices = new Devices(); +const session = new Session(); + +const paymentAction = new PaymentAction(); +const scenarioDummyBasket = new DummyBasketScenario(1); + +const device = devices.getFirstDevice(); + + +describe('Blik payment method', () => { + + context(devices.getDescription(device), () => { + + before(function () { + devices.setDevice(device); + }) + + beforeEach(() => { + session.resetBrowserSession(); + devices.setDevice(device); + }); + + it('Blik is existing in checkout', () => { + + scenarioDummyBasket.execute(); + + paymentAction.switchPaymentMethod('Blik'); + + // payment would only work using currency CHF which cannot be done at the moment + }) + + }) + +}) + diff --git a/tests/PHPUnit/Service/MollieApi/Builder/Payments/BlikOrderBuilderTest.php b/tests/PHPUnit/Service/MollieApi/Builder/Payments/BlikOrderBuilderTest.php new file mode 100644 index 000000000..82f24fed4 --- /dev/null +++ b/tests/PHPUnit/Service/MollieApi/Builder/Payments/BlikOrderBuilderTest.php @@ -0,0 +1,67 @@ +router->method('generate')->willReturn($redirectWebhookUrl); + $paymentMethod = TwintPayment::PAYMENT_METHOD_NAME; + + $this->paymentHandler = new TwintPayment( + $this->loggerService, + new FakeContainer() + ); + + $transactionId = Uuid::randomHex(); + $amountTotal = 27.0; + $taxStatus = CartPrice::TAX_STATE_GROSS; + $currencyISO = 'EUR'; + + $currency = new CurrencyEntity(); + $currency->setId(Uuid::randomHex()); + $currency->setIsoCode($currencyISO); + + $orderNumber = 'foo number'; + $lineItems = $this->getDummyLineItems(); + + $order = $this->getOrderEntity($amountTotal, $taxStatus, $currencyISO, $lineItems, $orderNumber); + + $actual = $this->builder->buildOrderPayload($order, $transactionId, $paymentMethod, $this->salesChannelContext, $this->paymentHandler, []); + + $expectedOrderLifeTime = (new DateTime())->setTimezone(new DateTimeZone('UTC')) + ->modify(sprintf('+%d day', $this->expiresAt)) + ->format('Y-m-d'); + + $expected = [ + 'amount' => (new MollieOrderPriceBuilder())->build($amountTotal, $currencyISO), + 'locale' => $this->localeCode, + 'method' => $paymentMethod, + 'orderNumber' => $orderNumber, + 'payment' => ['webhookUrl' => $redirectWebhookUrl], + 'redirectUrl' => $redirectWebhookUrl, + 'webhookUrl' => $redirectWebhookUrl, + 'lines' => $this->getExpectedLineItems($taxStatus, $lineItems, $currency), + 'billingAddress' => $this->getExpectedTestAddress($this->address, $this->email), + 'shippingAddress' => $this->getExpectedTestAddress($this->address, $this->email), + 'expiresAt' => $expectedOrderLifeTime + ]; + + self::assertSame($expected, $actual); + } +} diff --git a/tests/PHPUnit/Service/MollieApi/Builder/Payments/TwintOrderBuilderTest.php b/tests/PHPUnit/Service/MollieApi/Builder/Payments/TwintOrderBuilderTest.php index ed85d1c6a..0ba8932aa 100644 --- a/tests/PHPUnit/Service/MollieApi/Builder/Payments/TwintOrderBuilderTest.php +++ b/tests/PHPUnit/Service/MollieApi/Builder/Payments/TwintOrderBuilderTest.php @@ -23,7 +23,7 @@ public function testOrderBuild(): void $this->router->method('generate')->willReturn($redirectWebhookUrl); $paymentMethod = TwintPayment::PAYMENT_METHOD_NAME; - $this->paymentHandler = new KlarnaPayLaterPayment( + $this->paymentHandler = new TwintPayment( $this->loggerService, new FakeContainer() ); diff --git a/tests/PHPUnit/Service/PaymentMethodServiceTest.php b/tests/PHPUnit/Service/PaymentMethodServiceTest.php index 4dbd0061c..8c01d69a5 100644 --- a/tests/PHPUnit/Service/PaymentMethodServiceTest.php +++ b/tests/PHPUnit/Service/PaymentMethodServiceTest.php @@ -7,6 +7,7 @@ use Kiener\MolliePayments\Handler\Method\BankTransferPayment; use Kiener\MolliePayments\Handler\Method\BelfiusPayment; use Kiener\MolliePayments\Handler\Method\BilliePayment; +use Kiener\MolliePayments\Handler\Method\BlikPayment; use Kiener\MolliePayments\Handler\Method\CreditCardPayment; use Kiener\MolliePayments\Handler\Method\EpsPayment; use Kiener\MolliePayments\Handler\Method\GiftCardPayment; @@ -127,6 +128,7 @@ public function testSupportedMethods(): void In3Payment::class, PosPayment::class, TwintPayment::class, + BlikPayment::class, ]; $handlers = $this->paymentMethodService->getPaymentHandlers();