Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Pro.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Paypal\Model;
8 
12 
17 class Pro
18 {
22  const PAYMENT_REVIEW_ACCEPT = 'accept';
23 
24  const PAYMENT_REVIEW_DENY = 'deny';
25 
31  protected $_config;
32 
38  protected $_api;
39 
45  protected $_infoInstance;
46 
52  protected $_apiType = \Magento\Paypal\Model\Api\Nvp::class;
53 
59  protected $_configType = \Magento\Paypal\Model\Config::class;
60 
64  protected $_configFactory;
65 
69  protected $_apiFactory;
70 
74  protected $_infoFactory;
75 
80 
87  public function __construct(
88  \Magento\Paypal\Model\Config\Factory $configFactory,
89  \Magento\Paypal\Model\Api\Type\Factory $apiFactory,
90  \Magento\Paypal\Model\InfoFactory $infoFactory,
92  ) {
93  $this->_configFactory = $configFactory;
94  $this->_apiFactory = $apiFactory;
95  $this->_infoFactory = $infoFactory;
96  $this->transactionRepository = $transactionRepository;
97  }
98 
106  public function setMethod($code, $storeId = null)
107  {
108  if (null === $this->_config) {
109  $params = [$code];
110  if (null !== $storeId) {
111  $params[] = $storeId;
112  }
113  $this->_config = $this->_configFactory->create($this->_configType, ['params' => $params]);
114  } else {
115  $this->_config->setMethod($code);
116  if (null !== $storeId) {
117  $this->_config->setStoreId($storeId);
118  }
119  }
120  return $this;
121  }
122 
130  public function setConfig(\Magento\Paypal\Model\Config $instance, $storeId = null)
131  {
132  $this->_config = $instance;
133  if (null !== $storeId) {
134  $this->_config->setStoreId($storeId);
135  }
136  return $this;
137  }
138 
144  public function getConfig()
145  {
146  return $this->_config;
147  }
148 
155  public function getApi()
156  {
157  if (null === $this->_api) {
158  $this->_api = $this->_apiFactory->create($this->_apiType);
159  }
160  $this->_api->setConfigObject($this->_config);
161  return $this->_api;
162  }
163 
169  public function resetApi()
170  {
171  $this->_api = null;
172 
173  return $this;
174  }
175 
181  public function getInfo()
182  {
183  if (null === $this->_infoInstance) {
184  $this->_infoInstance = $this->_infoFactory->create();
185  }
186  return $this->_infoInstance;
187  }
188 
196  public function importPaymentInfo(\Magento\Framework\DataObject $from, \Magento\Payment\Model\InfoInterface $to)
197  {
198  // update PayPal-specific payment information in the payment object
199  $this->getInfo()->importToPayment($from, $to);
200 
205  if ($from->getDataUsingMethod(\Magento\Paypal\Model\Info::IS_FRAUD)) {
206  $to->setIsTransactionPending(true);
207  $to->setIsFraudDetected(true);
209  $to->setIsTransactionPending(true);
210  }
211 
212  // give generic info about transaction state
213  if (Info::isPaymentSuccessful($to)) {
214  $to->setIsTransactionApproved(true);
215  } elseif (Info::isPaymentFailed($to)) {
216  $to->setIsTransactionDenied(true);
217  }
218 
219  return $this;
220  }
221 
229  public function void(\Magento\Framework\DataObject $payment)
230  {
231  $authTransactionId = $this->_getParentTransactionId($payment);
232  if ($authTransactionId) {
233  $api = $this->getApi();
234  $api->setPayment($payment)->setAuthorizationId($authTransactionId)->callDoVoid();
235  $this->importPaymentInfo($api, $payment);
236  } else {
237  throw new \Magento\Framework\Exception\LocalizedException(
238  __('You need an authorization transaction to void.')
239  );
240  }
241  }
242 
251  public function capture(\Magento\Framework\DataObject $payment, $amount)
252  {
253  $authTransactionId = $this->_getParentTransactionId($payment);
254  if (!$authTransactionId) {
255  return false;
256  }
257  $api = $this->getApi()
258  ->setAuthorizationId($authTransactionId)
259  ->setIsCaptureComplete($payment->isCaptureFinal($amount))
260  ->setAmount($amount);
261 
262  $order = $payment->getOrder();
263  $orderIncrementId = $order->getIncrementId();
264  $api->setCurrencyCode($order->getBaseCurrencyCode())
265  ->setInvNum($orderIncrementId)
266  ->setCustref($orderIncrementId)
267  ->setPonum($order->getId());
268  // TODO: pass 'NOTE' to API
269 
270  $api->callDoCapture();
272  }
273 
282  public function refund(\Magento\Framework\DataObject $payment, $amount)
283  {
284  $captureTxnId = $this->_getParentTransactionId($payment);
285  if ($captureTxnId) {
286  $api = $this->getApi();
287  $order = $payment->getOrder();
288  $api->setPayment(
289  $payment
290  )->setTransactionId(
291  $captureTxnId
292  )->setAmount(
293  $amount
294  )->setCurrencyCode(
295  $order->getBaseCurrencyCode()
296  );
297  $canRefundMore = $payment->getCreditmemo()->getInvoice()->canRefund();
298  $isFullRefund = !$canRefundMore &&
299  0 == (double)$order->getBaseTotalOnlineRefunded() + (double)$order->getBaseTotalOfflineRefunded();
300  $api->setRefundType(
301  $isFullRefund
302  ? \Magento\Paypal\Model\Config::REFUND_TYPE_FULL
303  : \Magento\Paypal\Model\Config::REFUND_TYPE_PARTIAL
304  );
305  $api->callRefundTransaction();
306  $this->_importRefundResultToPayment($api, $payment, $canRefundMore);
307  } else {
308  throw new \Magento\Framework\Exception\LocalizedException(
309  __('We can\'t issue a refund transaction because there is no capture transaction.')
310  );
311  }
312  }
313 
320  public function cancel(\Magento\Framework\DataObject $payment)
321  {
322  if (!$payment->getOrder()->getInvoiceCollection()->count()) {
323  $this->void($payment);
324  }
325  }
326 
334  {
335  $pendingReason = $payment->getAdditionalInformation(\Magento\Paypal\Model\Info::PENDING_REASON_GLOBAL);
336  return $this->_isPaymentReviewRequired(
337  $payment
339  }
340 
348  {
350  }
351 
359  public function reviewPayment(\Magento\Payment\Model\InfoInterface $payment, $action)
360  {
361  $api = $this->getApi()->setTransactionId($payment->getLastTransId());
362 
363  // check whether the review is still needed
364  $api->callGetTransactionDetails();
365  $this->importPaymentInfo($api, $payment);
367  return false;
368  }
369 
370  // perform the review action
371  $api->setAction($action)->callManagePendingTransactionStatus();
372  $api->callGetTransactionDetails();
373  $this->importPaymentInfo($api, $payment);
374  return true;
375  }
376 
384  public function fetchTransactionInfo(\Magento\Payment\Model\InfoInterface $payment, $transactionId)
385  {
386  $api = $this->getApi()->setTransactionId($transactionId)->setRawResponseNeeded(true);
387  $api->callGetTransactionDetails();
388  $this->importPaymentInfo($api, $payment);
389  $data = $api->getRawSuccessResponseData();
390  return $data ? $data : [];
391  }
392 
400  protected function _importCaptureResultToPayment($api, $payment)
401  {
402  $payment->setTransactionId($api->getTransactionId())->setIsTransactionClosed(false);
403  $this->importPaymentInfo($api, $payment);
404  }
405 
414  protected function _importRefundResultToPayment($api, $payment, $canRefundMore)
415  {
416  $payment->setTransactionId(
417  $api->getRefundTransactionId()
418  )->setIsTransactionClosed(
419  1 // refund initiated by merchant
420  )->setShouldCloseParentTransaction(
421  !$canRefundMore
422  );
423  $this->importPaymentInfo($api, $payment);
424  }
425 
432  protected function _getParentTransactionId(\Magento\Framework\DataObject $payment)
433  {
434  return $payment->getParentTransactionId();
435  }
436 }
static isPaymentReviewRequired(\Magento\Payment\Model\InfoInterface $payment)
Definition: Info.php:338
cancel(\Magento\Framework\DataObject $payment)
Definition: Pro.php:320
void(\Magento\Framework\DataObject $payment)
Definition: Pro.php:229
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
const PAYMENT_REVIEW_DENY
Definition: Pro.php:24
reviewPayment(\Magento\Payment\Model\InfoInterface $payment, $action)
Definition: Pro.php:359
__construct(\Magento\Paypal\Model\Config\Factory $configFactory, \Magento\Paypal\Model\Api\Type\Factory $apiFactory, \Magento\Paypal\Model\InfoFactory $infoFactory, TransactionRepositoryInterface $transactionRepository)
Definition: Pro.php:87
capture(\Magento\Framework\DataObject $payment, $amount)
Definition: Pro.php:251
$order
Definition: order.php:55
__()
Definition: __.php:13
static isPaymentFailed(\Magento\Payment\Model\InfoInterface $payment)
Definition: Info.php:411
$amount
Definition: order.php:14
const PAYMENT_REVIEW_ACCEPT
Definition: Pro.php:22
$payment
Definition: order.php:17
canReviewPayment(\Magento\Payment\Model\InfoInterface $payment)
Definition: Pro.php:333
static isPaymentSuccessful(\Magento\Payment\Model\InfoInterface $payment)
Definition: Info.php:381
_importRefundResultToPayment($api, $payment, $canRefundMore)
Definition: Pro.php:414
_getParentTransactionId(\Magento\Framework\DataObject $payment)
Definition: Pro.php:432
setConfig(\Magento\Paypal\Model\Config $instance, $storeId=null)
Definition: Pro.php:130
refund(\Magento\Framework\DataObject $payment, $amount)
Definition: Pro.php:282
setMethod($code, $storeId=null)
Definition: Pro.php:106
const PENDING_REASON_GLOBAL
Definition: Info.php:64
$configFactory
Definition: config_data.php:43
_isPaymentReviewRequired(\Magento\Payment\Model\InfoInterface $payment)
Definition: Pro.php:347
_importCaptureResultToPayment($api, $payment)
Definition: Pro.php:400
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
importPaymentInfo(\Magento\Framework\DataObject $from, \Magento\Payment\Model\InfoInterface $to)
Definition: Pro.php:196
$code
Definition: info.phtml:12
fetchTransactionInfo(\Magento\Payment\Model\InfoInterface $payment, $transactionId)
Definition: Pro.php:384