Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

[Checkout] Set Payment Method and Place Order Mutation #723

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@

use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderInterface;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Framework\GraphQL\DataObjectConverter;

/**
* DataProvider Model for Authorizenet
*/
class AuthorizenetDataProvider implements AdditionalDataProviderInterface
{
private const PATH_ADDITIONAL_DATA = 'input/payment_method/additional_data/authorizenet_acceptjs';
private const PATH_ADDITIONAL_DATA = 'authorizenet_acceptjs';

/**
* @var ArrayManager
Expand All @@ -36,12 +35,12 @@ public function __construct(
/**
* Return additional data
*
* @param array $args
* @param array $data
* @return array
*/
public function getData(array $args): array
public function getData(array $data): array
{
$additionalData = $this->arrayManager->get(static::PATH_ADDITIONAL_DATA, $args) ?? [];
$additionalData = $this->arrayManager->get(static::PATH_ADDITIONAL_DATA, $data) ?? [];
foreach ($additionalData as $key => $value) {
$additionalData[$this->snakeCaseToCamelCase($key)] = $value;
unset($additionalData[$key]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ interface AdditionalDataProviderInterface
/**
* Return Additional Data
*
* @param array $args
* @param array $data
* @return array
*/
public function getData(array $args): array;
public function getData(array $data): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public function __construct(array $dataProviders = [])
* Return additional data for the payment method
*
* @param string $methodCode
* @param array $args
* @param array $data
* @return array
*/
public function getData(string $methodCode, array $args): array
public function getData(string $methodCode, array $data): array
{
$additionalData = [];
if (isset($this->dataProviders[$methodCode])) {
$additionalData = $this->dataProviders[$methodCode]->getData($args);
$additionalData = $this->dataProviders[$methodCode]->getData($data);
}

return $additionalData;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\Data\PaymentInterface;
use Magento\Quote\Api\Data\PaymentInterfaceFactory;
use Magento\Quote\Api\PaymentMethodManagementInterface;
use Magento\Quote\Model\Quote;
use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderPool;

class SetPaymentMethodOnCart
{
/**
* @var PaymentMethodManagementInterface
*/
private $paymentMethodManagement;

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

/**
* @var AdditionalDataProviderPool
*/
private $additionalDataProviderPool;

/**
* @param PaymentMethodManagementInterface $paymentMethodManagement
* @param PaymentInterfaceFactory $paymentFactory
* @param AdditionalDataProviderPool $additionalDataProviderPool
*/
public function __construct(
PaymentMethodManagementInterface $paymentMethodManagement,
PaymentInterfaceFactory $paymentFactory,
AdditionalDataProviderPool $additionalDataProviderPool
) {
$this->paymentMethodManagement = $paymentMethodManagement;
$this->paymentFactory = $paymentFactory;
$this->additionalDataProviderPool = $additionalDataProviderPool;
}

/**
* Set payment method on cart
*
* @param Quote $cart
* @param array $paymentData
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(Quote $cart, array $paymentData): void
{
if (!isset($paymentData['code']) || empty($paymentData['code'])) {
throw new GraphQlInputException(__('Required parameter "code" for "payment_method" is missing.'));
}
$paymentMethodCode = $paymentData['code'];

$poNumber = $paymentData['purchase_order_number'] ?? null;
$additionalData = isset($paymentData['additional_data'])
? $this->additionalDataProviderPool->getData($paymentMethodCode, $paymentData['additional_data'])
: [];

$payment = $this->paymentFactory->create([
'data' => [
PaymentInterface::KEY_METHOD => $paymentMethodCode,
PaymentInterface::KEY_PO_NUMBER => $poNumber,
PaymentInterface::KEY_ADDITIONAL_DATA => $additionalData,
]
]);

try {
$this->paymentMethodManagement->set($cart->getId(), $payment);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}
}
}
4 changes: 2 additions & 2 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/PlaceOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value

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

if ($context->getUserId() === 0) {
if ((int)$context->getUserId() === 0) {
if (!$cart->getCustomerEmail()) {
throw new GraphQlInputException(__("Guest email for cart is missing. Please enter"));
throw new GraphQlInputException(__("Guest email for cart is missing."));
}
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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\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\QuoteGraphQl\Model\Cart\SetPaymentMethodOnCart;
use Magento\Sales\Api\OrderRepositoryInterface;

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

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

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

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

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

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}
$maskedCartId = $args['input']['cart_id'];

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

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

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@

namespace Magento\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\Data\PaymentInterface;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Quote\Api\Data\PaymentInterfaceFactory;
use Magento\Quote\Api\PaymentMethodManagementInterface;
use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderPool;
use Magento\Framework\App\ObjectManager;
use Magento\QuoteGraphQl\Model\Cart\SetPaymentMethodOnCart as SetPaymentMethodOnCartModel;

/**
* Mutation resolver for setting payment method for shopping cart
Expand All @@ -32,37 +25,20 @@ class SetPaymentMethodOnCart implements ResolverInterface
private $getCartForUser;

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

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

/**
* @var AdditionalDataProviderPool
*/
private $additionalDataProviderPool;
private $setPaymentMethodOnCart;

/**
* @param GetCartForUser $getCartForUser
* @param PaymentMethodManagementInterface $paymentMethodManagement
* @param PaymentInterfaceFactory $paymentFactory
* @param AdditionalDataProviderPool $additionalDataProviderPool
* @param SetPaymentMethodOnCartModel $setPaymentMethodOnCart
*/
public function __construct(
GetCartForUser $getCartForUser,
PaymentMethodManagementInterface $paymentMethodManagement,
PaymentInterfaceFactory $paymentFactory,
AdditionalDataProviderPool $additionalDataProviderPool = null
SetPaymentMethodOnCartModel $setPaymentMethodOnCart
) {
$this->getCartForUser = $getCartForUser;
$this->paymentMethodManagement = $paymentMethodManagement;
$this->paymentFactory = $paymentFactory;
$this->additionalDataProviderPool = $additionalDataProviderPool
?: ObjectManager::getInstance()->get(AdditionalDataProviderPool::class);
$this->setPaymentMethodOnCart = $setPaymentMethodOnCart;
}

/**
Expand All @@ -78,28 +54,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
if (!isset($args['input']['payment_method']['code']) || empty($args['input']['payment_method']['code'])) {
throw new GraphQlInputException(__('Required parameter "code" for "payment_method" is missing.'));
}
$paymentMethodCode = $args['input']['payment_method']['code'];

$poNumber = $args['input']['payment_method']['purchase_order_number'] ?? null;
$additionalData = $this->additionalDataProviderPool->getData($paymentMethodCode, $args) ?? [];
$paymentData = $args['input']['payment_method'];

$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 => $additionalData,
]]
);

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

return [
'cart' => [
Expand Down
6 changes: 6 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Mutation {
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart")
setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")
setGuestEmailOnCart(input: SetGuestEmailOnCartInput): SetGuestEmailOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetGuestEmailOnCart")
setPaymentMethodAndPlaceOrder(input: SetPaymentMethodAndPlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentAndPlaceOrder")
placeOrder(input: PlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\PlaceOrder")
}

Expand Down Expand Up @@ -119,6 +120,11 @@ input ShippingMethodInput {
method_code: String!
}

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

input PlaceOrderInput {
cart_id: String!
}
Expand Down
Loading