Skip to content

Commit 908ed15

Browse files
authored
Merge pull request #3811 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents e5b0392 + 8d19d7c commit 908ed15

18 files changed

+1448
-388
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public function __construct(
4545
* Get cart for user
4646
*
4747
* @param string $cartHash
48-
* @param int|null $userId
48+
* @param int|null $customerId
4949
* @return Quote
5050
* @throws GraphQlAuthorizationException
5151
* @throws GraphQlNoSuchEntityException
5252
*/
53-
public function execute(string $cartHash, ?int $userId): Quote
53+
public function execute(string $cartHash, ?int $customerId): Quote
5454
{
5555
try {
5656
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
@@ -69,14 +69,14 @@ public function execute(string $cartHash, ?int $userId): Quote
6969
);
7070
}
7171

72-
$customerId = (int)$cart->getCustomerId();
72+
$cartCustomerId = (int)$cart->getCustomerId();
7373

7474
/* Guest cart, allow operations */
75-
if (!$customerId) {
75+
if (!$cartCustomerId && null === $customerId) {
7676
return $cart;
7777
}
7878

79-
if ($customerId !== $userId) {
79+
if ($cartCustomerId !== $customerId) {
8080
throw new GraphQlAuthorizationException(
8181
__(
8282
'The current user cannot perform operations on cart "%masked_cart_id"',
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
15+
/**
16+
* @inheritdoc
17+
*/
18+
class SelectedPaymentMethod implements ResolverInterface
19+
{
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
24+
{
25+
if (!isset($value['model'])) {
26+
throw new LocalizedException(__('"model" value should be specified'));
27+
}
28+
29+
/** @var \Magento\Quote\Model\Quote $cart */
30+
$cart = $value['model'];
31+
32+
$payment = $cart->getPayment();
33+
if (!$payment) {
34+
return [];
35+
}
36+
37+
return [
38+
'code' => $payment->getMethod(),
39+
'purchase_order_number' => $payment->getPoNumber(),
40+
];
41+
}
42+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Framework\Stdlib\ArrayManager;
16+
use Magento\Quote\Api\Data\PaymentInterface;
17+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
18+
use Magento\Quote\Api\Data\PaymentInterfaceFactory;
19+
use Magento\Quote\Api\PaymentMethodManagementInterface;
20+
21+
/**
22+
* Mutation resolver for setting payment method for shopping cart
23+
*/
24+
class SetPaymentMethodOnCart implements ResolverInterface
25+
{
26+
/**
27+
* @var GetCartForUser
28+
*/
29+
private $getCartForUser;
30+
31+
/**
32+
* @var ArrayManager
33+
*/
34+
private $arrayManager;
35+
36+
/**
37+
* @var PaymentMethodManagementInterface
38+
*/
39+
private $paymentMethodManagement;
40+
41+
/**
42+
* @var PaymentInterfaceFactory
43+
*/
44+
private $paymentFactory;
45+
46+
/**
47+
* @param GetCartForUser $getCartForUser
48+
* @param ArrayManager $arrayManager
49+
* @param PaymentMethodManagementInterface $paymentMethodManagement
50+
* @param PaymentInterfaceFactory $paymentFactory
51+
*/
52+
public function __construct(
53+
GetCartForUser $getCartForUser,
54+
ArrayManager $arrayManager,
55+
PaymentMethodManagementInterface $paymentMethodManagement,
56+
PaymentInterfaceFactory $paymentFactory
57+
) {
58+
$this->getCartForUser = $getCartForUser;
59+
$this->arrayManager = $arrayManager;
60+
$this->paymentMethodManagement = $paymentMethodManagement;
61+
$this->paymentFactory = $paymentFactory;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
68+
{
69+
$maskedCartId = (string)$this->arrayManager->get('input/cart_id', $args);
70+
if (!$maskedCartId) {
71+
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
72+
}
73+
74+
$paymentMethod = $this->arrayManager->get('input/payment_method', $args);
75+
if (!$paymentMethod) {
76+
throw new GraphQlInputException(__('Required parameter "payment_method" is missing'));
77+
}
78+
79+
$paymentMethodCode = (string) $this->arrayManager->get('input/payment_method/code', $args);
80+
if (!$paymentMethodCode) {
81+
throw new GraphQlInputException(__('Required parameter payment "code" is missing'));
82+
}
83+
84+
$poNumber = $this->arrayManager->get('input/payment_method/purchase_order_number', $args);
85+
86+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
87+
$payment = $this->paymentFactory->create([
88+
'data' => [
89+
PaymentInterface::KEY_METHOD => $paymentMethodCode,
90+
PaymentInterface::KEY_PO_NUMBER => $poNumber,
91+
PaymentInterface::KEY_ADDITIONAL_DATA => [],
92+
]
93+
]);
94+
95+
try {
96+
$this->paymentMethodManagement->set($cart->getId(), $payment);
97+
} catch (LocalizedException $e) {
98+
throw new GraphQlInputException(__($e->getMessage()));
99+
}
100+
101+
return [
102+
'cart' => [
103+
'cart_id' => $maskedCartId,
104+
'model' => $cart,
105+
],
106+
];
107+
}
108+
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\QuoteGraphQl\Model\Resolver;
99

10+
use Magento\Framework\Exception\LocalizedException;
1011
use Magento\Framework\GraphQl\Config\Element\Field;
1112
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1213
use Magento\Framework\GraphQl\Query\ResolverInterface;
@@ -67,7 +68,7 @@ public function __construct(
6768
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
6869
{
6970
$shippingAddresses = $this->arrayManager->get('input/shipping_addresses', $args);
70-
$maskedCartId = (string) $this->arrayManager->get('input/cart_id', $args);
71+
$maskedCartId = (string)$this->arrayManager->get('input/cart_id', $args);
7172

7273
if (!$maskedCartId) {
7374
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
@@ -79,7 +80,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7980

8081
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
8182

82-
$this->setShippingAddressesOnCart->execute($context, $cart, $shippingAddresses);
83+
try {
84+
$this->setShippingAddressesOnCart->execute($context, $cart, $shippingAddresses);
85+
} catch (LocalizedException $e) {
86+
throw new GraphQlInputException(__($e->getMessage()));
87+
}
8388

8489
return [
8590
'cart' => [

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Mutation {
1414
setShippingAddressesOnCart(input: SetShippingAddressesOnCartInput): SetShippingAddressesOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingAddressesOnCart")
1515
setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetBillingAddressOnCart")
1616
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart")
17+
setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")
1718
}
1819

1920
input AddSimpleProductsToCartInput {
@@ -102,6 +103,24 @@ input ShippingMethodForAddressInput {
102103
method_code: String!
103104
}
104105

106+
input SetPaymentMethodOnCartInput {
107+
cart_id: String!
108+
payment_method: PaymentMethodInput!
109+
}
110+
111+
input PaymentMethodInput {
112+
code: String! @doc(description:"Payment method code")
113+
purchase_order_number: String @doc(description:"Purchase order number")
114+
additional_data: PaymentMethodAdditionalDataInput
115+
}
116+
117+
input PaymentMethodAdditionalDataInput {
118+
}
119+
120+
type SetPaymentMethodOnCartOutput {
121+
cart: Cart!
122+
}
123+
105124
type SetBillingAddressOnCartOutput {
106125
cart: Cart!
107126
}
@@ -124,7 +143,8 @@ type Cart {
124143
applied_coupon: AppliedCoupon
125144
shipping_addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses")
126145
billing_address: CartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
127-
available_payment_methods : [PaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods")
146+
available_payment_methods : [AvailablePaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods")
147+
selected_payment_method: SelectedPaymentMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SelectedPaymentMethod")
128148
}
129149

130150
type CartAddress {
@@ -139,8 +159,8 @@ type CartAddress {
139159
country: CartAddressCountry
140160
telephone: String
141161
address_type: AdressTypeEnum
142-
selected_shipping_method: SelectedShippingMethod
143162
available_shipping_methods: [AvailableShippingMethod] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAdress\\AvailableShippingMethods")
163+
selected_shipping_method: SelectedShippingMethod
144164
items_weight: Float
145165
customer_notes: String
146166
cart_items: [CartItemQuantity]
@@ -177,11 +197,20 @@ type AvailableShippingMethod {
177197
price_incl_tax: Float!
178198
}
179199

180-
type PaymentMethod {
200+
type AvailablePaymentMethod {
181201
code: String @doc(description: "The payment method code")
182202
title: String @doc(description: "The payment method title.")
183203
}
184204

205+
type SelectedPaymentMethod {
206+
code: String @doc(description: "The payment method code")
207+
purchase_order_number: String @doc(description: "The purchase order number.")
208+
additional_data: SelectedPaymentMethodAdditionalData
209+
}
210+
211+
type SelectedPaymentMethodAdditionalData {
212+
}
213+
185214
enum AdressTypeEnum {
186215
SHIPPING
187216
BILLING

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/CreateEmptyCartTest.php

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)