Skip to content

Commit e95c678

Browse files
committed
Proposal For setPaymentMethodOnCart
Ref #293
1 parent 8dfe26a commit e95c678

17 files changed

+797
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# BraintreeGraphQl
2+
3+
**BraintreeGraphQl** provides type and resolver for method additional
4+
information.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "magento/module-braintree-graph-ql",
3+
"description": "N/A",
4+
"type": "magento2-module",
5+
"require": {
6+
"php": "~7.1.3||~7.2.0",
7+
"magento/framework": "*"
8+
},
9+
"suggest": {
10+
"magento/module-graph-ql": "*"
11+
},
12+
"license": [
13+
"OSL-3.0",
14+
"AFL-3.0"
15+
],
16+
"autoload": {
17+
"files": [
18+
"registration.php"
19+
],
20+
"psr-4": {
21+
"Magento\\BraintreeGraphQl\\": ""
22+
}
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataBuilderPool">
10+
<arguments>
11+
<argument name="builders" xsi:type="array">
12+
<item name="braintree" xsi:type="object">BraintreeAdditionalDataBuilder</item>
13+
<item name="braintree_vault" xsi:type="object">BraintreeVaultAdditionalDataBuilder</item>
14+
</argument>
15+
</arguments>
16+
</type>
17+
<virtualType name="BraintreeAdditionalDataBuilder" type="Magento\QuoteGraphQl\Model\Cart\Payment\DefaultAdditionalDataBuilder">
18+
<arguments>
19+
<argument name="methodCode" xsi:type="const">Magento\Braintree\Model\Ui\ConfigProvider::CODE</argument>
20+
</arguments>
21+
</virtualType>
22+
<virtualType name="BraintreeVaultAdditionalDataBuilder" type="Magento\QuoteGraphQl\Model\Cart\Payment\DefaultAdditionalDataBuilder">
23+
<arguments>
24+
<argument name="methodCode" xsi:type="const">Magento\Braintree\Model\Ui\ConfigProvider::CC_VAULT_CODE</argument>
25+
</arguments>
26+
</virtualType>
27+
</config>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_BraintreeGraphQl"/>
10+
</config>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
input PaymentMethodInput {
5+
braintree: BraintreeInput
6+
braintree_vault: BraintreeVaultInput
7+
}
8+
9+
input BraintreeInput {
10+
payment_method_nonce: String!
11+
is_active_payment_token_enabler: Boolean!
12+
}
13+
14+
input BraintreeVaultInput {
15+
payment_method_nonce: String!
16+
public_hash: String!
17+
is_active_payment_token_enabler: Boolean!
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\Component\ComponentRegistrar;
9+
10+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_BraintreeGraphQl', __DIR__);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart\Payment;
9+
10+
interface AdditionalDataBuilderInterface
11+
{
12+
public function build(array $args): array;
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart\Payment;
9+
10+
class AdditionalDataBuilderPool
11+
{
12+
/**
13+
* @var AdditionalDataBuilderInterface[]
14+
*/
15+
private $builders = [];
16+
17+
public function __construct(
18+
array $builders
19+
) {
20+
$this->builders = $builders;
21+
}
22+
23+
public function buildForMethod(string $methodCode, array $args): array
24+
{
25+
$additionalData = [];
26+
if (isset($this->builders[$methodCode])) {
27+
$additionalData = $this->builders[$methodCode]->build($args);
28+
}
29+
30+
return $additionalData;
31+
}
32+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart\Payment;
9+
10+
use Magento\Framework\Stdlib\ArrayManager;
11+
12+
class DefaultAdditionalDataBuilder implements AdditionalDataBuilderInterface
13+
{
14+
private const INPUT_PATH_ADDITIONAL_DATA = 'input/payment_method/%s';
15+
16+
/**
17+
* @var ArrayManager
18+
*/
19+
private $arrayManager;
20+
21+
/**
22+
* @var string
23+
*/
24+
private $methodCode;
25+
26+
public function __construct(
27+
ArrayManager $arrayManager,
28+
string $methodCode = ''
29+
) {
30+
$this->arrayManager = $arrayManager;
31+
$this->methodCode = $methodCode;
32+
}
33+
34+
public function build(array $args): array
35+
{
36+
return $this->arrayManager->get($this->getAdditionalDataPath(), $args) ?? [];
37+
}
38+
39+
private function getAdditionalDataPath(): string
40+
{
41+
return sprintf(static::INPUT_PATH_ADDITIONAL_DATA, $this->methodCode);
42+
}
43+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart\Payment;
9+
10+
use Magento\Framework\Api\DataObjectHelper;
11+
use Magento\Framework\Stdlib\ArrayManager;
12+
use Magento\Quote\Api\Data\PaymentInterface;
13+
use Magento\Quote\Api\Data\PaymentInterfaceFactory;
14+
15+
class MethodBuilder
16+
{
17+
/**
18+
* @var PaymentInterfaceFactory
19+
*/
20+
private $paymentFactory;
21+
22+
/**
23+
* @var AdditionalDataBuilderPool
24+
*/
25+
private $additionalDataBuilderPool;
26+
27+
/**
28+
* @var ArrayManager
29+
*/
30+
private $arrayManager;
31+
32+
/**
33+
* @param PaymentInterfaceFactory $paymentFactory
34+
* @param AdditionalDataBuilderPool $additionalDataBuilderPool
35+
* @param ArrayManager $arrayManager
36+
*/
37+
public function __construct(
38+
PaymentInterfaceFactory $paymentFactory,
39+
AdditionalDataBuilderPool $additionalDataBuilderPool,
40+
ArrayManager $arrayManager
41+
) {
42+
$this->paymentFactory = $paymentFactory;
43+
$this->additionalDataBuilderPool = $additionalDataBuilderPool;
44+
$this->arrayManager = $arrayManager;
45+
}
46+
47+
public function build(array $args): PaymentInterface
48+
{
49+
$method = (string) $this->arrayManager->get('input/payment_method/method', $args);
50+
51+
return $this->paymentFactory->create([
52+
'data' => [
53+
PaymentInterface::KEY_METHOD => $method,
54+
PaymentInterface::KEY_PO_NUMBER => $this->arrayManager->get('input/payment_method/po_number', $args),
55+
PaymentInterface::KEY_ADDITIONAL_DATA => $this->additionalDataBuilderPool->buildForMethod(
56+
$method,
57+
$args
58+
),
59+
]
60+
]);
61+
}
62+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart\Payment;
9+
10+
use Magento\Quote\Model\Quote;
11+
12+
/**
13+
* Extract data from payment
14+
*/
15+
class PaymentDataProvider
16+
{
17+
/**
18+
* Extract data from cart
19+
*
20+
* @param Quote $cart
21+
* @return array
22+
*/
23+
public function getCartPayment(Quote $cart): array
24+
{
25+
$payment = $cart->getPayment();
26+
if (!$payment) {
27+
return [];
28+
}
29+
30+
return [
31+
'method' => $payment->getMethod(),
32+
'po_number' => $payment->getPoNumber(),
33+
];
34+
}
35+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\QuoteGraphQl\Model\Cart\Payment\PaymentDataProvider;
15+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
class CartPaymentMethod implements ResolverInterface
21+
{
22+
/**
23+
* @var PaymentDataProvider
24+
*/
25+
private $paymentDataProvider;
26+
27+
/**
28+
* @var GetCartForUser
29+
*/
30+
private $getCartForUser;
31+
32+
/**
33+
* @param PaymentDataProvider $paymentDataProvider
34+
* @param GetCartForUser $getCartForUser
35+
*/
36+
public function __construct(
37+
PaymentDataProvider $paymentDataProvider,
38+
GetCartForUser $getCartForUser
39+
) {
40+
$this->paymentDataProvider = $paymentDataProvider;
41+
$this->getCartForUser = $getCartForUser;
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
48+
{
49+
if (!isset($value['cart_id'])) {
50+
throw new LocalizedException(__('"model" value should be specified'));
51+
}
52+
53+
$maskedCartId = $value['cart_id'];
54+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
55+
56+
return $this->paymentDataProvider->getCartPayment($cart);
57+
}
58+
}

0 commit comments

Comments
 (0)