Skip to content

Commit e95b3c0

Browse files
committed
Return full cart type name
1 parent 9a68d68 commit e95b3c0

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php

+55-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
1415
use Magento\GraphQl\Model\Query\ContextInterface;
1516
use Magento\Vault\Model\PaymentTokenManagement;
1617

@@ -19,18 +20,44 @@
1920
*/
2021
class PaymentTokens implements ResolverInterface
2122
{
23+
/**
24+
* Cart types
25+
*/
26+
const BASE_CART_TYPES = [
27+
'VI' => 'Visa',
28+
'MC' => 'MasterCard',
29+
'AE' => 'American Express',
30+
'DN' => 'Diners',
31+
'DI' => 'Discover',
32+
'JCB' => 'JCB',
33+
'UN' => 'UnionPay',
34+
'MI' => 'Maestro International',
35+
'MD' => 'Maestro Domestic',
36+
'HC' => 'Hipercard',
37+
'ELO' => 'Elo',
38+
'AU' => 'Aura'
39+
];
40+
2241
/**
2342
* @var PaymentTokenManagement
2443
*/
2544
private $paymentTokenManagement;
2645

46+
/**
47+
* @var JsonSerializer
48+
*/
49+
private $serializer;
50+
2751
/**
2852
* @param PaymentTokenManagement $paymentTokenManagement
53+
* @param JsonSerializer $serializer
2954
*/
3055
public function __construct(
31-
PaymentTokenManagement $paymentTokenManagement
56+
PaymentTokenManagement $paymentTokenManagement,
57+
JsonSerializer $serializer
3258
) {
3359
$this->paymentTokenManagement = $paymentTokenManagement;
60+
$this->serializer = $serializer;
3461
}
3562

3663
/**
@@ -56,9 +83,35 @@ public function resolve(
5683
'public_hash' => $token->getPublicHash(),
5784
'payment_method_code' => $token->getPaymentMethodCode(),
5885
'type' => $token->getType(),
59-
'details' => $token->getTokenDetails(),
86+
'details' => $this->getCartDetailsInformation($token->getTokenDetails()),
6087
];
6188
}
6289
return ['items' => $result];
6390
}
91+
92+
/**
93+
* Set full cart type information
94+
*
95+
* @param string|null $tokenDetails
96+
* @return string
97+
*/
98+
private function getCartDetailsInformation(?string $tokenDetails): ?string
99+
{
100+
if (is_null($tokenDetails)) {
101+
return $tokenDetails;
102+
}
103+
104+
$cartDetails = $this->serializer->unserialize($tokenDetails);
105+
if (!isset($cartDetails['cc_type'])) {
106+
return $cartDetails;
107+
}
108+
109+
$ccCartType = $cartDetails['cc_type'];
110+
111+
if (array_key_exists($ccCartType, self::BASE_CART_TYPES)) {
112+
$cartDetails['cc_type'] = self::BASE_CART_TYPES[$ccCartType];
113+
}
114+
115+
return $this->serializer->serialize($cartDetails);
116+
}
64117
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Vault/CustomerPaymentTokensTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,17 @@ public function testGetCustomerPaymentTokens()
8383
}
8484
QUERY;
8585
$response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
86-
8786
$this->assertCount(2, $response['customerPaymentTokens']['items']);
8887
$this->assertArrayHasKey('public_hash', $response['customerPaymentTokens']['items'][0]);
8988
$this->assertArrayHasKey('details', $response['customerPaymentTokens']['items'][0]);
9089
$this->assertArrayHasKey('payment_method_code', $response['customerPaymentTokens']['items'][0]);
9190
$this->assertArrayHasKey('type', $response['customerPaymentTokens']['items'][0]);
9291
// Validate gateway token is NOT returned
9392
$this->assertArrayNotHasKey('gateway_token', $response['customerPaymentTokens']['items'][0]);
93+
$cartDetails1 = json_decode($response['customerPaymentTokens']['items'][0]['details'], true);
94+
$cartDetails2 = json_decode($response['customerPaymentTokens']['items'][1]['details'], true);
95+
$this->assertSame('Visa', $cartDetails1['cc_type']);
96+
$this->assertSame('American Express', $cartDetails2['cc_type']);
9497
}
9598

9699
/**

dev/tests/integration/testsuite/Magento/Vault/_files/payment_tokens.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@
4848
'payment_method_code' => 'fifth',
4949
'type' => 'card',
5050
'expires_at' => date('Y-m-d h:i:s', strtotime('+1 month')),
51-
'is_active' => 1
51+
'is_active' => 1,
52+
'details' => '{"cc_type":"VI","cc_exp_year":2023,"cc_exp_month":3,"cc_last_4":"8431"}'
5253
],
5354
[
5455
'customer_id' => 1,
5556
'public_hash' => '345678',
5657
'payment_method_code' => 'sixth',
5758
'type' => 'account',
5859
'expires_at' => date('Y-m-d h:i:s', strtotime('+1 month')),
59-
'is_active' => 1
60+
'is_active' => 1,
61+
'details' => '{"cc_type":"AE","cc_exp_year":2023,"cc_exp_month":3,"cc_last_4":"8431"}'
6062
],
6163
];
6264
/** @var array $tokenData */

0 commit comments

Comments
 (0)