Shortcut.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Block\Bml;
  7. use Magento\Catalog\Block as CatalogBlock;
  8. use Magento\Paypal\Helper\Shortcut\ValidatorInterface;
  9. use Magento\Paypal\Model\ConfigFactory;
  10. use Magento\Paypal\Model\Config;
  11. use Magento\Framework\App\ObjectManager;
  12. /**
  13. * Class shortcut
  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. private $_paymentMethodCode = '';
  29. /**
  30. * Shortcut alias
  31. *
  32. * @var string
  33. */
  34. private $_alias = '';
  35. /**
  36. * Start express action
  37. *
  38. * @var string
  39. */
  40. private $_startAction = '';
  41. /**
  42. * @var \Magento\Payment\Helper\Data
  43. */
  44. private $_paymentData;
  45. /**
  46. * @var \Magento\Framework\Math\Random
  47. */
  48. private $_mathRandom;
  49. /**
  50. * Bml method code
  51. *
  52. * @var string
  53. */
  54. private $_bmlMethodCode = '';
  55. /**
  56. * Shortcut image path
  57. */
  58. const SHORTCUT_IMAGE = 'https://www.paypalobjects.com/webstatic/en_US/i/buttons/ppcredit-logo-medium.png';
  59. /**
  60. * @var ValidatorInterface
  61. */
  62. private $_shortcutValidator;
  63. /**
  64. * @var Config
  65. */
  66. private $config;
  67. /**
  68. * @param \Magento\Framework\View\Element\Template\Context $context
  69. * @param \Magento\Payment\Helper\Data $paymentData
  70. * @param \Magento\Framework\Math\Random $mathRandom
  71. * @param ValidatorInterface $shortcutValidator
  72. * @param string $paymentMethodCode
  73. * @param string $startAction
  74. * @param string $alias
  75. * @param string $bmlMethodCode
  76. * @param string $shortcutTemplate
  77. * @param array $data
  78. * @param ConfigFactory|null $config
  79. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  80. * @codingStandardsIgnoreStart
  81. */
  82. public function __construct(
  83. \Magento\Framework\View\Element\Template\Context $context,
  84. \Magento\Payment\Helper\Data $paymentData,
  85. \Magento\Framework\Math\Random $mathRandom,
  86. ValidatorInterface $shortcutValidator,
  87. $paymentMethodCode,
  88. $startAction,
  89. $alias,
  90. $bmlMethodCode,
  91. $shortcutTemplate,
  92. array $data = [],
  93. ConfigFactory $config = null
  94. ) {
  95. $this->_paymentData = $paymentData;
  96. $this->_mathRandom = $mathRandom;
  97. $this->_shortcutValidator = $shortcutValidator;
  98. $this->_paymentMethodCode = $paymentMethodCode;
  99. $this->_startAction = $startAction;
  100. $this->_alias = $alias;
  101. $this->setTemplate($shortcutTemplate);
  102. $this->_bmlMethodCode = $bmlMethodCode;
  103. $this->config = $config
  104. ? $config->create()
  105. : ObjectManager::getInstance()->get(ConfigFactory::class)->create();
  106. $this->config->setMethod($this->_paymentMethodCode);
  107. parent::__construct($context, $data);
  108. }
  109. //@codingStandardsIgnoreEnd
  110. /**
  111. * @inheritdoc
  112. */
  113. protected function _beforeToHtml()
  114. {
  115. $result = parent::_beforeToHtml();
  116. $isInCatalog = $this->getIsInCatalogProduct();
  117. if (!$this->_shortcutValidator->validate($this->_paymentMethodCode, $isInCatalog)
  118. || (bool)(int)$this->config->getValue('in_context')
  119. ) {
  120. $this->_shouldRender = false;
  121. return $result;
  122. }
  123. /** @var \Magento\Paypal\Model\Express $method */
  124. $method = $this->_paymentData->getMethodInstance($this->_bmlMethodCode);
  125. if (!$method->isAvailable()) {
  126. $this->_shouldRender = false;
  127. return $result;
  128. }
  129. $this->setShortcutHtmlId($this->_mathRandom->getUniqueHash('ec_shortcut_bml_'))
  130. ->setCheckoutUrl($this->getUrl($this->_startAction))
  131. ->setImageUrl(self::SHORTCUT_IMAGE)
  132. ->setAdditionalLinkImage(
  133. [
  134. 'href' => 'https://www.securecheckout.billmelater.com/paycapture-content/'
  135. . 'fetch?hash=AU826TU8&content=/bmlweb/ppwpsiw.html',
  136. 'src' => 'https://www.paypalobjects.com/webstatic/en_US/btn/btn_bml_text.png',
  137. ]
  138. );
  139. return $result;
  140. }
  141. /**
  142. * Render the block if needed
  143. *
  144. * @return string
  145. */
  146. protected function _toHtml()
  147. {
  148. if (!$this->_shouldRender) {
  149. return '';
  150. }
  151. return parent::_toHtml();
  152. }
  153. /**
  154. * Check is "OR" label position before shortcut
  155. *
  156. * @return bool
  157. */
  158. public function isOrPositionBefore()
  159. {
  160. return $this->getShowOrPosition() == CatalogBlock\ShortcutButtons::POSITION_BEFORE;
  161. }
  162. /**
  163. * Check is "OR" label position after shortcut
  164. *
  165. * @return bool
  166. */
  167. public function isOrPositionAfter()
  168. {
  169. return $this->getShowOrPosition() == CatalogBlock\ShortcutButtons::POSITION_AFTER;
  170. }
  171. /**
  172. * Get shortcut alias
  173. *
  174. * @return string
  175. */
  176. public function getAlias()
  177. {
  178. return $this->_alias;
  179. }
  180. }