From fe8cc058dbc617ab9c5b3e181a599d923e0f7e06 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Mon, 11 Feb 2019 21:05:32 +0200 Subject: [PATCH] graphQl-292: payment method list --- .../AvailablePaymentMethodsDataProvider.php | 53 +++++++++++++++++++ .../AvailablePaymentMethodsResolver.php | 48 +++++++++++++++++ .../Magento/QuoteGraphQl/etc/schema.graphqls | 6 +++ 3 files changed, 107 insertions(+) create mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/PaymentMethod/AvailablePaymentMethodsDataProvider.php create mode 100644 app/code/Magento/QuoteGraphQl/Model/Resolver/AvailablePaymentMethodsResolver.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/PaymentMethod/AvailablePaymentMethodsDataProvider.php b/app/code/Magento/QuoteGraphQl/Model/Cart/PaymentMethod/AvailablePaymentMethodsDataProvider.php new file mode 100644 index 0000000000000..daa51d8729995 --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/PaymentMethod/AvailablePaymentMethodsDataProvider.php @@ -0,0 +1,53 @@ +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; + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/AvailablePaymentMethodsResolver.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/AvailablePaymentMethodsResolver.php new file mode 100644 index 0000000000000..17747ec11b25a --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/AvailablePaymentMethodsResolver.php @@ -0,0 +1,48 @@ +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); + } +} diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index 4c1101a5f90a8..072810de0bb47 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -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 { @@ -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