mirror of
https://github.com/wp-graphql/wp-graphql-woocommerce.git
synced 2026-08-14 12:53:44 +02:00
fix: PaymentToken child types fixed. (#739)
This commit is contained in:
@@ -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' ) );
|
||||
},
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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 );
|
||||
|
||||
Reference in New Issue
Block a user