Skip to content

Commit d646aef

Browse files
PWA-1892: GraphQL support for async order module
PWA-1892: GraphQL support for async order module - add fixtures PWA-1892: Moving getCartForUser and error handling up to resolvers PWA-1892: Removing unnecessary parameter
1 parent ace9009 commit d646aef

File tree

13 files changed

+448
-118
lines changed

13 files changed

+448
-118
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1313
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1414
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
15+
use Magento\Quote\Api\CartManagementInterface;
1516
use Magento\Quote\Api\CartRepositoryInterface;
1617
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1718
use Magento\Quote\Model\Quote;
@@ -32,6 +33,11 @@ class GetCartForUser
3233
*/
3334
private $cartRepository;
3435

36+
/**
37+
* @var CheckCartCheckoutAllowance
38+
*/
39+
private $checkoutAllowance;
40+
3541
/**
3642
* @var StoreRepositoryInterface
3743
*/
@@ -40,15 +46,18 @@ class GetCartForUser
4046
/**
4147
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
4248
* @param CartRepositoryInterface $cartRepository
49+
* @param CheckCartCheckoutAllowance $checkoutAllowance
4350
* @param StoreRepositoryInterface $storeRepository
4451
*/
4552
public function __construct(
4653
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
4754
CartRepositoryInterface $cartRepository,
55+
CheckCartCheckoutAllowance $checkoutAllowance,
4856
StoreRepositoryInterface $storeRepository = null
4957
) {
5058
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
5159
$this->cartRepository = $cartRepository;
60+
$this->checkoutAllowance = $checkoutAllowance;
5261
$this->storeRepository = $storeRepository ?: ObjectManager::getInstance()->get(StoreRepositoryInterface::class);
5362
}
5463

@@ -107,6 +116,36 @@ public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
107116
return $cart;
108117
}
109118

