Skip to content

Commit ef3033f

Browse files
committed
MAGETWO-90106: Paypal cannot place the order when Payment Action = Order
1 parent 0672212 commit ef3033f

File tree

16 files changed

+1163
-134
lines changed

16 files changed

+1163
-134
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Paypal\Block\Adminhtml\Order;
9+
10+
use Magento\Backend\Block\Widget\Context;
11+
use Magento\Framework\Registry;
12+
use Magento\Paypal\Model\Adminhtml\Express;
13+
use Magento\Sales\Block\Adminhtml\Order\View as OrderView;
14+
use Magento\Sales\Helper\Reorder;
15+
use Magento\Sales\Model\Config;
16+
use Magento\Sales\Model\Order;
17+
use Magento\Framework\Exception\LocalizedException;
18+
19+
/**
20+
* Adminhtml sales order view
21+
* @api
22+
*/
23+
class View extends OrderView
24+
{
25+
/**
26+
* @var Express
27+
*/
28+
private $express;
29+
30+
/**
31+
* @param Context $context
32+
* @param Registry $registry
33+
* @param Config $salesConfig
34+
* @param Reorder $reorderHelper
35+
* @param Express $express
36+
* @param array $data
37+
*/
38+
public function __construct(
39+
Context $context,
40+
Registry $registry,
41+
Config $salesConfig,
42+
Reorder $reorderHelper,
43+
Express $express,
44+
array $data = []
45+
) {
46+
$this->express = $express;
47+
48+
parent::__construct(
49+
$context,
50+
$registry,
51+
$salesConfig,
52+
$reorderHelper,
53+
$data
54+
);
55+
}
56+
57+
/**
58+
* Constructor.
59+
*
60+
* @return void
61+
* @throws LocalizedException
62+
*/
63+
protected function _construct()
64+
{
65+
parent::_construct();
66+
67+
$order = $this->getOrder();
68+
if ($order === null) {
69+
return;
70+
}
71+
$message = __('Are you sure you want to authorize full order amount?');
72+
if ($this->_isAllowedAction('Magento_Paypal::authorization') && $this->canAuthorize($order)) {
73+
$this->addButton(
74+
'order_authorize',
75+
[
76+
'label' => __('Authorize'),
77+
'class' => 'authorize',
78+
'onclick' => "confirmSetLocation('{$message}', '{$this->getPaymentAuthorizationUrl()}')",
79+
]
80+
);
81+
}
82+
}
83+
84+
/**
85+
* Returns URL for authorization of full order amount.
86+
*
87+
* @return string
88+
*/
89+
private function getPaymentAuthorizationUrl(): string
90+
{
91+
return $this->getUrl('paypal/express/authorization');
92+
}
93+
94+
/**
95+
* Checks if order available for payment authorization.
96+
*
97+
* @param Order $order
98+
* @return bool
99+
* @throws LocalizedException
100+
*/
101+
public function canAuthorize(Order $order): bool
102+
{
103+
if ($order->canUnhold() || $order->isPaymentReview()) {
104+
return false;
105+
}
106+
107+
$state = $order->getState();
108+
if ($order->isCanceled() || $state === Order::STATE_COMPLETE || $state === Order::STATE_CLOSED) {
109+
return false;
110+
}
111+
112+
return $this->express->isOrderAuthorizationAllowed($order->getPayment());
113+
}
114+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Paypal\Controller\Adminhtml\Express;
9+
10+
use Magento\Framework\App\Response\Http\FileFactory;
11+
use Magento\Framework\Controller\Result\JsonFactory;
12+
use Magento\Framework\Controller\Result\RawFactory;
13+
use Magento\Framework\Controller\Result\Redirect;
14+
use Magento\Framework\Exception\LocalizedException;
15+
use Magento\Framework\Registry;
16+
use Magento\Framework\Translate\InlineInterface;
17+
use Magento\Framework\View\Result\LayoutFactory;
18+
use Magento\Framework\View\Result\PageFactory;
19+
use Magento\Paypal\Model\Adminhtml\Express;
20+
use Magento\Backend\App\Action;
21+
use Magento\Sales\Api\OrderManagementInterface;
22+
use Magento\Sales\Api\OrderRepositoryInterface;
23+
use Magento\Sales\Controller\Adminhtml\Order;
24+
use Psr\Log\LoggerInterface;
25+
26+
/**
27+
* Makes a payment authorization for Paypal Express
28+
* when payment action is order.
29+
*
30+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
31+
*/
32+
class Authorization extends Order
33+
{
34+
/**
35+
* Authorization level of a basic admin session
36+
* @see _isAllowed()
37+
*/
38+
const ADMIN_RESOURCE = 'Magento_Paypal::authorization';
39+
40+
/**
41+
* @var Express
42+
*/
43+
private $express;
44+
45+
/**
46+
* @param Action\Context $context
47+
* @param Registry $coreRegistry
48+
* @param FileFactory $fileFactory
49+
* @param InlineInterface $translateInline
50+
* @param PageFactory $resultPageFactory
51+
* @param JsonFactory $resultJsonFactory
52+
* @param LayoutFactory $resultLayoutFactory
53+
* @param RawFactory $resultRawFactory
54+
* @param OrderManagementInterface $orderManagement
55+
* @param OrderRepositoryInterface $orderRepository
56+
* @param LoggerInterface $logger
57+
* @param Express $express
58+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
59+
*/
60+
public function __construct(
61+
Action\Context $context,
62+
Registry $coreRegistry,
63+
FileFactory $fileFactory,
64+
InlineInterface $translateInline,
65+
PageFactory $resultPageFactory,
66+
JsonFactory $resultJsonFactory,
67+
LayoutFactory $resultLayoutFactory,
68+
RawFactory $resultRawFactory,
69+
OrderManagementInterface $orderManagement,
70+
OrderRepositoryInterface $orderRepository,
71+
LoggerInterface $logger,
72+
Express $express
73+
) {
74+
$this->express = $express;
75+
76+
parent::__construct(
77+
$context,
78+
$coreRegistry,
79+
$fileFactory,
80+
$translateInline,
81+
$resultPageFactory,
82+
$resultJsonFactory,
83+
$resultLayoutFactory,
84+
$resultRawFactory,
85+
$orderManagement,
86+
$orderRepository,
87+
$logger
88+
);
89+
}
90+
91+
/**
92+
* Authorize full order payment amount.
93+
*
94+
* @return Redirect
95+
*/
96+
public function execute(): Redirect
97+
{
98+
$resultRedirect = $this->resultRedirectFactory->create();
99+
$order = $this->_initOrder();
100+
if ($order !== false) {
101+
try {
102+
$this->express->authorizeOrder($order);
103+
$this->orderRepository->save($order);
104+
$this->messageManager->addSuccessMessage(__('Payment authorization has been successfully created.'));
105+
} catch (LocalizedException $e) {
106+
$this->messageManager->addErrorMessage($e->getMessage());
107+
} catch (\Throwable $e) {
108+
$this->messageManager->addErrorMessage(__('Unable to make payment authorization.'));
109+
}
110+
111+
$resultRedirect->setPath('sales/order/view', ['order_id' => $order->getId()]);
112+
} else {
113+
$resultRedirect->setPath('sales/order/index');
114+
}
115+
116+
return $resultRedirect;
117+
}
118+
}

0 commit comments

Comments
 (0)