SmartButton.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Paypal\Block\Express\InContext;
  8. use Magento\Paypal\Model\Config;
  9. use Magento\Paypal\Model\ConfigFactory;
  10. use Magento\Framework\View\Element\Template;
  11. use Magento\Catalog\Block\ShortcutInterface;
  12. use Magento\Framework\View\Element\Template\Context;
  13. use Magento\Framework\Serialize\SerializerInterface;
  14. use Magento\Paypal\Model\SmartButtonConfig;
  15. use Magento\Framework\UrlInterface;
  16. /**
  17. * Class Button
  18. */
  19. class SmartButton extends Template implements ShortcutInterface
  20. {
  21. private const ALIAS_ELEMENT_INDEX = 'alias';
  22. /**
  23. * @var Config
  24. */
  25. private $config;
  26. /**
  27. * @var SerializerInterface
  28. */
  29. private $serializer;
  30. /**
  31. * @var SmartButtonConfig
  32. */
  33. private $smartButtonConfig;
  34. /**
  35. * @var UrlInterface
  36. */
  37. private $urlBuilder;
  38. /**
  39. * @param Context $context
  40. * @param ConfigFactory $configFactory
  41. * @param SerializerInterface $serializer
  42. * @param SmartButtonConfig $smartButtonConfig
  43. * @param UrlInterface $urlBuilder
  44. * @param array $data
  45. */
  46. public function __construct(
  47. Context $context,
  48. ConfigFactory $configFactory,
  49. SerializerInterface $serializer,
  50. SmartButtonConfig $smartButtonConfig,
  51. UrlInterface $urlBuilder,
  52. array $data = []
  53. ) {
  54. parent::__construct($context, $data);
  55. $this->config = $configFactory->create();
  56. $this->config->setMethod(Config::METHOD_EXPRESS);
  57. $this->serializer = $serializer;
  58. $this->smartButtonConfig = $smartButtonConfig;
  59. $this->urlBuilder = $urlBuilder;
  60. }
  61. /**
  62. * Check is Paypal In-Context Express Checkout button should render in cart/mini-cart
  63. *
  64. * @return bool
  65. */
  66. private function shouldRender(): bool
  67. {
  68. $isInCatalog = $this->getIsInCatalogProduct();
  69. $isInContext = (bool)(int) $this->config->getValue('in_context');
  70. return ($isInContext && $isInCatalog);
  71. }
  72. /**
  73. * @inheritdoc
  74. */
  75. protected function _toHtml()
  76. {
  77. if (!$this->shouldRender()) {
  78. return '';
  79. }
  80. return parent::_toHtml();
  81. }
  82. /**
  83. * Get shortcut alias
  84. *
  85. * @return string
  86. */
  87. public function getAlias()
  88. {
  89. return $this->getData(self::ALIAS_ELEMENT_INDEX);
  90. }
  91. /**
  92. * Returns string to initialize js component
  93. *
  94. * @return string
  95. */
  96. public function getJsInitParams(): string
  97. {
  98. $clientConfig = [
  99. 'button' => 1,
  100. 'getTokenUrl' => $this->urlBuilder->getUrl(
  101. 'paypal/express/getTokenData',
  102. ['_secure' => $this->getRequest()->isSecure()]
  103. ),
  104. 'onAuthorizeUrl' => $this->urlBuilder->getUrl(
  105. 'paypal/express/onAuthorization',
  106. ['_secure' => $this->getRequest()->isSecure()]
  107. ),
  108. 'onCancelUrl' => $this->urlBuilder->getUrl(
  109. 'paypal/express/cancel',
  110. ['_secure' => $this->getRequest()->isSecure()]
  111. )
  112. ];
  113. $smartButtonsConfig = $this->smartButtonConfig->getConfig('product');
  114. $clientConfig = array_replace_recursive($clientConfig, $smartButtonsConfig);
  115. $config = [
  116. 'Magento_Paypal/js/in-context/product-express-checkout' => [
  117. 'clientConfig' => $clientConfig
  118. ]
  119. ];
  120. return $this->serializer->serialize($config);
  121. }
  122. }