Skip to content

Commit ae8d9a6

Browse files
committed
AC-6731 : Validation fixes for webAPI
1 parent 2c03260 commit ae8d9a6

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

app/code/Magento/Quote/Model/BillingAddressManagement.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
use Magento\Framework\App\ObjectManager;
1010
use Magento\Framework\Exception\InputException;
11+
use Magento\Framework\Exception\NoSuchEntityException;
1112
use Magento\Quote\Api\BillingAddressManagementInterface;
1213
use Magento\Quote\Api\Data\AddressInterface;
14+
use Magento\Quote\Api\Data\CartInterface;
1315
use Psr\Log\LoggerInterface as Logger;
1416

1517
/**
@@ -77,12 +79,8 @@ public function assign($cartId, AddressInterface $address, $useForShipping = fal
7779
/** @var \Magento\Quote\Model\Quote $quote */
7880
$quote = $this->quoteRepository->getActive($cartId);
7981

80-
// reset the address id, if it doesn't belong to quote.
81-
$old = $quote->getAddressesCollection()->getItemById($address->getId())
82-
?? $quote->getBillingAddress();
83-
if ($old !== null) {
84-
$address->setId($old->getId());
85-
}
82+
// validate the address
83+
$this->validateAddress($quote, $address);
8684

8785
$address->setCustomerId($quote->getCustomerId());
8886
$quote->removeAddress($quote->getBillingAddress()->getId());
@@ -121,4 +119,24 @@ private function getShippingAddressAssignment()
121119
}
122120
return $this->shippingAddressAssignment;
123121
}
122+
123+
/**
124+
* Validate address to be used for cart.
125+
*
126+
* @param CartInterface $cart
127+
* @param AddressInterface $address
128+
* @return void
129+
* @throws InputException The specified address belongs to another customer.
130+
* @throws NoSuchEntityException The specified customer ID or address ID is not valid.
131+
*/
132+
public function validateAddress(CartInterface $cart, AddressInterface $address): void
133+
{
134+
// check if address belongs to quote.
135+
if ($address->getId() !== null) {
136+
$old = $cart->getAddressesCollection()->getItemById($address->getId());
137+
if ($old === null) {
138+
throw new InputException(__('Invalid address'));
139+
}
140+
}
141+
}
124142
}

0 commit comments

Comments
 (0)