Skip to content

Commit 793fffd

Browse files
authored
ENGCOM-5813: Add input validation for authorizenet_acceptjs payment method #915
2 parents fd12564 + 2f2f492 commit 793fffd

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderInterface;
1111
use Magento\Framework\Stdlib\ArrayManager;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1213

1314
/**
1415
* SetPaymentMethod additional data provider model for Authorizenet payment method
@@ -36,10 +37,32 @@ public function __construct(
3637
*
3738
* @param array $data
3839
* @return array
40+
* @throws GraphQlInputException
3941
*/
4042
public function getData(array $data): array
4143
{
42-
$additionalData = $this->arrayManager->get(static::PATH_ADDITIONAL_DATA, $data) ?? [];
44+
if (!isset($data[self::PATH_ADDITIONAL_DATA])) {
45+
throw new GraphQlInputException(
46+
__('Required parameter "authorizenet_acceptjs" for "payment_method" is missing.')
47+
);
48+
}
49+
if (!isset($data[self::PATH_ADDITIONAL_DATA]['opaque_data_descriptor'])) {
50+
throw new GraphQlInputException(
51+
__('Required parameter "opaque_data_descriptor" for "authorizenet_acceptjs" is missing.')
52+
);
53+
}
54+
if (!isset($data[self::PATH_ADDITIONAL_DATA]['opaque_data_value'])) {
55+
throw new GraphQlInputException(
56+
__('Required parameter "opaque_data_value" for "authorizenet_acceptjs" is missing.')
57+
);
58+
}
59+
if (!isset($data[self::PATH_ADDITIONAL_DATA]['cc_last_4'])) {
60+
throw new GraphQlInputException(
61+
__('Required parameter "cc_last_4" for "authorizenet_acceptjs" is missing.')
62+
);
63+
}
64+
65+
$additionalData = $this->arrayManager->get(static::PATH_ADDITIONAL_DATA, $data);
4366
foreach ($additionalData as $key => $value) {
4467
$additionalData[$this->convertSnakeCaseToCamelCase($key)] = $value;
4568
unset($additionalData[$key]);

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,113 @@ public function dataProviderTestPlaceOrder(): array
109109
];
110110
}
111111

112+
/**
113+
* @magentoConfigFixture default_store carriers/flatrate/active 1
114+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/active 1
115+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/environment sandbox
116+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/login def_login
117+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/trans_key def_trans_key
118+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/public_client_key def_public_client_key
119+
* @magentoConfigFixture default_store payment/authorizenet_acceptjs/trans_signature_key def_trans_signature_key
120+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
121+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
122+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
123+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
124+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
125+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
126+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
127+
* @dataProvider dataProviderSetPaymentInvalidInput
128+
* @param \Closure $getMutationClosure
129+
* @param string $expectedMessage
130+
* @expectedException \Exception
131+
*/
132+
public function testSetPaymentInvalidInput(\Closure $getMutationClosure, string $expectedMessage)
133+
{
134+
$reservedOrderId = 'test_quote';
135+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
136+
137+
$setPaymentMutation = $getMutationClosure($maskedQuoteId);
138+
139+
$this->expectExceptionMessage($expectedMessage);
140+
$this->graphQlMutation($setPaymentMutation, [], '', $this->getHeaderMap());
141+
}
142+
143+
/**
144+
* Data provider for testSetPaymentInvalidInput
145+
*
146+
* @return array
147+
*/
148+
public function dataProviderSetPaymentInvalidInput(): array
149+
{
150+
return [
151+
[
152+
function (string $maskedQuoteId) {
153+
return $this->getInvalidSetPaymentMutation($maskedQuoteId);
154+
},
155+
'Required parameter "authorizenet_acceptjs" for "payment_method" is missing.',
156+
],
157+
[
158+
function (string $maskedQuoteId) {
159+
return $this->getInvalidAcceptJsInput($maskedQuoteId);
160+
},
161+
'for "authorizenet_acceptjs" is missing.'
162+
],
163+
];
164+
}
165+
166+
/**
167+
* Get setPaymentMethodOnCart missing additional data property
168+
*
169+
* @param string $maskedQuoteId
170+
* @return string
171+
*/
172+
private function getInvalidSetPaymentMutation(string $maskedQuoteId): string
173+
{
174+
return <<<QUERY
175+
mutation {
176+
setPaymentMethodOnCart(input:{
177+
cart_id:"{$maskedQuoteId}"
178+
payment_method:{
179+
code:"authorizenet_acceptjs"
180+
}
181+
}) {
182+
cart {
183+
selected_payment_method {
184+
code
185+
}
186+
}
187+
}
188+
}
189+
QUERY;
190+
}
191+
192+
/**
193+
* Get setPaymentMethodOnCart missing require additional data properties
194+
*
195+
* @param string $maskedQuoteId
196+
* @return string
197+
*/
198+
private function getInvalidAcceptJsInput(string $maskedQuoteId): string
199+
{
200+
return <<<QUERY
201+
mutation {
202+
setPaymentMethodOnCart(input:{
203+
cart_id:"{$maskedQuoteId}"
204+
payment_method:{
205+
code:"authorizenet_acceptjs"
206+
authorizenet_acceptjs: {}
207+
}
208+
}) {
209+
cart {
210+
selected_payment_method {
211+
code
212+
}
213+
}
214+
}
215+
}
216+
QUERY;
217+
}
218+
112219
private function assertPlaceOrderResponse(array $response, string $reservedOrderId): void
113220
{
114221
self::assertArrayHasKey('placeOrder', $response);

0 commit comments

Comments
 (0)