This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-4383: Prototype: setPaymentMethodOnCart #330
- Loading branch information
Showing
20 changed files
with
1,766 additions
and
388 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
42 changes: 42 additions & 0 deletions
42
app/code/Magento/QuoteGraphQl/Model/Resolver/SelectedPaymentMethod.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,42 @@ | ||
<?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\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class SelectedPaymentMethod implements ResolverInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (!isset($value['model'])) { | ||
throw new LocalizedException(__('"model" value should be specified')); | ||
} | ||
|
||
/** @var \Magento\Quote\Model\Quote $cart */ | ||
$cart = $value['model']; | ||
|
||
$payment = $cart->getPayment(); | ||
if (!$payment) { | ||
return []; | ||
} | ||
|
||
return [ | ||
'code' => $payment->getMethod(), | ||
'purchase_order_number' => $payment->getPoNumber(), | ||
]; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
app/code/Magento/QuoteGraphQl/Model/Resolver/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,108 @@ | ||
<?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\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\Framework\Stdlib\ArrayManager; | ||
use Magento\Quote\Api\Data\PaymentInterface; | ||
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser; | ||
use Magento\Quote\Api\Data\PaymentInterfaceFactory; | ||
use Magento\Quote\Api\PaymentMethodManagementInterface; | ||
|
||
/** | ||
* Mutation resolver for setting payment method for shopping cart | ||
*/ | ||
class SetPaymentMethodOnCart implements ResolverInterface | ||
{ | ||
/** | ||
* @var GetCartForUser | ||
*/ | ||
private $getCartForUser; | ||
|
||
/** | ||
* @var ArrayManager | ||
*/ | ||
private $arrayManager; | ||
|
||
/** | ||
* @var PaymentMethodManagementInterface | ||
*/ | ||
private $paymentMethodManagement; | ||
|
||
/** | ||
* @var PaymentInterfaceFactory | ||
*/ | ||
private $paymentFactory; | ||
|
||
/** | ||
* @param GetCartForUser $getCartForUser | ||
* @param ArrayManager $arrayManager | ||
* @param PaymentMethodManagementInterface $paymentMethodManagement | ||
* @param PaymentInterfaceFactory $paymentFactory | ||
*/ | ||
public function __construct( | ||
GetCartForUser $getCartForUser, | ||
ArrayManager $arrayManager, | ||
PaymentMethodManagementInterface $paymentMethodManagement, | ||
PaymentInterfaceFactory $paymentFactory | ||
) { | ||
$this->getCartForUser = $getCartForUser; | ||
$this->arrayManager = $arrayManager; | ||
$this->paymentMethodManagement = $paymentMethodManagement; | ||
$this->paymentFactory = $paymentFactory; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
$maskedCartId = (string)$this->arrayManager->get('input/cart_id', $args); | ||
if (!$maskedCartId) { | ||
throw new GraphQlInputException(__('Required parameter "cart_id" is missing')); | ||
} | ||
|
||
$paymentMethod = $this->arrayManager->get('input/payment_method', $args); | ||
if (!$paymentMethod) { | ||
throw new GraphQlInputException(__('Required parameter "payment_method" is missing')); | ||
} | ||
|
||
$paymentMethodCode = (string) $this->arrayManager->get('input/payment_method/code', $args); | ||
if (!$paymentMethodCode) { | ||
throw new GraphQlInputException(__('Required parameter payment "code" is missing')); | ||
} | ||
|
||
$poNumber = $this->arrayManager->get('input/payment_method/purchase_order_number', $args); | ||
|
||
$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 => [], | ||
] | ||
]); | ||
|
||
try { | ||
$this->paymentMethodManagement->set($cart->getId(), $payment); | ||
} catch (LocalizedException $e) { | ||
throw new GraphQlInputException(__($e->getMessage())); | ||
} | ||
|
||
return [ | ||
'cart' => [ | ||
'cart_id' => $maskedCartId, | ||
'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
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.