• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql-woocommerce / 6414965373

05 Oct 2023 05:21AM UTC coverage: 84.777% (-0.02%) from 84.793%
6414965373

push

github

web-flow
fix: paymentMethod fields no longer throw for guest users (#809)

* fix: paymentMethod fields no longer throw for guest users

* chore: CollectionStatsQueryTest reverted

6 of 6 new or added lines in 1 file covered. (100.0%)

11032 of 13013 relevant lines covered (84.78%)

58.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.89
/includes/type/object/class-customer-type.php
1
<?php
2
/**
3
 * WPObject Type - Customer_Type
4
 *
5
 * Registers WPObject type for WooCommerce customers
6
 *
7
 * @package WPGraphQL\WooCommerce\Type\WPObject
8
 * @since   0.0.1
9
 */
10

11
namespace WPGraphQL\WooCommerce\Type\WPObject;
12

13
use GraphQL\Error\UserError;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use WPGraphQL\AppContext;
16
use WPGraphQL\WooCommerce\Data\Connection\Downloadable_Item_Connection_Resolver;
17
use WPGraphQL\WooCommerce\Data\Factory;
18

19
/**
20
 * Class Customer_Type
21
 */
22
class Customer_Type {
23
        /**
24
         * Returns the "Customer" type fields.
25
         *
26
         * @param array $other_fields Extra fields configs to be added or override the default field definitions.
27
         *
28
         * @return array
29
         */
30
        public static function get_fields( $other_fields = [] ) {
31
                return array_merge(
114✔
32
                        [
114✔
33
                                'id'                    => [
114✔
34
                                        'type'        => [ 'non_null' => 'ID' ],
114✔
35
                                        'description' => __( 'The globally unique identifier for the customer', 'wp-graphql-woocommerce' ),
114✔
36
                                ],
114✔
37
                                'databaseId'            => [
114✔
38
                                        'type'        => 'Int',
114✔
39
                                        'description' => __( 'The ID of the customer in the database', 'wp-graphql-woocommerce' ),
114✔
40
                                        'resolve'     => static function ( $source ) {
114✔
41
                                                $database_id = absint( $source->ID );
12✔
42
                                                return ! empty( $database_id ) ? $database_id : null;
12✔
43
                                        },
114✔
44
                                ],
114✔
45
                                'isVatExempt'           => [
114✔
46
                                        'type'        => 'Boolean',
114✔
47
                                        'description' => __( 'Is customer VAT exempt?', 'wp-graphql-woocommerce' ),
114✔
48
                                ],
114✔
49
                                'hasCalculatedShipping' => [
114✔
50
                                        'type'        => 'Boolean',
114✔
51
                                        'description' => __( 'Has calculated shipping?', 'wp-graphql-woocommerce' ),
114✔
52
                                ],
114✔
53
                                'calculatedShipping'    => [
114✔
54
                                        'type'        => 'Boolean',
114✔
55
                                        'description' => __( 'Has customer calculated shipping?', 'wp-graphql-woocommerce' ),
114✔
56
                                ],
114✔
57
                                'lastOrder'             => [
114✔
58
                                        'type'        => 'Order',
114✔
59
                                        'description' => __( 'Gets the customers last order.', 'wp-graphql-woocommerce' ),
114✔
60
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
114✔
61
                                                return Factory::resolve_crud_object( $source->last_order_id, $context );
1✔
62
                                        },
114✔
63
                                ],
114✔
64
                                'orderCount'            => [
114✔
65
                                        'type'        => 'Int',
114✔
66
                                        'description' => __( 'Return the number of orders this customer has.', 'wp-graphql-woocommerce' ),
114✔
67
                                ],
114✔
68
                                'totalSpent'            => [
114✔
69
                                        'type'        => 'Float',
114✔
70
                                        'description' => __( 'Return how much money this customer has spent.', 'wp-graphql-woocommerce' ),
114✔
71
                                ],
114✔
72
                                'username'              => [
114✔
73
                                        'type'        => 'String',
114✔
74
                                        'description' => __( 'Return the customer\'s username.', 'wp-graphql-woocommerce' ),
114✔
75
                                ],
114✔
76
                                'email'                 => [
114✔
77
                                        'type'        => 'String',
114✔
78
                                        'description' => __( 'Return the customer\'s email.', 'wp-graphql-woocommerce' ),
114✔
79
                                ],
114✔
80
                                'firstName'             => [
114✔
81
                                        'type'        => 'String',
114✔
82
                                        'description' => __( 'Return the customer\'s first name.', 'wp-graphql-woocommerce' ),
114✔
83
                                ],
114✔
84
                                'lastName'              => [
114✔
85
                                        'type'        => 'String',
114✔
86
                                        'description' => __( 'Return the customer\'s last name.', 'wp-graphql-woocommerce' ),
114✔
87
                                ],
114✔
88
                                'displayName'           => [
114✔
89
                                        'type'        => 'String',
114✔
90
                                        'description' => __( 'Return the customer\'s display name.', 'wp-graphql-woocommerce' ),
114✔
91
                                ],
114✔
92
                                'role'                  => [
114✔
93
                                        'type'        => 'String',
114✔
94
                                        'description' => __( 'Return the customer\'s user role.', 'wp-graphql-woocommerce' ),
114✔
95
                                ],
114✔
96
                                'date'                  => [
114✔
97
                                        'type'        => 'String',
114✔
98
                                        'description' => __( 'Return the date customer was created', 'wp-graphql-woocommerce' ),
114✔
99
                                ],
114✔
100
                                'modified'              => [
114✔
101
                                        'type'        => 'String',
114✔
102
                                        'description' => __( 'Return the date customer was last updated', 'wp-graphql-woocommerce' ),
114✔
103
                                ],
114✔
104
                                'billing'               => [
114✔
105
                                        'type'        => 'CustomerAddress',
114✔
106
                                        'description' => __( 'Return the date customer billing address properties', 'wp-graphql-woocommerce' ),
114✔
107
                                ],
114✔
108
                                'shipping'              => [
114✔
109
                                        'type'        => 'CustomerAddress',
114✔
110
                                        'description' => __( 'Return the date customer shipping address properties', 'wp-graphql-woocommerce' ),
114✔
111
                                ],
114✔
112
                                'isPayingCustomer'      => [
114✔
113
                                        'type'        => 'Boolean',
114✔
114
                                        'description' => __( 'Return the date customer was last updated', 'wp-graphql-woocommerce' ),
114✔
115
                                ],
114✔
116
                                'metaData'              => Meta_Data_Type::get_metadata_field_definition(),
114✔
117
                                'session'               => [
114✔
118
                                        'type'        => [ 'list_of' => 'MetaData' ],
114✔
119
                                        'description' => __( 'Session data for the viewing customer', 'wp-graphql-woocommerce' ),
114✔
120
                                        'resolve'     => static function ( $source ) {
114✔
121
                                                /**
122
                                                 * Session Handler.
123
                                                 *
124
                                                 * @var \WC_Session_Handler $session
125
                                                 */
126
                                                $session = \WC()->session;
1✔
127

128
                                                if ( (string) $session->get_customer_id() === (string) $source->ID ) {
1✔
129
                                                        $session_data = $session->get_session_data();
1✔
130
                                                        $session      = [];
1✔
131
                                                        foreach ( $session_data as $key => $value ) {
1✔
132
                                                                $meta        = new \stdClass();
1✔
133
                                                                $meta->id    = null;
1✔
134
                                                                $meta->key   = $key;
1✔
135
                                                                $meta->value = maybe_unserialize( $value );
1✔
136
                                                                $session[]   = $meta;
1✔
137
                                                        }
138

139
                                                        return $session;
1✔
140
                                                }
141

142
                                                throw new UserError( __( 'It\'s not possible to access another user\'s session data', 'wp-graphql-woocommerce' ) );
×
143
                                        },
114✔
144
                                ],
114✔
145
                        ],
114✔
146
                        $other_fields
114✔
147
                );
114✔
148
        }
149

150
        /**
151
         * Returns the "Customer" type connections.
152
         *
153
         * @param array $other_connections Extra connections configs to be added or override the default connection definitions.
154
         *
155
         * @return array
156
         */
157
        public static function get_connections( $other_connections = [] ) {
158
                return array_merge(
114✔
159
                        [
114✔
160
                                'downloadableItems' => [
114✔
161
                                        'toType'         => 'DownloadableItem',
114✔
162
                                        'connectionArgs' => [
114✔
163
                                                'active'                => [
114✔
164
                                                        'type'        => 'Boolean',
114✔
165
                                                        'description' => __( 'Limit results to downloadable items that can be downloaded now.', 'wp-graphql-woocommerce' ),
114✔
166
                                                ],
114✔
167
                                                'expired'               => [
114✔
168
                                                        'type'        => 'Boolean',
114✔
169
                                                        'description' => __( 'Limit results to downloadable items that are expired.', 'wp-graphql-woocommerce' ),
114✔
170
                                                ],
114✔
171
                                                'hasDownloadsRemaining' => [
114✔
172
                                                        'type'        => 'Boolean',
114✔
173
                                                        'description' => __( 'Limit results to downloadable items that have downloads remaining.', 'wp-graphql-woocommerce' ),
114✔
174
                                                ],
114✔
175
                                        ],
114✔
176
                                        'resolve'        => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
114✔
177
                                                $resolver = new Downloadable_Item_Connection_Resolver( $source, $args, $context, $info );
1✔
178

179
                                                return $resolver->get_connection();
1✔
180
                                        },
114✔
181
                                ],
114✔
182
                        ],
114✔
183
                        $other_connections
114✔
184
                );
114✔
185
        }
