|
6 | 6 |
|
7 | 7 | namespace Magento\CheckoutAgreements\Model\Checkout\Plugin;
|
8 | 8 |
|
| 9 | +use Magento\Checkout\Api\AgreementsValidatorInterface; |
| 10 | +use Magento\Checkout\Api\GuestPaymentInformationManagementInterface; |
| 11 | +use Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface; |
9 | 12 | use Magento\CheckoutAgreements\Model\AgreementsProvider;
|
| 13 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 14 | +use Magento\Framework\Exception\CouldNotSaveException; |
| 15 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 16 | +use Magento\Quote\Api\Data\AddressInterface; |
| 17 | +use Magento\Quote\Api\Data\PaymentInterface; |
| 18 | +use Magento\Quote\Api\GuestCartRepositoryInterface; |
| 19 | +use Magento\Store\Model\App\Emulation; |
10 | 20 | use Magento\Store\Model\ScopeInterface;
|
11 | 21 | use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
|
12 | 22 |
|
@@ -40,62 +50,85 @@ class GuestValidation
|
40 | 50 | private $activeStoreAgreementsFilter;
|
41 | 51 |
|
42 | 52 | /**
|
43 |
| - * @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator |
44 |
| - * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration |
45 |
| - * @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList |
| 53 | + * @var GuestCartRepositoryInterface |
| 54 | + */ |
| 55 | + private GuestCartRepositoryInterface $quoteRepository; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var Emulation |
| 59 | + */ |
| 60 | + private Emulation $storeEmulation; |
| 61 | + |
| 62 | + /** |
| 63 | + * @param AgreementsValidatorInterface $agreementsValidator |
| 64 | + * @param ScopeConfigInterface $scopeConfiguration |
| 65 | + * @param CheckoutAgreementsListInterface $checkoutAgreementsList |
46 | 66 | * @param ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
|
| 67 | + * @param GuestCartRepositoryInterface $quoteRepository |
| 68 | + * @param Emulation $storeEmulation |
47 | 69 | */
|
48 | 70 | public function __construct(
|
49 | 71 | \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
|
50 | 72 | \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
|
51 | 73 | \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
|
52 |
| - \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter |
| 74 | + \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter, |
| 75 | + GuestCartRepositoryInterface $quoteRepository, |
| 76 | + Emulation $storeEmulation |
53 | 77 | ) {
|
54 | 78 | $this->agreementsValidator = $agreementsValidator;
|
55 | 79 | $this->scopeConfiguration = $scopeConfiguration;
|
56 | 80 | $this->checkoutAgreementsList = $checkoutAgreementsList;
|
57 | 81 | $this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
|
| 82 | + $this->quoteRepository = $quoteRepository; |
| 83 | + $this->storeEmulation = $storeEmulation; |
58 | 84 | }
|
59 | 85 |
|
60 | 86 | /**
|
61 | 87 | * Validates agreements before save payment information and order placing.
|
62 | 88 | *
|
63 |
| - * @param \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject |
| 89 | + * @param GuestPaymentInformationManagementInterface $subject |
64 | 90 | * @param string $cartId
|
65 | 91 | * @param string $email
|
66 |
| - * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod |
67 |
| - * @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress |
68 |
| - * @throws \Magento\Framework\Exception\CouldNotSaveException |
| 92 | + * @param PaymentInterface $paymentMethod |
| 93 | + * @param AddressInterface|null $billingAddress |
69 | 94 | * @return void
|
70 | 95 | * @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
| 96 | + * @throws CouldNotSaveException|NoSuchEntityException |
71 | 97 | */
|
72 | 98 | public function beforeSavePaymentInformationAndPlaceOrder(
|
73 |
| - \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject, |
| 99 | + GuestPaymentInformationManagementInterface $subject, |
74 | 100 | $cartId,
|
75 | 101 | $email,
|
76 |
| - \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, |
77 |
| - \Magento\Quote\Api\Data\AddressInterface $billingAddress = null |
| 102 | + PaymentInterface $paymentMethod, |
| 103 | + AddressInterface $billingAddress = null |
78 | 104 | ) {
|
79 | 105 | if ($this->isAgreementEnabled()) {
|
80 |
| - $this->validateAgreements($paymentMethod); |
| 106 | + $quote = $this->quoteRepository->get($cartId); |
| 107 | + $storeId = $quote->getStoreId(); |
| 108 | + $this->validateAgreements($paymentMethod, $storeId); |
81 | 109 | }
|
82 | 110 | }
|
83 | 111 |
|
84 | 112 | /**
|
85 | 113 | * Validates agreements.
|
86 | 114 | *
|
87 |
| - * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod |
88 |
| - * @throws \Magento\Framework\Exception\CouldNotSaveException |
| 115 | + * @param PaymentInterface $paymentMethod |
| 116 | + * @param int $storeId |
89 | 117 | * @return void
|
| 118 | + * @throws CouldNotSaveException |
90 | 119 | */
|
91 |
| - private function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod) |
| 120 | + private function validateAgreements(PaymentInterface $paymentMethod, int $storeId) |
92 | 121 | {
|
93 | 122 | $agreements = $paymentMethod->getExtensionAttributes() === null
|
94 | 123 | ? []
|
95 | 124 | : $paymentMethod->getExtensionAttributes()->getAgreementIds();
|
96 | 125 |
|
97 |
| - if (!$this->agreementsValidator->isValid($agreements)) { |
98 |
| - throw new \Magento\Framework\Exception\CouldNotSaveException( |
| 126 | + $this->storeEmulation->startEnvironmentEmulation($storeId); |
| 127 | + $isValid = $this->agreementsValidator->isValid($agreements); |
| 128 | + $this->storeEmulation->stopEnvironmentEmulation(); |
| 129 | + |
| 130 | + if (!$isValid) { |
| 131 | + throw new CouldNotSaveException( |
99 | 132 | __(
|
100 | 133 | "The order wasn't placed. "
|
101 | 134 | . "First, agree to the terms and conditions, then try placing your order again."
|
|
0 commit comments