Skip to content

Commit 361bb60

Browse files
authored
ENGCOM-5328: [Checkout] Set Payment Method and Place Order Mutation #723
2 parents 2140a60 + 9ebc3c3 commit 361bb60

File tree

11 files changed

+751
-63
lines changed

11 files changed

+751
-63
lines changed

app/code/Magento/AuthorizenetGraphQl/Model/AuthorizenetDataProvider.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99

1010
use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderInterface;
1111
use Magento\Framework\Stdlib\ArrayManager;
12-
use Magento\Framework\GraphQL\DataObjectConverter;
1312

1413
/**
1514
* DataProvider Model for Authorizenet
1615
*/
1716
class AuthorizenetDataProvider implements AdditionalDataProviderInterface
1817
{
19-
private const PATH_ADDITIONAL_DATA = 'input/payment_method/additional_data/authorizenet_acceptjs';
18+
private const PATH_ADDITIONAL_DATA = 'authorizenet_acceptjs';
2019

2120
/**
2221
* @var ArrayManager
@@ -36,12 +35,12 @@ public function __construct(
3635
/**
3736
* Return additional data
3837
*
39-
* @param array $args
38+
* @param array $data
4039
* @return array
4140
*/
42-
public function getData(array $args): array
41+
public function getData(array $data): array
4342
{
44-
$additionalData = $this->arrayManager->get(static::PATH_ADDITIONAL_DATA, $args) ?? [];
43+
$additionalData = $this->arrayManager->get(static::PATH_ADDITIONAL_DATA, $data) ?? [];
4544
foreach ($additionalData as $key => $value) {
4645
$additionalData[$this->snakeCaseToCamelCase($key)] = $value;
4746
unset($additionalData[$key]);

app/code/Magento/QuoteGraphQl/Model/Cart/Payment/AdditionalDataProviderInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ interface AdditionalDataProviderInterface
1515
/**
1616
* Return Additional Data
1717
*
18-
* @param array $args
18+
* @param array $data
1919
* @return array
2020
*/
21-
public function getData(array $args): array;
21+
public function getData(array $data): array;
2222
}

app/code/Magento/QuoteGraphQl/Model/Cart/Payment/AdditionalDataProviderPool.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public function __construct(array $dataProviders = [])
2929
* Return additional data for the payment method
3030
*
3131
* @param string $methodCode
32-
* @param array $args
32+
* @param array $data
3333
* @return array
3434
*/
35-
public function getData(string $methodCode, array $args): array
35+
public function getData(string $methodCode, array $data): array
3636
{
3737
$additionalData = [];
3838
if (isset($this->dataProviders[$methodCode])) {
39-
$additionalData = $this->dataProviders[$methodCode]->getData($args);
39+
$additionalData = $this->dataProviders[$methodCode]->getData($data);
4040
}
4141

4242
return $additionalData;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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\Api\Data\PaymentInterface;
15+
use Magento\Quote\Api\Data\PaymentInterfaceFactory;
16+
use Magento\Quote\Api\PaymentMethodManagementInterface;
17+
use Magento\Quote\Model\Quote;
18+
use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderPool;
19+
20+
/**
21+
* Set payment method on cart
22+
*/
23+
class SetPaymentMethodOnCart
24+
{
25+
/**
26+
* @var PaymentMethodManagementInterface
27+
*/
28+
private $paymentMethodManagement;
29+
30+
/**
31+
* @var PaymentInterfaceFactory
32+
*/
33+
private $paymentFactory;
34+
35+
/**
36+
* @var AdditionalDataProviderPool
37+
*/
38+
private $additionalDataProviderPool;
39+
40+
/**
41+
* @param PaymentMethodManagementInterface $paymentMethodManagement
42+
* @param PaymentInterfaceFactory $paymentFactory
43+
* @param AdditionalDataProviderPool $additionalDataProviderPool
44+
*/
45+
public function __construct(
46+
PaymentMethodManagementInterface $paymentMethodManagement,
47+
PaymentInterfaceFactory $paymentFactory,
48+
AdditionalDataProviderPool $additionalDataProviderPool
49+
) {
50+
$this->paymentMethodManagement = $paymentMethodManagement;
51+
$this->paymentFactory = $paymentFactory;
52+
$this->additionalDataProviderPool = $additionalDataProviderPool;
53+
}
54+
55+
/**
56+
* Set payment method on cart
57+
*
58+
* @param Quote $cart
59+
* @param array $paymentData
60+
* @throws GraphQlInputException
61+
* @throws GraphQlNoSuchEntityException
62+
*/
63+
public function execute(Quote $cart, array $paymentData): void
64+
{
65+
if (!isset($paymentData['code']) || empty($paymentData['code'])) {
66+
throw new GraphQlInputException(__('Required parameter "code" for "payment_method" is missing.'));
67+
}
68+
$paymentMethodCode = $paymentData['code'];
69+
70+
$poNumber = $paymentData['purchase_order_number'] ?? null;
71+
$additionalData = isset($paymentData['additional_data'])
72+
? $this->additionalDataProviderPool->getData($paymentMethodCode, $paymentData['additional_data'])
73+
: [];
74+
75+
$payment = $this->paymentFactory->create(
76+
[
77+
'data' =>
78+
[
79+
PaymentInterface::KEY_METHOD => $paymentMethodCode,
80+
PaymentInterface::KEY_PO_NUMBER => $poNumber,
81+
PaymentInterface::KEY_ADDITIONAL_DATA => $additionalData,
82+
],
83+
]
84+
);
85+
86+
try {
87+
$this->paymentMethodManagement->set($cart->getId(), $payment);
88+
} catch (NoSuchEntityException $e) {
89+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
90+
} catch (LocalizedException $e) {
91+
throw new GraphQlInputException(__($e->getMessage()), $e);
92+
}
93+
}
94+
}

app/code/Magento/QuoteGraphQl/Model/Resolver/PlaceOrder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6565

6666
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
6767

68-
if ($context->getUserId() === 0) {
68+
if ((int)$context->getUserId() === 0) {
6969
if (!$cart->getCustomerEmail()) {
70-
throw new GraphQlInputException(__("Guest email for cart is missing. Please enter"));
70+
throw new GraphQlInputException(__("Guest email for cart is missing."));
7171
}
7272
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
7373
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\Quote\Api\CartManagementInterface;
18+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
19+
use Magento\QuoteGraphQl\Model\Cart\SetPaymentMethodOnCart;
20+
use Magento\Sales\Api\OrderRepositoryInterface;
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
class SetPaymentAndPlaceOrder implements ResolverInterface
26+
{
27+
/**
28+
* @var CartManagementInterface
29+
*/
30+
private $cartManagement;
31+
32+
/**
33+
* @var GetCartForUser
34+
*/
35+
private $getCartForUser;
36+
37+
/**
38+
* @var OrderRepositoryInterface
39+
*/
40+
private $orderRepository;
41+
42+
/**
43+
* @var SetPaymentMethodOnCart
44+
*/
45+
private $setPaymentMethodOnCart;
46+
47+
/**
48+
* @param GetCartForUser $getCartForUser
49+
* @param CartManagementInterface $cartManagement
50+
* @param OrderRepositoryInterface $orderRepository
51+
* @param SetPaymentMethodOnCart $setPaymentMethodOnCart
52+
*/
53+
public function __construct(
54+
GetCartForUser $getCartForUser,
55+
CartManagementInterface $cartManagement,
56+
OrderRepositoryInterface $orderRepository,
57+
SetPaymentMethodOnCart $setPaymentMethodOnCart
58+
) {
59+
$this->getCartForUser = $getCartForUser;
60+
$this->cartManagement = $cartManagement;
61+
$this->orderRepository = $orderRepository;
62+
$this->setPaymentMethodOnCart = $setPaymentMethodOnCart;
63+
}
64+
65+
/**
66+
* @inheritdoc
67+
*/
68+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
69+
{
70+
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
71+
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
72+
}
73+
$maskedCartId = $args['input']['cart_id'];
74+
75+
if (!isset($args['input']['payment_method']['code']) || empty($args['input']['payment_method']['code'])) {
76+
throw new GraphQlInputException(__('Required parameter "code" for "payment_method" is missing.'));
77+
}
78+
$paymentData = $args['input']['payment_method'];
79+
80+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
81+
82+
if ((int)$context->getUserId() === 0) {
83+
if (!$cart->getCustomerEmail()) {
84+
throw new GraphQlInputException(__("Guest email for cart is missing."));
85+
}
86+
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
87+
}
88+
89+
$this->setPaymentMethodOnCart->execute($cart, $paymentData);
90+
91+
try {
92+
$orderId = $this->cartManagement->placeOrder($cart->getId());
93+
$order = $this->orderRepository->get($orderId);
94+
95+
return [
96+
'order' => [
97+
'order_id' => $order->getIncrementId(),
98+
],
99+
];
100+
} catch (NoSuchEntityException $e) {
101+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
102+
} catch (LocalizedException $e) {
103+
throw new GraphQlInputException(__('Unable to place order: %message', ['message' => $e->getMessage()]), $e);
104+
}
105+
}
106+
}

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

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,12 @@
77

88
namespace Magento\QuoteGraphQl\Model\Resolver;
99

10-
use Magento\Framework\Exception\LocalizedException;
11-
use Magento\Framework\Exception\NoSuchEntityException;
1210
use Magento\Framework\GraphQl\Config\Element\Field;
1311
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1512
use Magento\Framework\GraphQl\Query\ResolverInterface;
1613
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17-
use Magento\Quote\Api\Data\PaymentInterface;
1814
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
19-
use Magento\Quote\Api\Data\PaymentInterfaceFactory;
20-
use Magento\Quote\Api\PaymentMethodManagementInterface;
21-
use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderPool;
22-
use Magento\Framework\App\ObjectManager;
15+
use Magento\QuoteGraphQl\Model\Cart\SetPaymentMethodOnCart as SetPaymentMethodOnCartModel;
2316

2417
/**
2518
* Mutation resolver for setting payment method for shopping cart
@@ -32,37 +25,20 @@ class SetPaymentMethodOnCart implements ResolverInterface
3225
private $getCartForUser;
3326

3427
/**
35-
* @var PaymentMethodManagementInterface
28+
* @var SetPaymentMethodOnCartModel
3629
*/
37-
private $paymentMethodManagement;
38-
39-
/**
40-
* @var PaymentInterfaceFactory
41-
*/
42-
private $paymentFactory;
43-
44-
/**
45-
* @var AdditionalDataProviderPool
46-
*/
47-
private $additionalDataProviderPool;
30+
private $setPaymentMethodOnCart;
4831

4932
/**
5033
* @param GetCartForUser $getCartForUser
51-
* @param PaymentMethodManagementInterface $paymentMethodManagement
52-
* @param PaymentInterfaceFactory $paymentFactory
53-
* @param AdditionalDataProviderPool $additionalDataProviderPool
34+
* @param SetPaymentMethodOnCartModel $setPaymentMethodOnCart
5435
*/
5536
public function __construct(
5637
GetCartForUser $getCartForUser,
57-
PaymentMethodManagementInterface $paymentMethodManagement,
58-
PaymentInterfaceFactory $paymentFactory,
59-
AdditionalDataProviderPool $additionalDataProviderPool = null
38+
SetPaymentMethodOnCartModel $setPaymentMethodOnCart
6039
) {
6140
$this->getCartForUser = $getCartForUser;
62-
$this->paymentMethodManagement = $paymentMethodManagement;
63-
$this->paymentFactory = $paymentFactory;
64-
$this->additionalDataProviderPool = $additionalDataProviderPool
65-
?: ObjectManager::getInstance()->get(AdditionalDataProviderPool::class);
41+
$this->setPaymentMethodOnCart = $setPaymentMethodOnCart;
6642
}
6743

6844
/**
@@ -78,28 +54,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7854
if (!isset($args['input']['payment_method']['code']) || empty($args['input']['payment_method']['code'])) {
7955
throw new GraphQlInputException(__('Required parameter "code" for "payment_method" is missing.'));
8056
}
81-
$paymentMethodCode = $args['input']['payment_method']['code'];
82-
83-
$poNumber = $args['input']['payment_method']['purchase_order_number'] ?? null;
84-
$additionalData = $this->additionalDataProviderPool->getData($paymentMethodCode, $args) ?? [];
57+
$paymentData = $args['input']['payment_method'];
8558

8659
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
87-
$payment = $this->paymentFactory->create(
88-
[
89-
'data' => [
90-
PaymentInterface::KEY_METHOD => $paymentMethodCode,
91-
PaymentInterface::KEY_PO_NUMBER => $poNumber,
92-
PaymentInterface::KEY_ADDITIONAL_DATA => $additionalData,
93-
]]
94-
);
95-
96-
try {
97-
$this->paymentMethodManagement->set($cart->getId(), $payment);
98-
} catch (NoSuchEntityException $e) {
99-
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
100-
} catch (LocalizedException $e) {
101-
throw new GraphQlInputException(__($e->getMessage()), $e);
102-
}
60+
$this->setPaymentMethodOnCart->execute($cart, $paymentData);
10361

10462
return [
10563
'cart' => [

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Mutation {
1818
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart")
1919
setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")
2020
setGuestEmailOnCart(input: SetGuestEmailOnCartInput): SetGuestEmailOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetGuestEmailOnCart")
21+
setPaymentMethodAndPlaceOrder(input: SetPaymentMethodAndPlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentAndPlaceOrder")
2122
placeOrder(input: PlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\PlaceOrder")
2223
}
2324

@@ -119,6 +120,11 @@ input ShippingMethodInput {
119120
method_code: String!
120121
}
121122

123+
input SetPaymentMethodAndPlaceOrderInput {
124+
cart_id: String!
125+
payment_method: PaymentMethodInput!
126+
}
127+
122128
input PlaceOrderInput {
123129
cart_id: String!
124130
}

0 commit comments

Comments
 (0)