186

187
        /**
188
         * Registers Customer WPObject type and related fields.
189
         *
190
         * @return void
191
         */
192
        public static function register() {
193
                register_graphql_object_type(
114✔
194
                        'Customer',
114✔
195
                        [
114✔
196
                                'description' => __( 'A customer object', 'wp-graphql-woocommerce' ),
114✔
197
                                'interfaces'  => [ 'Node' ],
114✔
198
                                /**
199
                                 * Allows for a decisive filtering of the order fields.
200
                                 * Note: Only use if deregisteration or renaming the field(s) has failed.
201
                                 *
202
                                 * @param array $fields  Customer field definitions.
203
                                 * @return array
204
                                 */
205
                                'fields'      => apply_filters( 'woographql_customer_field_definitions', self::get_fields() ),
114✔
206
                                /**
207
                                 * Allows for a decisive filtering of the order connections.
208
                                 * Note: Only use if deregisteration or renaming the connection(s) has failed.
209
                                 *
210
                                 * @param array $connections  Customer connection definitions.
211
                                 * @return array
212
                                 */
213
                                'connections' => apply_filters( 'woographql_customer_connection_definitions', self::get_connections() ),
114✔
214
                        ]
114✔
215
                );
114✔
216

217
                /**
218
                 * Register "availablePaymentMethods" field to "Customer" type.
219
                 */
220
                register_graphql_fields(
114✔
221
                        'Customer',
114✔
222
                        [
114✔
223
                                'availablePaymentMethods'   => [
114✔
224
                                        'type'        => [ 'list_of' => 'PaymentToken' ],
114✔
225
                                        'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ),
114✔
226
                                        'resolve'     => static function ( $source ) {
114✔
227
                                                if ( get_current_user_id() === $source->ID ) {
1✔
228
                                                        return array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) );
1✔
229
                                                }
230

231
                                                if ( get_current_user_id() === 0 ) {
1✔
232
                                                        return [];
×
233
                                                }
234

235
                                                throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) );
1✔
236
                                        },
