-
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.
Ref #293
- Loading branch information
Showing
17 changed files
with
797 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# BraintreeGraphQl | ||
|
||
**BraintreeGraphQl** provides type and resolver for method additional | ||
information. |
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,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\\": "" | ||
} | ||
} | ||
} |
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,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> |
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,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> |
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,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! | ||
} |
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,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__); |
13 changes: 13 additions & 0 deletions
13
app/code/Magento/QuoteGraphQl/Model/Cart/Payment/AdditionalDataBuilderInterface.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,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; | ||
} |
32 changes: 32 additions & 0 deletions
32
app/code/Magento/QuoteGraphQl/Model/Cart/Payment/AdditionalDataBuilderPool.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,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; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
app/code/Magento/QuoteGraphQl/Model/Cart/Payment/DefaultAdditionalDataBuilder.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,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
62
app/code/Magento/QuoteGraphQl/Model/Cart/Payment/MethodBuilder.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,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 | ||
), | ||
] | ||
]); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/code/Magento/QuoteGraphQl/Model/Cart/Payment/PaymentDataProvider.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,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
58
app/code/Magento/QuoteGraphQl/Model/Resolver/CartPaymentMethod.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,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); | ||
} | ||
} |
Oops, something went wrong.