119+
/**
120+
* Gets the cart for the user validated and configured for guest checkout if applicable
121+
*
122+
* @param string $cartHash
123+
* @param int|null $customerId
124+
* @param int $storeId
125+
* @return Quote
126+
* @throws GraphQlAuthorizationException
127+
* @throws GraphQlInputException
128+
* @throws GraphQlNoSuchEntityException
129+
*/
130+
public function getCartForCheckout(string $cartHash, ?int $customerId, int $storeId): Quote
131+
{
132+
try {
133+
$cart = $this->execute($cartHash, $customerId, $storeId);
134+
} catch (NoSuchEntityException $e) {
135+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
136+
}
137+
$this->checkoutAllowance->execute($cart);
138+
139+
if ((null === $customerId || 0 === $customerId)) {
140+
if (!$cart->getCustomerEmail()) {
141+
throw new GraphQlInputException(__("Guest email for cart is missing."));
142+
}
143+
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
144+
}
145+
146+
return $cart;
147+
}
148+
110149
/**
111150
* Sets cart currency based on specified store.
112151
*
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\QuoteGraphQl\Model\Cart\Payment;
9+
10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11+
use Magento\Quote\Api\Data\PaymentInterface;
12+
use Magento\Quote\Api\Data\PaymentInterfaceFactory;
13+
14+
/**
15+
* Build payment method objects
16+
*/
17+
class PaymentMethodBuilder
18+
{
19+
/**
20+
* @var PaymentInterfaceFactory
21+
*/
22+
private $paymentFactory;
23+
24+
/**
25+
* @var AdditionalDataProviderPool
26+
*/
27+
private $paymentDataProvider;
28+
29+
/**
30+
* @param PaymentInterfaceFactory $paymentFactory
31+
* @param AdditionalDataProviderPool $paymentDataProvider
32+
*/
33+
public function __construct(
34+
PaymentInterfaceFactory $paymentFactory,
35+
AdditionalDataProviderPool $paymentDataProvider
36+
) {
37+
$this->paymentFactory = $paymentFactory;
38+
$this->paymentDataProvider = $paymentDataProvider;
39+
}
40+
41+
/**
42+
* Build a PaymentInterface object from the supplied data array
43+
*
44+
* @param array $paymentData
45+
* @return PaymentInterface
46+
* @throws GraphQlInputException
47+
*/
48+
public function build(array $paymentData): PaymentInterface
49+
{
50+
if (!isset($paymentData['code']) || empty($paymentData['code'])) {
51+
throw new GraphQlInputException(__('Required parameter "code" for "payment_method" is missing.'));
52+
}
53+
$paymentMethodCode = $paymentData['code'];
54+
55+
$poNumber = $paymentData['purchase_order_number'] ?? null;
56+
$additionalData = $this->paymentDataProvider->getData($paymentMethodCode, $paymentData);
57+
58+
return $this->paymentFactory->create(
59+
[
60+
'data' => [
61+
PaymentInterface::KEY_METHOD => $paymentMethodCode,
62+
PaymentInterface::KEY_PO_NUMBER => $poNumber,
63+
PaymentInterface::KEY_ADDITIONAL_DATA => $additionalData,
64+
],
65+
]
66+
);
67+
}
68+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Quote\Api\CartManagementInterface;
13+
use Magento\Quote\Api\PaymentMethodManagementInterface;
14+
use Magento\Quote\Model\Quote;
15+
16+
/**
17+
* Place an order
18+
*/
19+
class PlaceOrder
20+
{
21+
/**
22+
* @var PaymentMethodManagementInterface
23+
*/
24+
private $paymentManagement;
25+
26+
/**
27+
* @var CartManagementInterface
28+
*/
29+
private $cartManagement;
30+
31+
/**
32+
* @param PaymentMethodManagementInterface $paymentManagement
33+
* @param CartManagementInterface $cartManagement
34+
*/
35+
public function __construct(
36+
PaymentMethodManagementInterface $paymentManagement,
37+
CartManagementInterface $cartManagement
38+
) {
39+
$this->paymentManagement = $paymentManagement;
40+
$this->cartManagement = $cartManagement;
41+
}
42+
43+
/**
44+
* Place an order
45+
*
46+
* @param Quote $cart
47+
* @param string $maskedCartId
48+
* @param int $userId
49+
* @return int
50+
*
51+
* @throws LocalizedException
52+
* @throws NoSuchEntityException
53+
*
54+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
55+
*/
56+
public function execute(Quote $cart, string $maskedCartId, int $userId): int
57+
{
58+
$cartId = (int)$cart->getId();
59+
$paymentMethod = $this->paymentManagement->get($cartId);
60+
61+
return (int)$this->cartManagement->placeOrder($cartId, $paymentMethod);
62+
}
63+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Quote\Model\Quote;
15+
16+
/**
17+
* Set payment method and place order
18+
*/
19+
class SetPaymentAndPlaceOrder
20+
{
21+
/**
22+
* @var SetPaymentMethodOnCart
23+
*/
24+
private $setPaymentMethod;
25+
26+
/**
27+
* @var PlaceOrder
28+
*/
29+
private $placeOrder;
30+
31+
/**
32+
* @param SetPaymentMethodOnCart $setPaymentMethod
33+
* @param PlaceOrder $placeOrder
34+
*/
35+
public function __construct(
36+
SetPaymentMethodOnCart $setPaymentMethod,
37+
PlaceOrder $placeOrder
38+
) {
39+
$this->setPaymentMethod = $setPaymentMethod;
40+
$this->placeOrder = $placeOrder;
41+
}
42+
43+
/**
44+
* Set payment method and place order
45+
*
46+
* @param Quote $cart
47+
* @param string $maskedCartId
48+
* @param int $userId
49+
* @param array $paymentData
50+
* @return int
51+
*
52+
* @throws GraphQlInputException
53+
* @throws GraphQlNoSuchEntityException
54+
* @throws LocalizedException
55+
* @throws NoSuchEntityException
56+
*/
57+
public function execute(Quote $cart, string $maskedCartId, int $userId, array $paymentData): int
58+
{
59+
$this->setPaymentMethod->execute($cart, $paymentData);
60+
return $this->placeOrder->execute($cart, $maskedCartId, $userId);
61+
}
62+
}

app/code/Magento/QuoteGraphQl/Model/Cart/SetPaymentMethodOnCart.php

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
use Magento\Framework\Exception\NoSuchEntityException;
1616
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1717
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
18-
use Magento\Quote\Api\Data\PaymentInterface;
19-
use Magento\Quote\Api\Data\PaymentInterfaceFactory;
2018
use Magento\Quote\Api\PaymentMethodManagementInterface;
2119
use Magento\Quote\Model\Quote;
22-
use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderPool;
20+
use Magento\QuoteGraphQl\Model\Cart\Payment\PaymentMethodBuilder;
2321

2422
/**
2523
* Saves related payment method info for a cart.
@@ -34,14 +32,9 @@ class SetPaymentMethodOnCart
3432
private $paymentMethodManagement;
3533

3634
/**
37-
* @var PaymentInterfaceFactory
35+
* @var PaymentMethodBuilder
3836
*/
39-
private $paymentFactory;
40-
41-
/**
42-
* @var AdditionalDataProviderPool
43-
*/
44-
private $additionalDataProviderPool;
37+
private $paymentMethodBuilder;
4538

4639
/**
4740
* @var PaymentSavingRateLimiterInterface
@@ -50,23 +43,20 @@ class SetPaymentMethodOnCart
5043

5144
/**
5245
* @param PaymentMethodManagementInterface $paymentMethodManagement
53-
* @param PaymentInterfaceFactory $paymentFactory
54-
* @param AdditionalDataProviderPool $additionalDataProviderPool
46+
* @param PaymentMethodBuilder $paymentMethodBuilder
5547
* @param PaymentProcessingRateLimiterInterface|null $paymentRateLimiter
5648
* @param PaymentSavingRateLimiterInterface|null $savingRateLimiter
5749
*
5850
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5951
*/
6052
public function __construct(
6153
PaymentMethodManagementInterface $paymentMethodManagement,
62-
PaymentInterfaceFactory $paymentFactory,
63-
AdditionalDataProviderPool $additionalDataProviderPool,
54+
PaymentMethodBuilder $paymentMethodBuilder,
6455
?PaymentProcessingRateLimiterInterface $paymentRateLimiter = null,
6556
?PaymentSavingRateLimiterInterface $savingRateLimiter = null
6657
) {
6758
$this->paymentMethodManagement = $paymentMethodManagement;
68-
$this->paymentFactory = $paymentFactory;
69-
$this->additionalDataProviderPool = $additionalDataProviderPool;
59+
$this->paymentMethodBuilder = $paymentMethodBuilder;
7060
$this->paymentRateLimiter = $savingRateLimiter
7161
?? ObjectManager::getInstance()->get(PaymentSavingRateLimiterInterface::class);
7262
}
@@ -92,23 +82,7 @@ public function execute(Quote $cart, array $paymentData): void
9282
throw new GraphQlInputException(__($exception->getMessage()), $exception);
9383
}
9484

95-
if (!isset($paymentData['code']) || empty($paymentData['code'])) {
96-
throw new GraphQlInputException(__('Required parameter "code" for "payment_method" is missing.'));
97-
}
98-
$paymentMethodCode = $paymentData['code'];
99-
100-
$poNumber = $paymentData['purchase_order_number'] ?? null;
101-
$additionalData = $this->additionalDataProviderPool->getData($paymentMethodCode, $paymentData);
102-
103-
$payment = $this->paymentFactory->create(
104-
[
105-
'data' => [
106-
PaymentInterface::KEY_METHOD => $paymentMethodCode,
107-
PaymentInterface::KEY_PO_NUMBER => $poNumber,
108-
PaymentInterface::KEY_ADDITIONAL_DATA => $additionalData,
109-
],
110-
]
111-
);
85+
$payment = $this->paymentMethodBuilder->build($paymentData);
11286

11387
try {
11488
$this->paymentMethodManagement->set($cart->getId(), $payment);

0 commit comments

Comments
 (0)