114✔
237
                                ],
114✔
238
                                'availablePaymentMethodsCC' => [
114✔
239
                                        'type'        => [ 'list_of' => 'PaymentTokenCC' ],
114✔
240
                                        'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ),
114✔
241
                                        'resolve'     => static function ( $source ) {
114✔
242
                                                if ( get_current_user_id() === $source->ID ) {
1✔
243
                                                        return array_filter(
1✔
244
                                                                array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) ),
1✔
245
                                                                static function ( $token ) {
1✔
246
                                                                        return 'CC' === $token->get_type();
1✔
247
                                                                }
1✔
248
                                                        );
1✔
249
                                                }
250

251
                                                if ( get_current_user_id() === 0 ) {
1✔
252
                                                        return [];
×
253
                                                }
254

255
                                                throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) );
1✔
256
                                        },
114✔
257
                                ],
114✔
258
                                'availablePaymentMethodsEC' => [
114✔
259
                                        'type'        => [ 'list_of' => 'PaymentTokenECheck' ],
114✔
260
                                        'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ),
114✔
261
                                        'resolve'     => static function ( $source ) {
114✔
262
                                                if ( get_current_user_id() === $source->ID ) {
1✔
263
                                                        return array_filter(
1✔
264
                                                                array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) ),
1✔
265
                                                                static function ( $token ) {
1✔
266
                                                                        return 'eCheck' === $token->get_type();
1✔
267
                                                                }
1✔
268
                                                        );
1✔
269
                                                }
