diff --git a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php index a3afeb05b16e0..e3dfe5e6db07b 100644 --- a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php +++ b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php @@ -7,10 +7,12 @@ namespace Magento\VaultGraphQl\Model\Resolver; +use Magento\Framework\App\ObjectManager; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\Framework\Serialize\Serializer\Json as JsonSerializer; use Magento\GraphQl\Model\Query\ContextInterface; use Magento\Vault\Model\PaymentTokenManagement; @@ -19,18 +21,44 @@ */ class PaymentTokens implements ResolverInterface { + /** + * Cart types + */ + private const BASE_CART_TYPES = [ + 'VI' => 'Visa', + 'MC' => 'MasterCard', + 'AE' => 'American Express', + 'DN' => 'Diners', + 'DI' => 'Discover', + 'JCB' => 'JCB', + 'UN' => 'UnionPay', + 'MI' => 'Maestro International', + 'MD' => 'Maestro Domestic', + 'HC' => 'Hipercard', + 'ELO' => 'Elo', + 'AU' => 'Aura' + ]; + /** * @var PaymentTokenManagement */ private $paymentTokenManagement; + /** + * @var JsonSerializer + */ + private $serializer; + /** * @param PaymentTokenManagement $paymentTokenManagement + * @param JsonSerializer|null $serializer */ public function __construct( - PaymentTokenManagement $paymentTokenManagement + PaymentTokenManagement $paymentTokenManagement, + JsonSerializer $serializer = null ) { $this->paymentTokenManagement = $paymentTokenManagement; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(JsonSerializer::class); } /** @@ -56,9 +84,33 @@ public function resolve( 'public_hash' => $token->getPublicHash(), 'payment_method_code' => $token->getPaymentMethodCode(), 'type' => $token->getType(), - 'details' => $token->getTokenDetails(), + 'details' => $this->getCartDetailsInformation($token->getTokenDetails()), ]; } return ['items' => $result]; } + + /** + * Set full cart type information + * + * @param string|null $tokenDetails + * @return string + */ + private function getCartDetailsInformation(?string $tokenDetails): ?string + { + if ($tokenDetails === null) { + return $tokenDetails; + } + + $cartDetails = $this->serializer->unserialize($tokenDetails); + + foreach ($cartDetails as $key => $value) { + if (array_key_exists($value, self::BASE_CART_TYPES)) { + $cartDetails[$key] = self::BASE_CART_TYPES[$value]; + break; + } + } + + return $this->serializer->serialize($cartDetails); + } } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Vault/CustomerPaymentTokensTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Vault/CustomerPaymentTokensTest.php index 0643529a27b5a..d21069ff8be1a 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Vault/CustomerPaymentTokensTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Vault/CustomerPaymentTokensTest.php @@ -83,7 +83,6 @@ public function testGetCustomerPaymentTokens() } QUERY; $response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword)); - $this->assertCount(2, $response['customerPaymentTokens']['items']); $this->assertArrayHasKey('public_hash', $response['customerPaymentTokens']['items'][0]); $this->assertArrayHasKey('details', $response['customerPaymentTokens']['items'][0]); @@ -91,6 +90,10 @@ public function testGetCustomerPaymentTokens() $this->assertArrayHasKey('type', $response['customerPaymentTokens']['items'][0]); // Validate gateway token is NOT returned $this->assertArrayNotHasKey('gateway_token', $response['customerPaymentTokens']['items'][0]); + $cartDetails1 = json_decode($response['customerPaymentTokens']['items'][0]['details'], true); + $cartDetails2 = json_decode($response['customerPaymentTokens']['items'][1]['details'], true); + $this->assertSame('Visa', $cartDetails1['type']); + $this->assertSame('American Express', $cartDetails2['cc_type']); } /** diff --git a/dev/tests/integration/testsuite/Magento/Vault/_files/payment_tokens.php b/dev/tests/integration/testsuite/Magento/Vault/_files/payment_tokens.php index b9f21b4b1bd5e..c73f5d5121c90 100644 --- a/dev/tests/integration/testsuite/Magento/Vault/_files/payment_tokens.php +++ b/dev/tests/integration/testsuite/Magento/Vault/_files/payment_tokens.php @@ -48,7 +48,8 @@ 'payment_method_code' => 'fifth', 'type' => 'card', 'expires_at' => date('Y-m-d h:i:s', strtotime('+1 month')), - 'is_active' => 1 + 'is_active' => 1, + 'details' => '{"type":"VI","maskedCC":"1117","expirationDate":"11/2023"}' ], [ 'customer_id' => 1, @@ -56,7 +57,8 @@ 'payment_method_code' => 'sixth', 'type' => 'account', 'expires_at' => date('Y-m-d h:i:s', strtotime('+1 month')), - 'is_active' => 1 + 'is_active' => 1, + 'details' => '{"cc_type":"AE","cc_exp_year":2023,"cc_exp_month":3,"cc_last_4":"8431"}' ], ]; /** @var array $tokenData */