Skip to content

Commit

Permalink
Merge pull request #134 from vippsas/publication-2.4.29
Browse files Browse the repository at this point in the history
Fix issue with Klarna Checkout
  • Loading branch information
voleye authored Aug 22, 2022
2 parents 1a2b5ea + 00b6adf commit 32534e3
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 51 deletions.
31 changes: 17 additions & 14 deletions Controller/Payment/InitExpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,27 +170,35 @@ 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
]
);
}

if (!$quote->getCheckoutMethod()) {
/**
* @param $quote
*/
private function setCheckoutMethod($quote): void
{
if (!$quote->getCheckoutMethod(true)) {
if ($this->customerSession->isLoggedIn()) {
$quote->setCheckoutMethod(Onepage::METHOD_CUSTOMER);
} elseif ($this->checkoutHelper->isAllowedGuestCheckout($quote)) {
Expand All @@ -199,11 +207,6 @@ private function initiatePayment()
$quote->setCheckoutMethod(Onepage::METHOD_REGISTER);
}
}

$quote->setIsActive(false);
$this->cartRepository->save($quote);

return $responseData;
}

/**
Expand Down
8 changes: 2 additions & 6 deletions Controller/Payment/InitRegular.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
]
);
}
Expand Down
18 changes: 6 additions & 12 deletions Controller/Payment/Klarna/InitRegular.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
);
}
Expand All @@ -215,29 +215,23 @@ 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());
switch ($quote->getCheckoutMethod()) {
switch ($quote->getCheckoutMethod(true)) {
case Onepage::METHOD_CUSTOMER:
$this->paymentInformationManagement->savePaymentInformationAndPlaceOrder(
$maskedQuoteId,
$quote->getId(),
$quote->getPayment()
);
break;
case Onepage::METHOD_GUEST:
default:
$this->guestPaymentInformationManagement->savePaymentInformationAndPlaceOrder(
$maskedQuoteId,
$quote->getCustomerEmail(),
$quote->getPayment()
);
break;
case Onepage::METHOD_REGISTER:
default:
break;
}
}

Expand All @@ -246,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)) {
Expand Down
17 changes: 9 additions & 8 deletions Gateway/Request/Initiate/InitPreprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 [];
Expand Down
28 changes: 19 additions & 9 deletions Gateway/Response/InitiateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -56,24 +55,32 @@ class InitiateHandler implements HandlerInterface
*/
private $orderRepository;

/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* InitiateHandler constructor.
*
* @param SubjectReader $subjectReader
* @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;
}

/**
Expand All @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
~ IN THE SOFTWARE.
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vipps_Payment" setup_version="2.4.28">
<module name="Vipps_Payment" setup_version="2.4.29">
<sequence>
<module name="Magento_Sales" />
<module name="Magento_Payment" />
Expand Down

0 comments on commit 32534e3

Please sign in to comment.