Button.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Block\Paypal;
  7. use Magento\Braintree\Gateway\Config\PayPal\Config;
  8. use Magento\Braintree\Model\Ui\ConfigProvider;
  9. use Magento\Catalog\Block\ShortcutInterface;
  10. use Magento\Checkout\Model\Session;
  11. use Magento\Framework\Locale\ResolverInterface;
  12. use Magento\Framework\View\Element\Template;
  13. use Magento\Framework\View\Element\Template\Context;
  14. use Magento\Payment\Model\MethodInterface;
  15. /**
  16. * Class Button
  17. */
  18. class Button extends Template implements ShortcutInterface
  19. {
  20. const ALIAS_ELEMENT_INDEX = 'alias';
  21. const BUTTON_ELEMENT_INDEX = 'button_id';
  22. /**
  23. * @var ResolverInterface
  24. */
  25. private $localeResolver;
  26. /**
  27. * @var Session
  28. */
  29. private $checkoutSession;
  30. /**
  31. * @var Config
  32. */
  33. private $config;
  34. /**
  35. * @var ConfigProvider
  36. */
  37. private $configProvider;
  38. /**
  39. * @var MethodInterface
  40. */
  41. private $payment;
  42. /**
  43. * Constructor
  44. *
  45. * @param Context $context
  46. * @param ResolverInterface $localeResolver
  47. * @param Session $checkoutSession
  48. * @param Config $config
  49. * @param ConfigProvider $configProvider
  50. * @param MethodInterface $payment
  51. * @param array $data
  52. */
  53. public function __construct(
  54. Context $context,
  55. ResolverInterface $localeResolver,
  56. Session $checkoutSession,
  57. Config $config,
  58. ConfigProvider $configProvider,
  59. MethodInterface $payment,
  60. array $data = []
  61. ) {
  62. parent::__construct($context, $data);
  63. $this->localeResolver = $localeResolver;
  64. $this->checkoutSession = $checkoutSession;
  65. $this->config = $config;
  66. $this->configProvider = $configProvider;
  67. $this->payment = $payment;
  68. }
  69. /**
  70. * @inheritdoc
  71. */
  72. protected function _toHtml()
  73. {
  74. if ($this->isActive()) {
  75. return parent::_toHtml();
  76. }
  77. return '';
  78. }
  79. /**
  80. * @inheritdoc
  81. */
  82. public function getAlias()
  83. {
  84. return $this->getData(self::ALIAS_ELEMENT_INDEX);
  85. }
  86. /**
  87. * @return string
  88. */
  89. public function getContainerId()
  90. {
  91. return $this->getData(self::BUTTON_ELEMENT_INDEX);
  92. }
  93. /**
  94. * @return string
  95. */
  96. public function getLocale()
  97. {
  98. return $this->localeResolver->getLocale();
  99. }
  100. /**
  101. * @return string
  102. */
  103. public function getCurrency()
  104. {
  105. return $this->checkoutSession->getQuote()->getCurrency()->getBaseCurrencyCode();
  106. }
  107. /**
  108. * @return float
  109. */
  110. public function getAmount()
  111. {
  112. return $this->checkoutSession->getQuote()->getBaseGrandTotal();
  113. }
  114. /**
  115. * @return bool
  116. */
  117. public function isActive()
  118. {
  119. return $this->payment->isAvailable($this->checkoutSession->getQuote()) &&
  120. $this->config->isDisplayShoppingCart();
  121. }
  122. /**
  123. * @return string
  124. */
  125. public function getMerchantName()
  126. {
  127. return $this->config->getMerchantName();
  128. }
  129. /**
  130. * @return string|null
  131. */
  132. public function getClientToken()
  133. {
  134. return $this->configProvider->getClientToken();
  135. }
  136. /**
  137. * @return string
  138. */
  139. public function getActionSuccess()
  140. {
  141. return $this->getUrl(ConfigProvider::CODE . '/paypal/review', ['_secure' => true]);
  142. }
  143. }