Skip to content

Commit

Permalink
GraphQL-292: [Payment methods] Get list of available payment methods …
Browse files Browse the repository at this point in the history
…for current cart
  • Loading branch information
naydav committed Feb 19, 2019
1 parent b2a8085 commit 3c8ac9b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,64 @@
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart\PaymentMethod;
namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Checkout\Api\PaymentInformationManagementInterface;
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\Quote\Api\Data\CartInterface;

/**
* Get array of available payment methods.
* Get list of active payment methods resolver.
*/
class AvailablePaymentMethodsDataProvider
class AvailablePaymentMethods implements ResolverInterface
{
/**
* @var PaymentInformationManagementInterface
*/
private $informationManagement;

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

/**
* @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->getPaymentMethodsData($cart);
}

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

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

return $paymentMethodsNested;
return $paymentMethodsData;
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type Cart {
applied_coupon: AppliedCoupon
shipping_addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses")
billing_address: CartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
available_payment_methods : [CheckoutPaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethodsResolver") @doc(description: "Available payment methods")
available_payment_methods : [CheckoutPaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods")
}

type CartAddress {
Expand Down

0 comments on commit 3c8ac9b

Please sign in to comment.