Shortcut.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\Paypal\Model\Config;
  8. use Magento\Catalog\Block as CatalogBlock;
  9. use Magento\Paypal\Helper\Shortcut\ValidatorInterface;
  10. /**
  11. * Paypal express checkout shortcut link
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class Shortcut extends \Magento\Framework\View\Element\Template implements CatalogBlock\ShortcutInterface
  16. {
  17. /**
  18. * Whether the block should be eventually rendered
  19. *
  20. * @var bool
  21. */
  22. protected $_shouldRender = true;
  23. /**
  24. * Payment method code
  25. *
  26. * @var string
  27. */
  28. protected $_paymentMethodCode = '';
  29. /**
  30. * Start express action
  31. *
  32. * @var string
  33. */
  34. protected $_startAction = '';
  35. /**
  36. * Express checkout model factory name
  37. *
  38. * @var string
  39. */
  40. protected $_checkoutType = '';
  41. /**
  42. * Shortcut alias
  43. *
  44. * @var string
  45. */
  46. protected $_alias = '';
  47. /**
  48. * @var \Magento\Paypal\Model\ConfigFactory
  49. */
  50. protected $_paypalConfigFactory;
  51. /**
  52. * @var \Magento\Checkout\Model\Session
  53. */
  54. protected $_checkoutSession;
  55. /**
  56. * @var \Magento\Paypal\Model\Express\Checkout\Factory
  57. */
  58. protected $_checkoutFactory;
  59. /**
  60. * @var \Magento\Framework\Math\Random
  61. */
  62. protected $_mathRandom;
  63. /**
  64. * @var \Magento\Framework\Locale\ResolverInterface
  65. */
  66. protected $_localeResolver;
  67. /**
  68. * @var ValidatorInterface
  69. */
  70. private $_shortcutValidator;
  71. /**
  72. * @var Config
  73. */
  74. private $config;
  75. /**
  76. * @param \Magento\Framework\View\Element\Template\Context $context
  77. * @param \Magento\Paypal\Model\ConfigFactory $paypalConfigFactory
  78. * @param \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory
  79. * @param \Magento\Framework\Math\Random $mathRandom
  80. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  81. * @param ValidatorInterface $shortcutValidator
  82. * @param string $paymentMethodCode
  83. * @param string $startAction
  84. * @param string $checkoutType
  85. * @param string $alias
  86. * @param string $shortcutTemplate
  87. * @param \Magento\Checkout\Model\Session $checkoutSession
  88. * @param array $data
  89. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  90. */
  91. public function __construct(
  92. \Magento\Framework\View\Element\Template\Context $context,
  93. \Magento\Paypal\Model\ConfigFactory $paypalConfigFactory,
  94. \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory,
  95. \Magento\Framework\Math\Random $mathRandom,
  96. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  97. ValidatorInterface $shortcutValidator,
  98. $paymentMethodCode,
  99. $startAction,
  100. $checkoutType,
  101. $alias,
  102. $shortcutTemplate,
  103. \Magento\Checkout\Model\Session $checkoutSession = null,
  104. array $data = []
  105. ) {
  106. $this->_paypalConfigFactory = $paypalConfigFactory;
  107. $this->_checkoutSession = $checkoutSession;
  108. $this->_checkoutFactory = $checkoutFactory;
  109. $this->_mathRandom = $mathRandom;
  110. $this->_localeResolver = $localeResolver;
  111. $this->_shortcutValidator = $shortcutValidator;
  112. $this->_paymentMethodCode = $paymentMethodCode;
  113. $this->_startAction = $startAction;
  114. $this->_checkoutType = $checkoutType;
  115. $this->_alias = $alias;
  116. $this->setTemplate($shortcutTemplate);
  117. parent::__construct($context, $data);
  118. $this->config = $this->_paypalConfigFactory->create();
  119. $this->config->setMethod($this->_paymentMethodCode);
  120. }
  121. /**
  122. * @inheritdoc
  123. */
  124. protected function _beforeToHtml()
  125. {
  126. $result = parent::_beforeToHtml();
  127. $isInCatalog = $this->getIsInCatalogProduct();
  128. if (!$this->_shortcutValidator->validate($this->_paymentMethodCode, $isInCatalog)
  129. || (bool)(int)$this->config->getValue('in_context')
  130. ) {
  131. $this->_shouldRender = false;
  132. return $result;
  133. }
  134. $quote = $isInCatalog || !$this->_checkoutSession ? null : $this->_checkoutSession->getQuote();
  135. // set misc data
  136. $this->setShortcutHtmlId(
  137. $this->_mathRandom->getUniqueHash('ec_shortcut_')
  138. )->setCheckoutUrl(
  139. $this->getUrl($this->_startAction)
  140. );
  141. // use static image if in catalog
  142. if ($isInCatalog || null === $quote) {
  143. $this->setImageUrl($this->config->getExpressCheckoutShortcutImageUrl($this->_localeResolver->getLocale()));
  144. } else {
  145. /**@todo refactor checkout model. Move getCheckoutShortcutImageUrl to helper or separate model */
  146. $parameters = ['params' => ['quote' => $quote, 'config' => $this->config]];
  147. $checkoutModel = $this->_checkoutFactory->create($this->_checkoutType, $parameters);
  148. $this->setImageUrl($checkoutModel->getCheckoutShortcutImageUrl());
  149. }
  150. return $result;
  151. }
  152. /**
  153. * Render the block if needed
  154. *
  155. * @return string
  156. */
  157. protected function _toHtml()
  158. {
  159. if (!$this->shouldRender()) {
  160. return '';
  161. }
  162. return parent::_toHtml();
  163. }
  164. /**
  165. * Check if we should render component
  166. *
  167. * @return bool
  168. */
  169. protected function shouldRender()
  170. {
  171. $this->config->setMethod(Config::METHOD_EXPRESS);
  172. $isInCatalog = $this->getIsInCatalogProduct();
  173. $isInContext = (bool)(int) $this->config->getValue('in_context');
  174. $isEnabled = ($isInContext && $isInCatalog) || !$isInContext;
  175. $this->config->setMethod($this->_paymentMethodCode);
  176. return $isEnabled && $this->_shouldRender;
  177. }
  178. /**
  179. * Check is "OR" label position before shortcut
  180. *
  181. * @return bool
  182. */
  183. public function isOrPositionBefore()
  184. {
  185. return $this->getShowOrPosition() == CatalogBlock\ShortcutButtons::POSITION_BEFORE;
  186. }
  187. /**
  188. * Check is "OR" label position after shortcut
  189. *
  190. * @return bool
  191. */
  192. public function isOrPositionAfter()
  193. {
  194. return $this->getShowOrPosition() == CatalogBlock\ShortcutButtons::POSITION_AFTER;
  195. }
  196. /**
  197. * Get shortcut alias
  198. *
  199. * @return string
  200. */
  201. public function getAlias()
  202. {
  203. return $this->_alias;
  204. }
  205. }