270

271
                                                if ( get_current_user_id() === 0 ) {
1✔
272
                                                        return [];
×
273
                                                }
274

275
                                                throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) );
1✔
276
                                        },
114✔
277
                                ],
114✔
278
                        ]
114✔
279
                );
114✔
280
        }
281

282
        /**
283
         * Registers fields that require the "QL_Session_Handler" class to work.
284
         *
285
         * @return void
286
         */
287
        public static function register_session_handler_fields() {
288
                /**
289
                 * Register the "sessionToken" field to the "Customer" type.
290
                 */
291
                register_graphql_field(
114✔
292
                        'Customer',
114✔
293
                        'sessionToken',
114✔
294
                        [
114✔
295
                                'type'        => 'String',
114✔
296
                                'description' => __( 'A JWT token that can be used in future requests to for WooCommerce session identification', 'wp-graphql-woocommerce' ),
114✔
297
                                'resolve'     => static function ( $source ) {
114✔
298
                                        if ( \get_current_user_id() === $source->ID || 'guest' === $source->id ) {
×
299
                                                /**
300
                                                 * Session handler.
301
                                                 *
302
                                                 * @var \WPGraphQL\WooCommerce\Utils\QL_Session_Handler $session
303
                                                 */
304
                                                $session = \WC()->session;
×
305

306
                                                return apply_filters( 'graphql_customer_session_token', $session->build_token() );
×
307
                                        }
308

309
                                        return null;
×
310
                                },
114✔
311
                        ]
114✔
312
                );
114✔
313
                /**
314
                 * Register the "wooSessionToken" field to the "User" type.
315
                 */
316
                register_graphql_field(
114✔
317
                        'User',
114✔
318
                        'wooSessionToken',
114✔
319
                        [
114✔
320
                                'type'        => 'String',
114✔
321
                                'description' => __( 'A JWT token that can be used in future requests to for WooCommerce session identification', 'wp-graphql-woocommerce' ),
114✔
322
                                'resolve'     => static function ( $source ) {
114✔
323
                                        if ( \get_current_user_id() === $source->userId ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
×
324
                                                /**
325
                                                 * Session handler
326
                                                 *
327
                                                 * @var \WPGraphQL\WooCommerce\Utils\QL_Session_Handler $session
328
                                                 */
329
                                                $session = \WC()->session;
×
330

331
                                                return apply_filters( 'graphql_customer_session_token', $session->build_token() );
×
332
                                        }
333

334
                                        return null;
×
335
                                },
114✔
336
                        ]
114✔
337
                );
114✔
338
        }
339

340
        /**
341
         * Registers selected authorizing_url_fields
342
         *
343
         * @param array $fields_to_register  Slugs of fields.
344
         * @return void
345
         */
346
        public static function register_authorizing_url_fields( $fields_to_register ) {
347
                if ( in_array( 'cart_url', $fields_to_register, true ) ) {
114✔
348
                        register_graphql_fields(
114✔
349
                                'Customer',
114✔
350
                                [
114✔
351
                                        'cartUrl'   => [
114✔
352
                                                'type'        => 'String',
114✔
353
                                                'description' => __( 'A nonced link to the cart page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' ),
114✔
354
                                                'resolve'     => static function ( $source ) {
114✔
355
                                                        // Get current customer and user ID.
356
                                                        $customer_id     = $source->ID;
1✔
357
                                                        $current_user_id = get_current_user_id();
1✔
358

359
                                                        // Return null if current user not user being queried.
360
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
1✔
361
                                                                return null;
1✔
362
                                                        }
363

364
                                                        // Build nonced url as an unauthenticated user.
365
                                                        $nonce_name   = woographql_setting( 'cart_url_nonce_param', '_wc_cart' );
1✔
366
                                                        $query_params = [
1✔
367
                                                                'session_id' => $customer_id,
1✔
368
                                                                $nonce_name  => woographql_create_nonce( "load-cart_{$customer_id}" ),
1✔
369
                                                        ];
1✔
370
                                                        $query_params = apply_filters( 'graphql_cart_url_query_params', $query_params, $customer_id, $source );
1✔
371
                                                        $url          = add_query_arg(
1✔
372
                                                                $query_params,
1✔
373
                                                                site_url( woographql_setting( 'authorizing_url_endpoint', 'transfer-session' ) )
1✔
374
                                                        );
1✔
375

376
                                                        return esc_url_raw( $url );
1✔
377
                                                },
114✔
378
                                        ],
114✔
379
                                        'cartNonce' => [
114✔
380
                                                'type'        => 'String',
114✔
381
                                                'description' => __( 'A nonce for the cart page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' ),
114✔
382
                                                'resolve'     => static function ( $source ) {
114✔
383
                                                        // Get current customer and user ID.
384
                                                        $customer_id     = $source->ID;
1✔
385
                                                        $current_user_id = get_current_user_id();
1✔
386

387
                                                        // Return null if current user not user being queried.
388
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
1✔
389
                                                                return null;
1✔
390
                                                        }
391

392
                                                        return woographql_create_nonce( "load-cart_{$customer_id}" );
1✔
393
                                                },
114✔
394
                                        ],
114✔
395
                                ]
114✔
396
                        );
