Skip to content

Commit 47c388a

Browse files
committed
ACP2E-1339: Orders can be placed via Rest API as a guest user even when "Allow Guest Checkout" is turned off
1 parent bab1f54 commit 47c388a

7 files changed

+388
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Checkout\Plugin\Api;
9+
10+
use Magento\Checkout\Helper\Data as CheckoutHelper;
11+
use Magento\Framework\Exception\CouldNotSaveException;
12+
use Magento\Quote\Api\CartRepositoryInterface;
13+
use Magento\Quote\Api\GuestBillingAddressManagementInterface;
14+
use Magento\Quote\Model\QuoteIdMask;
15+
use Magento\Quote\Model\QuoteIdMaskFactory;
16+
17+
class VerifyIsGuestCheckoutEnabledBeforeAssignBillingAddress
18+
{
19+
/**
20+
* @var CheckoutHelper
21+
*/
22+
private CheckoutHelper $checkoutHelper;
23+
24+
/**
25+
* @var QuoteIdMaskFactory
26+
*/
27+
private QuoteIdMaskFactory $quoteIdMaskFactory;
28+
29+
/**
30+
* @var CartRepositoryInterface
31+
*/
32+
private CartRepositoryInterface $cartRepository;
33+
34+
/**
35+
* @param CheckoutHelper $checkoutHelper
36+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
37+
* @param CartRepositoryInterface $cartRepository
38+
*/
39+
public function __construct(
40+
CheckoutHelper $checkoutHelper,
41+
QuoteIdMaskFactory $quoteIdMaskFactory,
42+
CartRepositoryInterface $cartRepository
43+
) {
44+
$this->checkoutHelper = $checkoutHelper;
45+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
46+
$this->cartRepository = $cartRepository;
47+
}
48+
49+
/**
50+
* Checks whether guest checkout is enabled before assigning billing address
51+
*
52+
* @param GuestBillingAddressManagementInterface $subject
53+
* @param string $cartId
54+
* @return void
55+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
56+
*/
57+
public function beforeAssign(
58+
GuestBillingAddressManagementInterface $subject,
59+
$cartId
60+
): void {
61+
/** @var $quoteIdMask QuoteIdMask */
62+
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
63+
$quote = $this->cartRepository->get($quoteIdMask->getQuoteId());
64+
if (!$this->checkoutHelper->isAllowedGuestCheckout($quote)) {
65+
throw new CouldNotSaveException(__('Sorry, guest checkout is not available.'));
66+
}
67+
}
68+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Checkout\Plugin\Api;
9+
10+
use Magento\Checkout\Helper\Data as CheckoutHelper;
11+
use Magento\Framework\Exception\CouldNotSaveException;
12+
use Magento\Quote\Api\CartRepositoryInterface;
13+
use Magento\Quote\Api\GuestCartManagementInterface;
14+
use Magento\Quote\Model\QuoteIdMask;
15+
use Magento\Quote\Model\QuoteIdMaskFactory;
16+
17+
class VerifyIsGuestCheckoutEnabledBeforePlaceOrder
18+
{
19+
/**
20+
* @var CheckoutHelper
21+
*/
22+
private CheckoutHelper $checkoutHelper;
23+
24+
/**
25+
* @var QuoteIdMaskFactory
26+
*/
27+
private QuoteIdMaskFactory $quoteIdMaskFactory;
28+
29+
/**
30+
* @var CartRepositoryInterface
31+
*/
32+
private CartRepositoryInterface $cartRepository;
33+
34+
/**
35+
* @param CheckoutHelper $checkoutHelper
36+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
37+
* @param CartRepositoryInterface $cartRepository
38+
*/
39+
public function __construct(
40+
CheckoutHelper $checkoutHelper,
41+
QuoteIdMaskFactory $quoteIdMaskFactory,
42+
CartRepositoryInterface $cartRepository
43+
) {
44+
$this->checkoutHelper = $checkoutHelper;
45+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
46+
$this->cartRepository = $cartRepository;
47+
}
48+
49+
/**
50+
* Checks whether guest checkout is enabled before placing order
51+
*
52+
* @param GuestCartManagementInterface $subject
53+
* @param string $cartId
54+
* @return void
55+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
56+
*/
57+
public function beforePlaceOrder(
58+
GuestCartManagementInterface $subject,
59+
$cartId
60+
): void {
61+
/** @var $quoteIdMask QuoteIdMask */
62+
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
63+
$quote = $this->cartRepository->get($quoteIdMask->getQuoteId());
64+
if (!$this->checkoutHelper->isAllowedGuestCheckout($quote)) {
65+
throw new CouldNotSaveException(__('Sorry, guest checkout is not available.'));
66+
}
67+
}
68+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Checkout\Plugin\Api;
9+
10+
use Magento\Checkout\Api\GuestPaymentInformationManagementInterface;
11+
use Magento\Checkout\Helper\Data as CheckoutHelper;
12+
use Magento\Framework\Exception\CouldNotSaveException;
13+
use Magento\Quote\Api\CartRepositoryInterface;
14+
use Magento\Quote\Model\QuoteIdMask;
15+
use Magento\Quote\Model\QuoteIdMaskFactory;
16+
17+
class VerifyIsGuestCheckoutEnabledBeforeSavePaymentInformation
18+
{
19+
/**
20+
* @var CheckoutHelper
21+
*/
22+
private CheckoutHelper $checkoutHelper;
23+
24+
/**
25+
* @var QuoteIdMaskFactory
26+
*/
27+
private QuoteIdMaskFactory $quoteIdMaskFactory;
28+
29+
/**
30+
* @var CartRepositoryInterface
31+
*/
32+
private CartRepositoryInterface $cartRepository;
33+
34+
/**
35+
* @param CheckoutHelper $checkoutHelper
36+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
37+
* @param CartRepositoryInterface $cartRepository
38+
*/
39+
public function __construct(
40+
CheckoutHelper $checkoutHelper,
41+
QuoteIdMaskFactory $quoteIdMaskFactory,
42+
CartRepositoryInterface $cartRepository
43+
) {
44+
$this->checkoutHelper = $checkoutHelper;
45+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
46+
$this->cartRepository = $cartRepository;
47+
}
48+
49+
/**
50+
* Checks whether guest checkout is enabled before saving payment information
51+
*
52+
* @param GuestPaymentInformationManagementInterface $subject
53+
* @param string $cartId
54+
* @return void
55+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
56+
*/
57+
public function beforeSavePaymentInformation(
58+
GuestPaymentInformationManagementInterface $subject,
59+
$cartId
60+
): void {
61+
/** @var $quoteIdMask QuoteIdMask */
62+
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
63+
$quote = $this->cartRepository->get($quoteIdMask->getQuoteId());
64+
if (!$this->checkoutHelper->isAllowedGuestCheckout($quote)) {
65+
throw new CouldNotSaveException(__('Sorry, guest checkout is not available.'));
66+
}
67+
}
68+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Checkout\Plugin\Api;
9+
10+
use Magento\Checkout\Api\GuestShippingInformationManagementInterface;
11+
use Magento\Checkout\Helper\Data as CheckoutHelper;
12+
use Magento\Framework\Exception\CouldNotSaveException;
13+
use Magento\Quote\Api\CartRepositoryInterface;
14+
use Magento\Quote\Model\QuoteIdMask;
15+
use Magento\Quote\Model\QuoteIdMaskFactory;
16+
17+
class VerifyIsGuestCheckoutEnabledBeforeSaveShippingInformation
18+
{
19+
/**
20+
* @var CheckoutHelper
21+
*/
22+
private CheckoutHelper $checkoutHelper;
23+
24+
/**
25+
* @var QuoteIdMaskFactory
26+
*/
27+
private QuoteIdMaskFactory $quoteIdMaskFactory;
28+
29+
/**
30+
* @var CartRepositoryInterface
31+
*/
32+
private CartRepositoryInterface $cartRepository;
33+
34+
/**
35+
* @param CheckoutHelper $checkoutHelper
36+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
37+
* @param CartRepositoryInterface $cartRepository
38+
*/
39+
public function __construct(
40+
CheckoutHelper $checkoutHelper,
41+
QuoteIdMaskFactory $quoteIdMaskFactory,
42+
CartRepositoryInterface $cartRepository
43+
) {
44+
$this->checkoutHelper = $checkoutHelper;
45+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
46+
$this->cartRepository = $cartRepository;
47+
}
48+
49+
/**
50+
* Checks whether guest checkout is enabled before saving shipping information
51+
*
52+
* @param GuestShippingInformationManagementInterface $subject
53+
* @param string $cartId
54+
* @return void
55+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
56+
*/
57+
public function beforeSaveAddressInformation(
58+
GuestShippingInformationManagementInterface $subject,
59+
$cartId
60+
): void {
61+
/** @var $quoteIdMask QuoteIdMask */
62+
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
63+
$quote = $this->cartRepository->get($quoteIdMask->getQuoteId());
64+
if (!$this->checkoutHelper->isAllowedGuestCheckout($quote)) {
65+
throw new CouldNotSaveException(__('Sorry, guest checkout is not available.'));
66+
}
67+
}
68+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Checkout\Plugin\Api;
9+
10+
use Magento\Checkout\Helper\Data as CheckoutHelper;
11+
use Magento\Framework\Exception\CouldNotSaveException;
12+
use Magento\Quote\Api\CartRepositoryInterface;
13+
use Magento\Quote\Api\GuestPaymentMethodManagementInterface;
14+
use Magento\Quote\Model\QuoteIdMask;
15+
use Magento\Quote\Model\QuoteIdMaskFactory;
16+
17+
class VerifyIsGuestCheckoutEnabledBeforeSetPaymentMethod
18+
{
19+
/**
20+
* @var CheckoutHelper
21+
*/
22+
private CheckoutHelper $checkoutHelper;
23+
24+
/**
25+
* @var QuoteIdMaskFactory
26+
*/
27+
private QuoteIdMaskFactory $quoteIdMaskFactory;
28+
29+
/**
30+
* @var CartRepositoryInterface
31+
*/
32+
private CartRepositoryInterface $cartRepository;
33+
34+
/**
35+
* @param CheckoutHelper $checkoutHelper
36+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
37+
* @param CartRepositoryInterface $cartRepository
38+
*/
39+
public function __construct(
40+
CheckoutHelper $checkoutHelper,
41+
QuoteIdMaskFactory $quoteIdMaskFactory,
42+
CartRepositoryInterface $cartRepository
43+
) {
44+
$this->checkoutHelper = $checkoutHelper;
45+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
46+
$this->cartRepository = $cartRepository;
47+
}
48+
49+
/**
50+
* Checks whether guest checkout is enabled before setting payment method
51+
*
52+
* @param GuestPaymentMethodManagementInterface $subject
53+
* @param string $cartId
54+
* @return void
55+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
56+
*/
57+
public function beforeSet(
58+
GuestPaymentMethodManagementInterface $subject,
59+
$cartId
60+
): void {
61+
/** @var $quoteIdMask QuoteIdMask */
62+
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
63+
$quote = $this->cartRepository->get($quoteIdMask->getQuoteId());
64+
if (!$this->checkoutHelper->isAllowedGuestCheckout($quote)) {
65+
throw new CouldNotSaveException(__('Sorry, guest checkout is not available.'));
66+
}
67+
}
68+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\Checkout\Api\GuestPaymentInformationManagementInterface">
10+
<plugin name="verify_is_guest_checkout_enabled" type="Magento\Checkout\Plugin\Api\VerifyIsGuestCheckoutEnabledBeforeSavePaymentInformation"/>
11+
</type>
12+
<type name="Magento\Checkout\Api\GuestShippingInformationManagementInterface">
13+
<plugin name="verify_is_guest_checkout_enabled" type="Magento\Checkout\Plugin\Api\VerifyIsGuestCheckoutEnabledBeforeSaveShippingInformation"/>
14+
</type>
15+
<type name="Magento\Quote\Api\GuestCartManagementInterface">
16+
<plugin name="verify_is_guest_checkout_enabled" type="Magento\Checkout\Plugin\Api\VerifyIsGuestCheckoutEnabledBeforePlaceOrder"/>
17+
</type>
18+
<type name="Magento\Quote\Api\GuestPaymentMethodManagementInterface">
19+
<plugin name="verify_is_guest_checkout_enabled" type="Magento\Checkout\Plugin\Api\VerifyIsGuestCheckoutEnabledBeforeSetPaymentMethod"/>
20+
</type>
21+
<type name="Magento\Quote\Api\GuestBillingAddressManagementInterface">
22+
<plugin name="verify_is_guest_checkout_enabled" type="Magento\Checkout\Plugin\Api\VerifyIsGuestCheckoutEnabledBeforeAssignBillingAddress"/>
23+
</type>
24+
</config>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\Checkout\Api\GuestPaymentInformationManagementInterface">
10+
<plugin name="verify_is_guest_checkout_enabled" type="Magento\Checkout\Plugin\Api\VerifyIsGuestCheckoutEnabledBeforeSavePaymentInformation"/>
11+
</type>
12+
<type name="Magento\Checkout\Api\GuestShippingInformationManagementInterface">
13+
<plugin name="verify_is_guest_checkout_enabled" type="Magento\Checkout\Plugin\Api\VerifyIsGuestCheckoutEnabledBeforeSaveShippingInformation"/>
14+
</type>
15+
<type name="Magento\Quote\Api\GuestCartManagementInterface">
16+
<plugin name="verify_is_guest_checkout_enabled" type="Magento\Checkout\Plugin\Api\VerifyIsGuestCheckoutEnabledBeforePlaceOrder"/>
17+
</type>
18+
<type name="Magento\Quote\Api\GuestPaymentMethodManagementInterface">
19+
<plugin name="verify_is_guest_checkout_enabled" type="Magento\Checkout\Plugin\Api\VerifyIsGuestCheckoutEnabledBeforeSetPaymentMethod"/>
20+
</type>
21+
<type name="Magento\Quote\Api\GuestBillingAddressManagementInterface">
22+
<plugin name="verify_is_guest_checkout_enabled" type="Magento\Checkout\Plugin\Api\VerifyIsGuestCheckoutEnabledBeforeAssignBillingAddress"/>
23+
</type>
24+
</config>

0 commit comments

Comments
 (0)