Skip to content

Commit

Permalink
Merge pull request #3811 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests - GraphQL
  • Loading branch information
naydav authored Feb 27, 2019
2 parents e5b0392 + 8d19d7c commit 908ed15
Show file tree
Hide file tree
Showing 18 changed files with 1,448 additions and 388 deletions.
10 changes: 5 additions & 5 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public function __construct(
* Get cart for user
*
* @param string $cartHash
* @param int|null $userId
* @param int|null $customerId
* @return Quote
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
*/
public function execute(string $cartHash, ?int $userId): Quote
public function execute(string $cartHash, ?int $customerId): Quote
{
try {
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
Expand All @@ -69,14 +69,14 @@ public function execute(string $cartHash, ?int $userId): Quote
);
}

$customerId = (int)$cart->getCustomerId();
$cartCustomerId = (int)$cart->getCustomerId();

/* Guest cart, allow operations */
if (!$customerId) {
if (!$cartCustomerId && null === $customerId) {
return $cart;
}

if ($customerId !== $userId) {
if ($cartCustomerId !== $customerId) {
throw new GraphQlAuthorizationException(
__(
'The current user cannot perform operations on cart "%masked_cart_id"',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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;

/**
* @inheritdoc
*/
class SelectedPaymentMethod implements ResolverInterface
{
/**
* @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'));
}

/** @var \Magento\Quote\Model\Quote $cart */
$cart = $value['model'];

$payment = $cart->getPayment();
if (!$payment) {
return [];
}

return [
'code' => $payment->getMethod(),
'purchase_order_number' => $payment->getPoNumber(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Quote\Api\Data\PaymentInterface;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Quote\Api\Data\PaymentInterfaceFactory;
use Magento\Quote\Api\PaymentMethodManagementInterface;

/**
* Mutation resolver for setting payment method for shopping cart
*/
class SetPaymentMethodOnCart implements ResolverInterface
{
/**
* @var GetCartForUser
*/
private $getCartForUser;

/**
* @var ArrayManager
*/
private $arrayManager;

/**
* @var PaymentMethodManagementInterface
*/
private $paymentMethodManagement;

/**
* @var PaymentInterfaceFactory
*/
private $paymentFactory;

/**
* @param GetCartForUser $getCartForUser
* @param ArrayManager $arrayManager
* @param PaymentMethodManagementInterface $paymentMethodManagement
* @param PaymentInterfaceFactory $paymentFactory
*/
public function __construct(
GetCartForUser $getCartForUser,
ArrayManager $arrayManager,
PaymentMethodManagementInterface $paymentMethodManagement,
PaymentInterfaceFactory $paymentFactory
) {
$this->getCartForUser = $getCartForUser;
$this->arrayManager = $arrayManager;
$this->paymentMethodManagement = $paymentMethodManagement;
$this->paymentFactory = $paymentFactory;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$maskedCartId = (string)$this->arrayManager->get('input/cart_id', $args);
if (!$maskedCartId) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}

$paymentMethod = $this->arrayManager->get('input/payment_method', $args);
if (!$paymentMethod) {
throw new GraphQlInputException(__('Required parameter "payment_method" is missing'));
}

$paymentMethodCode = (string) $this->arrayManager->get('input/payment_method/code', $args);
if (!$paymentMethodCode) {
throw new GraphQlInputException(__('Required parameter payment "code" is missing'));
}

$poNumber = $this->arrayManager->get('input/payment_method/purchase_order_number', $args);

$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
$payment = $this->paymentFactory->create([
'data' => [
PaymentInterface::KEY_METHOD => $paymentMethodCode,
PaymentInterface::KEY_PO_NUMBER => $poNumber,
PaymentInterface::KEY_ADDITIONAL_DATA => [],
]
]);

try {
$this->paymentMethodManagement->set($cart->getId(), $payment);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
}

return [
'cart' => [
'cart_id' => $maskedCartId,
'model' => $cart,
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
Expand Down Expand Up @@ -67,7 +68,7 @@ public function __construct(
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$shippingAddresses = $this->arrayManager->get('input/shipping_addresses', $args);
$maskedCartId = (string) $this->arrayManager->get('input/cart_id', $args);
$maskedCartId = (string)$this->arrayManager->get('input/cart_id', $args);

if (!$maskedCartId) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
Expand All @@ -79,7 +80,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value

$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());

$this->setShippingAddressesOnCart->execute($context, $cart, $shippingAddresses);
try {
$this->setShippingAddressesOnCart->execute($context, $cart, $shippingAddresses);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
}

return [
'cart' => [
Expand Down
35 changes: 32 additions & 3 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Mutation {
setShippingAddressesOnCart(input: SetShippingAddressesOnCartInput): SetShippingAddressesOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingAddressesOnCart")
setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetBillingAddressOnCart")
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart")
setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")
}

input AddSimpleProductsToCartInput {
Expand Down Expand Up @@ -102,6 +103,24 @@ input ShippingMethodForAddressInput {
method_code: String!
}

input SetPaymentMethodOnCartInput {
cart_id: String!
payment_method: PaymentMethodInput!
}

input PaymentMethodInput {
code: String! @doc(description:"Payment method code")
purchase_order_number: String @doc(description:"Purchase order number")
additional_data: PaymentMethodAdditionalDataInput
}

input PaymentMethodAdditionalDataInput {
}

type SetPaymentMethodOnCartOutput {
cart: Cart!
}

type SetBillingAddressOnCartOutput {
cart: Cart!
}
Expand All @@ -124,7 +143,8 @@ 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 : [PaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods")
available_payment_methods : [AvailablePaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods")
selected_payment_method: SelectedPaymentMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SelectedPaymentMethod")
}

type CartAddress {
Expand All @@ -139,8 +159,8 @@ type CartAddress {
country: CartAddressCountry
telephone: String
address_type: AdressTypeEnum
selected_shipping_method: SelectedShippingMethod
available_shipping_methods: [AvailableShippingMethod] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAdress\\AvailableShippingMethods")
selected_shipping_method: SelectedShippingMethod
items_weight: Float
customer_notes: String
cart_items: [CartItemQuantity]
Expand Down Expand Up @@ -177,11 +197,20 @@ type AvailableShippingMethod {
price_incl_tax: Float!
}

type PaymentMethod {
type AvailablePaymentMethod {
code: String @doc(description: "The payment method code")
title: String @doc(description: "The payment method title.")
}

type SelectedPaymentMethod {
code: String @doc(description: "The payment method code")
purchase_order_number: String @doc(description: "The purchase order number.")
additional_data: SelectedPaymentMethodAdditionalData
}

type SelectedPaymentMethodAdditionalData {
}

enum AdressTypeEnum {
SHIPPING
BILLING
Expand Down

This file was deleted.

Loading

0 comments on commit 908ed15

Please sign in to comment.