Form.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Block\Express;
  7. use Magento\Customer\Helper\Session\CurrentCustomer;
  8. use Magento\Framework\Locale\ResolverInterface;
  9. use Magento\Framework\View\Element\Template;
  10. use Magento\Framework\View\Element\Template\Context;
  11. use Magento\Paypal\Helper\Data;
  12. use Magento\Paypal\Model\Config;
  13. use Magento\Paypal\Model\ConfigFactory;
  14. use Magento\Paypal\Model\Express\Checkout;
  15. class Form extends \Magento\Payment\Block\Form
  16. {
  17. /**
  18. * Payment method code
  19. *
  20. * @var string
  21. */
  22. protected $_methodCode = Config::METHOD_WPP_EXPRESS;
  23. /**
  24. * Paypal data
  25. *
  26. * @var Data
  27. */
  28. protected $_paypalData;
  29. /**
  30. * @var ConfigFactory
  31. */
  32. protected $_paypalConfigFactory;
  33. /**
  34. * @var ResolverInterface
  35. */
  36. protected $_localeResolver;
  37. /**
  38. * @var null
  39. */
  40. protected $_config;
  41. /**
  42. * @var bool
  43. */
  44. protected $_isScopePrivate;
  45. /**
  46. * @var CurrentCustomer
  47. */
  48. protected $currentCustomer;
  49. /**
  50. * @param Context $context
  51. * @param ConfigFactory $paypalConfigFactory
  52. * @param ResolverInterface $localeResolver
  53. * @param Data $paypalData
  54. * @param CurrentCustomer $currentCustomer
  55. * @param array $data
  56. */
  57. public function __construct(
  58. Context $context,
  59. ConfigFactory $paypalConfigFactory,
  60. ResolverInterface $localeResolver,
  61. Data $paypalData,
  62. CurrentCustomer $currentCustomer,
  63. array $data = []
  64. ) {
  65. $this->_paypalData = $paypalData;
  66. $this->_paypalConfigFactory = $paypalConfigFactory;
  67. $this->_localeResolver = $localeResolver;
  68. $this->_config = null;
  69. $this->_isScopePrivate = true;
  70. $this->currentCustomer = $currentCustomer;
  71. parent::__construct($context, $data);
  72. }
  73. /**
  74. * Set template and redirect message
  75. *
  76. * @return null
  77. */
  78. protected function _construct()
  79. {
  80. $this->_config = $this->_paypalConfigFactory->create()
  81. ->setMethod($this->getMethodCode());
  82. $mark = $this->_getMarkTemplate();
  83. $mark->setPaymentAcceptanceMarkHref(
  84. $this->_config->getPaymentMarkWhatIsPaypalUrl($this->_localeResolver)
  85. )->setPaymentAcceptanceMarkSrc(
  86. $this->_config->getPaymentMarkImageUrl($this->_localeResolver->getLocale())
  87. );
  88. // known issue: code above will render only static mark image
  89. $this->_initializeRedirectTemplateWithMark($mark);
  90. parent::_construct();
  91. $this->setRedirectMessage(__('You will be redirected to the PayPal website.'));
  92. }
  93. /**
  94. * Payment method code getter
  95. *
  96. * @return string
  97. */
  98. public function getMethodCode()
  99. {
  100. return $this->_methodCode;
  101. }
  102. /**
  103. * Get initialized mark template
  104. *
  105. * @return Template
  106. */
  107. protected function _getMarkTemplate()
  108. {
  109. /** @var $mark Template */
  110. $mark = $this->_layout->createBlock(\Magento\Framework\View\Element\Template::class);
  111. $mark->setTemplate('Magento_Paypal::payment/mark.phtml');
  112. return $mark;
  113. }
  114. /**
  115. * Initializes redirect template and set mark
  116. * @param Template $mark
  117. *
  118. * @return void
  119. */
  120. protected function _initializeRedirectTemplateWithMark(Template $mark)
  121. {
  122. $this->setTemplate(
  123. 'Magento_Paypal::payment/redirect.phtml'
  124. )->setRedirectMessage(
  125. __('You will be redirected to the PayPal website when you place an order.')
  126. )->setMethodTitle(
  127. // Output PayPal mark, omit title
  128. ''
  129. )->setMethodLabelAfterHtml(
  130. $mark->toHtml()
  131. );
  132. }
  133. /**
  134. * Get billing agreement code
  135. *
  136. * @return string|null
  137. */
  138. public function getBillingAgreementCode()
  139. {
  140. $customerId = $this->currentCustomer->getCustomerId();
  141. return $this->_paypalData->shouldAskToCreateBillingAgreement($this->_config, $customerId)
  142. ? Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT
  143. : null;
  144. }
  145. }