Validation.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CheckoutAgreements\Model\Checkout\Plugin;
  7. use Magento\CheckoutAgreements\Model\AgreementsProvider;
  8. use Magento\Store\Model\ScopeInterface;
  9. use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
  10. /**
  11. * Class Validation
  12. */
  13. class Validation
  14. {
  15. /**
  16. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  17. */
  18. protected $scopeConfiguration;
  19. /**
  20. * @var \Magento\Checkout\Api\AgreementsValidatorInterface
  21. */
  22. protected $agreementsValidator;
  23. /**
  24. * @var \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface
  25. */
  26. private $checkoutAgreementsList;
  27. /**
  28. * @var \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter
  29. */
  30. private $activeStoreAgreementsFilter;
  31. /**
  32. * @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator
  33. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration
  34. * @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList
  35. * @param ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
  36. */
  37. public function __construct(
  38. \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
  39. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
  40. \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
  41. \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
  42. ) {
  43. $this->agreementsValidator = $agreementsValidator;
  44. $this->scopeConfiguration = $scopeConfiguration;
  45. $this->checkoutAgreementsList = $checkoutAgreementsList;
  46. $this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
  47. }
  48. /**
  49. * @param \Magento\Checkout\Api\PaymentInformationManagementInterface $subject
  50. * @param int $cartId
  51. * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
  52. * @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
  53. * @throws \Magento\Framework\Exception\CouldNotSaveException
  54. * @return void
  55. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  56. */
  57. public function beforeSavePaymentInformationAndPlaceOrder(
  58. \Magento\Checkout\Api\PaymentInformationManagementInterface $subject,
  59. $cartId,
  60. \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
  61. \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
  62. ) {
  63. if ($this->isAgreementEnabled()) {
  64. $this->validateAgreements($paymentMethod);
  65. }
  66. }
  67. /**
  68. * @param \Magento\Checkout\Api\PaymentInformationManagementInterface $subject
  69. * @param int $cartId
  70. * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
  71. * @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
  72. * @throws \Magento\Framework\Exception\CouldNotSaveException
  73. * @return void
  74. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  75. */
  76. public function beforeSavePaymentInformation(
  77. \Magento\Checkout\Api\PaymentInformationManagementInterface $subject,
  78. $cartId,
  79. \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
  80. \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
  81. ) {
  82. if ($this->isAgreementEnabled()) {
  83. $this->validateAgreements($paymentMethod);
  84. }
  85. }
  86. /**
  87. * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
  88. * @throws \Magento\Framework\Exception\CouldNotSaveException
  89. * @return void
  90. */
  91. protected function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
  92. {
  93. $agreements = $paymentMethod->getExtensionAttributes() === null
  94. ? []
  95. : $paymentMethod->getExtensionAttributes()->getAgreementIds();
  96. if (!$this->agreementsValidator->isValid($agreements)) {
  97. throw new \Magento\Framework\Exception\CouldNotSaveException(
  98. __(
  99. "The order wasn't placed. "
  100. . "First, agree to the terms and conditions, then try placing your order again."
  101. )
  102. );
  103. }
  104. }
  105. /**
  106. * Verify if agreement validation needed
  107. * @return bool
  108. */
  109. protected function isAgreementEnabled()
  110. {
  111. $isAgreementsEnabled = $this->scopeConfiguration->isSetFlag(
  112. AgreementsProvider::PATH_ENABLED,
  113. ScopeInterface::SCOPE_STORE
  114. );
  115. $agreementsList = $isAgreementsEnabled
  116. ? $this->checkoutAgreementsList->getList($this->activeStoreAgreementsFilter->buildSearchCriteria())
  117. : [];
  118. return (bool)($isAgreementsEnabled && count($agreementsList) > 0);
  119. }
  120. }