Skip to content

Commit e04a45c

Browse files
committed
Merge remote-tracking branch 'origin/ACP2E-4223' into PR_2025_10_24_muntianu
2 parents 7fa400a + 124f954 commit e04a45c

25 files changed

+1170
-385
lines changed

app/code/Magento/Checkout/Model/GuestPaymentInformationManagement.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Quote\Api\CartRepositoryInterface;
1616
use Magento\Framework\Exception\CouldNotSaveException;
1717
use Magento\Quote\Model\Quote;
18+
use Magento\Quote\Model\QuoteAddressValidationService;
1819
use Psr\Log\LoggerInterface as Logger;
1920

2021
/**
@@ -81,6 +82,11 @@ class GuestPaymentInformationManagement implements \Magento\Checkout\Api\GuestPa
8182
*/
8283
private $addressComparator;
8384

85+
/**
86+
* @var QuoteAddressValidationService
87+
*/
88+
private $quoteAddressValidationService;
89+
8490
/**
8591
* @param \Magento\Quote\Api\GuestBillingAddressManagementInterface $billingAddressManagement
8692
* @param \Magento\Quote\Api\GuestPaymentMethodManagementInterface $paymentMethodManagement
@@ -92,7 +98,9 @@ class GuestPaymentInformationManagement implements \Magento\Checkout\Api\GuestPa
9298
* @param PaymentProcessingRateLimiterInterface|null $paymentsRateLimiter
9399
* @param PaymentSavingRateLimiterInterface|null $savingRateLimiter
94100
* @param AddressComparatorInterface|null $addressComparator
101+
* @param QuoteAddressValidationService|null $quoteAddressValidationService
95102
* @codeCoverageIgnore
103+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
96104
*/
97105
public function __construct(
98106
\Magento\Quote\Api\GuestBillingAddressManagementInterface $billingAddressManagement,
@@ -104,7 +112,8 @@ public function __construct(
104112
Logger $logger,
105113
?PaymentProcessingRateLimiterInterface $paymentsRateLimiter = null,
106114
?PaymentSavingRateLimiterInterface $savingRateLimiter = null,
107-
?AddressComparatorInterface $addressComparator = null
115+
?AddressComparatorInterface $addressComparator = null,
116+
?QuoteAddressValidationService $quoteAddressValidationService = null
108117
) {
109118
$this->billingAddressManagement = $billingAddressManagement;
110119
$this->paymentMethodManagement = $paymentMethodManagement;
@@ -118,6 +127,8 @@ public function __construct(
118127
?? ObjectManager::getInstance()->get(PaymentSavingRateLimiterInterface::class);
119128
$this->addressComparator = $addressComparator
120129
?? ObjectManager::getInstance()->get(AddressComparatorInterface::class);
130+
$this->quoteAddressValidationService = $quoteAddressValidationService
131+
?? ObjectManager::getInstance()->get(QuoteAddressValidationService::class);
121132
$this->logger = $logger;
122133
}
123134

@@ -185,6 +196,9 @@ public function savePaymentInformation(
185196
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
186197
/** @var Quote $quote */
187198
$quote = $this->cartRepository->getActive($quoteIdMask->getQuoteId());
199+
200+
$this->quoteAddressValidationService->validateAddressesWithRules($quote, null, $billingAddress);
201+
188202
$shippingAddress = $quote->getShippingAddress();
189203
if ($this->addressComparator->isEqual($shippingAddress, $billingAddress)) {
190204
$shippingAddress->setSameAsBilling(1);

app/code/Magento/Checkout/Model/GuestShippingInformationManagement.php

Lines changed: 1 addition & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
namespace Magento\Checkout\Model;
77

88
use Magento\Checkout\Api\Data\ShippingInformationInterface;
9-
use Magento\Customer\Model\AddressFactory;
10-
use Magento\Framework\Exception\InputException;
11-
use Magento\Framework\Exception\LocalizedException;
12-
use Magento\Framework\Validator\Factory as ValidatorFactory;
13-
use Magento\Quote\Api\Data\AddressInterface;
149

1510
class GuestShippingInformationManagement implements \Magento\Checkout\Api\GuestShippingInformationManagementInterface
1611
{
@@ -24,145 +19,31 @@ class GuestShippingInformationManagement implements \Magento\Checkout\Api\GuestS
2419
*/
2520
protected $shippingInformationManagement;
2621

27-
/**
28-
* @var ValidatorFactory
29-
*/
30-
private $validatorFactory;
31-
32-
/**
33-
* @var AddressFactory
34-
*/
35-
private $addressFactory;
36-
3722
/**
3823
* @param \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory
3924
* @param \Magento\Checkout\Api\ShippingInformationManagementInterface $shippingInformationManagement
40-
* @param ValidatorFactory $validatorFactory
41-
* @param AddressFactory $addressFactory
4225
* @codeCoverageIgnore
4326
*/
4427
public function __construct(
4528
\Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory,
46-
\Magento\Checkout\Api\ShippingInformationManagementInterface $shippingInformationManagement,
47-
ValidatorFactory $validatorFactory,
48-
AddressFactory $addressFactory
29+
\Magento\Checkout\Api\ShippingInformationManagementInterface $shippingInformationManagement
4930
) {
5031
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
5132
$this->shippingInformationManagement = $shippingInformationManagement;
52-
$this->validatorFactory = $validatorFactory;
53-
$this->addressFactory = $addressFactory;
5433
}
5534

5635
/**
5736
* @inheritDoc
58-
*
59-
* @throws InputException
6037
*/
6138
public function saveAddressInformation(
6239
$cartId,
6340
ShippingInformationInterface $addressInformation
6441
) {
65-
$shippingAddress = $addressInformation->getShippingAddress();
66-
if ($shippingAddress) {
67-
$this->validateAddressAttributes($shippingAddress, 'shipping');
68-
}
69-
7042
/** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */
7143
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
7244
return $this->shippingInformationManagement->saveAddressInformation(
7345
(int) $quoteIdMask->getQuoteId(),
7446
$addressInformation
7547
);
7648
}
77-
78-
/**
79-
* Validate address attributes using customer_address validator with custom attributes
80-
*
81-
* @param AddressInterface $address
82-
* @param string $addressType
83-
* @return void
84-
* @throws InputException
85-
*/
86-
private function validateAddressAttributes(AddressInterface $address, string $addressType): void
87-
{
88-
try {
89-
$customerAddress = $this->createCustomerAddressFromQuoteAddress($address, $addressType);
90-
$extensionAttributes = $address->getExtensionAttributes();
91-
if ($extensionAttributes) {
92-
$extensionAttributesData = $extensionAttributes->__toArray();
93-
foreach ($extensionAttributesData as $attributeCode => $value) {
94-
if ($value !== null && $value !== '') {
95-
$customerAddress->setData($attributeCode, $value);
96-
}
97-
}
98-
}
99-
$customerAddress->setSkipRequiredValidation(true);
100-
$validator = $this->validatorFactory->createValidator('customer_address', 'save');
101-
if (!$validator->isValid($customerAddress)) {
102-
$this->throwValidationException($validator->getMessages(), $addressType);
103-
}
104-
} catch (LocalizedException $e) {
105-
throw new InputException(__($e->getMessage()));
106-
}
107-
}
108-
109-
/**
110-
* Create customer address object from quote address
111-
*
112-
* @param AddressInterface $address
113-
* @param string $addressType
114-
* @return \Magento\Customer\Model\Address
115-
*/
116-
private function createCustomerAddressFromQuoteAddress(
117-
AddressInterface $address,
118-
string $addressType
119-
): \Magento\Customer\Model\Address {
120-
$customerAddress = $this->addressFactory->create();
121-
$customerAddress->setData([
122-
'firstname' => $address->getFirstname(),
123-
'lastname' => $address->getLastname(),
124-
'street' => $address->getStreet(),
125-
'city' => $address->getCity(),
126-
'region' => $address->getRegion(),
127-
'region_id' => $address->getRegionId(),
128-
'region_code' => $address->getRegionCode(),
129-
'postcode' => $address->getPostcode(),
130-
'country_id' => $address->getCountryId(),
131-
'telephone' => $address->getTelephone(),
132-
'company' => $address->getCompany(),
133-
'email' => $address->getEmail(),
134-
'address_type' => $addressType
135-
]);
136-
137-
return $customerAddress;
138-
}
139-
140-
/**
141-
* Process validator messages and throw validation exception
142-
*
143-
* @param array $messages
144-
* @param string $addressType
145-
* @return void
146-
* @throws InputException
147-
*/
148-
private function throwValidationException(array $messages, string $addressType): void
149-
{
150-
$errorMessages = [];
151-
foreach ($messages as $message) {
152-
if (is_array($message)) {
153-
foreach ($message as $msg) {
154-
$errorMessages[] = $msg;
155-
}
156-
} else {
157-
$errorMessages[] = $message;
158-
}
159-
}
160-
throw new InputException(
161-
__(
162-
'The %1 address contains invalid data: %2',
163-
$addressType,
164-
implode(', ', $errorMessages)
165-
)
166-
);
167-
}
16849
}

app/code/Magento/Checkout/Model/PaymentInformationManagement.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Magento\Quote\Api\Data\PaymentInterface;
2020
use Magento\Quote\Model\Quote;
2121
use Magento\Quote\Model\Quote\Address;
22+
use Magento\Quote\Model\QuoteAddressValidationService;
2223
use Psr\Log\LoggerInterface;
2324

2425
/**
@@ -90,6 +91,11 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
9091
*/
9192
private $logger;
9293

94+
/**
95+
* @var QuoteAddressValidationService
96+
*/
97+
private $quoteAddressValidationService;
98+
9399
/**
94100
* @param \Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement
95101
* @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
@@ -102,6 +108,7 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
102108
* @param AddressRepositoryInterface|null $addressRepository
103109
* @param AddressComparatorInterface|null $addressComparator
104110
* @param LoggerInterface|null $logger
111+
* @param QuoteAddressValidationService|null $quoteAddressValidationService
105112
* @codeCoverageIgnore
106113
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
107114
*/
@@ -116,7 +123,8 @@ public function __construct(
116123
?CartRepositoryInterface $cartRepository = null,
117124
?AddressRepositoryInterface $addressRepository = null,
118125
?AddressComparatorInterface $addressComparator = null,
119-
?LoggerInterface $logger = null
126+
?LoggerInterface $logger = null,
127+
?QuoteAddressValidationService $quoteAddressValidationService = null
120128
) {
121129
$this->billingAddressManagement = $billingAddressManagement;
122130
$this->paymentMethodManagement = $paymentMethodManagement;
@@ -134,6 +142,8 @@ public function __construct(
134142
$this->addressComparator = $addressComparator
135143
?? ObjectManager::getInstance()->get(AddressComparatorInterface::class);
136144
$this->logger = $logger ?? ObjectManager::getInstance()->get(LoggerInterface::class);
145+
$this->quoteAddressValidationService = $quoteAddressValidationService
146+
?? ObjectManager::getInstance()->get(QuoteAddressValidationService::class);
137147
}
138148

139149
/**
@@ -205,6 +215,13 @@ public function savePaymentInformation(
205215
//It's necessary to verify the price rules with the customer data
206216
$billingAddress->setCustomerId($customerId);
207217
}
218+
219+
$this->quoteAddressValidationService->validateAddressesWithRules(
220+
$quote,
221+
null,
222+
$billingAddress
223+
);
224+
208225
$this->updateCustomerBillingAddressId($quote, $billingAddress);
209226
$quote->removeAddress($quote->getBillingAddress()->getId());
210227
$quote->setBillingAddress($billingAddress);

app/code/Magento/Checkout/Model/ShippingInformationManagement.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Magento\Quote\Model\Quote;
2727
use Magento\Quote\Model\Quote\TotalsCollector;
2828
use Magento\Quote\Model\QuoteAddressValidator;
29+
use Magento\Quote\Model\QuoteAddressValidationService;
2930
use Magento\Quote\Model\ShippingAssignmentFactory;
3031
use Magento\Quote\Model\ShippingFactory;
3132
use Psr\Log\LoggerInterface as Logger;
@@ -34,6 +35,7 @@
3435
* Class checkout shipping information management
3536
*
3637
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
38+
* @SuppressWarnings(PHPMD.TooManyFields)
3739
*/
3840
class ShippingInformationManagement implements ShippingInformationManagementInterface
3941
{
@@ -107,6 +109,11 @@ class ShippingInformationManagement implements ShippingInformationManagementInte
107109
*/
108110
private $addressComparator;
109111

112+
/**
113+
* @var QuoteAddressValidationService
114+
*/
115+
private $quoteAddressValidationService;
116+
110117
/**
111118
* @param PaymentMethodManagementInterface $paymentMethodManagement
112119
* @param PaymentDetailsFactory $paymentDetailsFactory
@@ -121,6 +128,7 @@ class ShippingInformationManagement implements ShippingInformationManagementInte
121128
* @param ShippingAssignmentFactory|null $shippingAssignmentFactory
122129
* @param ShippingFactory|null $shippingFactory
123130
* @param AddressComparatorInterface|null $addressComparator
131+
* @param QuoteAddressValidationService|null $quoteAddressValidationService
124132
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
125133
*/
126134
public function __construct(
@@ -137,6 +145,7 @@ public function __construct(
137145
?ShippingAssignmentFactory $shippingAssignmentFactory = null,
138146
?ShippingFactory $shippingFactory = null,
139147
?AddressComparatorInterface $addressComparator = null,
148+
?QuoteAddressValidationService $quoteAddressValidationService = null
140149
) {
141150
$this->paymentMethodManagement = $paymentMethodManagement;
142151
$this->paymentDetailsFactory = $paymentDetailsFactory;
@@ -155,6 +164,8 @@ public function __construct(
155164
->get(ShippingFactory::class);
156165
$this->addressComparator = $addressComparator
157166
?? ObjectManager::getInstance()->get(AddressComparatorInterface::class);
167+
$this->quoteAddressValidationService = $quoteAddressValidationService ?: ObjectManager::getInstance()
168+
->get(QuoteAddressValidationService::class);
158169
}
159170

160171
/**
@@ -177,13 +188,19 @@ public function saveAddressInformation(
177188

178189
$address = $addressInformation->getShippingAddress();
179190
$this->validateAddress($address);
191+
180192
$this->updateCustomerShippingAddressId($quote, $address);
181193
if (!$address->getCustomerAddressId()) {
182194
$address->setCustomerAddressId(null);
183195
}
184196

185197
try {
186198
$billingAddress = $addressInformation->getBillingAddress();
199+
$this->quoteAddressValidationService->validateAddressesWithRules(
200+
$quote,
201+
$address,
202+
$billingAddress
203+
);
187204
if ($billingAddress) {
188205
$this->updateCustomerBillingAddressId($quote, $billingAddress);
189206
if (!$billingAddress->getCustomerAddressId()) {

0 commit comments

Comments
 (0)