Skip to content

Commit

Permalink
Merge pull request #13 from atravkovs/paypal
Browse files Browse the repository at this point in the history
Adapted placeOrder and setPaymentMethodOnCart
  • Loading branch information
alfredsgenkins authored Nov 13, 2019
2 parents a656df4 + 7e336f7 commit 83a4d03
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "N/A",
"type": "magento2-module",
"require": {
"magento/magento2-base": "^2.3.1"
"magento/magento2-base": "^2.3.2"
},
"support": {
"source": "https://github.com/scandipwa/quote-graphql",
Expand Down
111 changes: 111 additions & 0 deletions src/Model/Resolver/PlaceOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* ScandiPWA - Progressive Web App for Magento
*
* Copyright © Scandiweb, Inc. All rights reserved.
* See LICENSE for license details.
*
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
* @package scandipwa/quote-graphql
* @link https://github.com/scandipwa/quote-graphql
*/
declare(strict_types=1);

namespace ScandiPWA\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartManagementInterface;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\QuoteGraphQl\Model\Cart\CheckCartCheckoutAllowance;

/**
* @inheritdoc
*/
class PlaceOrder implements ResolverInterface
{
/**
* @var CartManagementInterface
*/
private $cartManagement;

/**
* @var GetCartForUser
*/
private $getCartForUser;

/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

/**
* @var CheckCartCheckoutAllowance
*/
private $checkCartCheckoutAllowance;

/**
* @param GetCartForUser $getCartForUser
* @param CartManagementInterface $cartManagement
* @param OrderRepositoryInterface $orderRepository
* @param CheckCartCheckoutAllowance $checkCartCheckoutAllowance
*/
public function __construct(
GetCartForUser $getCartForUser,
CartManagementInterface $cartManagement,
OrderRepositoryInterface $orderRepository,
CheckCartCheckoutAllowance $checkCartCheckoutAllowance
) {
$this->getCartForUser = $getCartForUser;
$this->cartManagement = $cartManagement;
$this->orderRepository = $orderRepository;
$this->checkCartCheckoutAllowance = $checkCartCheckoutAllowance;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$guestCartId = $args['guestCartId'] ?? '';

$customerId = $context->getUserId();
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();

if ($guestCartId !== '') {
$cart = $this->getCartForUser->execute($guestCartId, $customerId, $storeId);
} else {
$cart = $this->cartManagement->getCartForCustomer($customerId);
}

$this->checkCartCheckoutAllowance->execute($cart);

if ((int)$context->getUserId() === 0) {
if (!$cart->getCustomerEmail()) {
throw new GraphQlInputException(__("Guest email for cart is missing."));
}
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
}

try {
$orderId = $this->cartManagement->placeOrder($cart->getId());
$order = $this->orderRepository->get($orderId);

return [
'order' => [
'order_id' => $order->getIncrementId(),
],
];
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__('Unable to place order: %message', ['message' => $e->getMessage()]), $e);
}
}
}
96 changes: 96 additions & 0 deletions src/Model/Resolver/SetPaymentMethodOnCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* ScandiPWA - Progressive Web App for Magento
*
* Copyright © Scandiweb, Inc. All rights reserved.
* See LICENSE for license details.
*
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
* @package scandipwa/quote-graphql
* @link https://github.com/scandipwa/quote-graphql
*/
declare(strict_types=1);

namespace ScandiPWA\QuoteGraphQl\Model\Resolver;

use Magento\Quote\Api\CartManagementInterface;
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\QuoteGraphQl\Model\Cart\CheckCartCheckoutAllowance;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\QuoteGraphQl\Model\Cart\SetPaymentMethodOnCart as SetPaymentMethodOnCartModel;

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

/**
* @var SetPaymentMethodOnCartModel
*/
private $setPaymentMethodOnCart;

/**
* @var CheckCartCheckoutAllowance
*/
private $checkCartCheckoutAllowance;

/**
* @param GetCartForUser $getCartForUser
* @param SetPaymentMethodOnCartModel $setPaymentMethodOnCart
* @param CheckCartCheckoutAllowance $checkCartCheckoutAllowance
*/
public function __construct(
GetCartForUser $getCartForUser,
CartManagementInterface $cartManagement,
SetPaymentMethodOnCartModel $setPaymentMethodOnCart,
CheckCartCheckoutAllowance $checkCartCheckoutAllowance
) {
$this->getCartForUser = $getCartForUser;
$this->cartManagement = $cartManagement;
$this->setPaymentMethodOnCart = $setPaymentMethodOnCart;
$this->checkCartCheckoutAllowance = $checkCartCheckoutAllowance;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$guestCartId = $args['input']['guest_cart_id'] ?? '';

if (empty($args['input']['payment_method']['code'])) {
throw new GraphQlInputException(__('Required parameter "code" for "payment_method" is missing.'));
}
$paymentData = $args['input']['payment_method'];

$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();

$customerId = $context->getUserId();
if ($guestCartId !== '') {
$cart = $this->getCartForUser->execute($guestCartId, $customerId, $storeId);
} else {
$cart = $this->cartManagement->getCartForCustomer($customerId);
}

$this->checkCartCheckoutAllowance->execute($cart);
$this->setPaymentMethodOnCart->execute($cart, $paymentData);

return [
'cart' => [
'model' => $cart,
],
];
}
}
7 changes: 7 additions & 0 deletions src/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Mutation {
savePaymentInformationAndPlaceOrder(paymentInformation: PaymentInformation!, guestCartId: String): OrderIdObject @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\SavePaymentInformationAndPlaceOrder")
saveCartItem(cartItem: CartItemInput!, guestCartId: String): Query @resolver(class:"\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\SaveCartItem")
removeCartItem(guestCartId: String, item_id: Int!): Query @resolver(class:"\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\RemoveCartItem")
s_setPaymentMethodOnCart(input: S_SetPaymentMethodOnCartInput!): SetPaymentMethodOnCartOutput @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")
s_placeOrder(guestCartId: String): PlaceOrderOutput @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\PlaceOrder")
applyCoupon(guestCartId: String, coupon_code: String!): Query @resolver(class:"\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\ApplyCoupon")
removeCoupon(guestCartId: String): Query @resolver(class:"\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\RemoveCoupon")
}
Expand All @@ -28,6 +30,11 @@ type Query {
getOrderById(id: Int!): Order @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\ExpandedOrderResolver") @doc(description: "The Sales Order query returns information about a Sales order")
}

input S_SetPaymentMethodOnCartInput {
guest_cart_id: String
payment_method: PaymentMethodInput!
}

input CartItemInput {
id: CartItemId
sku: String
Expand Down

0 comments on commit 83a4d03

Please sign in to comment.