Skip to content

Commit 7023a5e

Browse files
authored
ENGCOM-4158: Add endpoint for fetching/deleting vault tokens #307
2 parents 0173252 + d8acf0a commit 7023a5e

File tree

11 files changed

+457
-1
lines changed

11 files changed

+457
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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\VaultGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Vault\Api\PaymentTokenManagementInterface;
16+
use Magento\Vault\Api\PaymentTokenRepositoryInterface;
17+
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
18+
19+
/**
20+
* Delete Payment Token resolver, used for GraphQL mutation processing.
21+
*/
22+
class DeletePaymentToken implements ResolverInterface
23+
{
24+
/**
25+
* @var CheckCustomerAccount
26+
*/
27+
private $checkCustomerAccount;
28+
29+
/**
30+
* @var PaymentTokenManagementInterface
31+
*/
32+
private $paymentTokenManagement;
33+
34+
/**
35+
* @var PaymentTokenRepositoryInterface
36+
*/
37+
private $paymentTokenRepository;
38+
39+
/**
40+
* @param CheckCustomerAccount $checkCustomerAccount
41+
* @param PaymentTokenManagementInterface $paymentTokenManagement
42+
* @param PaymentTokenRepositoryInterface $paymentTokenRepository
43+
*/
44+
public function __construct(
45+
CheckCustomerAccount $checkCustomerAccount,
46+
PaymentTokenManagementInterface $paymentTokenManagement,
47+
PaymentTokenRepositoryInterface $paymentTokenRepository
48+
) {
49+
$this->checkCustomerAccount = $checkCustomerAccount;
50+
$this->paymentTokenManagement = $paymentTokenManagement;
51+
$this->paymentTokenRepository = $paymentTokenRepository;
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+
if (!isset($args['public_hash'])) {
65+
throw new GraphQlInputException(__('Specify the "public_hash" value.'));
66+
}
67+
68+
$currentUserId = $context->getUserId();
69+
$currentUserType = $context->getUserType();
70+
71+
$this->checkCustomerAccount->execute($currentUserId, $currentUserType);
72+
73+
$token = $this->paymentTokenManagement->getByPublicHash($args['public_hash'], $currentUserId);
74+
if (!$token) {
75+
throw new GraphQlNoSuchEntityException(
76+
__('Could not find a token using public hash: %1', $args['public_hash'])
77+
);
78+
}
79+
80+
return ['result' => $this->paymentTokenRepository->delete($token)];
81+
}
82+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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\VaultGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Query\ResolverInterface;
12+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13+
use Magento\Vault\Model\PaymentTokenManagement;
14+
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
15+
16+
/**
17+
* Customers Payment Tokens resolver, used for GraphQL request processing.
18+
*/
19+
class PaymentTokens implements ResolverInterface
20+
{
21+
/**
22+
* @var PaymentTokenManagement
23+
*/
24+
private $paymentTokenManagement;
25+
26+
/**
27+
* @var CheckCustomerAccount
28+
*/
29+
private $checkCustomerAccount;
30+
31+
/**
32+
* @param PaymentTokenManagement $paymentTokenManagement
33+
* @param CheckCustomerAccount $checkCustomerAccount
34+
*/
35+
public function __construct(
36+
PaymentTokenManagement $paymentTokenManagement,
37+
CheckCustomerAccount $checkCustomerAccount
38+
) {
39+
$this->paymentTokenManagement = $paymentTokenManagement;
40+
$this->checkCustomerAccount = $checkCustomerAccount;
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function resolve(
47+
Field $field,
48+
$context,
49+
ResolveInfo $info,
50+
array $value = null,
51+
array $args = null
52+
) {
53+
$currentUserId = $context->getUserId();
54+
$currentUserType = $context->getUserType();
55+
56+
$this->checkCustomerAccount->execute($currentUserId, $currentUserType);
57+
58+
$tokens = $this->paymentTokenManagement->getVisibleAvailableTokens($currentUserId);
59+
$result = [];
60+
61+
foreach ($tokens as $token) {
62+
$result[] = [
63+
'public_hash' => $token->getPublicHash(),
64+
'payment_method_code' => $token->getPaymentMethodCode(),
65+
'type' => $token->getType(),
66+
'details' => $token->getTokenDetails(),
67+
];
68+
}
69+
70+
return ['items' => $result];
71+
}
72+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# VaultGraphQl
2+
3+
**VaultGraphQl** provides type and resolver information for the GraphQl module
4+
to generate Vault (stored payment information) information endpoints. This module also
5+
provides mutations for modifying a payment token.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "magento/module-vault-graph-ql",
3+
"description": "N/A",
4+
"type": "magento2-module",
5+
"require": {
6+
"php": "~7.1.3||~7.2.0",
7+
"magento/framework": "*",
8+
"magento/module-vault": "*",
9+
"magento/module-customer-graph-ql": "*"
10+
},
11+
"license": [
12+
"OSL-3.0",
13+
"AFL-3.0"
14+
],
15+
"autoload": {
16+
"files": [
17+
"registration.php"
18+
],
19+
"psr-4": {
20+
"Magento\\VaultGraphQl\\": ""
21+
}
22+
}
23+
}
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_VaultGraphQl"/>
10+
</config>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
type Mutation {
5+
deletePaymentToken(public_hash: String!): DeletePaymentTokenOutput @resolver(class: "\\Magento\\VaultGraphQl\\Model\\Resolver\\DeletePaymentToken") @doc(description:"Delete a customer payment token")
6+
}
7+
8+
type DeletePaymentTokenOutput {
9+
result: Boolean!
10+
customerPaymentTokens: CustomerPaymentTokens @resolver(class: "\\Magento\\VaultGraphQl\\Model\\Resolver\\PaymentTokens")
11+
}
12+
13+
type Query {
14+
customerPaymentTokens: CustomerPaymentTokens @doc(description: "Return a list of customer payment tokens") @resolver(class: "\\Magento\\VaultGraphQl\\Model\\Resolver\\PaymentTokens")
15+
}
16+
17+
type CustomerPaymentTokens @resolver(class: "\\Magento\\VaultGraphQl\\Model\\Resolver\\PaymentTokens") {
18+
items: [PaymentToken]! @doc(description: "An array of payment tokens")
19+
}
20+
21+
type PaymentToken @doc(description: "The stored payment method available to the customer") {
22+
public_hash: String! @doc(description: "The public hash of the token")
23+
payment_method_code: String! @doc(description: "The payment method code associated with the token")
24+
type: PaymentTokenTypeEnum!
25+
details: String @doc(description: "Stored account details")
26+
}
27+
28+
enum PaymentTokenTypeEnum @doc(description: "The list of available payment token types") {
29+
card
30+
account
31+
}
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_VaultGraphQl', __DIR__);

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@
236236
"magento/module-usps": "*",
237237
"magento/module-variable": "*",
238238
"magento/module-vault": "*",
239+
"magento/module-vault-graph-ql": "*",
239240
"magento/module-version": "*",
240241
"magento/module-webapi": "*",
241242
"magento/module-webapi-async": "*",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)