|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © CM.com. All rights reserved. |
| 4 | + * See LICENSE.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace CM\Payments\Controller\Payment; |
| 10 | + |
| 11 | +use CM\Payments\Api\Service\OrderServiceInterface; |
| 12 | +use CM\Payments\Api\Service\PaymentServiceInterface; |
| 13 | +use Exception; |
| 14 | +use Magento\Checkout\Model\Session; |
| 15 | +use Magento\Framework\App\Action\Action; |
| 16 | +use Magento\Framework\App\Action\Context; |
| 17 | +use Magento\Framework\App\Action\HttpGetActionInterface; |
| 18 | +use Magento\Framework\Controller\Result\Redirect; |
| 19 | +use Magento\Framework\Controller\Result\RedirectFactory; |
| 20 | +use Magento\Framework\Exception\LocalizedException; |
| 21 | +use Magento\Framework\Message\ManagerInterface as MessageManagerInterface; |
| 22 | +use Magento\Framework\Phrase; |
| 23 | +use Psr\Log\LoggerInterface; |
| 24 | + |
| 25 | +class ElvConfirmation extends Action implements HttpGetActionInterface |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var MessageManagerInterface |
| 29 | + */ |
| 30 | + protected $messageManager; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Session |
| 34 | + */ |
| 35 | + private $checkoutSession; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var RedirectFactory |
| 39 | + */ |
| 40 | + private $redirectFactory; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var OrderServiceInterface |
| 44 | + */ |
| 45 | + private $orderService; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var PaymentServiceInterface |
| 49 | + */ |
| 50 | + private $paymentService; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var LoggerInterface |
| 54 | + */ |
| 55 | + private $logger; |
| 56 | + |
| 57 | + /** |
| 58 | + * Elv constructor |
| 59 | + * |
| 60 | + * @param Context $context |
| 61 | + * @param MessageManagerInterface $messageManager |
| 62 | + * @param Session $checkoutSession |
| 63 | + * @param RedirectFactory $redirectFactory |
| 64 | + * @param OrderServiceInterface $orderService |
| 65 | + * @param PaymentServiceInterface $paymentService |
| 66 | + * @param LoggerInterface $logger |
| 67 | + */ |
| 68 | + public function __construct( |
| 69 | + Context $context, |
| 70 | + MessageManagerInterface $messageManager, |
| 71 | + Session $checkoutSession, |
| 72 | + RedirectFactory $redirectFactory, |
| 73 | + OrderServiceInterface $orderService, |
| 74 | + PaymentServiceInterface $paymentService, |
| 75 | + LoggerInterface $logger |
| 76 | + ) { |
| 77 | + $this->messageManager = $messageManager; |
| 78 | + $this->checkoutSession = $checkoutSession; |
| 79 | + $this->redirectFactory = $redirectFactory; |
| 80 | + $this->orderService = $orderService; |
| 81 | + $this->paymentService = $paymentService; |
| 82 | + $this->logger = $logger; |
| 83 | + |
| 84 | + parent::__construct($context); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @inheritdoc |
| 89 | + */ |
| 90 | + public function execute() |
| 91 | + { |
| 92 | + try { |
| 93 | + $order = $this->checkoutSession->getLastRealOrder(); |
| 94 | + $orderId = $order->getRealOrderId(); |
| 95 | + |
| 96 | + if (!$orderId) { |
| 97 | + return $this->redirectToCheckoutCart(__('No order id found.')); |
| 98 | + } |
| 99 | + |
| 100 | + $cmOrder = $this->orderService->create($order->getEntityId()); |
| 101 | + if (!$cmOrder->getOrderReference()) { |
| 102 | + throw new LocalizedException(__('The order was not placed properly.')); |
| 103 | + } |
| 104 | + |
| 105 | + $cmPayment = $this->paymentService->create($order->getEntityId()); |
| 106 | + if (!$cmPayment->getId()) { |
| 107 | + throw new LocalizedException(__('The Payment was not finished properly!')); |
| 108 | + } |
| 109 | + |
| 110 | + return $this->redirectFactory->create() |
| 111 | + ->setPath('checkout/onepage/success'); |
| 112 | + } catch (Exception $exception) { |
| 113 | + $this->logger->critical($exception->getMessage()); |
| 114 | + |
| 115 | + return $this->redirectToCheckoutCart(__('Something went wrong while creating the order.')); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Return to checkout cart with error message |
| 121 | + * |
| 122 | + * @param Phrase $message |
| 123 | + * @return Redirect |
| 124 | + */ |
| 125 | + public function redirectToCheckoutCart(Phrase $message): Redirect |
| 126 | + { |
| 127 | + $this->checkoutSession->restoreQuote(); |
| 128 | + |
| 129 | + $this->messageManager->addErrorMessage($message); |
| 130 | + |
| 131 | + return $this->redirectFactory->create() |
| 132 | + ->setPath('checkout/cart'); |
| 133 | + } |
| 134 | +} |
0 commit comments