114✔
397
                }//end if
398

399
                if ( in_array( 'checkout_url', $fields_to_register, true ) ) {
114✔
400
                        register_graphql_fields(
114✔
401
                                'Customer',
114✔
402
                                [
114✔
403
                                        'checkoutUrl'   => [
114✔
404
                                                'type'        => 'String',
114✔
405
                                                'description' => __( 'A nonce link to the checkout page for session user. Expires in 24 hours.', 'wp-graphql-woocommerce' ),
114✔
406
                                                'resolve'     => static function ( $source ) {
114✔
407
                                                        // Get current customer and user ID.
408
                                                        $customer_id     = $source->ID;
1✔
409
                                                        $current_user_id = get_current_user_id();
1✔
410

411
                                                        // Return null if current user not user being queried.
412
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
1✔
413
                                                                return null;
1✔
414
                                                        }
415

416
                                                        // Build nonced url as an unauthenticated user.
417
                                                        $nonce_name   = woographql_setting( 'checkout_url_nonce_param', '_wc_checkout' );
1✔
418
                                                        $query_params = [
1✔
419
                                                                'session_id' => $customer_id,
1✔
420
                                                                $nonce_name  => woographql_create_nonce( "load-checkout_{$customer_id}" ),
1✔
421
                                                        ];
1✔
422
                                                        $query_params = apply_filters( 'graphql_checkout_url_query_params', $query_params, $customer_id, $source );
1✔
423
                                                        $url          = add_query_arg(
1✔
424
                                                                $query_params,
1✔
425
                                                                site_url( woographql_setting( 'authorizing_url_endpoint', 'transfer-session' ) )
1✔
426
                                                        );
1✔
427

428
                                                        return esc_url_raw( $url );
1✔
429
                                                },
114✔
430
                                        ],
114✔
431
                                        'checkoutNonce' => [
114✔
432
                                                'type'        => 'String',
114✔
433
                                                'description' => __( 'A nonce for the checkout page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' ),
114✔
434
                                                'resolve'     => static function ( $source ) {
114✔
435
                                                        // Get current customer and user ID.
436
                                                        $customer_id     = $source->ID;
1✔
437
                                                        $current_user_id = get_current_user_id();
1✔
438

439
                                                        // Return null if current user not user being queried.
440
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
1✔
441
                                                                return null;
1✔
442
                                                        }
443

444
                                                        return woographql_create_nonce( "load-checkout_{$customer_id}" );
1✔
445
                                                },
114✔
446
                                        ],
114✔
447
                                ]
114✔
448
                        );
114✔
449
                }//end if
450

