View.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Paypal\Block\Adminhtml\Order;
  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. * Adminhtml sales order view.
  18. * @api
  19. * @since 100.2.2
  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. parent::__construct(
  45. $context,
  46. $registry,
  47. $salesConfig,
  48. $reorderHelper,
  49. $data
  50. );
  51. }
  52. /**
  53. * Constructor.
  54. *
  55. * @return void
  56. * @throws LocalizedException
  57. * @since 100.2.2
  58. */
  59. protected function _construct()
  60. {
  61. parent::_construct();
  62. $order = $this->getOrder();
  63. if ($order === null) {
  64. return;
  65. }
  66. $message = __('Are you sure you want to authorize full order amount?');
  67. if ($this->_isAllowedAction('Magento_Paypal::authorization') && $this->canAuthorize($order)) {
  68. $this->addButton(
  69. 'order_authorize',
  70. [
  71. 'label' => __('Authorize'),
  72. 'class' => 'authorize',
  73. 'onclick' => "confirmSetLocation('{$message}', '{$this->getPaymentAuthorizationUrl()}')",
  74. ]
  75. );
  76. }
  77. }
  78. /**
  79. * Returns URL for authorization of full order amount.
  80. *
  81. * @return string
  82. */
  83. private function getPaymentAuthorizationUrl(): string
  84. {
  85. return $this->getUrl('paypal/express/authorization');
  86. }
  87. /**
  88. * Checks if order available for payment authorization.
  89. *
  90. * @param Order $order
  91. * @return bool
  92. * @throws LocalizedException
  93. * @since 100.2.2
  94. */
  95. public function canAuthorize(Order $order): bool
  96. {
  97. if ($order->canUnhold() || $order->isPaymentReview()) {
  98. return false;
  99. }
  100. $state = $order->getState();
  101. if ($order->isCanceled() || $state === Order::STATE_COMPLETE || $state === Order::STATE_CLOSED) {
  102. return false;
  103. }
  104. return $this->express->isOrderAuthorizationAllowed($order->getPayment());
  105. }
  106. }