Skip to content

Commit

Permalink
graphQl-292: payment method list
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy Boyko committed Feb 11, 2019
1 parent 92118f5 commit fe8cc05
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart\PaymentMethod;

use Magento\Checkout\Api\PaymentInformationManagementInterface;
use Magento\Quote\Api\Data\CartInterface;

/**
* Get array of available payment methods.
*/
class AvailablePaymentMethodsDataProvider
{
/**
* @var PaymentInformationManagementInterface
*/
private $informationManagement;

/**
* AvailablePaymentMethodsDataProvider constructor.
* @param PaymentInformationManagementInterface $informationManagement
*/
public function __construct(PaymentInformationManagementInterface $informationManagement)
{
$this->informationManagement = $informationManagement;
}

/**
* Collect and return information about available payment methods
*
* @param CartInterface $cart
* @return array
*/
public function getPaymentMethods(CartInterface $cart): array
{
$paymentInformation = $this->informationManagement->getPaymentInformation($cart->getId());
$paymentMethods = $paymentInformation->getPaymentMethods();

$paymentMethodsNested = [];
foreach ($paymentMethods as $paymentMethod) {
$paymentMethodsNested[] = [
'title' => $paymentMethod->getTitle(),
'code' => $paymentMethod->getCode()
];
}

return $paymentMethodsNested;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\QuoteGraphQl\Model\Cart\PaymentMethod\AvailablePaymentMethodsDataProvider;

/**
* Get list of active payment methods resolver.
*/
class AvailablePaymentMethodsResolver implements ResolverInterface
{
/**
* @var AvailablePaymentMethodsDataProvider
*/
private $addressDataProvider;

/**
* @param AvailablePaymentMethodsDataProvider $addressDataProvider
*/
public function __construct(
AvailablePaymentMethodsDataProvider $addressDataProvider
) {
$this->addressDataProvider = $addressDataProvider;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}

$cart = $value['model'];

return $this->addressDataProvider->getPaymentMethods($cart);
}
}
6 changes: 6 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type Cart {
items: [CartItemInterface]
applied_coupon: AppliedCoupon
addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartAddresses")
available_payment_methods : [CheckoutPaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethodsResolver") @doc(description: "Available payment methods")
}

type CartAddress {
Expand Down Expand Up @@ -141,6 +142,11 @@ type CheckoutShippingMethod {
# TODO: Add more complex structure for shipping rates
}

type CheckoutPaymentMethod @doc(description: "The type contains list of active payment methods") {
code : String @doc(description: "The payment method code")
title : String @doc(description: "The payment method title.")
}

enum AdressTypeEnum {
SHIPPING
BILLING
Expand Down

0 comments on commit fe8cc05

Please sign in to comment.