CheckoutValidator.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. class CheckoutValidator implements ValidatorInterface
  18. {
  19. /**
  20. * @var \Magento\Checkout\Model\Session
  21. */
  22. private $_checkoutSession;
  23. /**
  24. * @var \Magento\Payment\Helper\Data
  25. */
  26. private $_paymentData;
  27. /**
  28. * @var Validator
  29. */
  30. private $_shortcutValidator;
  31. /**
  32. * @param \Magento\Checkout\Model\Session $checkoutSession
  33. * @param Validator $shortcutValidator
  34. * @param \Magento\Payment\Helper\Data $paymentData
  35. */
  36. public function __construct(
  37. \Magento\Checkout\Model\Session $checkoutSession,
  38. Validator $shortcutValidator,
  39. \Magento\Payment\Helper\Data $paymentData
  40. ) {
  41. $this->_checkoutSession = $checkoutSession;
  42. $this->_paymentData = $paymentData;
  43. $this->_shortcutValidator = $shortcutValidator;
  44. }
  45. /**
  46. * Validates shortcut
  47. *
  48. * @param string $code
  49. * @param bool $isInCatalog
  50. * @return bool
  51. */
  52. public function validate($code, $isInCatalog)
  53. {
  54. return $this->_shortcutValidator->isContextAvailable($code, $isInCatalog)
  55. && $this->_shortcutValidator->isPriceOrSetAvailable($isInCatalog)
  56. && $this->isMethodQuoteAvailable($code, $isInCatalog)
  57. && $this->isQuoteSummaryValid($isInCatalog);
  58. }
  59. /**
  60. * Checks payment method and quote availability
  61. *
  62. * @param string $paymentCode
  63. * @param bool $isInCatalog
  64. * @return bool
  65. */
  66. public function isMethodQuoteAvailable($paymentCode, $isInCatalog)
  67. {
  68. $quote = $isInCatalog ? null : $this->_checkoutSession->getQuote();
  69. // check payment method availability
  70. /** @var \Magento\Payment\Model\Method\AbstractMethod $methodInstance */
  71. $methodInstance = $this->_paymentData->getMethodInstance($paymentCode);
  72. if (!$methodInstance->isAvailable($quote)) {
  73. return false;
  74. }
  75. return true;
  76. }
  77. /**
  78. * Validates minimum quote amount and zero grand total
  79. *
  80. * @param bool $isInCatalog
  81. * @return bool
  82. */
  83. public function isQuoteSummaryValid($isInCatalog)
  84. {
  85. $quote = $isInCatalog ? null : $this->_checkoutSession->getQuote();
  86. // validate minimum quote amount and validate quote for zero grandtotal
  87. if (null !== $quote && (!$quote->validateMinimumAmount() || !$quote->getGrandTotal())) {
  88. return false;
  89. }
  90. return true;
  91. }
  92. }