Skip to content

Commit 4939d9c

Browse files
committed
Validate additional input is present for associated payment method
1 parent c1eee4f commit 4939d9c

File tree

5 files changed

+143
-36
lines changed

5 files changed

+143
-36
lines changed

app/code/Magento/BraintreeGraphQl/Model/BraintreeDataProvider.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
namespace Magento\BraintreeGraphQl\Model;
99

10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1011
use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderInterface;
11-
use Magento\Framework\Stdlib\ArrayManager;
1212

1313
/**
1414
* Format Braintree input into value expected when setting payment method
@@ -17,28 +17,21 @@ class BraintreeDataProvider implements AdditionalDataProviderInterface
1717
{
1818
private const PATH_ADDITIONAL_DATA = 'braintree';
1919

20-
/**
21-
* @var ArrayManager
22-
*/
23-
private $arrayManager;
24-
25-
/**
26-
* @param ArrayManager $arrayManager
27-
*/
28-
public function __construct(
29-
ArrayManager $arrayManager
30-
) {
31-
$this->arrayManager = $arrayManager;
32-
}
33-
3420
/**
3521
* Format Braintree input into value expected when setting payment method
3622
*
3723
* @param array $args
3824
* @return array
25+
* @throws GraphQlInputException
3926
*/
4027
public function getData(array $args): array
4128
{
42-
return $this->arrayManager->get(static::PATH_ADDITIONAL_DATA, $args) ?? [];
29+
if (!isset($args[static::PATH_ADDITIONAL_DATA])) {
30+
throw new GraphQlInputException(
31+
__('Required parameter "braintree" for "payment_method" is missing.')
32+
);
33+
}
34+
35+
return $args[static::PATH_ADDITIONAL_DATA];
4336
}
4437
}

app/code/Magento/BraintreeGraphQl/Model/BraintreeVaultDataProvider.php

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

88
namespace Magento\BraintreeGraphQl\Model;
99

10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1011
use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderInterface;
11-
use Magento\Framework\Stdlib\ArrayManager;
1212

1313
/**
1414
* Format Braintree input into value expected when setting payment method
@@ -17,20 +17,6 @@ class BraintreeVaultDataProvider implements AdditionalDataProviderInterface
1717
{
1818
private const PATH_ADDITIONAL_DATA = 'braintree_cc_vault';
1919

20-
/**
21-
* @var ArrayManager
22-
*/
23-
private $arrayManager;
24-
25-
/**
26-
* @param ArrayManager $arrayManager
27-
*/
28-
public function __construct(
29-
ArrayManager $arrayManager
30-
) {
31-
$this->arrayManager = $arrayManager;
32-
}
33-
3420
/**
3521
* Format Braintree input into value expected when setting payment method
3622
*
@@ -39,6 +25,12 @@ public function __construct(
3925
*/
4026
public function getData(array $args): array
4127
{
42-
return $this->arrayManager->get(static::PATH_ADDITIONAL_DATA, $args) ?? [];
28+
if (!isset($args[static::PATH_ADDITIONAL_DATA])) {
29+
throw new GraphQlInputException(
30+
__('Required parameter "braintree_cc_vault" for "payment_method" is missing.')
31+
);
32+
}
33+
34+
return $args[static::PATH_ADDITIONAL_DATA];
4335
}
4436
}

