Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Direct.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Paypal\Model;
7 
10 
16 class Direct extends \Magento\Payment\Model\Method\Cc
17 {
22 
26  protected $_infoBlockType = \Magento\Paypal\Block\Payment\Info::class;
27 
33  protected $_isGateway = true;
34 
40  protected $_canAuthorize = true;
41 
47  protected $_canCapture = true;
48 
54  protected $_canCapturePartial = true;
55 
61  protected $_canRefund = true;
62 
68  protected $_canRefundInvoicePartial = true;
69 
75  protected $_canVoid = true;
76 
82  protected $_canUseInternal = true;
83 
89  protected $_canUseCheckout = true;
90 
96  protected $_canSaveCc = false;
97 
101  protected $_canFetchTransactionInfo = true;
102 
108  protected $_canReviewPayment = true;
109 
115  protected $_pro;
116 
120  protected $_storeManager;
121 
125  protected $_urlBuilder;
126 
130  protected $_requestHttp;
131 
135  protected $_cartFactory;
136 
157  public function __construct(
158  \Magento\Framework\Model\Context $context,
159  \Magento\Framework\Registry $registry,
160  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
161  \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
162  \Magento\Payment\Helper\Data $paymentData,
163  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
164  \Magento\Payment\Model\Method\Logger $logger,
165  \Magento\Framework\Module\ModuleListInterface $moduleList,
166  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
167  \Magento\Paypal\Model\ProFactory $proFactory,
169  \Magento\Framework\UrlInterface $urlBuilder,
170  \Magento\Framework\App\RequestInterface $requestHttp,
171  \Magento\Paypal\Model\CartFactory $cartFactory,
172  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
173  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
174  array $data = []
175  ) {
176  parent::__construct(
177  $context,
178  $registry,
179  $extensionFactory,
180  $customAttributeFactory,
181  $paymentData,
182  $scopeConfig,
183  $logger,
184  $moduleList,
185  $localeDate,
186  $resource,
187  $resourceCollection,
188  $data
189  );
190  $this->_storeManager = $storeManager;
191  $this->_urlBuilder = $urlBuilder;
192  $this->_requestHttp = $requestHttp;
193  $this->_cartFactory = $cartFactory;
194 
195  $proInstance = array_shift($data);
196  if ($proInstance && $proInstance instanceof \Magento\Paypal\Model\Pro) {
197  $this->_pro = $proInstance;
198  } else {
199  $this->_pro = $proFactory->create();
200  }
201  $this->_pro->setMethod($this->_code);
202  }
203 
211  public function setStore($store)
212  {
213  $this->setData('store', $store);
214  if (null === $store) {
215  $store = $this->_storeManager->getStore()->getId();
216  }
217  $this->_pro->getConfig()->setStoreId(is_object($store) ? $store->getId() : $store);
218  return $this;
219  }
220 
227  public function canUseForCurrency($currencyCode)
228  {
229  return $this->_pro->getConfig()->isCurrencyCodeSupported($currencyCode);
230  }
231 
238  public function getConfigPaymentAction()
239  {
240  return $this->_pro->getConfig()->getPaymentAction();
241  }
242 
248  public function getAllowedCcTypes()
249  {
250  $ccTypes = explode(',', $this->_pro->getConfig()->getValue('cctypes'));
251  $country = $this->_pro->getConfig()->getMerchantCountry();
252 
253  if ($country == 'GB') {
254  $ccTypes = array_intersect(['SM', 'SO', 'MC', 'DI', 'VI'], $ccTypes);
255  } elseif ($country == 'CA') {
256  $ccTypes = array_intersect(['MC', 'VI'], $ccTypes);
257  }
258  return implode(',', $ccTypes);
259  }
260 
267  public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
268  {
269  return parent::isAvailable($quote) && $this->_pro->getConfig()->isMethodAvailable();
270  }
271 
280  public function getConfigData($field, $storeId = null)
281  {
282  $value = null;
283  switch ($field) {
284  case 'cctypes':
285  $value = $this->getAllowedCcTypes();
286  break;
287  case 'order_place_redirect_url':
288  $value = $this->getOrderPlaceRedirectUrl();
289  break;
290  default:
291  $value = $this->_pro->getConfig()->getValue($field);
292  }
293  return $value;
294  }
295 
304  {
305  return $this->_placeOrder($payment, $amount);
306  }
307 
315  public function void(\Magento\Payment\Model\InfoInterface $payment)
316  {
317  $this->_pro->void($payment);
318  return $this;
319  }
320 
329  {
330  if (false === $this->_pro->capture($payment, $amount)) {
331  $this->_placeOrder($payment, $amount);
332  }
333  return $this;
334  }
335 
345  {
346  $this->_pro->refund($payment, $amount);
347  return $this;
348  }
349 
356  public function cancel(\Magento\Payment\Model\InfoInterface $payment)
357  {
358  $this->void($payment);
359 
360  return $this;
361  }
362 
367  public function canReviewPayment()
368  {
369  return parent::canReviewPayment() && $this->_pro->canReviewPayment($this->getInfoInstance());
370  }
371 
379  {
380  parent::acceptPayment($payment);
381  return $this->_pro->reviewPayment($payment, \Magento\Paypal\Model\Pro::PAYMENT_REVIEW_ACCEPT);
382  }
383 
391  {
392  parent::denyPayment($payment);
393  return $this->_pro->reviewPayment($payment, \Magento\Paypal\Model\Pro::PAYMENT_REVIEW_DENY);
394  }
395 
403  public function fetchTransactionInfo(\Magento\Payment\Model\InfoInterface $payment, $transactionId)
404  {
405  return $this->_pro->fetchTransactionInfo($payment, $transactionId);
406  }
407 
415  protected function _placeOrder(Payment $payment, $amount)
416  {
417  $order = $payment->getOrder();
418  $api = $this->_pro->getApi()->setPaymentAction(
419  $this->_pro->getConfig()->getValue('paymentAction')
420  )->setIpAddress(
421  $this->_requestHttp->getClientIp(false)
422  )->setAmount(
423  $amount
424  )->setCurrencyCode(
425  $order->getBaseCurrencyCode()
426  )->setInvNum(
427  $order->getIncrementId()
428  )->setEmail(
429  $order->getCustomerEmail()
430  )->setNotifyUrl(
431  $this->_urlBuilder->getUrl('paypal/ipn/')
432  )->setCreditCardType(
433  $payment->getCcType()
434  )->setCreditCardNumber(
435  $payment->getCcNumber()
436  )->setCreditCardExpirationDate(
437  $this->_getFormattedCcExpirationDate($payment->getCcExpMonth(), $payment->getCcExpYear())
438  )->setCreditCardCvv2(
439  $payment->getCcCid()
440  );
441 
442  // add shipping and billing addresses
443  if ($order->getIsVirtual()) {
444  $api->setAddress($order->getBillingAddress())->setSuppressShipping(true);
445  } else {
446  $api->setAddress($order->getShippingAddress());
447  $api->setBillingAddress($order->getBillingAddress());
448  }
449 
450  // add line items
451  $cart = $this->_cartFactory->create(['salesModel' => $order]);
452 
453  $api->setPaypalCart($cart)->setIsLineItemsEnabled($this->_pro->getConfig()->getValue('lineItemsEnabled'));
454 
455  // call api and import transaction and other payment information
456  $api->callDoDirectPayment();
457  $this->_importResultToPayment($api, $payment);
458 
459  try {
460  $api->callGetTransactionDetails();
461  } catch (\Magento\Framework\Exception\LocalizedException $e) {
462  // if we receive errors, but DoDirectPayment response is Success, then set Pending status for transaction
463  $payment->setIsTransactionPending(true);
464  }
465  $this->_importResultToPayment($api, $payment);
466  return $this;
467  }
468 
477  protected function _getFormattedCcExpirationDate($month, $year)
478  {
479  return sprintf('%02d%02d', $month, $year);
480  }
481 
489  protected function _importResultToPayment($api, $payment)
490  {
491  $payment->setTransactionId($api->getTransactionId())->setIsTransactionClosed(0);
492  $this->_pro->importPaymentInfo($api, $payment);
493  }
494 
500  public function canVoid()
501  {
502  return $this->_canVoid;
503  }
504 }
authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
Definition: Direct.php:303
void(\Magento\Payment\Model\InfoInterface $payment)
Definition: Direct.php:315
fetchTransactionInfo(\Magento\Payment\Model\InfoInterface $payment, $transactionId)
Definition: Direct.php:403
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
const PAYMENT_REVIEW_DENY
Definition: Pro.php:24
$quote
$order
Definition: order.php:55
$storeManager
_importResultToPayment($api, $payment)
Definition: Direct.php:489
$resource
Definition: bulk.php:12
$logger
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory, \Magento\Payment\Helper\Data $paymentData, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Payment\Model\Method\Logger $logger, \Magento\Framework\Module\ModuleListInterface $moduleList, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Paypal\Model\ProFactory $proFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\UrlInterface $urlBuilder, \Magento\Framework\App\RequestInterface $requestHttp, \Magento\Paypal\Model\CartFactory $cartFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Direct.php:157
$amount
Definition: order.php:14
const PAYMENT_REVIEW_ACCEPT
Definition: Pro.php:22
$payment
Definition: order.php:17
cancel(\Magento\Payment\Model\InfoInterface $payment)
Definition: Direct.php:356
$value
Definition: gender.phtml:16
denyPayment(\Magento\Payment\Model\InfoInterface $payment)
Definition: Direct.php:390
acceptPayment(\Magento\Payment\Model\InfoInterface $payment)
Definition: Direct.php:378
_getFormattedCcExpirationDate($month, $year)
Definition: Direct.php:477
refund(\Magento\Payment\Model\InfoInterface $payment, $amount)
Definition: Direct.php:344
capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
Definition: Direct.php:328
_placeOrder(Payment $payment, $amount)
Definition: Direct.php:415
isAvailable(\Magento\Quote\Api\Data\CartInterface $quote=null)
Definition: Direct.php:267
canUseForCurrency($currencyCode)
Definition: Direct.php:227
getConfigData($field, $storeId=null)
Definition: Direct.php:280