Skip to content

Commit 3c8ac9b

Browse files
committed
GraphQL-292: [Payment methods] Get list of available payment methods for current cart
1 parent b2a8085 commit 3c8ac9b

File tree

3 files changed

+26
-59
lines changed

3 files changed

+26
-59
lines changed
+25-10
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,64 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\QuoteGraphQl\Model\Cart\PaymentMethod;
8+
namespace Magento\QuoteGraphQl\Model\Resolver;
99

1010
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;
1115
use Magento\Quote\Api\Data\CartInterface;
1216

1317
/**
14-
* Get array of available payment methods.
18+
* Get list of active payment methods resolver.
1519
*/
16-
class AvailablePaymentMethodsDataProvider
20+
class AvailablePaymentMethods implements ResolverInterface
1721
{
1822
/**
1923
* @var PaymentInformationManagementInterface
2024
*/
2125
private $informationManagement;
2226

2327
/**
24-
* AvailablePaymentMethodsDataProvider constructor.
2528
* @param PaymentInformationManagementInterface $informationManagement
2629
*/
2730
public function __construct(PaymentInformationManagementInterface $informationManagement)
2831
{
2932
$this->informationManagement = $informationManagement;
3033
}
3134

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+
3248
/**
3349
* Collect and return information about available payment methods
3450
*
3551
* @param CartInterface $cart
3652
* @return array
3753
*/
38-
public function getPaymentMethods(CartInterface $cart): array
54+
private function getPaymentMethodsData(CartInterface $cart): array
3955
{
4056
$paymentInformation = $this->informationManagement->getPaymentInformation($cart->getId());
4157
$paymentMethods = $paymentInformation->getPaymentMethods();
4258

43-
$paymentMethodsNested = [];
59+
$paymentMethodsData = [];
4460
foreach ($paymentMethods as $paymentMethod) {
45-
$paymentMethodsNested[] = [
61+
$paymentMethodsData[] = [
4662
'title' => $paymentMethod->getTitle(),
47-
'code' => $paymentMethod->getCode()
63+
'code' => $paymentMethod->getCode(),
4864
];
4965
}
50-
51-
return $paymentMethodsNested;
66+
return $paymentMethodsData;
5267
}
5368
}

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

-48
This file was deleted.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +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 : [CheckoutPaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethodsResolver") @doc(description: "Available payment methods")
107+
available_payment_methods : [CheckoutPaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods")
108108
}
109109

110110
type CartAddress {

0 commit comments

Comments
 (0)