SmartButton.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\Minicart;
  8. use Magento\Checkout\Model\Session;
  9. use Magento\Payment\Model\MethodInterface;
  10. use Magento\Paypal\Model\Config;
  11. use Magento\Paypal\Model\ConfigFactory;
  12. use Magento\Framework\View\Element\Template;
  13. use Magento\Catalog\Block\ShortcutInterface;
  14. use Magento\Framework\View\Element\Template\Context;
  15. use Magento\Framework\Serialize\SerializerInterface;
  16. use Magento\Paypal\Model\SmartButtonConfig;
  17. use Magento\Framework\UrlInterface;
  18. use Magento\Quote\Model\QuoteIdToMaskedQuoteId;
  19. use Magento\Framework\Exception\NoSuchEntityException;
  20. /**
  21. * Class Button
  22. */
  23. class SmartButton extends Template implements ShortcutInterface
  24. {
  25. private const ALIAS_ELEMENT_INDEX = 'alias';
  26. /**
  27. * @var Config
  28. */
  29. private $config;
  30. /**
  31. * @var MethodInterface
  32. */
  33. private $payment;
  34. /**
  35. * @var Session
  36. */
  37. private $session;
  38. /**
  39. * @var SerializerInterface
  40. */
  41. private $serializer;
  42. /**
  43. * @var SmartButtonConfig
  44. */
  45. private $smartButtonConfig;
  46. /**
  47. * @var UrlInterface
  48. */
  49. private $urlBuilder;
  50. /**
  51. * @var QuoteIdToMaskedQuoteId
  52. */
  53. private $quoteIdMask;
  54. /**
  55. * @param Context $context
  56. * @param ConfigFactory $configFactory
  57. * @param Session $session
  58. * @param MethodInterface $payment
  59. * @param SerializerInterface $serializer
  60. * @param SmartButtonConfig $smartButtonConfig
  61. * @param UrlInterface $urlBuilder
  62. * @param QuoteIdToMaskedQuoteId $quoteIdToMaskedQuoteId
  63. * @param array $data
  64. */
  65. public function __construct(
  66. Context $context,
  67. ConfigFactory $configFactory,
  68. Session $session,
  69. MethodInterface $payment,
  70. SerializerInterface $serializer,
  71. SmartButtonConfig $smartButtonConfig,
  72. UrlInterface $urlBuilder,
  73. QuoteIdToMaskedQuoteId $quoteIdToMaskedQuoteId,
  74. array $data = []
  75. ) {
  76. parent::__construct($context, $data);
  77. $this->config = $configFactory->create();
  78. $this->config->setMethod(Config::METHOD_EXPRESS);
  79. $this->payment = $payment;
  80. $this->session = $session;
  81. $this->serializer = $serializer;
  82. $this->smartButtonConfig = $smartButtonConfig;
  83. $this->urlBuilder = $urlBuilder;
  84. $this->quoteIdMask = $quoteIdToMaskedQuoteId;
  85. }
  86. /**
  87. * Check `in_context` config value
  88. *
  89. * @return bool
  90. */
  91. private function isInContext(): bool
  92. {
  93. return (bool)(int) $this->config->getValue('in_context');
  94. }
  95. /**
  96. * Check `visible_on_cart` config value
  97. *
  98. * @return bool
  99. */
  100. private function isVisibleOnCart(): bool
  101. {
  102. return (bool)(int) $this->config->getValue('visible_on_cart');
  103. }
  104. /**
  105. * Check is Paypal In-Context Express Checkout button should render in cart/mini-cart
  106. *
  107. * @return bool
  108. */
  109. private function shouldRender(): bool
  110. {
  111. return $this->payment->isAvailable($this->session->getQuote())
  112. && $this->isInContext()
  113. && $this->isVisibleOnCart()
  114. && $this->getQuoteId()
  115. && !$this->getIsInCatalogProduct();
  116. }
  117. /**
  118. * @inheritdoc
  119. */
  120. protected function _toHtml()
  121. {
  122. if (!$this->shouldRender()) {
  123. return '';
  124. }
  125. return parent::_toHtml();
  126. }
  127. /**
  128. * Get shortcut alias
  129. *
  130. * @return string
  131. */
  132. public function getAlias()
  133. {
  134. return $this->getData(self::ALIAS_ELEMENT_INDEX);
  135. }
  136. /**
  137. * Returns string to initialize js component
  138. *
  139. * @return string
  140. */
  141. public function getJsInitParams(): string
  142. {
  143. $config = [];
  144. $quoteId = $this->getQuoteId();
  145. if (!empty($quoteId)) {
  146. $clientConfig = [
  147. 'quoteId' => $quoteId,
  148. 'customerId' => $this->session->getQuote()->getCustomerId(),
  149. 'button' => 1,
  150. 'getTokenUrl' => $this->urlBuilder->getUrl(
  151. 'paypal/express/getTokenData',
  152. ['_secure' => $this->getRequest()->isSecure()]
  153. ),
  154. 'onAuthorizeUrl' => $this->urlBuilder->getUrl(
  155. 'paypal/express/onAuthorization',
  156. ['_secure' => $this->getRequest()->isSecure()]
  157. ),
  158. 'onCancelUrl' => $this->urlBuilder->getUrl(
  159. 'paypal/express/cancel',
  160. ['_secure' => $this->getRequest()->isSecure()]
  161. )
  162. ];
  163. $smartButtonsConfig = $this->getIsShoppingCart()
  164. ? $this->smartButtonConfig->getConfig('cart')
  165. : $this->smartButtonConfig->getConfig('mini_cart');
  166. $clientConfig = array_replace_recursive($clientConfig, $smartButtonsConfig);
  167. $config = [
  168. 'Magento_Paypal/js/in-context/button' => [
  169. 'clientConfig' => $clientConfig
  170. ]
  171. ];
  172. }
  173. $json = $this->serializer->serialize($config);
  174. return $json;
  175. }
  176. /**
  177. * Returns container id
  178. *
  179. * @return string
  180. */
  181. public function getContainerId(): string
  182. {
  183. return $this->getData('button_id');
  184. }
  185. /**
  186. * Get quote id from session
  187. *
  188. * @return string
  189. */
  190. private function getQuoteId(): string
  191. {
  192. $quoteId = (int)$this->session->getQuoteId();
  193. if (!$this->session->getQuote()->getCustomerId()) {
  194. try {
  195. $quoteId = $this->quoteIdMask->execute($quoteId);
  196. } catch (NoSuchEntityException $e) {
  197. $quoteId = "";
  198. }
  199. }
  200. return (string)$quoteId;
  201. }
  202. }