GuestValidation.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 GuestValidation
  12. *
  13. * Plugin that checks if checkout agreement enabled and validates all agreements.
  14. * Current plugin is duplicate from Magento\CheckoutAgreements\Model\Checkout\Plugin\Validation due to different
  15. * interfaces of payment information and makes check before processing of payment information.
  16. */
  17. class GuestValidation
  18. {
  19. /**
  20. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  21. */
  22. private $scopeConfiguration;
  23. /**
  24. * @var \Magento\Checkout\Api\AgreementsValidatorInterface
  25. */
  26. private $agreementsValidator;
  27. /**
  28. * @var \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface
  29. */
  30. private $checkoutAgreementsList;
  31. /**
  32. * @var \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter
  33. */
  34. private $activeStoreAgreementsFilter;
  35. /**
  36. * @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator
  37. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration
  38. * @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList
  39. * @param ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
  40. */
  41. public function __construct(
  42. \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
  43. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
  44. \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
  45. \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
  46. ) {
  47. $this->agreementsValidator = $agreementsValidator;
  48. $this->scopeConfiguration = $scopeConfiguration;
  49. $this->checkoutAgreementsList = $checkoutAgreementsList;
  50. $this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
  51. }
  52. /**
  53. * @param \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject
  54. * @param string $cartId
  55. * @param string $email
  56. * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
  57. * @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
  58. * @throws \Magento\Framework\Exception\CouldNotSaveException
  59. * @return void
  60. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  61. */
  62. public function beforeSavePaymentInformationAndPlaceOrder(
  63. \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject,
  64. $cartId,
  65. $email,
  66. \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
  67. \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
  68. ) {
  69. if ($this->isAgreementEnabled()) {
  70. $this->validateAgreements($paymentMethod);
  71. }
  72. }
  73. /**
  74. * @param \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject
  75. * @param string $cartId
  76. * @param string $email
  77. * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
  78. * @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
  79. * @throws \Magento\Framework\Exception\CouldNotSaveException
  80. * @return void
  81. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  82. */
  83. public function beforeSavePaymentInformation(
  84. \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject,
  85. $cartId,
  86. $email,
  87. \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
  88. \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
  89. ) {
  90. if ($this->isAgreementEnabled()) {
  91. $this->validateAgreements($paymentMethod);
  92. }
  93. }
  94. /**
  95. * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
  96. * @throws \Magento\Framework\Exception\CouldNotSaveException
  97. * @return void
  98. */
  99. private function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
  100. {
  101. $agreements = $paymentMethod->getExtensionAttributes() === null
  102. ? []
  103. : $paymentMethod->getExtensionAttributes()->getAgreementIds();
  104. if (!$this->agreementsValidator->isValid($agreements)) {
  105. throw new \Magento\Framework\Exception\CouldNotSaveException(
  106. __(
  107. "The order wasn't placed. "
  108. . "First, agree to the terms and conditions, then try placing your order again."
  109. )
  110. );
  111. }
  112. }
  113. /**
  114. * Verify if agreement validation needed
  115. * @return bool
  116. */
  117. private function isAgreementEnabled()
  118. {
  119. $isAgreementsEnabled = $this->scopeConfiguration->isSetFlag(
  120. AgreementsProvider::PATH_ENABLED,
  121. ScopeInterface::SCOPE_STORE
  122. );
  123. $agreementsList = $isAgreementsEnabled
  124. ? $this->checkoutAgreementsList->getList($this->activeStoreAgreementsFilter->buildSearchCriteria())
  125. : [];
  126. return (bool)($isAgreementsEnabled && count($agreementsList) > 0);
  127. }
  128. }