Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GuestValidation.php
Go to the documentation of this file.
1 <?php
8 
12 
21 {
25  private $scopeConfiguration;
26 
30  private $agreementsValidator;
31 
35  private $checkoutAgreementsList;
36 
40  private $activeStoreAgreementsFilter;
41 
48  public function __construct(
49  \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
50  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
51  \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
52  \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
53  ) {
54  $this->agreementsValidator = $agreementsValidator;
55  $this->scopeConfiguration = $scopeConfiguration;
56  $this->checkoutAgreementsList = $checkoutAgreementsList;
57  $this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
58  }
59 
71  \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject,
72  $cartId,
73  $email,
74  \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
75  \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
76  ) {
77  if ($this->isAgreementEnabled()) {
78  $this->validateAgreements($paymentMethod);
79  }
80  }
81 
93  \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject,
94  $cartId,
95  $email,
96  \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
97  \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
98  ) {
99  if ($this->isAgreementEnabled()) {
100  $this->validateAgreements($paymentMethod);
101  }
102  }
103 
109  private function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
110  {
111  $agreements = $paymentMethod->getExtensionAttributes() === null
112  ? []
113  : $paymentMethod->getExtensionAttributes()->getAgreementIds();
114 
115  if (!$this->agreementsValidator->isValid($agreements)) {
116  throw new \Magento\Framework\Exception\CouldNotSaveException(
117  __(
118  "The order wasn't placed. "
119  . "First, agree to the terms and conditions, then try placing your order again."
120  )
121  );
122  }
123  }
124 
129  private function isAgreementEnabled()
130  {
131  $isAgreementsEnabled = $this->scopeConfiguration->isSetFlag(
134  );
135  $agreementsList = $isAgreementsEnabled
136  ? $this->checkoutAgreementsList->getList($this->activeStoreAgreementsFilter->buildSearchCriteria())
137  : [];
138  return (bool)($isAgreementsEnabled && count($agreementsList) > 0);
139  }
140 }
__construct(\Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration, \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList, \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter)
$billingAddress
Definition: order.php:25
$email
Definition: details.phtml:13
beforeSavePaymentInformationAndPlaceOrder(\Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject, $cartId, $email, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress=null)
__()
Definition: __.php:13
beforeSavePaymentInformation(\Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject, $cartId, $email, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress=null)
$cartId
Definition: quote.php:22