-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* hosted checkout * minor refactoring
- Loading branch information
Showing
15 changed files
with
500 additions
and
189 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
Controller/Payment/Checkout/AbstractPaymentController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace Mondu\Mondu\Controller\Payment\Checkout; | ||
|
||
use Magento\Checkout\Model\Session; | ||
use Magento\Framework\App\ActionInterface; | ||
use Magento\Framework\App\RequestInterface; | ||
use Magento\Framework\App\Response\RedirectInterface; | ||
use Magento\Framework\App\ResponseInterface; | ||
use Magento\Framework\Controller\Result\JsonFactory; | ||
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface; | ||
use Mondu\Mondu\Helpers\Logger\Logger as MonduFileLogger; | ||
use Mondu\Mondu\Model\Request\Factory as RequestFactory; | ||
|
||
abstract class AbstractPaymentController implements ActionInterface | ||
{ | ||
/** | ||
* @var RedirectInterface | ||
*/ | ||
protected $redirect; | ||
/** | ||
* @var ResponseInterface | ||
*/ | ||
protected $response; | ||
/** | ||
* @var Session | ||
*/ | ||
protected $checkoutSession; | ||
|
||
/** | ||
* @var MessageManagerInterface | ||
*/ | ||
protected $messageManager; | ||
|
||
/** | ||
* @var RequestInterface | ||
*/ | ||
protected $request; | ||
|
||
/** | ||
* @var MonduFileLogger | ||
*/ | ||
protected $monduFileLogger; | ||
|
||
/** | ||
* @var RequestFactory | ||
*/ | ||
protected $requestFactory; | ||
|
||
/** | ||
* @var JsonFactory | ||
*/ | ||
protected $jsonResultFactory; | ||
|
||
/** | ||
* Execute | ||
* | ||
* @return ResponseInterface|\Magento\Framework\Controller\ResultInterface | ||
*/ | ||
abstract public function execute(); | ||
|
||
/** | ||
* @param RequestInterface $request | ||
* @param ResponseInterface $response | ||
* @param RedirectInterface $redirect | ||
* @param Session $checkoutSession | ||
* @param MessageManagerInterface $messageManager | ||
* @param MonduFileLogger $monduFileLogger | ||
* @param RequestFactory $requestFactory | ||
* @param JsonFactory $jsonResultFactory | ||
*/ | ||
public function __construct( | ||
RequestInterface $request, | ||
ResponseInterface $response, | ||
RedirectInterface $redirect, | ||
Session $checkoutSession, | ||
MessageManagerInterface $messageManager, | ||
MonduFileLogger $monduFileLogger, | ||
RequestFactory $requestFactory, | ||
JsonFactory $jsonResultFactory | ||
) { | ||
$this->request = $request; | ||
$this->response = $response; | ||
$this->redirect = $redirect; | ||
$this->checkoutSession = $checkoutSession; | ||
$this->messageManager = $messageManager; | ||
$this->monduFileLogger = $monduFileLogger; | ||
$this->requestFactory = $requestFactory; | ||
$this->jsonResultFactory = $jsonResultFactory; | ||
} | ||
|
||
/** | ||
* Redirect user to url | ||
* | ||
* @param string $path | ||
* @return void | ||
*/ | ||
protected function redirect($path) | ||
{ | ||
$this->redirect->redirect($this->response, $path); | ||
} | ||
|
||
/** | ||
* Process exceptions | ||
* | ||
* @param \Exception $e | ||
* @param string $message | ||
* @return void | ||
*/ | ||
protected function processException(\Exception $e, $message) | ||
{ | ||
$this->messageManager->addExceptionMessage($e, __($message)); | ||
$this->redirect('checkout/cart'); | ||
} | ||
|
||
/** | ||
* Redirect with error message | ||
* | ||
* @param string $message | ||
* @return void | ||
*/ | ||
protected function redirectWithErrorMessage($message) | ||
{ | ||
$this->messageManager->addErrorMessage(__($message)); | ||
$this->redirect('checkout/cart'); | ||
} | ||
} |
181 changes: 181 additions & 0 deletions
181
Controller/Payment/Checkout/AbstractSuccessController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
<?php | ||
|
||
namespace Mondu\Mondu\Controller\Payment\Checkout; | ||
|
||
use Magento\Checkout\Model\Session; | ||
use Magento\Framework\App\RequestInterface; | ||
use Magento\Framework\App\Response\RedirectInterface; | ||
use Magento\Framework\App\ResponseInterface; | ||
use Magento\Framework\Controller\Result\JsonFactory; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface; | ||
use Magento\Quote\Model\Quote; | ||
use Magento\Sales\Model\Order\Email\Sender\OrderSender; | ||
use Magento\Checkout\Helper\Data as CheckoutData; | ||
use Magento\Quote\Api\CartManagementInterface; | ||
use Mondu\Mondu\Helpers\Logger\Logger as MonduFileLogger; | ||
use Mondu\Mondu\Model\Request\Factory as RequestFactory; | ||
|
||
abstract class AbstractSuccessController extends AbstractPaymentController | ||
{ | ||
/** | ||
* @var \Magento\Customer\Model\Session | ||
*/ | ||
protected $customerSession; | ||
|
||
/** | ||
* @var OrderSender | ||
*/ | ||
protected $orderSender; | ||
|
||
/** | ||
* @var CartManagementInterface | ||
*/ | ||
private $quoteManagement; | ||
|
||
/** | ||
* @var CheckoutData | ||
*/ | ||
private $checkoutData; | ||
|
||
/** | ||
* @param RequestInterface $request | ||
* @param ResponseInterface $response | ||
* @param RedirectInterface $redirect | ||
* @param Session $checkoutSession | ||
* @param MessageManagerInterface $messageManager | ||
* @param MonduFileLogger $monduFileLogger | ||
* @param RequestFactory $requestFactory | ||
* @param JsonFactory $jsonResultFactory | ||
* @param \Magento\Customer\Model\Session $customerSession | ||
* @param OrderSender $orderSender | ||
* @param CheckoutData $checkoutData | ||
* @param CartManagementInterface $quoteManagement | ||
*/ | ||
public function __construct( | ||
RequestInterface $request, | ||
ResponseInterface $response, | ||
RedirectInterface $redirect, | ||
Session $checkoutSession, | ||
MessageManagerInterface $messageManager, | ||
MonduFileLogger $monduFileLogger, | ||
RequestFactory $requestFactory, | ||
JsonFactory $jsonResultFactory, | ||
\Magento\Customer\Model\Session $customerSession, | ||
OrderSender $orderSender, | ||
CheckoutData $checkoutData, | ||
CartManagementInterface $quoteManagement | ||
) { | ||
parent::__construct( | ||
$request, | ||
$response, | ||
$redirect, | ||
$checkoutSession, | ||
$messageManager, | ||
$monduFileLogger, | ||
$requestFactory, | ||
$jsonResultFactory, | ||
); | ||
$this->customerSession = $customerSession; | ||
$this->orderSender = $orderSender; | ||
|
||
$this->checkoutData = $checkoutData; | ||
$this->quoteManagement = $quoteManagement; | ||
} | ||
|
||
/** | ||
* Authorize Mondu Order | ||
* | ||
* @param string $monduId | ||
* @param string $referenceId | ||
* @return mixed | ||
* @throws LocalizedException | ||
* @throws \Exception | ||
*/ | ||
protected function authorizeMonduOrder($monduId, $referenceId) | ||
{ | ||
$authorizeRequest = $this->requestFactory->create(RequestFactory::CONFIRM_ORDER); | ||
|
||
return $authorizeRequest->process(['orderUid' => $monduId, 'referenceId' => $referenceId]); | ||
} | ||
|
||
/** | ||
* Prepare quote for guest checkout order submit | ||
* | ||
* @param Quote $quote | ||
* @return Quote | ||
*/ | ||
protected function prepareGuestQuote(Quote $quote) | ||
{ | ||
$billingAddress = $quote->getBillingAddress(); | ||
|
||
$email = $billingAddress->getOrigData('email') !== null | ||
? $billingAddress->getOrigData('email') : $billingAddress->getEmail(); | ||
|
||
$quote->setCustomerId(null) | ||
->setCustomerEmail($email) | ||
->setCustomerIsGuest(true) | ||
->setCustomerGroupId(\Magento\Customer\Model\Group::NOT_LOGGED_IN_ID); | ||
|
||
return $quote; | ||
} | ||
|
||
/** | ||
* Get magento checkut method | ||
* | ||
* @return string | ||
* @throws LocalizedException | ||
* @throws \Magento\Framework\Exception\NoSuchEntityException | ||
*/ | ||
protected function getCheckoutMethod() | ||
{ | ||
$quote = $this->checkoutSession->getQuote(); | ||
|
||
if ($this->customerSession->isLoggedIn()) { | ||
return \Magento\Checkout\Model\Type\Onepage::METHOD_CUSTOMER; | ||
} | ||
if (!$quote->getCheckoutMethod()) { | ||
if ($this->checkoutData->isAllowedGuestCheckout($quote)) { | ||
$quote->setCheckoutMethod(\Magento\Checkout\Model\Type\Onepage::METHOD_GUEST); | ||
} else { | ||
$quote->setCheckoutMethod(\Magento\Checkout\Model\Type\Onepage::METHOD_REGISTER); | ||
} | ||
} | ||
return $quote->getCheckoutMethod(); | ||
} | ||
|
||
/** | ||
* Place order in magento | ||
* | ||
* @param Quote $quote | ||
* @return mixed | ||
* @throws LocalizedException | ||
*/ | ||
protected function placeOrder($quote) | ||
{ | ||
if ($this->getCheckoutMethod() == \Magento\Checkout\Model\Type\Onepage::METHOD_GUEST) { | ||
$this->prepareGuestQuote($quote); | ||
} | ||
|
||
$quote->collectTotals(); | ||
|
||
return $this->quoteManagement->submit($quote); | ||
} | ||
|
||
/** | ||
* Get External reference id to be used | ||
* | ||
* @param Quote $quote | ||
* @return string | ||
* @throws \Exception | ||
*/ | ||
protected function getExternalReferenceId(Quote $quote) | ||
{ | ||
$reservedOrderId = $quote->getReservedOrderId(); | ||
if (!$reservedOrderId) { | ||
$quote->reserveOrderId()->save(); | ||
$reservedOrderId = $quote->getReservedOrderId(); | ||
} | ||
return $reservedOrderId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Mondu\Mondu\Controller\Payment\Checkout; | ||
|
||
class Cancel extends AbstractPaymentController | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute() | ||
{ | ||
$this->redirectWithErrorMessage('Mondu: Order has been canceled'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Mondu\Mondu\Controller\Payment\Checkout; | ||
|
||
class Decline extends AbstractPaymentController | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute() | ||
{ | ||
$this->redirectWithErrorMessage('Mondu: Order has been declined'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Mondu\Mondu\Controller\Payment\Checkout; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Exception\NotFoundException; | ||
use Magento\Quote\Model\Quote; | ||
|
||
class Success extends AbstractSuccessController | ||
{ | ||
/** | ||
* @inheritDoc | ||
* | ||
* @throws NotFoundException | ||
*/ | ||
public function execute() | ||
{ | ||
$monduId = $this->request->getParam('order_uuid'); | ||
|
||
if (!$monduId) { | ||
throw new NotFoundException(__('Not found')); | ||
} | ||
|
||
try { | ||
$quote = $this->checkoutSession->getQuote(); | ||
$this->authorizeMonduOrder($monduId, $this->getExternalReferenceId($quote)); | ||
|
||
$order = $this->placeOrder($quote); | ||
|
||
$this->checkoutSession->clearHelperData(); | ||
$quoteId = $this->checkoutSession->getQuoteId(); | ||
$this->checkoutSession->setLastQuoteId($quoteId)->setLastSuccessQuoteId($quoteId); | ||
|
||
if ($order) { | ||
$order->addStatusHistoryComment(__('Mondu: order id %1', $monduId)); | ||
$order->save(); | ||
$this->checkoutSession->setLastOrderId($order->getId()) | ||
->setLastRealOrderId($order->getIncrementId()) | ||
->setLastOrderStatus($order->getStatus()); | ||
|
||
if (!$order->getEmailSent()) { | ||
$this->orderSender->send($order); | ||
} | ||
} | ||
$this->redirect('checkout/onepage/success/'); | ||
|
||
} catch (LocalizedException $e) { | ||
$this->processException($e, 'Mondu: An error occurred while trying to confirm the order'); | ||
} catch (\Exception $e) { | ||
$this->processException($e, 'Mondu: Error during the order process'); | ||
} | ||
} | ||
} |
Oops, something went wrong.