Skip to content

Commit 8f8fca1

Browse files
ENGCOM-4352: graphQl-292: payment method list coverage #327
- Merge Pull Request magento/graphql-ce#327 from magento/graphql-ce:graphQl-292-payment-method-list - Merged commits: 1. fe8cc05 2. b2a8085 3. 3c8ac9b 4. fd114ec 5. 99af179 6. 7c42d2a
2 parents 310648c + 7c42d2a commit 8f8fca1

File tree

3 files changed

+226
-0
lines changed

3 files changed

+226
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\Checkout\Api\PaymentInformationManagementInterface;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Quote\Api\Data\CartInterface;
16+
17+
/**
18+
* Get list of active payment methods resolver.
19+
*/
20+
class AvailablePaymentMethods implements ResolverInterface
21+
{
22+
/**
23+
* @var PaymentInformationManagementInterface
24+
*/
25+
private $informationManagement;
26+
27+
/**
28+
* @param PaymentInformationManagementInterface $informationManagement
29+
*/
30+
public function __construct(PaymentInformationManagementInterface $informationManagement)
31+
{
32+
$this->informationManagement = $informationManagement;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
39+
{
40+
if (!isset($value['model'])) {
41+
throw new LocalizedException(__('"model" value should be specified'));
42+
}
43+
44+
$cart = $value['model'];
45+
return $this->getPaymentMethodsData($cart);
46+
}
47+
48+
/**
49+
* Collect and return information about available payment methods
50+
*
51+
* @param CartInterface $cart
52+
* @return array
53+
*/
54+
private function getPaymentMethodsData(CartInterface $cart): array
55+
{
56+
$paymentInformation = $this->informationManagement->getPaymentInformation($cart->getId());
57+
$paymentMethods = $paymentInformation->getPaymentMethods();
58+
59+
$paymentMethodsData = [];
60+
foreach ($paymentMethods as $paymentMethod) {
61+
$paymentMethodsData[] = [
62+
'title' => $paymentMethod->getTitle(),
63+
'code' => $paymentMethod->getCode(),
64+
];
65+
}
66+
return $paymentMethodsData;
67+
}
68+
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type Cart {
104104
applied_coupon: AppliedCoupon
105105
shipping_addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses")
106106
billing_address: CartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
107+
available_payment_methods : [PaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods")
107108
}
108109

109110
type CartAddress {
@@ -155,6 +156,11 @@ type AvailableShippingMethod {
155156
price_incl_tax: Float!
156157
}
157158

159+
type PaymentMethod {
160+
code: String @doc(description: "The payment method code")
161+
title: String @doc(description: "The payment method title.")
162+
}
163+
158164
enum AdressTypeEnum {
159165
SHIPPING
160166
BILLING
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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;
9+
10+
use Magento\Integration\Api\CustomerTokenServiceInterface;
11+
use Magento\Quote\Model\Quote;
12+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
13+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\TestFramework\TestCase\GraphQlAbstract;
16+
17+
/**
18+
* Test for getting cart information
19+
*/
20+
class GetAvailablePaymentMethodsTest extends GraphQlAbstract
21+
{
22+
/**
23+
* @var CustomerTokenServiceInterface
24+
*/
25+
private $customerTokenService;
26+
27+
/**
28+
* @var QuoteResource
29+
*/
30+
private $quoteResource;
31+
32+
/**
33+
* @var Quote
34+
*/
35+
private $quote;
36+
37+
/**
38+
* @var QuoteIdToMaskedQuoteIdInterface
39+
*/
40+
private $quoteIdToMaskedId;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp()
46+
{
47+
$objectManager = Bootstrap::getObjectManager();
48+
$this->quoteResource = $objectManager->create(QuoteResource::class);
49+
$this->quote = $objectManager->create(Quote::class);
50+
$this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class);
51+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
52+
}
53+
54+
/**
55+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
56+
*/
57+
public function testGetCartWithPaymentMethodsForRegisteredCustomer()
58+
{
59+
$reservedOrderId = 'test_order_item_with_items';
60+
$this->quoteResource->load(
61+
$this->quote,
62+
$reservedOrderId,
63+
'reserved_order_id'
64+
);
65+
66+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
67+
$query = $this->prepareGetCartQuery($maskedQuoteId);
68+
69+
$response = $this->sendRequestWithToken($query);
70+
71+
self::assertArrayHasKey('cart', $response);
72+
self::assertNotEmpty($response['cart']['items']);
73+
self::assertNotEmpty($response['cart']['available_payment_methods']);
74+
self::assertCount(1, $response['cart']['available_payment_methods']);
75+
self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']);
76+
self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']);
77+
}
78+
79+
/**
80+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
81+
*/
82+
public function testGetCartWithPaymentMethodsForGuest()
83+
{
84+
$reservedOrderId = 'test_order_1';
85+
$this->quoteResource->load(
86+
$this->quote,
87+
$reservedOrderId,
88+
'reserved_order_id'
89+
);
90+
91+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
92+
$query = $this->prepareGetCartQuery($maskedQuoteId);
93+
94+
$response = $this->graphQlQuery($query);
95+
96+
self::assertArrayHasKey('cart', $response);
97+
98+
self::assertNotEmpty($response['cart']['available_payment_methods']);
99+
self::assertCount(2, $response['cart']['available_payment_methods']);
100+
self::assertEquals('checkmo', $response['cart']['available_payment_methods'][0]['code']);
101+
self::assertEquals('Check / Money order', $response['cart']['available_payment_methods'][0]['title']);
102+
self::assertEquals('free', $response['cart']['available_payment_methods'][1]['code']);
103+
self::assertEquals(
104+
'No Payment Information Required',
105+
$response['cart']['available_payment_methods'][1]['title']
106+
);
107+
}
108+
109+
/**
110+
* Generates query for setting the specified shipping method on cart
111+
*
112+
* @param string $maskedQuoteId
113+
* @return string
114+
*/
115+
private function prepareGetCartQuery(
116+
string $maskedQuoteId
117+
) : string {
118+
return <<<QUERY
119+
{
120+
cart(cart_id: "$maskedQuoteId") {
121+
applied_coupon {
122+
code
123+
}
124+
items {
125+
id
126+
}
127+
available_payment_methods {
128+
code,
129+
title
130+
}
131+
}
132+
}
133+
134+
QUERY;
135+
}
136+
137+
/**
138+
* Sends a GraphQL request with using a bearer token
139+
*
140+
* @param string $query
141+
* @return array
142+
* @throws \Magento\Framework\Exception\AuthenticationException
143+
*/
144+
private function sendRequestWithToken(string $query): array
145+
{
146+
147+
$customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
148+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
149+
150+
return $this->graphQlQuery($query, [], '', $headerMap);
151+
}
152+
}

0 commit comments

Comments
 (0)