From c13df34a0b19a8e8e4c5877aea607c870dc80c27 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Thu, 21 Mar 2024 13:17:13 +0100 Subject: [PATCH 1/2] NTR: PISHPS-247: Klarna One payment (#727) Co-authored-by: Vitalij Mik --- src/Handler/Method/KlarnaOnePayment.php | 32 +++++++++ src/Resources/config/services/handlers.xml | 8 +++ src/Service/PaymentMethodService.php | 2 + .../Payments/KlarnaOneOrderBuilderTest.php | 67 +++++++++++++++++++ .../Service/PaymentMethodServiceTest.php | 2 + 5 files changed, 111 insertions(+) create mode 100644 src/Handler/Method/KlarnaOnePayment.php create mode 100644 tests/PHPUnit/Service/MollieApi/Builder/Payments/KlarnaOneOrderBuilderTest.php diff --git a/src/Handler/Method/KlarnaOnePayment.php b/src/Handler/Method/KlarnaOnePayment.php new file mode 100644 index 000000000..a0dcaf1ad --- /dev/null +++ b/src/Handler/Method/KlarnaOnePayment.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 5ffa6d2d6..68ff045d9 100644 --- a/src/Resources/config/services/handlers.xml +++ b/src/Resources/config/services/handlers.xml @@ -190,6 +190,14 @@ + + + + + + + + diff --git a/src/Service/PaymentMethodService.php b/src/Service/PaymentMethodService.php index 6e2fe5349..9ea8e9b8f 100644 --- a/src/Service/PaymentMethodService.php +++ b/src/Service/PaymentMethodService.php @@ -17,6 +17,7 @@ use Kiener\MolliePayments\Handler\Method\In3Payment; use Kiener\MolliePayments\Handler\Method\IngHomePayPayment; use Kiener\MolliePayments\Handler\Method\KbcPayment; +use Kiener\MolliePayments\Handler\Method\KlarnaOnePayment; use Kiener\MolliePayments\Handler\Method\KlarnaPayLaterPayment; use Kiener\MolliePayments\Handler\Method\KlarnaPayNowPayment; use Kiener\MolliePayments\Handler\Method\KlarnaSliceItPayment; @@ -430,6 +431,7 @@ public function getPaymentHandlers(): array KlarnaPayLaterPayment::class, KlarnaPayNowPayment::class, KlarnaSliceItPayment::class, + KlarnaOnePayment::class, PayPalPayment::class, PaySafeCardPayment::class, Przelewy24Payment::class, diff --git a/tests/PHPUnit/Service/MollieApi/Builder/Payments/KlarnaOneOrderBuilderTest.php b/tests/PHPUnit/Service/MollieApi/Builder/Payments/KlarnaOneOrderBuilderTest.php new file mode 100644 index 000000000..402f29313 --- /dev/null +++ b/tests/PHPUnit/Service/MollieApi/Builder/Payments/KlarnaOneOrderBuilderTest.php @@ -0,0 +1,67 @@ +router->method('generate')->willReturn($redirectWebhookUrl); + $paymentMethod = PaymentMethod::KLARNA_PAY_LATER; + + $this->paymentHandler = new KlarnaOnePayment( + $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/PaymentMethodServiceTest.php b/tests/PHPUnit/Service/PaymentMethodServiceTest.php index 8c01d69a5..f7796943f 100644 --- a/tests/PHPUnit/Service/PaymentMethodServiceTest.php +++ b/tests/PHPUnit/Service/PaymentMethodServiceTest.php @@ -15,6 +15,7 @@ use Kiener\MolliePayments\Handler\Method\iDealPayment; use Kiener\MolliePayments\Handler\Method\In3Payment; use Kiener\MolliePayments\Handler\Method\KbcPayment; +use Kiener\MolliePayments\Handler\Method\KlarnaOnePayment; use Kiener\MolliePayments\Handler\Method\KlarnaPayLaterPayment; use Kiener\MolliePayments\Handler\Method\KlarnaPayNowPayment; use Kiener\MolliePayments\Handler\Method\KlarnaSliceItPayment; @@ -120,6 +121,7 @@ public function testSupportedMethods(): void KlarnaPayLaterPayment::class, KlarnaPayNowPayment::class, KlarnaSliceItPayment::class, + KlarnaOnePayment::class, PayPalPayment::class, PaySafeCardPayment::class, Przelewy24Payment::class, From edbe0dde5e7ccf4825d0e4cd59c389ce02b63542 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Thu, 21 Mar 2024 13:17:48 +0100 Subject: [PATCH 2/2] NTR: PISHPS-264: use SW logic for shipping methods in Apple Pay (#731) Co-authored-by: Vitalij Mik --- src/Resources/config/services.xml | 1 + src/Service/ShippingMethodService.php | 27 ++++++++++++--------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index b79e98b45..6ecbf5795 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -108,6 +108,7 @@ + diff --git a/src/Service/ShippingMethodService.php b/src/Service/ShippingMethodService.php index ad2f62f56..0337c7e96 100644 --- a/src/Service/ShippingMethodService.php +++ b/src/Service/ShippingMethodService.php @@ -2,13 +2,13 @@ namespace Kiener\MolliePayments\Service; +use Shopware\Core\Checkout\Shipping\SalesChannel\AbstractShippingMethodRoute; use Shopware\Core\Checkout\Shipping\ShippingMethodCollection; use Shopware\Core\Checkout\Shipping\ShippingMethodEntity; use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; -use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter; -use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; use Shopware\Core\System\SalesChannel\SalesChannelContext; +use Symfony\Component\HttpFoundation\Request; class ShippingMethodService { @@ -17,14 +17,20 @@ class ShippingMethodService */ private $shippingMethodRepository; + /** + * @var AbstractShippingMethodRoute + */ + private $shippingMethodRoute; + /** * Creates a new instance of the shipping method repository. * * @param EntityRepository $shippingMethodRepository */ - public function __construct($shippingMethodRepository) + public function __construct($shippingMethodRepository, AbstractShippingMethodRoute $shippingMethodRoute) { $this->shippingMethodRepository = $shippingMethodRepository; + $this->shippingMethodRoute = $shippingMethodRoute; } /** @@ -52,18 +58,9 @@ public function getShippingMethodById(string $shippingMethodId, SalesChannelCont */ public function getActiveShippingMethods(SalesChannelContext $salesChannelContext): ShippingMethodCollection { - $criteria = (new Criteria()) - ->addFilter(new EqualsFilter('active', true)) - ->addFilter(new EqualsFilter('salesChannels.id', $salesChannelContext->getSalesChannel()->getId())) - ->addFilter(new EqualsAnyFilter('availabilityRuleId', $salesChannelContext->getRuleIds())) - ->addAssociation('prices') - ->addAssociation('salesChannels'); - - /** @var ShippingMethodCollection $shippingMethods */ - $shippingMethods = $this->shippingMethodRepository - ->search($criteria, $salesChannelContext->getContext()) - ->getEntities(); + $request = new Request(); + $request->query->set('onlyAvailable', '1'); - return $shippingMethods->filterByActiveRules($salesChannelContext); + return $this->shippingMethodRoute->load($request, $salesChannelContext, new Criteria())->getShippingMethods(); } }