-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-5328: [Checkout] Set Payment Method and Place Order Mutation #723
- Loading branch information
Showing
11 changed files
with
751 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
app/code/Magento/QuoteGraphQl/Model/Cart/SetPaymentMethodOnCart.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?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; | ||
|
||
/** | ||
* Set payment method on cart | ||
*/ | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentAndPlaceOrder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?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()); | ||
|
||
if ((int)$context->getUserId() === 0) { | ||
if (!$cart->getCustomerEmail()) { | ||
throw new GraphQlInputException(__("Guest email for cart is missing.")); | ||
} | ||
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST); | ||
} | ||
|
||
$this->setPaymentMethodOnCart->execute($cart, $paymentData); | ||
|
||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.