Skip to content

Commit 7a17373

Browse files
committed
Merge branch 'MAGETWO-80233' into MPI-PR
2 parents f676ca5 + 137dc66 commit 7a17373

File tree

17 files changed

+1128
-134
lines changed

17 files changed

+1128
-134
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Paypal\Block\Adminhtml\Order;
7+
8+
use Magento\Backend\Block\Widget\Context;
9+
use Magento\Framework\Registry;
10+
use Magento\Paypal\Model\Adminhtml\Express;
11+
use Magento\Sales\Block\Adminhtml\Order\View as OrderView;
12+
use Magento\Sales\Helper\Reorder;
13+
use Magento\Sales\Model\Config;
14+
use Magento\Sales\Model\Order;
15+
use Magento\Framework\Exception\LocalizedException;
16+
17+
/**
18+
* Adminhtml sales order view
19+
* @api
20+
*/
21+
class View extends OrderView
22+
{
23+
/**
24+
* @var Express
25+
*/
26+
private $express;
27+
/**
28+
* @param Context $context
29+
* @param Registry $registry
30+
* @param Config $salesConfig
31+
* @param Reorder $reorderHelper
32+
* @param Express $express
33+
* @param array $data
34+
*/
35+
public function __construct(
36+
Context $context,
37+
Registry $registry,
38+
Config $salesConfig,
39+
Reorder $reorderHelper,
40+
Express $express,
41+
array $data = []
42+
) {
43+
$this->express = $express;
44+
45+
parent::__construct(
46+
$context,
47+
$registry,
48+
$salesConfig,
49+
$reorderHelper,
50+
$data
51+
);
52+
}
53+
54+
/**
55+
* Constructor
56+
*
57+
* @return void
58+
* @throws LocalizedException
59+
*/
60+
protected function _construct()
61+
{
62+
parent::_construct();
63+
64+
$order = $this->getOrder();
65+
if (!$order) {
66+
return;
67+
}
68+
$message = __('Are you sure you want to authorize full order amount?');
69+
if ($this->_isAllowedAction('Magento_Paypal::authorization') && $this->canAuthorize($order)) {
70+
$this->addButton(
71+
'order_authorize',
72+
[
73+
'label' => __('Authorize'),
74+
'class' => 'authorize',
75+
'onclick' => "confirmSetLocation('{$message}', '{$this->getPaymentAuthorizationUrl()}')"
76+
]
77+
);
78+
}
79+
}
80+
81+
/**
82+
* Returns URL for authorization of full order amount.
83+
*
84+
* @return string
85+
*/
86+
private function getPaymentAuthorizationUrl(): string
87+
{
88+
return $this->getUrl('paypal/express/authorization');
89+
}
90+
91+
/**
92+
* Checks if order available for payment authorization.
93+
*
94+
* @param Order $order
95+
* @return bool
96+
* @throws LocalizedException
97+
*/
98+
public function canAuthorize(Order $order): bool
99+
{
100+
if ($order->canUnhold() || $order->isPaymentReview()) {
101+
return false;
102+
}
103+
104+
$state = $order->getState();
105+
if ($order->isCanceled() || $state === Order::STATE_COMPLETE || $state === Order::STATE_CLOSED) {
106+
return false;
107+
}
108+
109+
return $this->express->isOrderAuthorizationAllowed($order->getPayment());
110+
}
111+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)