diff --git a/Controller/Finish/Index.php b/Controller/Finish/Index.php index a9941c58..7fe49f78 100644 --- a/Controller/Finish/Index.php +++ b/Controller/Finish/Index.php @@ -5,6 +5,8 @@ namespace Paynl\Payment\Controller\Finish; +use Magento\Checkout\Model\Session; + /** * Description of Redirect * @@ -19,69 +21,70 @@ class Index extends \Magento\Framework\App\Action\Action protected $_config; /** - * - * @var \Magento\Sales\Model\OrderFactory + * @var Session */ - protected $_orderFactory; + protected $_checkoutSession; + /** + * Index constructor. + * @param \Magento\Framework\App\Action\Context $context + * @param \Paynl\Payment\Model\Config $config + * @param Session $checkoutSession + */ public function __construct( \Magento\Framework\App\Action\Context $context, \Paynl\Payment\Model\Config $config, - \Magento\Sales\Model\OrderFactory $orderFactory + Session $checkoutSession ) { $this->_config = $config; - $this->_orderFactory = $orderFactory; + $this->_checkoutSession = $checkoutSession; 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()) { + $this->_getCheckoutSession()->start(); + $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 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..c2b96d49 --- /dev/null +++ b/etc/frontend/sections.xml @@ -0,0 +1,12 @@ + + + +
+
+ + +
+
+ + \ No newline at end of file