Skip to content

Commit a6e32e8

Browse files
committed
Mutation for creating braintree client token
This token is required by the sdk when creating nonces client-side
1 parent 3720ec0 commit a6e32e8

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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\BraintreeGraphQl\Model\Resolver;
9+
10+
use Magento\Braintree\Gateway\Config\Config;
11+
use Magento\Braintree\Gateway\Request\PaymentDataBuilder;
12+
use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\Store\Model\StoreManagerInterface;
18+
19+
/**
20+
* Resolver for generating Braintree client token
21+
*/
22+
class CreateBraintreeClientToken implements ResolverInterface
23+
{
24+
/**
25+
* @var StoreManagerInterface
26+
*/
27+
private $storeManager;
28+
29+
/**
30+
* @var Config
31+
*/
32+
private $config;
33+
34+
/**
35+
* @var BraintreeAdapterFactory
36+
*/
37+
private $adapterFactory;
38+
39+
/**
40+
* @param StoreManagerInterface $storeManager
41+
* @param Config $config
42+
* @param BraintreeAdapterFactory $adapterFactory
43+
*/
44+
public function __construct(
45+
StoreManagerInterface $storeManager,
46+
Config $config,
47+
BraintreeAdapterFactory $adapterFactory
48+
) {
49+
$this->storeManager = $storeManager;
50+
$this->config = $config;
51+
$this->adapterFactory = $adapterFactory;
52+
}
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
public function resolve(
58+
Field $field,
59+
$context,
60+
ResolveInfo $info,
61+
array $value = null,
62+
array $args = null
63+
) {
64+
$storeId = $this->storeManager->getStore()->getId();
65+
66+
if (!$this->config->isActive($storeId)) {
67+
throw new GraphQlInputException(__('The Braintree payment method is not active.'));
68+
}
69+
70+
$params = [];
71+
$merchantAccountId = $this->config->getMerchantAccountId($storeId);
72+
if (!empty($merchantAccountId)) {
73+
$params[PaymentDataBuilder::MERCHANT_ACCOUNT_ID] = $merchantAccountId;
74+
}
75+
76+
return $this->adapterFactory->create($storeId)->generate($params);
77+
}
78+
}

app/code/Magento/BraintreeGraphQl/etc/schema.graphqls

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Copyright © Magento, Inc. All rights reserved.
22
# See COPYING.txt for license details.
33

4+
type Mutation {
5+
createBraintreeClientToken: String! @resolver(class: "\\Magento\\BraintreeGraphQl\\Model\\Resolver\\CreateBraintreeClientToken") @doc(description:"Creates Braintree Client Token for creating client-side nonce.")
6+
}
7+
48
input PaymentMethodInput {
59
braintree: BraintreeInput
610
braintree_cc_vault: BraintreeCcVaultInput

dev/tests/api-functional/_files/Magento/TestModuleBraintree/Model/Adapter/BraintreeAdapter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,13 @@ public function sale(array $attributes)
6262
{
6363
return $this->mockResponseDataProvider->generateMockSaleResponse($attributes);
6464
}
65+
66+
/**
67+
* @param array $params
68+
* @return string|\Braintree\Result\Error
69+
*/
70+
public function generate(array $params = [])
71+
{
72+
return $this->mockResponseDataProvider->generateMockClientToken();
73+
}
6574
}

dev/tests/api-functional/_files/Magento/TestModuleBraintree/Model/MockResponseDataProvider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public function generateMockNonceResponse(string $token): \Braintree\Instance
5454
return new \Braintree\Result\Successful($nonce, 'paymentMethodNonce');
5555
}
5656

57+
/**
58+
* Create mock client token
59+
*
60+
* @return string
61+
*/
62+
public function generateMockClientToken(): string
63+
{
64+
return $this->random->getRandomString(32);
65+
}
66+
5767
/**
5868
* Create Braintree transaction from provided request attributes
5969
*
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\GraphQl\Braintree;
9+
10+
use Magento\TestFramework\TestCase\GraphQlAbstract;
11+
12+
/**
13+
* Test creating Braintree client token mutation
14+
*/
15+
class CreateBraintreeClientTokenTest extends GraphQlAbstract
16+
{
17+
/**
18+
* Test creating Braintree client token
19+
*
20+
* @magentoApiDataFixture Magento/GraphQl/Braintree/_files/enable_braintree_payment.php
21+
*/
22+
public function testCreateBraintreeClientToken()
23+
{
24+
$response = $this->graphQlMutation($this->getMutation());
25+
26+
self::assertArrayHasKey('createBraintreeClientToken', $response);
27+
self::assertNotEmpty($response['createBraintreeClientToken']);
28+
}
29+
30+
/**
31+
* Test creating Braintree client token when method is disabled
32+
*
33+
* @expectedException \Exception
34+
* @expectedExceptionMessage payment method is not active
35+
*/
36+
public function testCreateBraintreeClientTokenNotActive()
37+
{
38+
$this->graphQlMutation($this->getMutation());
39+
}
40+
41+
private function getMutation(): string
42+
{
43+
return <<<QUERY
44+
mutation {
45+
createBraintreeClientToken
46+
}
47+
QUERY;
48+
}
49+
}

0 commit comments

Comments
 (0)