From 8a8cdd079dcf8f5768f00de46287081ac74000bb Mon Sep 17 00:00:00 2001 From: Volodymyr Klymenko Date: Mon, 22 Aug 2022 16:10:33 +0300 Subject: [PATCH 1/3] Fix issue with Klarna Checkout --- Controller/Payment/InitExpress.php | 29 ++++++++++--------- Controller/Payment/InitRegular.php | 8 ++--- Controller/Payment/Klarna/InitRegular.php | 12 ++------ Gateway/Request/Initiate/InitPreprocessor.php | 17 ++++++----- Gateway/Response/InitiateHandler.php | 28 ++++++++++++------ composer.json | 2 +- etc/module.xml | 2 +- 7 files changed, 51 insertions(+), 47 deletions(-) diff --git a/Controller/Payment/InitExpress.php b/Controller/Payment/InitExpress.php index 0164af7..45f3539 100644 --- a/Controller/Payment/InitExpress.php +++ b/Controller/Payment/InitExpress.php @@ -170,26 +170,34 @@ public function execute() */ private function initiatePayment() { - $responseData = null; - $quote = $this->checkoutSession->getQuote(); - $quote->getPayment() - ->setAdditionalInformation(Vipps::METHOD_TYPE_KEY, Vipps::METHOD_TYPE_EXPRESS_CHECKOUT); + $this->setCheckoutMethod($quote); + if ($quote->getCheckoutMethod() === Onepage::METHOD_REGISTER) { + throw new LocalizedException(__('Sorry, guest checkout is not available.')); + } $shippingAddress = $quote->getShippingAddress(); - $shippingAddress->setShippingMethod(null); + $shippingAddress->setShippingMethod(''); $quote->collectTotals(); - $responseData = $this->commandManager->initiatePayment( + $quote->setIsActive(false); + + return $this->commandManager->initiatePayment( $quote->getPayment(), [ 'amount' => $quote->getGrandTotal(), - InitiateBuilderInterface::PAYMENT_TYPE_KEY - => InitiateBuilderInterface::PAYMENT_TYPE_EXPRESS_CHECKOUT + InitiateBuilderInterface::PAYMENT_TYPE_KEY => InitiateBuilderInterface::PAYMENT_TYPE_EXPRESS_CHECKOUT, + Vipps::METHOD_TYPE_KEY => Vipps::METHOD_TYPE_EXPRESS_CHECKOUT ] ); + } + /** + * @param $quote + */ + private function setCheckoutMethod($quote): void + { if (!$quote->getCheckoutMethod()) { if ($this->customerSession->isLoggedIn()) { $quote->setCheckoutMethod(Onepage::METHOD_CUSTOMER); @@ -199,11 +207,6 @@ private function initiatePayment() $quote->setCheckoutMethod(Onepage::METHOD_REGISTER); } } - - $quote->setIsActive(false); - $this->cartRepository->save($quote); - - return $responseData; } /** diff --git a/Controller/Payment/InitRegular.php b/Controller/Payment/InitRegular.php index 7ee466a..31307dc 100644 --- a/Controller/Payment/InitRegular.php +++ b/Controller/Payment/InitRegular.php @@ -112,10 +112,6 @@ public function execute() // init Vipps payment and retrieve redirect url $responseData = $this->initiatePayment($quote); - $quote->getPayment() - ->setAdditionalInformation(Vipps::METHOD_TYPE_KEY, Vipps::METHOD_TYPE_REGULAR_CHECKOUT); - - $this->cartRepository->save($quote); $response->setData($responseData); } catch (LocalizedException $e) { $this->logger->critical($this->enlargeMessage($e)); @@ -143,8 +139,8 @@ private function initiatePayment(CartInterface $quote) $quote->getPayment(), [ 'amount' => $quote->getGrandTotal(), - InitiateBuilderInterface::PAYMENT_TYPE_KEY => - InitiateBuilderInterface::PAYMENT_TYPE_REGULAR_PAYMENT + InitiateBuilderInterface::PAYMENT_TYPE_KEY => InitiateBuilderInterface::PAYMENT_TYPE_REGULAR_PAYMENT, + Vipps::METHOD_TYPE_KEY => Vipps::METHOD_TYPE_REGULAR_CHECKOUT ] ); } diff --git a/Controller/Payment/Klarna/InitRegular.php b/Controller/Payment/Klarna/InitRegular.php index 9b5d10d..234fff6 100644 --- a/Controller/Payment/Klarna/InitRegular.php +++ b/Controller/Payment/Klarna/InitRegular.php @@ -201,8 +201,8 @@ private function initiatePayment(CartInterface $quote) $quote->getPayment(), [ 'amount' => $quote->getGrandTotal(), - InitiateBuilderInterface::PAYMENT_TYPE_KEY => - InitiateBuilderInterface::PAYMENT_TYPE_REGULAR_PAYMENT + InitiateBuilderInterface::PAYMENT_TYPE_KEY => InitiateBuilderInterface::PAYMENT_TYPE_REGULAR_PAYMENT, + Vipps::METHOD_TYPE_KEY => Vipps::METHOD_TYPE_REGULAR_CHECKOUT ] ); } @@ -215,9 +215,6 @@ private function initiatePayment(CartInterface $quote) */ private function placeOrder(CartInterface $quote): void { - $quote->getPayment() - ->setAdditionalInformation(Vipps::METHOD_TYPE_KEY, Vipps::METHOD_TYPE_REGULAR_CHECKOUT); - $this->setCheckoutMethod($quote); $maskedQuoteId = $this->quoteIdToMaskedQuoteId->execute((int)$quote->getId()); @@ -228,16 +225,13 @@ private function placeOrder(CartInterface $quote): void $quote->getPayment() ); break; - case Onepage::METHOD_GUEST: + default: $this->guestPaymentInformationManagement->savePaymentInformationAndPlaceOrder( $maskedQuoteId, $quote->getCustomerEmail(), $quote->getPayment() ); break; - case Onepage::METHOD_REGISTER: - default: - break; } } diff --git a/Gateway/Request/Initiate/InitPreprocessor.php b/Gateway/Request/Initiate/InitPreprocessor.php index bc019cf..d6aa0f4 100755 --- a/Gateway/Request/Initiate/InitPreprocessor.php +++ b/Gateway/Request/Initiate/InitPreprocessor.php @@ -18,8 +18,9 @@ use Magento\Payment\Gateway\Data\PaymentDataObjectInterface; use Magento\Payment\Gateway\Data\Quote\QuoteAdapter; use Magento\Quote\Api\CartRepositoryInterface; -use Magento\Quote\Model\Quote\Payment; +use Magento\Quote\Model\Quote\Payment as QuotePayment; use Vipps\Payment\Gateway\Request\SubjectReader; +use Vipps\Payment\Model\Method\Vipps; /** * Class InitPreprocessor @@ -63,18 +64,18 @@ public function build(array $buildSubject) { /** @var PaymentDataObjectInterface $paymentDO */ $paymentDO = $this->subjectReader->readPayment($buildSubject); - /** @var Payment $payment */ - $orderAdapter = $paymentDO->getOrder(); + $payment = $paymentDO->getPayment(); - if ($orderAdapter instanceof QuoteAdapter) { - /** @var \Magento\Quote\Model\Quote $quote */ - $quote = $this->cartRepository->get($orderAdapter->getId()); + if ($payment instanceof QuotePayment) { + $payment->setMethod('vipps'); - $quote->getPayment()->setMethod('vipps'); - + $quote = $payment->getQuote(); if (!$quote->getReservedOrderId()) { $quote->reserveOrderId(); } + + $quote->getPayment() + ->setAdditionalInformation(Vipps::METHOD_TYPE_KEY, $buildSubject[Vipps::METHOD_TYPE_KEY] ?? null); } return []; diff --git a/Gateway/Response/InitiateHandler.php b/Gateway/Response/InitiateHandler.php index fedc0b9..ca3b242 100644 --- a/Gateway/Response/InitiateHandler.php +++ b/Gateway/Response/InitiateHandler.php @@ -16,16 +16,15 @@ namespace Vipps\Payment\Gateway\Response; -use Magento\Payment\Gateway\Data\Order\OrderAdapter; use Magento\Payment\Gateway\Data\PaymentDataObjectInterface; use Magento\Payment\Gateway\Http\Transfer; use Magento\Payment\Gateway\Response\HandlerInterface; -use Magento\Quote\Model\Quote\Payment; -use Magento\Sales\Api\Data\OrderInterface; +use Magento\Quote\Model\Quote\Payment as QuotePayment; +use Magento\Sales\Model\Order\Payment as OrderPayment; +use Magento\Quote\Api\CartRepositoryInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Vipps\Payment\Api\Data\QuoteInterface; use Vipps\Payment\Gateway\Request\SubjectReader; -use Vipps\Payment\Model\Method\Vipps; use Vipps\Payment\Model\QuoteFactory; use Vipps\Payment\Model\QuoteRepository; @@ -56,6 +55,11 @@ class InitiateHandler implements HandlerInterface */ private $orderRepository; + /** + * @var CartRepositoryInterface + */ + private $cartRepository; + /** * InitiateHandler constructor. * @@ -63,17 +67,20 @@ class InitiateHandler implements HandlerInterface * @param QuoteFactory $quoteFactory * @param QuoteRepository $quoteRepository * @param OrderRepositoryInterface $orderRepository + * @param CartRepositoryInterface $cartRepository */ public function __construct( SubjectReader $subjectReader, QuoteFactory $quoteFactory, QuoteRepository $quoteRepository, - OrderRepositoryInterface $orderRepository + OrderRepositoryInterface $orderRepository, + CartRepositoryInterface $cartRepository ) { $this->subjectReader = $subjectReader; $this->quoteFactory = $quoteFactory; $this->quoteRepository = $quoteRepository; $this->orderRepository = $orderRepository; + $this->cartRepository = $cartRepository; } /** @@ -90,14 +97,17 @@ public function handle(array $handlingSubject, array $responseBody) //@codingSta $transfer = $handlingSubject['transferObject']; /** @var PaymentDataObjectInterface $paymentDO */ $paymentDO = $this->subjectReader->readPayment($handlingSubject); - /** @var Payment $payment */ + + $payment = $paymentDO->getPayment(); $orderAdapter = $paymentDO->getOrder(); - if ($orderAdapter instanceof OrderAdapter) { + if ($payment instanceof OrderPayment) { $order = $this->orderRepository->get($orderAdapter->getId()); $quoteId = $order->getQuoteId(); - } else { - $quoteId = $orderAdapter->getId(); + } elseif ($payment instanceof QuotePayment) { + $cart = $payment->getQuote(); + $this->cartRepository->save($cart); + $quoteId = $cart->getId(); } /** @var QuoteInterface $vippsQuote */ diff --git a/composer.json b/composer.json index 1d8c8c5..9397977 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "type": "magento2-module", "description": "Vipps Payment Method", "license": "proprietary", - "version": "2.4.28", + "version": "2.4.29", "require": { "magento/framework": "103.0.*", "magento/module-sales": "103.0.*", diff --git a/etc/module.xml b/etc/module.xml index 340cf1e..91cc567 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -14,7 +14,7 @@ ~ IN THE SOFTWARE. --> - + From 0595648db2e31a70ca936193f2e2d393b7fdc87b Mon Sep 17 00:00:00 2001 From: Volodymyr Klymenko Date: Mon, 22 Aug 2022 17:20:16 +0300 Subject: [PATCH 2/3] Fix Klarna issue --- Controller/Payment/InitExpress.php | 2 +- Controller/Payment/Klarna/InitRegular.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Controller/Payment/InitExpress.php b/Controller/Payment/InitExpress.php index 45f3539..2eabb72 100644 --- a/Controller/Payment/InitExpress.php +++ b/Controller/Payment/InitExpress.php @@ -198,7 +198,7 @@ private function initiatePayment() */ private function setCheckoutMethod($quote): void { - if (!$quote->getCheckoutMethod()) { + if (!$quote->getCheckoutMethod(true)) { if ($this->customerSession->isLoggedIn()) { $quote->setCheckoutMethod(Onepage::METHOD_CUSTOMER); } elseif ($this->checkoutHelper->isAllowedGuestCheckout($quote)) { diff --git a/Controller/Payment/Klarna/InitRegular.php b/Controller/Payment/Klarna/InitRegular.php index 234fff6..8811507 100644 --- a/Controller/Payment/Klarna/InitRegular.php +++ b/Controller/Payment/Klarna/InitRegular.php @@ -221,7 +221,7 @@ private function placeOrder(CartInterface $quote): void switch ($quote->getCheckoutMethod()) { case Onepage::METHOD_CUSTOMER: $this->paymentInformationManagement->savePaymentInformationAndPlaceOrder( - $maskedQuoteId, + $quote->getId(), $quote->getPayment() ); break; @@ -240,7 +240,7 @@ private function placeOrder(CartInterface $quote): void */ private function setCheckoutMethod(Quote $quote) { - if (!$quote->getCheckoutMethod()) { + if (!$quote->getCheckoutMethod(true)) { if ($this->customerSession->isLoggedIn()) { $quote->setCheckoutMethod(Onepage::METHOD_CUSTOMER); } elseif ($this->checkoutHelper->isAllowedGuestCheckout($quote)) { From 00b6adf80b6aa2a176e116d794e87d3cf38ad3d0 Mon Sep 17 00:00:00 2001 From: Volodymyr Klymenko Date: Mon, 22 Aug 2022 17:22:06 +0300 Subject: [PATCH 3/3] Fix Klarna issue --- Controller/Payment/Klarna/InitRegular.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controller/Payment/Klarna/InitRegular.php b/Controller/Payment/Klarna/InitRegular.php index 8811507..fa4012b 100644 --- a/Controller/Payment/Klarna/InitRegular.php +++ b/Controller/Payment/Klarna/InitRegular.php @@ -218,7 +218,7 @@ private function placeOrder(CartInterface $quote): void $this->setCheckoutMethod($quote); $maskedQuoteId = $this->quoteIdToMaskedQuoteId->execute((int)$quote->getId()); - switch ($quote->getCheckoutMethod()) { + switch ($quote->getCheckoutMethod(true)) { case Onepage::METHOD_CUSTOMER: $this->paymentInformationManagement->savePaymentInformationAndPlaceOrder( $quote->getId(),