Skip to content

Commit fe8cc05

Browse files
author
Vitaliy Boyko
committed
graphQl-292: payment method list
1 parent 92118f5 commit fe8cc05

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\Cart\PaymentMethod;
9+
10+
use Magento\Checkout\Api\PaymentInformationManagementInterface;
11+
use Magento\Quote\Api\Data\CartInterface;
12+
13+
/**
14+
* Get array of available payment methods.
15+
*/
16+
class AvailablePaymentMethodsDataProvider
17+
{
18+
/**
19+
* @var PaymentInformationManagementInterface
20+
*/
21+
private $informationManagement;
22+
23+
/**
24+
* AvailablePaymentMethodsDataProvider constructor.
25+
* @param PaymentInformationManagementInterface $informationManagement
26+
*/
27+
public function __construct(PaymentInformationManagementInterface $informationManagement)
28+
{
29+
$this->informationManagement = $informationManagement;
30+
}
31+
32+
/**
33+
* Collect and return information about available payment methods
34+
*
35+
* @param CartInterface $cart
36+
* @return array
37+
*/
38+
public function getPaymentMethods(CartInterface $cart): array
39+
{
40+
$paymentInformation = $this->informationManagement->getPaymentInformation($cart->getId());
41+
$paymentMethods = $paymentInformation->getPaymentMethods();
42+
43+
$paymentMethodsNested = [];
44+
foreach ($paymentMethods as $paymentMethod) {
45+
$paymentMethodsNested[] = [
46+
'title' => $paymentMethod->getTitle(),
47+
'code' => $paymentMethod->getCode()
48+
];
49+
}
50+
51+
return $paymentMethodsNested;
52+
}
53+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
use Magento\QuoteGraphQl\Model\Cart\PaymentMethod\AvailablePaymentMethodsDataProvider;
15+
16+
/**
17+
* Get list of active payment methods resolver.
18+
*/
19+
class AvailablePaymentMethodsResolver implements ResolverInterface
20+
{
21+
/**
22+
* @var AvailablePaymentMethodsDataProvider
23+
*/
24+
private $addressDataProvider;
25+
26+
/**
27+
* @param AvailablePaymentMethodsDataProvider $addressDataProvider
28+
*/
29+
public function __construct(
30+
AvailablePaymentMethodsDataProvider $addressDataProvider
31+
) {
32+
$this->addressDataProvider = $addressDataProvider;
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+
46+
return $this->addressDataProvider->getPaymentMethods($cart);
47+
}
48+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ type Cart {
9898
items: [CartItemInterface]
9999
applied_coupon: AppliedCoupon
100100
addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartAddresses")
101+
available_payment_methods : [CheckoutPaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethodsResolver") @doc(description: "Available payment methods")
101102
}
102103

103104
type CartAddress {
@@ -141,6 +142,11 @@ type CheckoutShippingMethod {
141142
# TODO: Add more complex structure for shipping rates
142143
}
143144

145+
type CheckoutPaymentMethod @doc(description: "The type contains list of active payment methods") {
146+
code : String @doc(description: "The payment method code")
147+
title : String @doc(description: "The payment method title.")
148+
}
149+
144150
enum AdressTypeEnum {
145151
SHIPPING
146152
BILLING

0 commit comments

Comments
 (0)