Data.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Helper;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Payment\Api\Data\PaymentMethodInterface;
  9. use Magento\Payment\Api\PaymentMethodListInterface;
  10. use Magento\Payment\Model\Method\InstanceFactory;
  11. use Magento\Payment\Model\MethodInterface;
  12. use Magento\Paypal\Model\Billing\Agreement\MethodInterface as BillingAgreementMethodInterface;
  13. /**
  14. * Paypal Data helper
  15. */
  16. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  17. {
  18. const HTML_TRANSACTION_ID =
  19. '<a target="_blank" href="https://www%1$s.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%2$s">%2$s</a>';
  20. /**
  21. * Cache for shouldAskToCreateBillingAgreement()
  22. *
  23. * @var bool
  24. */
  25. protected static $_shouldAskToCreateBillingAgreement = null;
  26. /**
  27. * @var \Magento\Payment\Helper\Data
  28. */
  29. protected $_paymentData;
  30. /**
  31. * @var \Magento\Paypal\Model\Billing\AgreementFactory
  32. */
  33. protected $_agreementFactory;
  34. /**
  35. * @var array
  36. */
  37. private $methodCodes;
  38. /**
  39. * @var \Magento\Paypal\Model\ConfigFactory
  40. */
  41. private $configFactory;
  42. /**
  43. * @var PaymentMethodListInterface
  44. */
  45. private $paymentMethodList;
  46. /**
  47. * @var InstanceFactory
  48. */
  49. private $paymentMethodInstanceFactory;
  50. /**
  51. * @param \Magento\Framework\App\Helper\Context $context
  52. * @param \Magento\Payment\Helper\Data $paymentData
  53. * @param \Magento\Paypal\Model\Billing\AgreementFactory $agreementFactory
  54. * @param \Magento\Paypal\Model\ConfigFactory $configFactory
  55. * @param array $methodCodes
  56. */
  57. public function __construct(
  58. \Magento\Framework\App\Helper\Context $context,
  59. \Magento\Payment\Helper\Data $paymentData,
  60. \Magento\Paypal\Model\Billing\AgreementFactory $agreementFactory,
  61. \Magento\Paypal\Model\ConfigFactory $configFactory,
  62. array $methodCodes
  63. ) {
  64. $this->_paymentData = $paymentData;
  65. $this->_agreementFactory = $agreementFactory;
  66. $this->methodCodes = $methodCodes;
  67. $this->configFactory = $configFactory;
  68. parent::__construct($context);
  69. }
  70. /**
  71. * Check whether customer should be asked confirmation whether to sign a billing agreement
  72. *
  73. * @param \Magento\Paypal\Model\Config $config
  74. * @param int $customerId
  75. * @return bool
  76. */
  77. public function shouldAskToCreateBillingAgreement(\Magento\Paypal\Model\Config $config, $customerId)
  78. {
  79. if (null === self::$_shouldAskToCreateBillingAgreement) {
  80. self::$_shouldAskToCreateBillingAgreement = false;
  81. if ($customerId && $config->shouldAskToCreateBillingAgreement()) {
  82. if ($this->_agreementFactory->create()->needToCreateForCustomer($customerId)) {
  83. self::$_shouldAskToCreateBillingAgreement = true;
  84. }
  85. }
  86. }
  87. return self::$_shouldAskToCreateBillingAgreement;
  88. }
  89. /**
  90. * Retrieve available billing agreement methods
  91. *
  92. * @param null|string|bool|int|\Magento\Store\Model\Store $store
  93. * @param \Magento\Quote\Model\Quote|null $quote
  94. * @return BillingAgreementMethodInterface[]
  95. */
  96. public function getBillingAgreementMethods($store = null, $quote = null)
  97. {
  98. $activeMethods = array_map(
  99. function (PaymentMethodInterface $method) {
  100. return $this->getPaymentMethodInstanceFactory()->create($method);
  101. },
  102. $this->getPaymentMethodList()->getActiveList($store)
  103. );
  104. $result = array_filter(
  105. $activeMethods,
  106. function (MethodInterface $method) use ($quote) {
  107. return $method instanceof BillingAgreementMethodInterface && $method->isAvailable($quote);
  108. }
  109. );
  110. return $result;
  111. }
  112. /**
  113. * Get HTML representation of transaction id
  114. *
  115. * @param string $methodCode
  116. * @param string $txnId
  117. * @return string
  118. */
  119. public function getHtmlTransactionId($methodCode, $txnId)
  120. {
  121. if (in_array($methodCode, $this->methodCodes)) {
  122. /** @var \Magento\Paypal\Model\Config $config */
  123. $config = $this->configFactory->create()->setMethod($methodCode);
  124. $sandboxFlag = ($config->getValue('sandboxFlag') ? '.sandbox' : '');
  125. return sprintf(self::HTML_TRANSACTION_ID, $sandboxFlag, $txnId);
  126. }
  127. return $txnId;
  128. }
  129. /**
  130. * Get payment method list.
  131. *
  132. * @return PaymentMethodListInterface
  133. * @deprecated 100.2.0
  134. */
  135. private function getPaymentMethodList()
  136. {
  137. if ($this->paymentMethodList === null) {
  138. $this->paymentMethodList = ObjectManager::getInstance()->get(
  139. PaymentMethodListInterface::class
  140. );
  141. }
  142. return $this->paymentMethodList;
  143. }
  144. /**
  145. * Get payment method instance factory.
  146. *
  147. * @return InstanceFactory
  148. * @deprecated 100.2.0
  149. */
  150. private function getPaymentMethodInstanceFactory()
  151. {
  152. if ($this->paymentMethodInstanceFactory === null) {
  153. $this->paymentMethodInstanceFactory = ObjectManager::getInstance()->get(
  154. InstanceFactory::class
  155. );
  156. }
  157. return $this->paymentMethodInstanceFactory;
  158. }
  159. }