|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Paypal\Controller\Adminhtml\Express; |
| 7 | + |
| 8 | +use Magento\Framework\App\Response\Http\FileFactory; |
| 9 | +use Magento\Framework\Controller\Result\JsonFactory; |
| 10 | +use Magento\Framework\Controller\Result\RawFactory; |
| 11 | +use Magento\Framework\Controller\Result\Redirect; |
| 12 | +use Magento\Framework\Exception\LocalizedException; |
| 13 | +use Magento\Framework\Registry; |
| 14 | +use Magento\Framework\Translate\InlineInterface; |
| 15 | +use Magento\Framework\View\Result\LayoutFactory; |
| 16 | +use Magento\Framework\View\Result\PageFactory; |
| 17 | +use Magento\Paypal\Model\Adminhtml\Express; |
| 18 | +use Magento\Backend\App\Action; |
| 19 | +use Magento\Sales\Api\OrderManagementInterface; |
| 20 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 21 | +use Magento\Sales\Controller\Adminhtml\Order; |
| 22 | +use Psr\Log\LoggerInterface; |
| 23 | + |
| 24 | +/** |
| 25 | + * Makes a payment authorization for Paypal Express |
| 26 | + * when payment action is order. |
| 27 | + * |
| 28 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 29 | + */ |
| 30 | +class Authorization extends Order |
| 31 | +{ |
| 32 | + /** |
| 33 | + * Authorization level of a basic admin session |
| 34 | + * @see _isAllowed() |
| 35 | + */ |
| 36 | + const ADMIN_RESOURCE = 'Magento_Paypal::authorization'; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var Express |
| 40 | + */ |
| 41 | + private $express; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param Action\Context $context |
| 45 | + * @param Registry $coreRegistry |
| 46 | + * @param FileFactory $fileFactory |
| 47 | + * @param InlineInterface $translateInline |
| 48 | + * @param PageFactory $resultPageFactory |
| 49 | + * @param JsonFactory $resultJsonFactory |
| 50 | + * @param LayoutFactory $resultLayoutFactory |
| 51 | + * @param RawFactory $resultRawFactory |
| 52 | + * @param OrderManagementInterface $orderManagement |
| 53 | + * @param OrderRepositoryInterface $orderRepository |
| 54 | + * @param LoggerInterface $logger |
| 55 | + * @param Express $express |
| 56 | + * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
| 57 | + */ |
| 58 | + public function __construct( |
| 59 | + Action\Context $context, |
| 60 | + Registry $coreRegistry, |
| 61 | + FileFactory $fileFactory, |
| 62 | + InlineInterface $translateInline, |
| 63 | + PageFactory $resultPageFactory, |
| 64 | + JsonFactory $resultJsonFactory, |
| 65 | + LayoutFactory $resultLayoutFactory, |
| 66 | + RawFactory $resultRawFactory, |
| 67 | + OrderManagementInterface $orderManagement, |
| 68 | + OrderRepositoryInterface $orderRepository, |
| 69 | + LoggerInterface $logger, |
| 70 | + Express $express |
| 71 | + ) { |
| 72 | + $this->express = $express; |
| 73 | + |
| 74 | + parent::__construct( |
| 75 | + $context, |
| 76 | + $coreRegistry, |
| 77 | + $fileFactory, |
| 78 | + $translateInline, |
| 79 | + $resultPageFactory, |
| 80 | + $resultJsonFactory, |
| 81 | + $resultLayoutFactory, |
| 82 | + $resultRawFactory, |
| 83 | + $orderManagement, |
| 84 | + $orderRepository, |
| 85 | + $logger |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Authorize full order payment amount. |
| 91 | + * |
| 92 | + * @return Redirect |
| 93 | + */ |
| 94 | + public function execute(): Redirect |
| 95 | + { |
| 96 | + $resultRedirect = $this->resultRedirectFactory->create(); |
| 97 | + if ($order = $this->_initOrder()) { |
| 98 | + try { |
| 99 | + $this->express->authorizeOrder($order); |
| 100 | + $this->orderRepository->save($order); |
| 101 | + $this->messageManager->addSuccessMessage(__('Payment authorization has been successfully created.')); |
| 102 | + } catch (LocalizedException $e) { |
| 103 | + $this->messageManager->addErrorMessage($e->getMessage()); |
| 104 | + } catch (\Exception $e) { |
| 105 | + $this->messageManager->addErrorMessage(__('Unable to make payment authorization.')); |
| 106 | + } |
| 107 | + |
| 108 | + $resultRedirect->setPath('sales/order/view', ['order_id' => $order->getId()]); |
| 109 | + } else { |
| 110 | + $resultRedirect->setPath('sales/order/index'); |
| 111 | + } |
| 112 | + |
| 113 | + return $resultRedirect; |
| 114 | + } |
| 115 | +} |
0 commit comments