Skip to content

Commit

Permalink
Merge pull request #198 from FatchipRobert/MAG2-77-Back-button-problem
Browse files Browse the repository at this point in the history
MAG2-77 Back button problems
  • Loading branch information
T-Kuchel authored Nov 14, 2018
2 parents c4a950c + 3944eb9 commit a43fd28
Show file tree
Hide file tree
Showing 29 changed files with 1,034 additions and 211 deletions.
13 changes: 2 additions & 11 deletions Controller/Onepage/Cancel.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,12 @@ public function execute()
$orderId = $this->checkoutSession->getLastOrderId();
$order = $orderId ? $this->orderFactory->create()->load($orderId) : false;
if ($order) {
$sQuoteId = $order->getQuoteId();

$order->cancel()->save();
$this->checkoutSession->restoreQuote();
$this->checkoutSession
->unsLastQuoteId()
->unsLastSuccessQuoteId()
->unsLastOrderId()
->unsLastRealOrderId();

// Use the old quote/basket again
$this->checkoutSession->setQuoteId($sQuoteId);
$this->checkoutSession->setLoadInactive();
$oQuote = $this->checkoutSession->getQuote();
$oQuote->setIsActive(1)->setReservedOrderId(null)->save();
$this->checkoutSession->replaceQuote($oQuote);
->unsLastOrderId();
}
} catch (LocalizedException $e) {
$this->messageManager->addExceptionMessage($e, $e->getMessage());
Expand Down
8 changes: 8 additions & 0 deletions Controller/Onepage/PlaceOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ public function execute()
protected function placeOrder()
{
$oQuote = $this->checkoutSession->getQuote();

if ($oQuote->getSubtotal() != $this->checkoutSession->getPayoneGenericpaymentSubtotal()) {
// The basket was changed - abort current checkout
$this->messageManager->addErrorMessage('An error occured during the Checkout.');
$this->_redirect('checkout/cart');
return;
}

$oQuote->getBillingAddress()->setShouldIgnoreValidation(true);
if (!$oQuote->getIsVirtual()) {
$oQuote->getShippingAddress()->setShouldIgnoreValidation(true);
Expand Down
156 changes: 153 additions & 3 deletions Controller/Onepage/Returned.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

namespace Payone\Core\Controller\Onepage;

use Magento\Sales\Model\Order;

/**
* Controller for handling return from payment provider
*/
Expand All @@ -38,18 +40,161 @@ class Returned extends \Magento\Framework\App\Action\Action
*/
protected $checkoutSession;

/**
* Quote management object
*
* @var \Magento\Quote\Model\QuoteManagement
*/
protected $quoteManagement;

/**
* Order repository
*
* @var \Magento\Sales\Api\OrderRepositoryInterface
*/
protected $orderRepository;

/**
* Order repository
*
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;

/**
* PAYONE database helper
*
* @var \Payone\Core\Helper\Database
*/
protected $databaseHelper;

/**
* TransactionStatus factory
*
* @var \Payone\Core\Model\Entities\TransactionStatusFactory
*/
protected $statusFactory;

/**
* TransactionStatus handler
*
* @var \Payone\Core\Model\Handler\TransactionStatus
*/
protected $transactionStatusHandler;

/**
* Constructor
*
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Quote\Model\QuoteManagement $quoteManagement
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
* @param \Payone\Core\Helper\Database $databaseHelper
* @param \Payone\Core\Model\Entities\TransactionStatusFactory $statusFactory
* @param \Payone\Core\Model\Handler\TransactionStatus $transactionStatusHandler
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Checkout\Model\Session $checkoutSession
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Quote\Model\QuoteManagement $quoteManagement,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
\Payone\Core\Helper\Database $databaseHelper,
\Payone\Core\Model\Entities\TransactionStatusFactory $statusFactory,
\Payone\Core\Model\Handler\TransactionStatus $transactionStatusHandler
) {
parent::__construct($context);
$this->checkoutSession = $checkoutSession;
$this->quoteManagement = $quoteManagement;
$this->orderRepository = $orderRepository;
$this->databaseHelper = $databaseHelper;
$this->quoteRepository = $quoteRepository;
$this->statusFactory = $statusFactory;
$this->transactionStatusHandler = $transactionStatusHandler;
}

/**
* Order was canceled before because of multi-tab browsing or back-button cancelling
* Create a clean order for the payment
*
* @param Order $canceledOrder
* @return void
*/
protected function createSubstituteOrder(Order $canceledOrder)
{
$this->checkoutSession->setPayoneCreatingSubstituteOrder(true);

$oOldQuote = $this->quoteRepository->get($canceledOrder->getQuoteId());
$oOldQuote->setIsActive(true);
$oOldQuote->setReservedOrderId(null);
$oOldQuote->save();

$orderId = $this->quoteManagement->placeOrder($oOldQuote->getId());
$newOrder = $this->orderRepository->get($orderId);

$oldData = $canceledOrder->getData();
foreach ($oldData as $sKey => $sValue) {
if (stripos($sKey, 'payone') !== false) {
$newOrder->setData($sKey, $sValue);
}
}

$newOrder->setPayoneCancelSubstituteIncrementId($canceledOrder->getIncrementId());
$newOrder->save();

$this->checkoutSession->setLastOrderId($newOrder->getId());
$this->checkoutSession->setLastRealOrderId($newOrder->getIncrementId());
$this->checkoutSession->getQuote()->setIsActive(false)->save();

$this->databaseHelper->relabelTransaction($canceledOrder->getId(), $newOrder->getId(), $newOrder->getPayment()->getId());
$this->databaseHelper->relabelApiProtocol($canceledOrder->getIncrementId(), $newOrder->getIncrementId());
$this->databaseHelper->relabelOrderPayment($canceledOrder->getIncrementId(), $newOrder->getId());

$this->handleTransactionStatus($canceledOrder, $newOrder);

$this->checkoutSession->unsPayoneCreatingSubstituteOrder();
}

/**
* Handle stored TransactionStatus
*
* @param Order $canceledOrder
* @param Order $newOrder
* @return void
*/
protected function handleTransactionStatus(Order $canceledOrder, Order $newOrder)
{
$aTransactionStatusIds = $this->databaseHelper->getNotHandledTransactionsByOrderId($canceledOrder->getIncrementId());
foreach ($aTransactionStatusIds as $aRow) {
$oTransactionStatus = $this->statusFactory->create();
$oTransactionStatus->load($aRow['id']);

$this->transactionStatusHandler->handle($newOrder, $oTransactionStatus->getRawStatusArray());

$oTransactionStatus->setHasBeenHandled(true);
$oTransactionStatus->save();
}
}

/**
* Get canceled order.
* Return order if found.
* Return false if not found or not canceled
*
* @return bool|Order
*/
protected function getCanceledOrder()
{
$order = $this->checkoutSession->getLastRealOrder();
if (!$order->getId() && !empty($this->checkoutSession->getPayoneCanceledOrder())) {
$order->loadByIncrementId($this->checkoutSession->getPayoneCanceledOrder());
}
$this->checkoutSession->unsPayoneCanceledOrder();
if ($order->getStatus() == Order::STATE_CANCELED) {
return $order;
}
return false;
}

/**
Expand All @@ -62,6 +207,11 @@ public function execute()
{
$this->checkoutSession->unsPayoneCustomerIsRedirected();

$canceledOrder = $this->getCanceledOrder();
if ($canceledOrder !== false) {
$this->createSubstituteOrder($canceledOrder);
}

$this->_redirect($this->_url->getUrl('checkout/onepage/success'));
}
}
1 change: 1 addition & 0 deletions Controller/Paypal/Express.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public function execute()
$oQuote->save();

$this->checkoutSession->setPayoneWorkorderId($aResponse['workorderid']);
$this->checkoutSession->setPayoneGenericpaymentSubtotal($oQuote->getSubtotal());
$this->_redirect($aResponse['redirecturl']);
}
return;
Expand Down
Loading

0 comments on commit a43fd28

Please sign in to comment.