Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Validation.php
Go to the documentation of this file.
1 <?php
8 
12 
17 {
22 
27 
31  private $checkoutAgreementsList;
32 
36  private $activeStoreAgreementsFilter;
37 
44  public function __construct(
45  \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
47  \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
48  \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
49  ) {
50  $this->agreementsValidator = $agreementsValidator;
51  $this->scopeConfiguration = $scopeConfiguration;
52  $this->checkoutAgreementsList = $checkoutAgreementsList;
53  $this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
54  }
55 
66  \Magento\Checkout\Api\PaymentInformationManagementInterface $subject,
67  $cartId,
68  \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
69  \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
70  ) {
71  if ($this->isAgreementEnabled()) {
72  $this->validateAgreements($paymentMethod);
73  }
74  }
75 
86  \Magento\Checkout\Api\PaymentInformationManagementInterface $subject,
87  $cartId,
88  \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
89  \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
90  ) {
91  if ($this->isAgreementEnabled()) {
92  $this->validateAgreements($paymentMethod);
93  }
94  }
95 
101  protected function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
102  {
103  $agreements = $paymentMethod->getExtensionAttributes() === null
104  ? []
105  : $paymentMethod->getExtensionAttributes()->getAgreementIds();
106 
107  if (!$this->agreementsValidator->isValid($agreements)) {
108  throw new \Magento\Framework\Exception\CouldNotSaveException(
109  __(
110  "The order wasn't placed. "
111  . "First, agree to the terms and conditions, then try placing your order again."
112  )
113  );
114  }
115  }
116 
121  protected function isAgreementEnabled()
122  {
123  $isAgreementsEnabled = $this->scopeConfiguration->isSetFlag(
126  );
127  $agreementsList = $isAgreementsEnabled
128  ? $this->checkoutAgreementsList->getList($this->activeStoreAgreementsFilter->buildSearchCriteria())
129  : [];
130  return (bool)($isAgreementsEnabled && count($agreementsList) > 0);
131  }
132 }
validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
Definition: Validation.php:101
beforeSavePaymentInformation(\Magento\Checkout\Api\PaymentInformationManagementInterface $subject, $cartId, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress=null)
Definition: Validation.php:85
$billingAddress
Definition: order.php:25
__construct(\Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration, \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList, \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter)
Definition: Validation.php:44
__()
Definition: __.php:13
$cartId
Definition: quote.php:22
beforeSavePaymentInformationAndPlaceOrder(\Magento\Checkout\Api\PaymentInformationManagementInterface $subject, $cartId, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress=null)
Definition: Validation.php:65