Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Express.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
15 use Magento\Framework\Exception\LocalizedExceptionFactory;
22 use Magento\Paypal\Model\CartFactory;
24 use Magento\Paypal\Model\ProFactory;
35 
41 class Express extends PaypalExpress
42 {
46  private $authCommand;
47 
70  public function __construct(
71  Context $context,
73  ExtensionAttributesFactory $extensionFactory,
75  Data $paymentData,
76  ScopeConfigInterface $scopeConfig,
78  ProFactory $proFactory,
80  UrlInterface $urlBuilder,
81  CartFactory $cartFactory,
82  Session $checkoutSession,
83  LocalizedExceptionFactory $exception,
86  AuthorizeCommand $authCommand,
88  AbstractDb $resourceCollection = null,
89  $data = []
90  ) {
91  $this->authCommand = $authCommand;
92 
93  parent::__construct(
94  $context,
95  $registry,
96  $extensionFactory,
98  $paymentData,
99  $scopeConfig,
100  $logger,
101  $proFactory,
103  $urlBuilder,
104  $cartFactory,
105  $checkoutSession,
106  $exception,
109  $resource,
110  $resourceCollection,
111  $data
112  );
113  }
114 
122  public function authorizeOrder(OrderInterface $order)
123  {
124  $baseTotalDue = $order->getBaseTotalDue();
125 
127  $payment = $order->getPayment();
128  if (!$payment || !$this->isOrderAuthorizationAllowed($payment)) {
129  throw new LocalizedException(__('Authorization is not allowed.'));
130  }
131 
132  $orderTransaction = $this->getOrderTransaction($payment);
133 
134  $api = $this->_callDoAuthorize($baseTotalDue, $payment, $orderTransaction->getTxnId());
135  $this->_pro->importPaymentInfo($api, $payment);
136 
137  $payment->resetTransactionAdditionalInfo()
138  ->setIsTransactionClosed(false)
139  ->setTransactionId($api->getTransactionId())
140  ->setParentTransactionId($orderTransaction->getTxnId());
141 
142  $transaction = $payment->addTransaction(Transaction::TYPE_AUTH, null, true);
143  $message = $this->authCommand->execute($payment, $baseTotalDue, $payment->getOrder());
144  $message = $payment->prependMessage($message);
145 
146  $payment->addTransactionCommentsToOrder($transaction, $message);
147  $payment->setAmountAuthorized($order->getTotalDue());
148  $payment->setBaseAmountAuthorized($baseTotalDue);
149 
150  return $this;
151  }
152 
159  private function hasAuthorization(Payment $payment): bool
160  {
161  return (bool) ($payment->getAmountAuthorized() ?? 0);
162  }
163 
172  {
173  if ($payment->getMethod() === Config::METHOD_EXPRESS &&
174  $payment->getMethodInstance()->getConfigPaymentAction() === AbstractMethod::ACTION_ORDER
175  ) {
176  return !$this->hasAuthorization($payment);
177  }
178 
179  return false;
180  }
181 }
$transaction
$order
Definition: order.php:55
$storeManager
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$message
$payment
Definition: order.php:17
_callDoAuthorize($amount, $payment, $parentTransactionId)
Definition: Express.php:775
isOrderAuthorizationAllowed(Payment $payment)
Definition: Express.php:171
__construct(Context $context, Registry $registry, ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, Data $paymentData, ScopeConfigInterface $scopeConfig, Logger $logger, ProFactory $proFactory, StoreManagerInterface $storeManager, UrlInterface $urlBuilder, CartFactory $cartFactory, Session $checkoutSession, LocalizedExceptionFactory $exception, TransactionRepositoryInterface $transactionRepository, BuilderInterface $transactionBuilder, AuthorizeCommand $authCommand, AbstractResource $resource=null, AbstractDb $resourceCollection=null, $data=[])
Definition: Express.php:70