-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from atravkovs/paypal
Adapted placeOrder and setPaymentMethodOnCart
- Loading branch information
Showing
4 changed files
with
215 additions
and
1 deletion.
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
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); | ||
} | ||
} | ||
} |
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,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, | ||
], | ||
]; | ||
} | ||
} |
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