451
                if ( in_array( 'account_url', $fields_to_register, true ) ) {
114✔
452
                        register_graphql_fields(
114✔
453
                                'Customer',
114✔
454
                                [
114✔
455
                                        'accountUrl'   => [
114✔
456
                                                'type'        => 'String',
114✔
457
                                                'description' => __( 'A nonce link to the account page for session user. Expires in 24 hours.', 'wp-graphql-woocommerce' ),
114✔
458
                                                'resolve'     => static function ( $source ) {
114✔
459
                                                        if ( ! is_user_logged_in() ) {
×
460
                                                                return null;
×
461
                                                        }
462

463
                                                        // Get current customer and user ID.
464
                                                        $customer_id     = $source->ID;
×
465
                                                        $current_user_id = get_current_user_id();
×
466

467
                                                        // Return null if current user not user being queried.
468
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
×
469
                                                                return null;
×
470
                                                        }
471

472
                                                        // Build nonced url as an unauthenticated user.
473
                                                        $nonce_name   = woographql_setting( 'account_url_nonce_param', '_wc_account' );
×
474
                                                        $query_params = [
×
475
                                                                'session_id' => $customer_id,
×
476
                                                                $nonce_name  => woographql_create_nonce( "load-account_{$customer_id}" ),
×
477
                                                        ];
×
478
                                                        $query_params = apply_filters( 'graphql_account_url_query_params', $query_params, $customer_id, $source );
×
479
                                                        $url          = add_query_arg(
×
480
                                                                $query_params,
×
481
                                                                site_url( woographql_setting( 'authorizing_url_endpoint', 'transfer-session' ) )
×
482
                                                        );
×
483

484
                                                        return esc_url_raw( $url );
×
485
                                                },
114✔
486
                                        ],
114✔
487
                                        'accountNonce' => [
114✔
488
                                                'type'        => 'String',
114✔
489
                                                'description' => __( 'A nonce for the account page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' ),
114✔
490
                                                'resolve'     => static function ( $source ) {
114✔
491
                                                        if ( ! is_user_logged_in() ) {
×
492
                                                                return null;
×
493
                                                        }
494

495
                                                        // Get current customer and user ID.
496
                                                        $customer_id     = $source->ID;
×
497
                                                        $current_user_id = get_current_user_id();
×
498

499
                                                        // Return null if current user not user being queried.
500
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
×
501
                                                                return null;
×
502
                                                        }
503

504
                                                        return woographql_create_nonce( "load-account_{$customer_id}" );
×
505
                                                },
114✔
506
                                        ],
114✔
507
                                ]
114✔
508
                        );
114✔
509
                }//end if
510

511
                if ( in_array( 'add_payment_method_url', $fields_to_register, true ) ) {
114✔
512
                        register_graphql_fields(
114✔
513
                                'Customer',
114✔
514
                                [
114✔
515
                                        'addPaymentMethodUrl'   => [
114✔
516
                                                'type'        => 'String',
114✔
517
                                                'description' => __( 'A nonce link to the add payment method page for the authenticated user. Expires in 24 hours.', 'wp-graphql-woocommerce' ),
114✔
518
                                                'resolve'     => static function ( $source ) {
114✔
519
                                                        if ( ! is_user_logged_in() ) {
1✔
520
                                                                return null;
×
521
                                                        }
522

523
                                                        // Get current customer and user ID.
524
                                                        $customer_id     = $source->ID;
1✔
525
                                                        $current_user_id = get_current_user_id();
1✔
526

527
                                                        // Return null if current user not user being queried.
528
                                                        if ( $current_user_id !== $customer_id ) {
1✔
529
                                                                return null;
1✔
530
                                                        }
531

532
                                                        // Build nonced url as an unauthenticated user.
533
                                                        $nonce_name = woographql_setting( 'add_payment_method_url_nonce_param', '_wc_payment' );
1✔
534
                                                        $url        = add_query_arg(
1✔
535
                                                                [
1✔
536
                                                                        'session_id' => $customer_id,
1✔
537
                                                                        $nonce_name  => woographql_create_nonce( "add-payment-method_{$customer_id}" ),
1✔
538
                                                                ],
1✔
539
                                                                site_url( woographql_setting( 'authorizing_url_endpoint', 'transfer-session' ) )
1✔
540
                                                        );
1✔
541

542
                                                        return esc_url_raw( $url );
1✔
543
                                                },
114✔
544
                                        ],
114✔
545
                                        'addPaymentMethodNonce' => [
114✔
546
                                                'type'        => 'String',
114✔
547
                                                'description' => __( 'A nonce for the add payment method page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' ),
114✔
548
                                                'resolve'     => static function ( $source ) {
114✔
549
                                                        if ( ! is_user_logged_in() ) {
1✔
550
                                                                return null;
×
551
                                                        }
552

553
                                                        // Get current customer and user ID.
554
                                                        $customer_id     = $source->ID;
1✔
555
                                                        $current_user_id = get_current_user_id();
1✔
556

557
                                                        // Return null if current user not user being queried.
558
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
1✔
559
                                                                return null;
1✔
560
                                                        }
561

562
                                                        return woographql_create_nonce( "add-payment-method_{$customer_id}" );
1✔
563
                                                },
114✔
564
                                        ],
114✔
565
                                ]
114✔
566
                        );
114✔
567
                }//end if
568
        }
569
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc