PayflowExpress.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model;
  7. use Magento\Sales\Model\Order\Payment\Transaction;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class PayflowExpress extends \Magento\Paypal\Model\Express
  12. {
  13. /**
  14. * @var string
  15. */
  16. protected $_code = Config::METHOD_WPP_PE_EXPRESS;
  17. /**
  18. * @var string
  19. */
  20. protected $_formBlockType = \Magento\Paypal\Block\PayflowExpress\Form::class;
  21. /**
  22. * Express Checkout payment method instance
  23. *
  24. * @var Express
  25. */
  26. protected $_ecInstance = null;
  27. /**
  28. * @var InfoFactory
  29. */
  30. protected $_paypalInfoFactory;
  31. /**
  32. * Availability option
  33. *
  34. * @var bool
  35. */
  36. protected $_canFetchTransactionInfo = false;
  37. /**
  38. * Payment Method feature
  39. *
  40. * @var bool
  41. */
  42. protected $_canReviewPayment = false;
  43. /**
  44. * @param \Magento\Framework\Model\Context $context
  45. * @param \Magento\Framework\Registry $registry
  46. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  47. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  48. * @param \Magento\Payment\Helper\Data $paymentData
  49. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  50. * @param \Magento\Payment\Model\Method\Logger $logger
  51. * @param ProFactory $proFactory
  52. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  53. * @param \Magento\Framework\UrlInterface $urlBuilder
  54. * @param CartFactory $cartFactory
  55. * @param \Magento\Checkout\Model\Session $checkoutSession
  56. * @param \Magento\Framework\Exception\LocalizedExceptionFactory $exception
  57. * @param \Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository
  58. * @param Transaction\BuilderInterface $transactionBuilder
  59. * @param InfoFactory $paypalInfoFactory
  60. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  61. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  62. * @param array $data
  63. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  64. */
  65. public function __construct(
  66. \Magento\Framework\Model\Context $context,
  67. \Magento\Framework\Registry $registry,
  68. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  69. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  70. \Magento\Payment\Helper\Data $paymentData,
  71. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  72. \Magento\Payment\Model\Method\Logger $logger,
  73. ProFactory $proFactory,
  74. \Magento\Store\Model\StoreManagerInterface $storeManager,
  75. \Magento\Framework\UrlInterface $urlBuilder,
  76. CartFactory $cartFactory,
  77. \Magento\Checkout\Model\Session $checkoutSession,
  78. \Magento\Framework\Exception\LocalizedExceptionFactory $exception,
  79. \Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository,
  80. \Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface $transactionBuilder,
  81. InfoFactory $paypalInfoFactory,
  82. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  83. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  84. array $data = []
  85. ) {
  86. parent::__construct(
  87. $context,
  88. $registry,
  89. $extensionFactory,
  90. $customAttributeFactory,
  91. $paymentData,
  92. $scopeConfig,
  93. $logger,
  94. $proFactory,
  95. $storeManager,
  96. $urlBuilder,
  97. $cartFactory,
  98. $checkoutSession,
  99. $exception,
  100. $transactionRepository,
  101. $transactionBuilder,
  102. $resource,
  103. $resourceCollection,
  104. $data
  105. );
  106. $this->_paypalInfoFactory = $paypalInfoFactory;
  107. }
  108. /**
  109. * EC PE won't be available if the EC is available
  110. *
  111. * @param \Magento\Quote\Api\Data\CartInterface|\Magento\Quote\Model\Quote|null $quote
  112. * @return bool
  113. */
  114. public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
  115. {
  116. if (!parent::isAvailable($quote)) {
  117. return false;
  118. }
  119. if (!$this->_ecInstance) {
  120. $this->_ecInstance = $this->_paymentData->getMethodInstance(
  121. Config::METHOD_WPP_EXPRESS
  122. );
  123. }
  124. if ($quote) {
  125. $this->_ecInstance->setStore($quote->getStoreId());
  126. }
  127. return !$this->_ecInstance->isAvailable();
  128. }
  129. /**
  130. * Import payment info to payment
  131. *
  132. * @param Api\Nvp $api
  133. * @param \Magento\Sales\Model\Order\Payment $payment
  134. * @return void
  135. */
  136. protected function _importToPayment($api, $payment)
  137. {
  138. $payment->setTransactionId(
  139. $api->getPaypalTransactionId()
  140. )->setIsTransactionClosed(
  141. 0
  142. )->setAdditionalInformation(
  143. Express\Checkout::PAYMENT_INFO_TRANSPORT_REDIRECT,
  144. $api->getRedirectRequired() || $api->getRedirectRequested()
  145. )->setIsTransactionPending(
  146. $api->getIsPaymentPending()
  147. )->setTransactionAdditionalInfo(
  148. Payflow\Pro::TRANSPORT_PAYFLOW_TXN_ID,
  149. $api->getTransactionId()
  150. );
  151. $payment->setPreparedMessage(__('Payflow PNREF: #%1.', $api->getTransactionId()));
  152. $this->_paypalInfoFactory->create()->importToPayment($api, $payment);
  153. }
  154. /**
  155. * Checkout redirect URL getter for onepage checkout (hardcode)
  156. *
  157. * @see \Magento\Checkout\Controller\Onepage::savePaymentAction()
  158. * @see \Magento\Quote\Model\Quote\Payment::getCheckoutRedirectUrl()
  159. * @return string
  160. */
  161. public function getCheckoutRedirectUrl()
  162. {
  163. return $this->_urlBuilder->getUrl('paypal/payflowexpress/start');
  164. }
  165. /**
  166. * Check refund availability.
  167. * The main factor is that the last capture transaction exists and has an Payflow\Pro::TRANSPORT_PAYFLOW_TXN_ID in
  168. * additional information(needed to perform online refund. Requirement of the Payflow gateway)
  169. *
  170. * @return bool
  171. */
  172. public function canRefund()
  173. {
  174. /** @var \Magento\Sales\Model\Order\Payment $payment */
  175. $payment = $this->getInfoInstance();
  176. // we need the last capture transaction was made
  177. $captureTransaction = $this->transactionRepository->getByTransactionType(
  178. Transaction::TYPE_CAPTURE,
  179. $payment->getId(),
  180. $payment->getOrder()->getId()
  181. );
  182. return $captureTransaction && $captureTransaction->getAdditionalInformation(
  183. Payflow\Pro::TRANSPORT_PAYFLOW_TXN_ID
  184. ) && $this->_canRefund;
  185. }
  186. }