Skip to content

Commit

Permalink
Proposal For setPaymentMethodOnCart
Browse files Browse the repository at this point in the history
Ref #293
  • Loading branch information
pmclain committed Feb 7, 2019
1 parent 8dfe26a commit e95c678
Show file tree
Hide file tree
Showing 17 changed files with 797 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/code/Magento/BraintreeGraphQl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# BraintreeGraphQl

**BraintreeGraphQl** provides type and resolver for method additional
information.
24 changes: 24 additions & 0 deletions app/code/Magento/BraintreeGraphQl/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "magento/module-braintree-graph-ql",
"description": "N/A",
"type": "magento2-module",
"require": {
"php": "~7.1.3||~7.2.0",
"magento/framework": "*"
},
"suggest": {
"magento/module-graph-ql": "*"
},
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\BraintreeGraphQl\\": ""
}
}
}
27 changes: 27 additions & 0 deletions app/code/Magento/BraintreeGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataBuilderPool">
<arguments>
<argument name="builders" xsi:type="array">
<item name="braintree" xsi:type="object">BraintreeAdditionalDataBuilder</item>
<item name="braintree_vault" xsi:type="object">BraintreeVaultAdditionalDataBuilder</item>
</argument>
</arguments>
</type>
<virtualType name="BraintreeAdditionalDataBuilder" type="Magento\QuoteGraphQl\Model\Cart\Payment\DefaultAdditionalDataBuilder">
<arguments>
<argument name="methodCode" xsi:type="const">Magento\Braintree\Model\Ui\ConfigProvider::CODE</argument>
</arguments>
</virtualType>
<virtualType name="BraintreeVaultAdditionalDataBuilder" type="Magento\QuoteGraphQl\Model\Cart\Payment\DefaultAdditionalDataBuilder">
<arguments>
<argument name="methodCode" xsi:type="const">Magento\Braintree\Model\Ui\ConfigProvider::CC_VAULT_CODE</argument>
</arguments>
</virtualType>
</config>
10 changes: 10 additions & 0 deletions app/code/Magento/BraintreeGraphQl/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_BraintreeGraphQl"/>
</config>
18 changes: 18 additions & 0 deletions app/code/Magento/BraintreeGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

input PaymentMethodInput {
braintree: BraintreeInput
braintree_vault: BraintreeVaultInput
}

input BraintreeInput {
payment_method_nonce: String!
is_active_payment_token_enabler: Boolean!
}

input BraintreeVaultInput {
payment_method_nonce: String!
public_hash: String!
is_active_payment_token_enabler: Boolean!
}
10 changes: 10 additions & 0 deletions app/code/Magento/BraintreeGraphQl/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_BraintreeGraphQl', __DIR__);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart\Payment;

interface AdditionalDataBuilderInterface
{
public function build(array $args): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart\Payment;

class AdditionalDataBuilderPool
{
/**
* @var AdditionalDataBuilderInterface[]
*/
private $builders = [];

public function __construct(
array $builders
) {
$this->builders = $builders;
}

public function buildForMethod(string $methodCode, array $args): array
{
$additionalData = [];
if (isset($this->builders[$methodCode])) {
$additionalData = $this->builders[$methodCode]->build($args);
}

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

namespace Magento\QuoteGraphQl\Model\Cart\Payment;

use Magento\Framework\Stdlib\ArrayManager;

class DefaultAdditionalDataBuilder implements AdditionalDataBuilderInterface
{
private const INPUT_PATH_ADDITIONAL_DATA = 'input/payment_method/%s';

/**
* @var ArrayManager
*/
private $arrayManager;

/**
* @var string
*/
private $methodCode;

public function __construct(
ArrayManager $arrayManager,
string $methodCode = ''
) {
$this->arrayManager = $arrayManager;
$this->methodCode = $methodCode;
}

public function build(array $args): array
{
return $this->arrayManager->get($this->getAdditionalDataPath(), $args) ?? [];
}

private function getAdditionalDataPath(): string
{
return sprintf(static::INPUT_PATH_ADDITIONAL_DATA, $this->methodCode);
}
}
62 changes: 62 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Cart/Payment/MethodBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart\Payment;

use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Quote\Api\Data\PaymentInterface;
use Magento\Quote\Api\Data\PaymentInterfaceFactory;

class MethodBuilder
{
/**
* @var PaymentInterfaceFactory
*/
private $paymentFactory;

/**
* @var AdditionalDataBuilderPool
*/
private $additionalDataBuilderPool;

/**
* @var ArrayManager
*/
private $arrayManager;

/**
* @param PaymentInterfaceFactory $paymentFactory
* @param AdditionalDataBuilderPool $additionalDataBuilderPool
* @param ArrayManager $arrayManager
*/
public function __construct(
PaymentInterfaceFactory $paymentFactory,
AdditionalDataBuilderPool $additionalDataBuilderPool,
ArrayManager $arrayManager
) {
$this->paymentFactory = $paymentFactory;
$this->additionalDataBuilderPool = $additionalDataBuilderPool;
$this->arrayManager = $arrayManager;
}

public function build(array $args): PaymentInterface
{
$method = (string) $this->arrayManager->get('input/payment_method/method', $args);

return $this->paymentFactory->create([
'data' => [
PaymentInterface::KEY_METHOD => $method,
PaymentInterface::KEY_PO_NUMBER => $this->arrayManager->get('input/payment_method/po_number', $args),
PaymentInterface::KEY_ADDITIONAL_DATA => $this->additionalDataBuilderPool->buildForMethod(
$method,
$args
),
]
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart\Payment;

use Magento\Quote\Model\Quote;

/**
* Extract data from payment
*/
class PaymentDataProvider
{
/**
* Extract data from cart
*
* @param Quote $cart
* @return array
*/
public function getCartPayment(Quote $cart): array
{
$payment = $cart->getPayment();
if (!$payment) {
return [];
}

return [
'method' => $payment->getMethod(),
'po_number' => $payment->getPoNumber(),
];
}
}
58 changes: 58 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartPaymentMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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;
use Magento\QuoteGraphQl\Model\Cart\Payment\PaymentDataProvider;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;

/**
* @inheritdoc
*/
class CartPaymentMethod implements ResolverInterface
{
/**
* @var PaymentDataProvider
*/
private $paymentDataProvider;

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

/**
* @param PaymentDataProvider $paymentDataProvider
* @param GetCartForUser $getCartForUser
*/
public function __construct(
PaymentDataProvider $paymentDataProvider,
GetCartForUser $getCartForUser
) {
$this->paymentDataProvider = $paymentDataProvider;
$this->getCartForUser = $getCartForUser;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['cart_id'])) {
throw new LocalizedException(__('"model" value should be specified'));
}

$maskedCartId = $value['cart_id'];
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());

return $this->paymentDataProvider->getCartPayment($cart);
}
}
Loading

0 comments on commit e95c678

Please sign in to comment.