From b7efd66ae237ce458bba771e564e05de8c012552 Mon Sep 17 00:00:00 2001 From: Vladimir Kerkhoff Date: Tue, 29 Dec 2015 12:33:51 +0100 Subject: [PATCH 1/3] Restore cart after canceled payment --- Controller/Finish/Index.php | 86 ++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 35 deletions(-) diff --git a/Controller/Finish/Index.php b/Controller/Finish/Index.php index a9941c58..e8137231 100644 --- a/Controller/Finish/Index.php +++ b/Controller/Finish/Index.php @@ -5,6 +5,9 @@ namespace Paynl\Payment\Controller\Finish; +use Magento\Checkout\Model\Session; +use Magento\Sales\Model\Order\Email\Sender\OrderSender; + /** * Description of Redirect * @@ -24,64 +27,77 @@ class Index extends \Magento\Framework\App\Action\Action */ protected $_orderFactory; + /** + * @var Session + */ + protected $_checkoutSession; + + /** + * Index constructor. + * @param \Magento\Framework\App\Action\Context $context + * @param \Paynl\Payment\Model\Config $config + * @param Session $checkoutSession + * @param OrderSender $orderSender + * @param \Magento\Sales\Model\OrderFactory $orderFactory + */ public function __construct( \Magento\Framework\App\Action\Context $context, \Paynl\Payment\Model\Config $config, + Session $checkoutSession, + OrderSender $orderSender, \Magento\Sales\Model\OrderFactory $orderFactory ) { $this->_config = $config; + $this->_checkoutSession = $checkoutSession; + $this->orderSender = $orderSender; $this->_orderFactory = $orderFactory; parent::__construct($context); } - private function _reorder($orderId) + + public function execute() { - /** @var \Magento\Sales\Model\Order $order */ - $order = $this->_orderFactory->create()->loadByIncrementId($orderId);; - /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ - $resultRedirect = $this->resultRedirectFactory->create(); + \Paynl\Config::setApiToken($this->_config->getApiToken()); + $transaction = \Paynl\Transaction::getForReturn(); - /* @var $cart \Magento\Checkout\Model\Cart */ - $cart = $this->_objectManager->get('Magento\Checkout\Model\Cart'); - $cart->truncate(); + /** @var \Magento\Sales\Model\Order $order */ + $order = $this->_getCheckoutSession()->getLastRealOrder(); - $items = $order->getItemsCollection(); - foreach ($items as $item) { + $resultRedirect = $this->resultRedirectFactory->create(); + if ($transaction->isPaid() || $transaction->isPending()) { + $resultRedirect->setPath('checkout/onepage/success'); + } else { + //canceled, re-activate quote try { - $cart->addOrderItem($item); - } catch (\Magento\Framework\Exception\LocalizedException $e) { - if ($this->_objectManager->get('Magento\Checkout\Model\Session')->getUseNotice(true)) { - $this->messageManager->addNotice($e->getMessage()); + // if there is an order - cancel it + /** @var \Magento\Sales\Model\Order $order */ + $order = $this->_getCheckoutSession()->getLastRealOrder(); + if ($order && $order->getId() && $order->getQuoteId() == $this->_getCheckoutSession()->getQuoteId()) { + $order->cancel()->save(); + $this->_getCheckoutSession()->restoreQuote(); + $this->messageManager->addNotice(__('Payment canceled')); } else { - $this->messageManager->addError($e->getMessage()); + $this->messageManager->addNotice(__('Payment canceled, but unable to cancel order')); } - return $resultRedirect->setPath('*/*/history'); + } catch (\Magento\Framework\Exception\LocalizedException $e) { + $this->messageManager->addExceptionMessage($e, $e->getMessage()); } catch (\Exception $e) { - $this->messageManager->addException($e, - __('We can\'t add this item to your shopping cart right now.')); - return $resultRedirect->setPath('checkout/cart'); + $this->messageManager->addExceptionMessage($e, __('Unable to cancel order')); } + $resultRedirect->setPath('checkout/cart'); } - - $cart->save(); - return $resultRedirect->setPath('checkout/cart'); + return $resultRedirect; } - public function execute() + /** + * Return checkout session object + * + * @return Session + */ + protected function _getCheckoutSession() { - \Paynl\Config::setApiToken($this->_config->getApiToken()); - - $transaction = \Paynl\Transaction::getForReturn(); - - if ($transaction->isPaid() || $transaction->isPending()) { - $resultRedirect = $this->resultRedirectFactory->create(); - return $resultRedirect->setPath('checkout/onepage/success'); - } else { - //canceled, reorder - $this->messageManager->addNotice(__('Payment canceled')); - return $this->_reorder($transaction->getDescription()); - } + return $this->_checkoutSession; } } \ No newline at end of file From d1358183cb2b89dfa8e5d3ceab43d72c656d0d72 Mon Sep 17 00:00:00 2001 From: Vladimir Kerkhoff Date: Tue, 29 Dec 2015 12:42:01 +0100 Subject: [PATCH 2/3] Added sections.xml to invalidate local storage after order place and added getOrderPlaceRedirectUrl on payment methods to disable e-mail at order place (will be send when exchange payment processing is done) --- Controller/Finish/Index.php | 17 ++--------------- Model/Paymentmethod/PaymentMethod.php | 6 +++++- etc/frontend/sections.xml | 8 ++++++++ 3 files changed, 15 insertions(+), 16 deletions(-) create mode 100644 etc/frontend/sections.xml diff --git a/Controller/Finish/Index.php b/Controller/Finish/Index.php index e8137231..7fe49f78 100644 --- a/Controller/Finish/Index.php +++ b/Controller/Finish/Index.php @@ -6,7 +6,6 @@ namespace Paynl\Payment\Controller\Finish; use Magento\Checkout\Model\Session; -use Magento\Sales\Model\Order\Email\Sender\OrderSender; /** * Description of Redirect @@ -21,12 +20,6 @@ class Index extends \Magento\Framework\App\Action\Action */ protected $_config; - /** - * - * @var \Magento\Sales\Model\OrderFactory - */ - protected $_orderFactory; - /** * @var Session */ @@ -37,26 +30,19 @@ class Index extends \Magento\Framework\App\Action\Action * @param \Magento\Framework\App\Action\Context $context * @param \Paynl\Payment\Model\Config $config * @param Session $checkoutSession - * @param OrderSender $orderSender - * @param \Magento\Sales\Model\OrderFactory $orderFactory */ public function __construct( \Magento\Framework\App\Action\Context $context, \Paynl\Payment\Model\Config $config, - Session $checkoutSession, - OrderSender $orderSender, - \Magento\Sales\Model\OrderFactory $orderFactory + Session $checkoutSession ) { $this->_config = $config; $this->_checkoutSession = $checkoutSession; - $this->orderSender = $orderSender; - $this->_orderFactory = $orderFactory; parent::__construct($context); } - public function execute() { \Paynl\Config::setApiToken($this->_config->getApiToken()); @@ -67,6 +53,7 @@ public function execute() $resultRedirect = $this->resultRedirectFactory->create(); if ($transaction->isPaid() || $transaction->isPending()) { + $this->_getCheckoutSession()->start(); $resultRedirect->setPath('checkout/onepage/success'); } else { //canceled, re-activate quote diff --git a/Model/Paymentmethod/PaymentMethod.php b/Model/Paymentmethod/PaymentMethod.php index 8fcd41a1..f66f95eb 100644 --- a/Model/Paymentmethod/PaymentMethod.php +++ b/Model/Paymentmethod/PaymentMethod.php @@ -26,7 +26,6 @@ public function getInstructions() return trim($this->getConfigData('instructions')); } - public function initSettings() { @@ -53,4 +52,9 @@ public function initialize($paymentAction, $stateObject) $stateObject->setIsNotified(false); } + public function getOrderPlaceRedirectUrl() + { + return true; + } + } \ No newline at end of file diff --git a/etc/frontend/sections.xml b/etc/frontend/sections.xml new file mode 100644 index 00000000..305ad566 --- /dev/null +++ b/etc/frontend/sections.xml @@ -0,0 +1,8 @@ + + + +
+
+ + \ No newline at end of file From a2a7d980ef4b0d64e99d30ef90ea95fa44d7ac2b Mon Sep 17 00:00:00 2001 From: Vladimir Kerkhoff Date: Tue, 29 Dec 2015 13:24:33 +0100 Subject: [PATCH 3/3] Updated sections.xml --- etc/frontend/sections.xml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/etc/frontend/sections.xml b/etc/frontend/sections.xml index 305ad566..c2b96d49 100644 --- a/etc/frontend/sections.xml +++ b/etc/frontend/sections.xml @@ -1,7 +1,11 @@ - + +
+
+ +