diff --git a/Controller/Payment/Fallback.php b/Controller/Payment/Fallback.php index c9a1cb1a..ef5d4ca7 100644 --- a/Controller/Payment/Fallback.php +++ b/Controller/Payment/Fallback.php @@ -30,6 +30,7 @@ use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Message\ManagerInterface; use Magento\Framework\Session\SessionManagerInterface; +use Magento\Payment\Gateway\ConfigInterface; use Magento\Quote\Api\CartRepositoryInterface; use Magento\Quote\Model\Quote; use Magento\Sales\Api\OrderManagementInterface; @@ -102,6 +103,11 @@ class Fallback implements ActionInterface, CsrfAwareActionInterface */ private $resultFactory; + /** + * @var ConfigInterface + */ + private $config; + /** * Fallback constructor. * @@ -115,6 +121,9 @@ class Fallback implements ActionInterface, CsrfAwareActionInterface * @param OrderManagementInterface $orderManagement * @param Compliance $compliance * @param LoggerInterface $logger + * @param ConfigInterface $config + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( ResultFactory $resultFactory, @@ -126,7 +135,8 @@ public function __construct( QuoteRepositoryInterface $vippsQuoteRepository, OrderManagementInterface $orderManagement, Compliance $compliance, - LoggerInterface $logger + LoggerInterface $logger, + ConfigInterface $config ) { $this->resultFactory = $resultFactory; $this->request = $request; @@ -138,6 +148,7 @@ public function __construct( $this->gdprCompliance = $compliance; $this->orderManagement = $orderManagement; $this->logger = $logger; + $this->config = $config; } /** @@ -145,6 +156,7 @@ public function __construct( */ public function execute() { + $transaction = null; /** @var Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); try { @@ -164,7 +176,20 @@ public function execute() . ' Please contact to store administrator.') ); } finally { - $this->storeLastOrderOrRestoreQuote(); + $vippsQuote = $this->getVippsQuote(true); + $cartPersistence = $this->config->getValue('cancellation_cart_persistence'); + $transactionWasCancelled = $transaction && $transaction->transactionWasCancelled(); + + if ($vippsQuote->getOrderId()) { + if ($transactionWasCancelled && $cartPersistence) { + $this->restoreQuote($vippsQuote); + } else { + $this->storeLastOrder($vippsQuote); + } + } else { + $this->restoreQuote($vippsQuote); + } + if (isset($e)) { if ($this->getVippsQuote()->getOrderId()) { $resultRedirect->setPath('checkout/onepage/failure', ['_secure' => true]); @@ -287,9 +312,8 @@ private function prepareResponse(Redirect $resultRedirect, Transaction $transact * Method to update Checkout session with Last Placed Order * Or restore quote if order was not placed (ex. Express Checkout) */ - private function storeLastOrderOrRestoreQuote() + private function storeLastOrder(QuoteInterface $vippsQuote) { - $vippsQuote = $this->getVippsQuote(true); if ($vippsQuote->getOrderId()) { $this->checkoutSession ->clearStorage() @@ -300,15 +324,23 @@ private function storeLastOrderOrRestoreQuote() ->setLastOrderStatus( $this->orderManagement->getStatus($vippsQuote->getOrderId()) ); - } else { - $quote = $this->cartRepository->get($vippsQuote->getQuoteId()); + } + } - /** @var Quote $quote */ - $quote->setIsActive(true); - $quote->setReservedOrderId(null); + /** + * @param QuoteInterface $vippsQuote + * + * @throws NoSuchEntityException + */ + private function restoreQuote(QuoteInterface $vippsQuote) + { + $quote = $this->cartRepository->get($vippsQuote->getQuoteId()); - $this->cartRepository->save($quote); - $this->checkoutSession->replaceQuote($quote); - } + /** @var Quote $quote */ + $quote->setIsActive(true); + $quote->setReservedOrderId(null); + + $this->cartRepository->save($quote); + $this->checkoutSession->replaceQuote($quote); } } diff --git a/Gateway/Validator/AvailabilityValidator.php b/Gateway/Validator/AvailabilityValidator.php index 2114f79d..ce3db64b 100644 --- a/Gateway/Validator/AvailabilityValidator.php +++ b/Gateway/Validator/AvailabilityValidator.php @@ -53,6 +53,8 @@ public function __construct( * * @return ResultInterface * @throws NoSuchEntityException + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function validate(array $validationSubject) { diff --git a/Model/TransactionProcessor.php b/Model/TransactionProcessor.php index cf4b9bd6..b698f56c 100644 --- a/Model/TransactionProcessor.php +++ b/Model/TransactionProcessor.php @@ -302,7 +302,6 @@ private function acquireLock($reservedOrderId) throw new AcquireLockException( __('Can not acquire lock for order "%1"', $reservedOrderId) ); - } return $lockName; diff --git a/Observer/OrderPaymentAfter.php b/Observer/OrderPaymentAfter.php index cf978e6f..c20b607a 100644 --- a/Observer/OrderPaymentAfter.php +++ b/Observer/OrderPaymentAfter.php @@ -71,7 +71,5 @@ public function execute(Observer $observer) $order->setState(Order::STATE_PAYMENT_REVIEW); $order->setStatus(Order::STATE_PAYMENT_REVIEW); } - - return; } } diff --git a/composer.json b/composer.json index 6ac0e8d6..664365bb 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "type": "magento2-module", "description": "Vipps Payment Method", "license": "proprietary", - "version": "2.4.7", + "version": "2.4.8", "require": { "magento/framework": "103.0.*", "magento/module-sales": "103.0.*", diff --git a/etc/adminhtml/system/cancellation.xml b/etc/adminhtml/system/cancellation.xml index 78e17184..4507fd5a 100644 --- a/etc/adminhtml/system/cancellation.xml +++ b/etc/adminhtml/system/cancellation.xml @@ -18,6 +18,16 @@ + + + Magento\Config\Model\Config\Source\Yesno + + + + payment/vipps/cancellation_cart_persistence + + diff --git a/etc/config.xml b/etc/config.xml index 4bb39d95..c867ba21 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -39,6 +39,7 @@ + 1 auto 3 10 diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index b51efd1c..0491d0b7 100755 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -83,4 +83,9 @@ + + + Vipps\Payment\Gateway\Config\Config + +