Skip to content

Commit

Permalink
magento#716: [Checkout] Set Payment Method and Place Order Mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav committed Jun 20, 2019
1 parent a5feb01 commit 3842378
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class AuthorizenetDataProvider implements AdditionalDataProviderInterface
{
private const PATH_ADDITIONAL_DATA = 'input/payment_method/additional_data/authorizenet_acceptjs';
private const PATH_ADDITIONAL_DATA = 'payment_method/additional_data/authorizenet_acceptjs';

/**
* @var ArrayManager
Expand All @@ -36,12 +36,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,7 @@
*/
class PaypalExpressAdditionalDataProvider implements AdditionalDataProviderInterface
{

private const PATH_ADDITIONAL_DATA = 'input/payment_method/additional_data';
private const PATH_ADDITIONAL_DATA = 'payment_method/additional_data';

/**
* @var ArrayManager
Expand All @@ -35,12 +34,12 @@ public function __construct(
/**
* Returns 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(self::PATH_ADDITIONAL_DATA, $args) ?? [];
$additionalData = $this->arrayManager->get(self::PATH_ADDITIONAL_DATA, $data) ?? [];

return $additionalData;
}
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 @@ -15,6 +15,7 @@
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
{
Expand All @@ -28,27 +29,43 @@ class SetPaymentMethodOnCart
*/
private $paymentFactory;

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

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

public function execute(array $paymentData, Quote $cart): Quote
/**
* 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 = $paymentData['additional_data'] ?? [];
$additionalData = $this->additionalDataProviderPool->getData($paymentMethodCode, $paymentData) ?? [];

$payment = $this->paymentFactory->create([
'data' => [
Expand All @@ -65,7 +82,5 @@ public function execute(array $paymentData, Quote $cart): Quote
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}

return $cart;
}
}
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
Expand Up @@ -16,6 +16,7 @@
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;

/**
Expand All @@ -39,21 +40,21 @@ class SetPaymentAndPlaceOrder implements ResolverInterface
private $orderRepository;

/**
* @var \Magento\QuoteGraphQl\Model\Cart\SetPaymentMethodOnCart
* @var SetPaymentMethodOnCart
*/
private $setPaymentMethodOnCart;

/**
* @param GetCartForUser $getCartForUser
* @param CartManagementInterface $cartManagement
* @param OrderRepositoryInterface $orderRepository
* @param \Magento\QuoteGraphQl\Model\Cart\SetPaymentMethodOnCart $setPaymentMethodOnCart
* @param SetPaymentMethodOnCart $setPaymentMethodOnCart
*/
public function __construct(
GetCartForUser $getCartForUser,
CartManagementInterface $cartManagement,
OrderRepositoryInterface $orderRepository,
\Magento\QuoteGraphQl\Model\Cart\SetPaymentMethodOnCart $setPaymentMethodOnCart
SetPaymentMethodOnCart $setPaymentMethodOnCart
) {
$this->getCartForUser = $getCartForUser;
$this->cartManagement = $cartManagement;
Expand All @@ -77,11 +78,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$paymentData = $args['input']['payment_method'];

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

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
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
7 changes: 6 additions & 1 deletion app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +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: SetPaymentMethodOnCartInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentAndPlaceOrder")
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 @@ -120,6 +120,11 @@ input ShippingMethodInput {
method_code: String!
}

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

input PlaceOrderInput {
cart_id: String!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,16 @@ public function testPlaceOrderIfCartIdIsMissed()
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
*
* @expectedException \Exception
* @expectedExceptionMessage Guest email for cart is missing.
*/
public function testPlaceOrderWithNoEmail()
{
$reservedOrderId = 'test_quote';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
$query = $this->getQuery($maskedQuoteId);

self::expectExceptionMessage("Guest email for cart is missing. Please enter");
$this->graphQlMutation($query);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,29 @@ public function testSetPaymentOnNonExistentCart()
$this->graphQlMutation($query);
}

/**
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_shipping_methods.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php
*
* @expectedException \Exception
* @expectedExceptionMessage Guest email for cart is missing.
*/
public function testPlaceOrderWithNoEmail()
{
$methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE;
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId, $methodCode);

$this->graphQlMutation($query);
}

/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
Expand Down

0 comments on commit 3842378

Please sign in to comment.