Skip to content

Commit

Permalink
Hosted checkout (#82)
Browse files Browse the repository at this point in the history
* hosted checkout

* minor refactoring
  • Loading branch information
tikohov20 authored Jul 10, 2023
1 parent d523c77 commit 6d3a352
Show file tree
Hide file tree
Showing 15 changed files with 500 additions and 189 deletions.
127 changes: 127 additions & 0 deletions Controller/Payment/Checkout/AbstractPaymentController.php
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 Controller/Payment/Checkout/AbstractSuccessController.php
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;
}
}
14 changes: 14 additions & 0 deletions Controller/Payment/Checkout/Cancel.php
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');
}
}
14 changes: 14 additions & 0 deletions Controller/Payment/Checkout/Decline.php
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');
}
}
53 changes: 53 additions & 0 deletions Controller/Payment/Checkout/Success.php
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');
}
}
}
Loading

0 comments on commit 6d3a352

Please sign in to comment.