From e95b3c0286d289efcc61a0be05593f34f27ca898 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 14 Apr 2021 16:19:47 +0300 Subject: [PATCH 1/8] Return full cart type name --- .../Model/Resolver/PaymentTokens.php | 57 ++++++++++++++++++- .../Vault/CustomerPaymentTokensTest.php | 5 +- .../Magento/Vault/_files/payment_tokens.php | 6 +- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php index a3afeb05b16e0..1fe6ee4bc0186 100644 --- a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php +++ b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php @@ -11,6 +11,7 @@ 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 +20,44 @@ */ class PaymentTokens implements ResolverInterface { + /** + * Cart types + */ + 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 $serializer */ public function __construct( - PaymentTokenManagement $paymentTokenManagement + PaymentTokenManagement $paymentTokenManagement, + JsonSerializer $serializer ) { $this->paymentTokenManagement = $paymentTokenManagement; + $this->serializer = $serializer; } /** @@ -56,9 +83,35 @@ 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 (is_null($tokenDetails)) { + return $tokenDetails; + } + + $cartDetails = $this->serializer->unserialize($tokenDetails); + if (!isset($cartDetails['cc_type'])) { + return $cartDetails; + } + + $ccCartType = $cartDetails['cc_type']; + + if (array_key_exists($ccCartType, self::BASE_CART_TYPES)) { + $cartDetails['cc_type'] = self::BASE_CART_TYPES[$ccCartType]; + } + + 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..e8967ea73d835 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['cc_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..3eb8622d10a46 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' => '{"cc_type":"VI","cc_exp_year":2023,"cc_exp_month":3,"cc_last_4":"8431"}' ], [ '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 */ From ae2b9db900b61039859bbc752b695df85f95e4fd Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 14 Apr 2021 17:06:22 +0300 Subject: [PATCH 2/8] Added compatibility for payflowpro and braintree --- .../VaultGraphQl/Model/Resolver/PaymentTokens.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php index 1fe6ee4bc0186..56b9bfce2ab0e 100644 --- a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php +++ b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php @@ -102,14 +102,11 @@ private function getCartDetailsInformation(?string $tokenDetails): ?string } $cartDetails = $this->serializer->unserialize($tokenDetails); - if (!isset($cartDetails['cc_type'])) { - return $cartDetails; - } - - $ccCartType = $cartDetails['cc_type']; - if (array_key_exists($ccCartType, self::BASE_CART_TYPES)) { - $cartDetails['cc_type'] = self::BASE_CART_TYPES[$ccCartType]; + foreach ($cartDetails as $key => $value) { + if (array_key_exists($value, self::BASE_CART_TYPES)) { + $cartDetails[$key] = self::BASE_CART_TYPES[$value]; + } } return $this->serializer->serialize($cartDetails); From 6f17874338cd3d853b678ada1e0cc044d5c7bd97 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 14 Apr 2021 17:09:35 +0300 Subject: [PATCH 3/8] minor test adjustments --- .../Magento/GraphQl/Vault/CustomerPaymentTokensTest.php | 2 +- .../testsuite/Magento/Vault/_files/payment_tokens.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 e8967ea73d835..d21069ff8be1a 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Vault/CustomerPaymentTokensTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Vault/CustomerPaymentTokensTest.php @@ -92,7 +92,7 @@ public function testGetCustomerPaymentTokens() $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['cc_type']); + $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 3eb8622d10a46..c73f5d5121c90 100644 --- a/dev/tests/integration/testsuite/Magento/Vault/_files/payment_tokens.php +++ b/dev/tests/integration/testsuite/Magento/Vault/_files/payment_tokens.php @@ -49,7 +49,7 @@ 'type' => 'card', 'expires_at' => date('Y-m-d h:i:s', strtotime('+1 month')), 'is_active' => 1, - 'details' => '{"cc_type":"VI","cc_exp_year":2023,"cc_exp_month":3,"cc_last_4":"8431"}' + 'details' => '{"type":"VI","maskedCC":"1117","expirationDate":"11/2023"}' ], [ 'customer_id' => 1, From 418e928b92eb3e557aac31e3f147fbc62a6cb77f Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Wed, 14 Apr 2021 17:19:01 +0300 Subject: [PATCH 4/8] minor improvement --- app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php index 56b9bfce2ab0e..09f7207d675a4 100644 --- a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php +++ b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php @@ -106,6 +106,7 @@ private function getCartDetailsInformation(?string $tokenDetails): ?string foreach ($cartDetails as $key => $value) { if (array_key_exists($value, self::BASE_CART_TYPES)) { $cartDetails[$key] = self::BASE_CART_TYPES[$value]; + break; } } From 6bd1911a10c8ecb33561575ca1dba3c4d09df1c0 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Mon, 26 Apr 2021 15:19:23 +0300 Subject: [PATCH 5/8] Update app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php Co-authored-by: Dmytro Cheshun --- app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php index 09f7207d675a4..9ade3a06be923 100644 --- a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php +++ b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php @@ -54,7 +54,7 @@ class PaymentTokens implements ResolverInterface */ public function __construct( PaymentTokenManagement $paymentTokenManagement, - JsonSerializer $serializer + JsonSerializer $serializer = null ) { $this->paymentTokenManagement = $paymentTokenManagement; $this->serializer = $serializer; From a72ad77b45680f1be1aa88adee8acc35063c09f0 Mon Sep 17 00:00:00 2001 From: Oleh Usik Date: Mon, 26 Apr 2021 15:25:35 +0300 Subject: [PATCH 6/8] added backward compatible --- .../Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php index 9ade3a06be923..80b4483ebcc34 100644 --- a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php +++ b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php @@ -7,6 +7,7 @@ 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; @@ -50,14 +51,14 @@ class PaymentTokens implements ResolverInterface /** * @param PaymentTokenManagement $paymentTokenManagement - * @param JsonSerializer $serializer + * @param JsonSerializer|null $serializer */ public function __construct( PaymentTokenManagement $paymentTokenManagement, JsonSerializer $serializer = null ) { $this->paymentTokenManagement = $paymentTokenManagement; - $this->serializer = $serializer; + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(JsonSerializer::class); } /** From ad14a28c814c8a9accb404158b8f897d15614e6f Mon Sep 17 00:00:00 2001 From: Indrani Sonawane Date: Thu, 4 Jul 2024 19:39:33 +0530 Subject: [PATCH 7/8] Fixed the static test failures --- .../Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php | 4 ++-- .../Magento/GraphQl/Vault/CustomerPaymentTokensTest.php | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php index 80b4483ebcc34..e3dfe5e6db07b 100644 --- a/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php +++ b/app/code/Magento/VaultGraphQl/Model/Resolver/PaymentTokens.php @@ -24,7 +24,7 @@ class PaymentTokens implements ResolverInterface /** * Cart types */ - const BASE_CART_TYPES = [ + private const BASE_CART_TYPES = [ 'VI' => 'Visa', 'MC' => 'MasterCard', 'AE' => 'American Express', @@ -98,7 +98,7 @@ public function resolve( */ private function getCartDetailsInformation(?string $tokenDetails): ?string { - if (is_null($tokenDetails)) { + if ($tokenDetails === null) { return $tokenDetails; } 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 d21069ff8be1a..c02213b459f27 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Vault/CustomerPaymentTokensTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Vault/CustomerPaymentTokensTest.php @@ -190,7 +190,8 @@ public function testDeletePaymentTokenIfUserIsNotAuthorized() public function testDeletePaymentTokenInvalidPublicHash() { $this->expectException(\Exception::class); - $this->expectExceptionMessage('GraphQL response contains errors: Could not find a token using public hash: ksdfk392ks'); + $this->expectExceptionMessage('GraphQL response contains errors: + Could not find a token using public hash: ksdfk392ks'); $currentEmail = 'customer@example.com'; $currentPassword = 'password'; From b9bba14493e5c2007fe7ecc71b02999c9a600a95 Mon Sep 17 00:00:00 2001 From: engcom-Dash Date: Wed, 11 Sep 2024 15:19:47 +0530 Subject: [PATCH 8/8] 32744: revert unwanted changes --- .../Magento/GraphQl/Vault/CustomerPaymentTokensTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 c02213b459f27..d21069ff8be1a 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Vault/CustomerPaymentTokensTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Vault/CustomerPaymentTokensTest.php @@ -190,8 +190,7 @@ public function testDeletePaymentTokenIfUserIsNotAuthorized() public function testDeletePaymentTokenInvalidPublicHash() { $this->expectException(\Exception::class); - $this->expectExceptionMessage('GraphQL response contains errors: - Could not find a token using public hash: ksdfk392ks'); + $this->expectExceptionMessage('GraphQL response contains errors: Could not find a token using public hash: ksdfk392ks'); $currentEmail = 'customer@example.com'; $currentPassword = 'password';