Skip to content

Commit 0348d7d

Browse files
author
Vitaliy Boyko
committed
graphQl-533: added additional data to payment methods, added tests
1 parent 8e8dc28 commit 0348d7d

File tree

6 files changed

+320
-76
lines changed

6 files changed

+320
-76
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7070
$paymentMethodCode = $args['input']['payment_method']['code'];
7171

7272
$poNumber = $args['input']['payment_method']['purchase_order_number'] ?? null;
73+
$additionalData = $args['input']['payment_method']['additional_data'] ?? [];
7374

7475
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
7576
$payment = $this->paymentFactory->create([
7677
'data' => [
7778
PaymentInterface::KEY_METHOD => $paymentMethodCode,
7879
PaymentInterface::KEY_PO_NUMBER => $poNumber,
79-
PaymentInterface::KEY_ADDITIONAL_DATA => [],
80+
PaymentInterface::KEY_ADDITIONAL_DATA => $additionalData,
8081
]
8182
]);
8283

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,13 @@ input SetPaymentMethodOnCartInput {
126126

127127
input PaymentMethodInput {
128128
code: String! @doc(description:"Payment method code")
129+
additional_data: PaymentMethodAdditionalDataInput @doc(description:"Additional payment data")
129130
purchase_order_number: String @doc(description:"Purchase order number")
130131
}
131132

133+
input PaymentMethodAdditionalDataInput {
134+
}
135+
132136
type SetPaymentMethodOnCartOutput {
133137
cart: Cart!
134138
}
@@ -222,9 +226,13 @@ type AvailablePaymentMethod {
222226

223227
type SelectedPaymentMethod {
224228
code: String @doc(description: "The payment method code")
229+
additional_data: SelectedPaymentMethodAdditionalData @doc(description: "Additional payment data")
225230
purchase_order_number: String @doc(description: "The purchase order number.")
226231
}
227232

233+
type SelectedPaymentMethodAdditionalData {
234+
}
235+
228236
enum AdressTypeEnum {
229237
SHIPPING
230238
BILLING
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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\GraphQl\Quote\Customer;
9+
10+
use Exception;
11+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
12+
use Magento\Integration\Api\CustomerTokenServiceInterface;
13+
use Magento\OfflinePayments\Model\Purchaseorder;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\TestFramework\TestCase\GraphQlAbstract;
16+
17+
/**
18+
* Test for setting offline payment methods on cart by customer
19+
*/
20+
class SetOfflinePaymentMethodOnCartTest extends GraphQlAbstract
21+
{
22+
/**
23+
* @var GetMaskedQuoteIdByReservedOrderId
24+
*/
25+
private $getMaskedQuoteIdByReservedOrderId;
26+
27+
/**
28+
* @var CustomerTokenServiceInterface
29+
*/
30+
private $customerTokenService;
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
protected function setUp()
36+
{
37+
$objectManager = Bootstrap::getObjectManager();
38+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
39+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
40+
}
41+
42+
/**
43+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
44+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
45+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
46+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
47+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
48+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
49+
*/
50+
public function testSetPurchaseOrderPaymentMethodOnCartWithSimpleProduct()
51+
{
52+
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
53+
$purchaseOrderNumber = '123456';
54+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
55+
56+
$query = <<<QUERY
57+
mutation {
58+
setPaymentMethodOnCart(input: {
59+
cart_id: "$maskedQuoteId"
60+
payment_method: {
61+
code: "$methodCode"
62+
purchase_order_number: "$purchaseOrderNumber"
63+
}
64+
}) {
65+
cart {
66+
selected_payment_method {
67+
code
68+
purchase_order_number
69+
}
70+
}
71+
}
72+
}
73+
QUERY;
74+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
75+
76+
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
77+
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
78+
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
79+
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
80+
self::assertEquals($purchaseOrderNumber, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['purchase_order_number']);
81+
}
82+
83+
/**
84+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
85+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
86+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
87+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
88+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
89+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
90+
*
91+
* @expectedException Exception
92+
* @expectedExceptionMessage Purchase order number is a required field.
93+
*/
94+
public function testSetPurchaseOrderPaymentMethodOnCartWithoutPurchaseOrderNumber()
95+
{
96+
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
97+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
98+
99+
$query = <<<QUERY
100+
mutation {
101+
setPaymentMethodOnCart(input: {
102+
cart_id: "$maskedQuoteId"
103+
payment_method: {
104+
code: "$methodCode"
105+
}
106+
}) {
107+
cart {
108+
selected_payment_method {
109+
code
110+
}
111+
}
112+
}
113+
}
114+
QUERY;
115+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
116+
}
117+
118+
/**
119+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
120+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
121+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
122+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
123+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
124+
*
125+
* @expectedException Exception
126+
* @expectedExceptionMessage The requested Payment Method is not available.
127+
*/
128+
public function testSetDisabledPurchaseOrderPaymentMethodOnCart()
129+
{
130+
$methodCode = Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE;
131+
$purchaseOrderNumber = '123456';
132+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
133+
134+
$query = <<<QUERY
135+
mutation {
136+
setPaymentMethodOnCart(input: {
137+
cart_id: "$maskedQuoteId"
138+
payment_method: {
139+
code: "$methodCode"
140+
purchase_order_number: "$purchaseOrderNumber"
141+
}
142+
}) {
143+
cart {
144+
selected_payment_method {
145+
code
146+
purchase_order_number
147+
}
148+
}
149+
}
150+
}
151+
QUERY;
152+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
153+
}
154+
155+
/**
156+
* @param string $username
157+
* @param string $password
158+
* @return array
159+
*/
160+
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
161+
{
162+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
163+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
164+
return $headerMap;
165+
}
166+
}

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

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -60,44 +60,6 @@ public function testSetPaymentOnCartWithSimpleProduct()
6060
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
6161
}
6262

63-
/**
64-
* @magentoApiDataFixture Magento/Customer/_files/customer.php
65-
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
66-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
67-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
68-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
69-
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
70-
*/
71-
public function testSetPurchaseOrderPaymentMethodOnCartWithSimpleProduct()
72-
{
73-
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
74-
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
75-
76-
$query = <<<QUERY
77-
mutation {
78-
setPaymentMethodOnCart(input: {
79-
cart_id: "$maskedQuoteId"
80-
payment_method: {
81-
code: "$methodCode",
82-
purchase_order_number: "123456"
83-
}
84-
}) {
85-
cart {
86-
selected_payment_method {
87-
code
88-
}
89-
}
90-
}
91-
}
92-
QUERY;
93-
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
94-
95-
self::assertArrayHasKey('setPaymentMethodOnCart', $response);
96-
self::assertArrayHasKey('cart', $response['setPaymentMethodOnCart']);
97-
self::assertArrayHasKey('selected_payment_method', $response['setPaymentMethodOnCart']['cart']);
98-
self::assertEquals($methodCode, $response['setPaymentMethodOnCart']['cart']['selected_payment_method']['code']);
99-
}
100-
10163
/**
10264
* @magentoApiDataFixture Magento/Customer/_files/customer.php
10365
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php

0 commit comments

Comments
 (0)