Skip to content

Commit fb4b738

Browse files
authored
fix: PaymentToken child types fixed. (#739)
1 parent ca50a90 commit fb4b738

File tree

2 files changed

+97
-11
lines changed

2 files changed

+97
-11
lines changed

includes/type/object/class-customer-type.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,54 @@ public static function register() {
224224
/**
225225
* Register "availablePaymentMethods" field to "Customer" type.
226226
*/
227-
register_graphql_field(
227+
register_graphql_fields(
228228
'Customer',
229-
'availablePaymentMethods',
230229
[
231-
'type' => [ 'list_of' => 'PaymentToken' ],
232-
'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ),
233-
'resolve' => function( $source ) {
234-
if ( get_current_user_id() === $source->ID ) {
235-
return array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) );
236-
}
237-
238-
throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) );
239-
},
230+
'availablePaymentMethods' => [
231+
'type' => [ 'list_of' => 'PaymentToken' ],
232+
'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ),
233+
'resolve' => function( $source ) {
234+
if ( get_current_user_id() === $source->ID ) {
235+
return array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) );
236+
}
237+
238+
throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) );
239+
},
240+
],
241+
'availablePaymentMethodsCC' => [
242+
'type' => [ 'list_of' => 'PaymentTokenCC' ],
243+
'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ),
244+
'resolve' => function( $source ) {
245+
if ( get_current_user_id() === $source->ID ) {
246+
$tokens = array_filter(
247+
array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) ),
248+
function ( $token ) {
249+
return 'CC' === $token->get_type();
250+
}
251+
);
252+
return $tokens;
253+
}
254+
255+
throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) );
256+
},
257+
],
258+
'availablePaymentMethodsEC' => [
259+
'type' => [ 'list_of' => 'PaymentTokenECheck' ],
260+
'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ),
261+
'resolve' => function( $source ) {
262+
if ( get_current_user_id() === $source->ID ) {
263+
$tokens = array_filter(
264+
array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) ),
265+
function ( $token ) {
266+
return 'eCheck' === $token->get_type();
267+
}
268+
);
269+
return $tokens;
270+
}
271+
272+
throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) );
273+
},
274+
],
240275
]
241276
);
242277
}

tests/wpunit/CustomerQueriesTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,19 @@ public function testCustomerAvailablePaymentMethodsField() {
505505
last4
506506
}
507507
}
508+
availablePaymentMethodsCC {
509+
id
510+
tokenId
511+
last4
512+
expiryMonth
513+
expiryYear
514+
cardType
515+
}
516+
availablePaymentMethodsEC {
517+
id
518+
tokenId
519+
last4
520+
}
508521
}
509522
}
510523
';
@@ -546,6 +559,28 @@ public function testCustomerAvailablePaymentMethodsField() {
546559
$this->expectedField( 'cardType', 'visa' ),
547560
]
548561
),
562+
$this->expectedNode(
563+
'customer.availablePaymentMethodsCC',
564+
[
565+
$this->expectedField( 'id', $this->toRelayId( 'token', $token_cc->get_id() ) ),
566+
$this->expectedField( 'tokenId', $token_cc->get_id() ),
567+
$this->expectedField( 'last4', 1234 ),
568+
$this->expectedField( 'expiryMonth', $expiry_month ),
569+
$this->expectedField( 'expiryYear', $expiry_year ),
570+
$this->expectedField( 'cardType', 'visa' ),
571+
]
572+
),
573+
$this->expectedNode(
574+
'customer.availablePaymentMethodsEC',
575+
[
576+
$this->not()->expectedField( 'id', $this->toRelayId( 'token', $token_cc->get_id() ) ),
577+
$this->not()->expectedField( 'tokenId', $token_cc->get_id() ),
578+
$this->not()->expectedField( 'last4', 1234 ),
579+
$this->not()->expectedField( 'expiryMonth', $expiry_month ),
580+
$this->not()->expectedField( 'expiryYear', $expiry_year ),
581+
$this->not()->expectedField( 'cardType', 'visa' ),
582+
]
583+
),
549584
$this->expectedNode(
550585
'customer.availablePaymentMethods',
551586
[
@@ -554,6 +589,22 @@ public function testCustomerAvailablePaymentMethodsField() {
554589
$this->expectedField( 'last4', 4567 ),
555590
]
556591
),
592+
$this->expectedNode(
593+
'customer.availablePaymentMethodsCC',
594+
[
595+
$this->not()->expectedField( 'id', $this->toRelayId( 'token', $token_ec->get_id() ) ),
596+
$this->not()->expectedField( 'tokenId', $token_ec->get_id() ),
597+
$this->not()->expectedField( 'last4', 4567 ),
598+
]
599+
),
600+
$this->expectedNode(
601+
'customer.availablePaymentMethodsEC',
602+
[
603+
$this->expectedField( 'id', $this->toRelayId( 'token', $token_ec->get_id() ) ),
604+
$this->expectedField( 'tokenId', $token_ec->get_id() ),
605+
$this->expectedField( 'last4', 4567 ),
606+
]
607+
),
557608
];
558609

559610
$this->assertQuerySuccessful( $response, $expected );

0 commit comments

Comments
 (0)