From 2306ab2428288d435c25a20cea7e6b725f942c87 Mon Sep 17 00:00:00 2001 From: Artjoms Travkovs Date: Fri, 11 Oct 2019 20:38:41 +0300 Subject: [PATCH 1/3] Added placeOrder & setPaymentMethodOnCart --- src/Model/Resolver/PlaceOrder.php | 111 ++++++++++++++++++ src/Model/Resolver/SetPaymentMethodOnCart.php | 96 +++++++++++++++ src/etc/schema.graphqls | 7 ++ 3 files changed, 214 insertions(+) create mode 100644 src/Model/Resolver/PlaceOrder.php create mode 100644 src/Model/Resolver/SetPaymentMethodOnCart.php diff --git a/src/Model/Resolver/PlaceOrder.php b/src/Model/Resolver/PlaceOrder.php new file mode 100644 index 0000000..55e8050 --- /dev/null +++ b/src/Model/Resolver/PlaceOrder.php @@ -0,0 +1,111 @@ +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); + } + } +} diff --git a/src/Model/Resolver/SetPaymentMethodOnCart.php b/src/Model/Resolver/SetPaymentMethodOnCart.php new file mode 100644 index 0000000..bb6639b --- /dev/null +++ b/src/Model/Resolver/SetPaymentMethodOnCart.php @@ -0,0 +1,96 @@ +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, + ], + ]; + } +} diff --git a/src/etc/schema.graphqls b/src/etc/schema.graphqls index f8967bc..b8e7eac 100755 --- a/src/etc/schema.graphqls +++ b/src/etc/schema.graphqls @@ -17,12 +17,19 @@ 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") } type Query { getCartForCustomer(guestCartId: String): QuoteData @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\GetCartForCustomer") } +input S_SetPaymentMethodOnCartInput { + guest_cart_id: String + payment_method: PaymentMethodInput! +} + input CartItemInput { sku: String! qty: Int! From ed1ff25a5f4515248ac81429ca7c5f10e708aa21 Mon Sep 17 00:00:00 2001 From: Artjoms Travkovs Date: Mon, 28 Oct 2019 21:22:47 +0200 Subject: [PATCH 2/3] Updated M2 version requirement --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b861ad0..c34033c 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "N/A", "type": "magento2-module", "require": { - "magento/magento2-base": "^2.3.1" + "magento/magento2-base": "^2.3.3" }, "support": { "source": "https://github.com/scandipwa/quote-graphql", From 7e336f7fc60b218a33ac0cb4dc9699485097bd75 Mon Sep 17 00:00:00 2001 From: Artjoms Travkovs Date: Mon, 28 Oct 2019 21:26:34 +0200 Subject: [PATCH 3/3] Updated M2 version requirement typo --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c34033c..52076f2 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "N/A", "type": "magento2-module", "require": { - "magento/magento2-base": "^2.3.3" + "magento/magento2-base": "^2.3.2" }, "support": { "source": "https://github.com/scandipwa/quote-graphql",