From fb4b73850cecccc4316cfbe66c650f94168b76d8 Mon Sep 17 00:00:00 2001 From: Geoffrey K Taylor Date: Thu, 20 Apr 2023 21:04:06 -0400 Subject: [PATCH] fix: PaymentToken child types fixed. (#739) --- includes/type/object/class-customer-type.php | 57 ++++++++++++++++---- tests/wpunit/CustomerQueriesTest.php | 51 ++++++++++++++++++ 2 files changed, 97 insertions(+), 11 deletions(-) diff --git a/includes/type/object/class-customer-type.php b/includes/type/object/class-customer-type.php index bb1271bb..f3414605 100644 --- a/includes/type/object/class-customer-type.php +++ b/includes/type/object/class-customer-type.php @@ -224,19 +224,54 @@ class Customer_Type { /** * Register "availablePaymentMethods" field to "Customer" type. */ - register_graphql_field( + register_graphql_fields( 'Customer', - 'availablePaymentMethods', [ - 'type' => [ 'list_of' => 'PaymentToken' ], - 'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ), - 'resolve' => function( $source ) { - if ( get_current_user_id() === $source->ID ) { - return array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) ); - } - - throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) ); - }, + 'availablePaymentMethods' => [ + 'type' => [ 'list_of' => 'PaymentToken' ], + 'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ), + 'resolve' => function( $source ) { + if ( get_current_user_id() === $source->ID ) { + return array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) ); + } + + throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) ); + }, + ], + 'availablePaymentMethodsCC' => [ + 'type' => [ 'list_of' => 'PaymentTokenCC' ], + 'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ), + 'resolve' => function( $source ) { + if ( get_current_user_id() === $source->ID ) { + $tokens = array_filter( + array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) ), + function ( $token ) { + return 'CC' === $token->get_type(); + } + ); + return $tokens; + } + + throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) ); + }, + ], + 'availablePaymentMethodsEC' => [ + 'type' => [ 'list_of' => 'PaymentTokenECheck' ], + 'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ), + 'resolve' => function( $source ) { + if ( get_current_user_id() === $source->ID ) { + $tokens = array_filter( + array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) ), + function ( $token ) { + return 'eCheck' === $token->get_type(); + } + ); + return $tokens; + } + + throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) ); + }, + ], ] ); } diff --git a/tests/wpunit/CustomerQueriesTest.php b/tests/wpunit/CustomerQueriesTest.php index 72da327d..388793f5 100644 --- a/tests/wpunit/CustomerQueriesTest.php +++ b/tests/wpunit/CustomerQueriesTest.php @@ -505,6 +505,19 @@ class CustomerQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraph last4 } } + availablePaymentMethodsCC { + id + tokenId + last4 + expiryMonth + expiryYear + cardType + } + availablePaymentMethodsEC { + id + tokenId + last4 + } } } '; @@ -546,6 +559,28 @@ class CustomerQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraph $this->expectedField( 'cardType', 'visa' ), ] ), + $this->expectedNode( + 'customer.availablePaymentMethodsCC', + [ + $this->expectedField( 'id', $this->toRelayId( 'token', $token_cc->get_id() ) ), + $this->expectedField( 'tokenId', $token_cc->get_id() ), + $this->expectedField( 'last4', 1234 ), + $this->expectedField( 'expiryMonth', $expiry_month ), + $this->expectedField( 'expiryYear', $expiry_year ), + $this->expectedField( 'cardType', 'visa' ), + ] + ), + $this->expectedNode( + 'customer.availablePaymentMethodsEC', + [ + $this->not()->expectedField( 'id', $this->toRelayId( 'token', $token_cc->get_id() ) ), + $this->not()->expectedField( 'tokenId', $token_cc->get_id() ), + $this->not()->expectedField( 'last4', 1234 ), + $this->not()->expectedField( 'expiryMonth', $expiry_month ), + $this->not()->expectedField( 'expiryYear', $expiry_year ), + $this->not()->expectedField( 'cardType', 'visa' ), + ] + ), $this->expectedNode( 'customer.availablePaymentMethods', [ @@ -554,6 +589,22 @@ class CustomerQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraph $this->expectedField( 'last4', 4567 ), ] ), + $this->expectedNode( + 'customer.availablePaymentMethodsCC', + [ + $this->not()->expectedField( 'id', $this->toRelayId( 'token', $token_ec->get_id() ) ), + $this->not()->expectedField( 'tokenId', $token_ec->get_id() ), + $this->not()->expectedField( 'last4', 4567 ), + ] + ), + $this->expectedNode( + 'customer.availablePaymentMethodsEC', + [ + $this->expectedField( 'id', $this->toRelayId( 'token', $token_ec->get_id() ) ), + $this->expectedField( 'tokenId', $token_ec->get_id() ), + $this->expectedField( 'last4', 4567 ), + ] + ), ]; $this->assertQuerySuccessful( $response, $expected );