Skip to content

Commit

Permalink
fix: PaymentToken child types fixed. (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 authored Apr 21, 2023
1 parent ca50a90 commit fb4b738
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 11 deletions.
57 changes: 46 additions & 11 deletions includes/type/object/class-customer-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,19 +224,54 @@ public static function register() {
/**
* 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' ) );
},
],
]
);
}
Expand Down
51 changes: 51 additions & 0 deletions tests/wpunit/CustomerQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,19 @@ public function testCustomerAvailablePaymentMethodsField() {
last4
}
}
availablePaymentMethodsCC {
id
tokenId
last4
expiryMonth
expiryYear
cardType
}
availablePaymentMethodsEC {
id
tokenId
last4
}
}
}
';
Expand Down Expand Up @@ -546,6 +559,28 @@ public function testCustomerAvailablePaymentMethodsField() {
$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',
[
Expand All @@ -554,6 +589,22 @@ public function testCustomerAvailablePaymentMethodsField() {
$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 );
Expand Down

0 comments on commit fb4b738

Please sign in to comment.