app/code/Magento/BraintreeGraphQl/Plugin/SetVaultPaymentNonce.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public function beforeExecute(
5353
\Magento\Quote\Model\Quote $quote,
5454
array $paymentData
5555
): array {
56-
if ($paymentData['code'] !== ConfigProvider::CC_VAULT_CODE) {
56+
if ($paymentData['code'] !== ConfigProvider::CC_VAULT_CODE
57+
|| !isset($paymentData[ConfigProvider::CC_VAULT_CODE])
58+
) {
5759
return [$quote, $paymentData];
5860
}
5961

dev/tests/api-functional/testsuite/Magento/GraphQl/Braintree/Customer/SetPaymentMethodTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,41 @@ public function testPlaceOrderWithVault()
183183
$this->assertPlaceOrderResponse($placeOrderResponse, $reservedOrderId);
184184
}
185185

186+
/**
187+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
188+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
189+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
190+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
191+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
192+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
193+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
194+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
195+
* @magentoApiDataFixture Magento/GraphQl/Braintree/_files/enable_braintree_payment.php
196+
* @dataProvider dataProviderTestSetPaymentMethodInvalidInput
197+
* @expectedException \Exception
198+
* @param string $methodCode
199+
*/
200+
public function testSetPaymentMethodInvalidInput(string $methodCode)
201+
{
202+
$reservedOrderId = 'test_quote';
203+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
204+
205+
$setPaymentQuery = $this->getSetPaymentBraintreeQueryInvalidInput(
206+
$maskedQuoteId,
207+
$methodCode
208+
);
209+
$this->expectExceptionMessage("Required parameter \"$methodCode\" for \"payment_method\" is missing.");
210+
$this->graphQlMutation($setPaymentQuery, [], '', $this->getHeaderMap());
211+
}
212+
213+
public function dataProviderTestSetPaymentMethodInvalidInput(): array
214+
{
215+
return [
216+
['braintree'],
217+
['braintree_cc_vault'],
218+
];
219+
}
220+
186221
private function assertPlaceOrderResponse(array $response, string $reservedOrderId): void
187222
{
188223
self::assertArrayHasKey('placeOrder', $response);
@@ -260,6 +295,31 @@ private function getSetPaymentBraintreeVaultQuery(
260295
QUERY;
261296
}
262297

298+
/**
299+
* @param string $maskedQuoteId
300+
* @param string $methodCode
301+
* @return string
302+
*/
303+
private function getSetPaymentBraintreeQueryInvalidInput(string $maskedQuoteId, string $methodCode): string
304+
{
305+
return <<<QUERY
306+
mutation {
307+
setPaymentMethodOnCart(input:{
308+
cart_id:"{$maskedQuoteId}"
309+
payment_method:{
310+
code:"{$methodCode}"
311+
}
312+
}) {
313+
cart {
314+
selected_payment_method {
315+
code
316+
}
317+
}
318+
}
319+
}
320+
QUERY;
321+
}
322+
263323
/**
264324
* @param string $maskedQuoteId
265325
* @return string

dev/tests/api-functional/testsuite/Magento/GraphQl/Braintree/Guest/SetPaymentMethodTest.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ protected function setUp()
6868
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
6969
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
7070
* @magentoApiDataFixture Magento/GraphQl/Braintree/_files/enable_braintree_payment.php
71+
* @dataProvider dataProviderTestPlaceOrder
7172
*/
72-
public function testPlaceOrder()
73+
public function testPlaceOrder(string $nonce)
7374
{
7475
$reservedOrderId = 'test_quote';
7576
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
7677

77-
$setPaymentQuery = $this->getSetPaymentBraintreeQuery($maskedQuoteId);
78+
$setPaymentQuery = $this->getSetPaymentBraintreeQuery($maskedQuoteId, $nonce);
7879
$setPaymentResponse = $this->graphQlMutation($setPaymentQuery);
7980

8081
$this->assertSetPaymentMethodResponse($setPaymentResponse, 'braintree');
@@ -85,6 +86,41 @@ public function testPlaceOrder()
8586
$this->assertPlaceOrderResponse($placeOrderResponse, $reservedOrderId);
8687
}
8788

89+
/**
90+
* Data provider for testPlaceOrder
91+
*
92+
* @return array
93+
*/
94+
public function dataProviderTestPlaceOrder(): array
95+
{
96+
return [
97+
['fake-valid-nonce'],
98+
['fake-apple-pay-visa-nonce'],
99+
];
100+
}
101+
102+
/**
103+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
104+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
105+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
106+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php
107+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
108+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
109+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
110+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
111+
* @magentoApiDataFixture Magento/GraphQl/Braintree/_files/enable_braintree_payment.php
112+
* @expectedException \Exception
113+
*/
114+
public function testSetPaymentMethodInvalidInput()
115+
{
116+
$reservedOrderId = 'test_quote';
117+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
118+
119+
$setPaymentQuery = $this->getSetPaymentBraintreeQueryInvalidInput($maskedQuoteId);
120+
$this->expectExceptionMessage("Required parameter \"braintree\" for \"payment_method\" is missing.");
121+
$this->graphQlMutation($setPaymentQuery);
122+
}
123+
88124
private function assertPlaceOrderResponse(array $response, string $reservedOrderId): void
89125
{
90126
self::assertArrayHasKey('placeOrder', $response);
@@ -106,7 +142,7 @@ private function assertSetPaymentMethodResponse(array $response, string $methodC
106142
* @param string $maskedQuoteId
107143
* @return string
108144
*/
109-
private function getSetPaymentBraintreeQuery(string $maskedQuoteId): string
145+
private function getSetPaymentBraintreeQuery(string $maskedQuoteId, string $nonce): string
110146
{
111147
return <<<QUERY
112148
mutation {
@@ -130,6 +166,30 @@ private function getSetPaymentBraintreeQuery(string $maskedQuoteId): string
130166
QUERY;
131167
}
132168

169+
/**
170+
* @param string $maskedQuoteId
171+
* @return string
172+
*/
173+
private function getSetPaymentBraintreeQueryInvalidInput(string $maskedQuoteId): string
174+
{
175+
return <<<QUERY
176+
mutation {
177+
setPaymentMethodOnCart(input:{
178+
cart_id:"{$maskedQuoteId}"
179+
payment_method:{
180+
code:"braintree"
181+
}
182+
}) {
183+
cart {
184+
selected_payment_method {
185+
code
186+
}
187+
}
188+
}
189+
}
190+
QUERY;
191+
}
192+
133193
/**
134194
* @param string $maskedQuoteId
135195
* @return string

0 commit comments

Comments
 (0)