Validator.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Payment\Helper\Shortcut;
  17. /**
  18. * @SuppressWarnings(PHPMD.UnusedPrivateField)
  19. */
  20. class Validator implements ValidatorInterface
  21. {
  22. /**
  23. * @var \Amazon\Payment\Gateway\Config\Config
  24. */
  25. private $amazonConfig;
  26. /**
  27. * @var \Magento\Framework\Registry
  28. */
  29. private $registry;
  30. /**
  31. * @var \Magento\Catalog\Model\ProductTypes\ConfigInterface
  32. */
  33. private $productTypeConfig;
  34. /**
  35. * @var \Magento\Payment\Helper\Data
  36. */
  37. private $paymentData;
  38. /**
  39. * @var \Amazon\Core\Helper\CategoryExclusion
  40. */
  41. private $categoryExclusionHelper;
  42. /**
  43. * Validator constructor.
  44. *
  45. * @param \Amazon\Payment\Gateway\Config\Config $amazonConfig
  46. * @param \Magento\Framework\Registry $registry
  47. * @param \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig
  48. * @param \Magento\Payment\Helper\Data $paymentData
  49. * @param \Amazon\Core\Helper\CategoryExclusion $categoryExclusionHelper
  50. */
  51. public function __construct(
  52. \Amazon\Payment\Gateway\Config\Config $amazonConfig,
  53. \Magento\Framework\Registry $registry,
  54. \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
  55. \Magento\Payment\Helper\Data $paymentData,
  56. \Amazon\Core\Helper\CategoryExclusion $categoryExclusionHelper
  57. ) {
  58. $this->amazonConfig = $amazonConfig;
  59. $this->registry = $registry;
  60. $this->productTypeConfig = $productTypeConfig;
  61. $this->paymentData = $paymentData;
  62. $this->categoryExclusionHelper = $categoryExclusionHelper;
  63. }
  64. /**
  65. * Validates shortcut
  66. *
  67. * @param string $code
  68. * @param bool $isInCatalog
  69. * @return bool
  70. */
  71. public function validate($code, $isInCatalog)
  72. {
  73. return $this->isContextAvailable($code, $isInCatalog)
  74. && $this->isPriceOrSetAvailable($isInCatalog)
  75. && $this->isMethodAvailable($code);
  76. }
  77. /**
  78. * Checks visibility of context (cart or product page)
  79. *
  80. * @param string $paymentCode Payment method code
  81. * @param bool $isInCatalog
  82. * @return bool
  83. */
  84. public function isContextAvailable($paymentCode, $isInCatalog)
  85. {
  86. $this->amazonConfig->setMethodCode($this->amazonConfig::CODE);
  87. // check visibility on product page
  88. if ($isInCatalog && $this->amazonConfig->getValue('pwa_pp_button_is_visible')) {
  89. $currentProduct = $this->registry->registry('current_product');
  90. if ($currentProduct !== null) {
  91. if ($this->categoryExclusionHelper->productHasExcludedCategory($currentProduct)) {
  92. return false;
  93. }
  94. } else {
  95. if ($this->categoryExclusionHelper->isQuoteDirty()) {
  96. return false;
  97. }
  98. }
  99. return true;
  100. }
  101. return false;
  102. }
  103. /**
  104. * Check is product available depending on final price or type set(configurable)
  105. *
  106. * @param bool $isInCatalog
  107. * @return bool
  108. */
  109. public function isPriceOrSetAvailable($isInCatalog)
  110. {
  111. if ($isInCatalog) {
  112. // Show PayPal shortcut on a product view page only if product has nonzero price
  113. /** @var $currentProduct \Magento\Catalog\Model\Product */
  114. $currentProduct = $this->registry->registry('current_product');
  115. if ($currentProduct !== null) {
  116. $productPrice = (double)$currentProduct->getFinalPrice();
  117. $typeInstance = $currentProduct->getTypeInstance();
  118. if (empty($productPrice)
  119. && !$this->productTypeConfig->isProductSet($currentProduct->getTypeId())
  120. && !$typeInstance->canConfigure($currentProduct)
  121. ) {
  122. return false;
  123. }
  124. }
  125. }
  126. return true;
  127. }
  128. /**
  129. * Checks payment method and quote availability
  130. *
  131. * @param string $paymentCode
  132. * @return bool
  133. */
  134. public function isMethodAvailable($paymentCode)
  135. {
  136. // check payment method availability
  137. /** @var \Magento\Payment\Model\Method\AbstractMethod $methodInstance */
  138. $methodInstance = $this->paymentData->getMethodInstance($paymentCode);
  139. if (!$methodInstance->isAvailable()) {
  140. return false;
  141. }
  142. return true;
  143. }
  144. }