10
10
*/
11
11
class WC_Stripe_Customer {
12
12
13
+ /**
14
+ * Constant for the customer context when adding a payment method.
15
+ */
16
+ public const CUSTOMER_CONTEXT_ADD_PAYMENT_METHOD = 'add_payment_method ' ;
17
+
18
+ /**
19
+ * Constant for the customer context when paying for an order via the "Pay for Order" page.
20
+ */
21
+ public const CUSTOMER_CONTEXT_PAY_FOR_ORDER = 'pay_for_order ' ;
22
+
23
+ /**
24
+ * Constants for the customer contexts where minimal billing details are permitted.
25
+ */
26
+ public const MINIMAL_BILLING_DETAILS_CONTEXTS = [
27
+ self ::CUSTOMER_CONTEXT_ADD_PAYMENT_METHOD ,
28
+ self ::CUSTOMER_CONTEXT_PAY_FOR_ORDER ,
29
+ ];
30
+
13
31
/**
14
32
* String prefix for Stripe payment methods request transient.
15
33
*/
@@ -209,19 +227,19 @@ protected function generate_customer_request( $args = [] ) {
209
227
/**
210
228
* Validate that we have valid data before we try to create a customer.
211
229
*
212
- * @param array $create_customer_request
213
- * @param bool $is_add_payment_method_page
230
+ * @param array $create_customer_request The base data to build the customer request.
231
+ * @param null|string $current_context Flag to indicate whether we are in a context where limited details are permitted.
214
232
*
215
233
* @throws WC_Stripe_Exception
216
234
*/
217
- private function validate_create_customer_request ( $ create_customer_request , $ is_add_payment_method_page = false ) {
235
+ private function validate_create_customer_request ( $ create_customer_request , ? string $ current_context = null ) {
218
236
/**
219
237
* Filters the required customer fields when creating a customer in Stripe.
220
238
*
221
239
* @since 9.7.0
222
- * @param array $required_fields The required customer fields as derived from the required billing fields in checkout.
240
+ * @param array $required_fields The required customer fields as derived from the required billing fields in checkout. In some contexts, like adding a payment method, we allow minimal details to be provided.
223
241
*/
224
- $ required_fields = apply_filters ( 'wc_stripe_create_customer_required_fields ' , $ this ->get_create_customer_required_fields ( $ is_add_payment_method_page ) );
242
+ $ required_fields = apply_filters ( 'wc_stripe_create_customer_required_fields ' , $ this ->get_create_customer_required_fields ( $ current_context ) );
225
243
226
244
foreach ( $ required_fields as $ field => $ field_requirements ) {
227
245
if ( true === $ field_requirements ) {
@@ -258,13 +276,12 @@ private function validate_create_customer_request( $create_customer_request, $is
258
276
/**
259
277
* Get the list of required fields for the create customer request.
260
278
*
261
- * @param bool $is_add_payment_method_page
279
+ * @param string|null $current_context The context we are creating the customer in.
262
280
*
263
281
* @return array
264
282
*/
265
- private function get_create_customer_required_fields ( $ is_add_payment_method_page = false ) {
266
- // If we are on the add payment method page, we need to check just for the email field.
267
- if ( $ is_add_payment_method_page ) {
283
+ private function get_create_customer_required_fields ( ?string $ current_context = null ) {
284
+ if ( in_array ( $ current_context , self ::MINIMAL_BILLING_DETAILS_CONTEXTS , true ) ) {
268
285
return [
269
286
'email ' => true ,
270
287
];
@@ -422,19 +439,22 @@ public function get_existing_customer( $email, $name ) {
422
439
* Create a customer via API.
423
440
*
424
441
* @param array $args
425
- * @param bool $is_add_payment_method_page Whether the request is for the add payment method page .
442
+ * @param string|null $current_context The context we are creating the customer in .
426
443
* @return WP_Error|int
427
444
*
428
445
* @throws WC_Stripe_Exception
429
446
*/
430
- public function create_customer ( $ args = [], $ is_add_payment_method_page = false ) {
447
+ public function create_customer ( $ args = [], $ current_context = null ) {
431
448
$ args = $ this ->generate_customer_request ( $ args );
432
449
433
450
// For guest users, check if a customer already exists with the same email and name in Stripe account before creating a new one.
434
451
if ( ! $ this ->get_id () && 0 === $ this ->get_user_id () && ! empty ( $ args ['email ' ] ) && ! empty ( $ args ['name ' ] ) ) {
435
452
$ response = $ this ->get_existing_customer ( $ args ['email ' ], $ args ['name ' ] );
436
453
}
437
454
455
+ // $current_context was initially introduced as a boolean flag, so check for old callers.
456
+ $ current_context = $ this ->normalize_current_context ( $ current_context );
457
+
438
458
if ( empty ( $ response ) ) {
439
459
/**
440
460
* Filters the arguments used to create a customer.
@@ -445,7 +465,7 @@ public function create_customer( $args = [], $is_add_payment_method_page = false
445
465
*/
446
466
$ create_customer_args = apply_filters ( 'wc_stripe_create_customer_args ' , $ args );
447
467
448
- $ this ->validate_create_customer_request ( $ create_customer_args , $ is_add_payment_method_page );
468
+ $ this ->validate_create_customer_request ( $ create_customer_args , $ current_context );
449
469
450
470
$ response = WC_Stripe_API::request ( $ create_customer_args , 'customers ' );
451
471
} else {
@@ -523,19 +543,45 @@ public function update_customer( $args = [], $is_retry = false ) {
523
543
* Updates existing Stripe customer or creates new customer for User through API.
524
544
*
525
545
* @param array $args Additional arguments for the request (optional).
546
+ * @param string|null $current_context The context we are creating the customer in.
526
547
*
527
548
* @return string Customer ID
528
549
*
529
550
* @throws WC_Stripe_Exception
530
551
*/
531
- public function update_or_create_customer ( $ args = [], $ is_add_payment_method_page = false ) {
552
+ public function update_or_create_customer ( $ args = [], $ current_context = null ) {
532
553
if ( empty ( $ this ->get_id () ) ) {
533
- return $ this ->recreate_customer ( $ args , $ is_add_payment_method_page );
554
+ // $current_context was initially introduced as a boolean flag, so check for old callers.
555
+ $ current_context = $ this ->normalize_current_context ( $ current_context );
556
+
557
+ return $ this ->recreate_customer ( $ args , $ current_context );
534
558
} else {
535
559
return $ this ->update_customer ( $ args );
536
560
}
537
561
}
538
562
563
+ /**
564
+ * Normalize the current context to a string, as the argument was initially introduced as a boolean flag.
565
+ *
566
+ * @param string|bool|null $current_context The current context.
567
+ * @return string|null The normalized context.
568
+ */
569
+ private function normalize_current_context ( $ current_context ): ?string {
570
+ if ( null === $ current_context ) {
571
+ return null ;
572
+ }
573
+
574
+ if ( is_bool ( $ current_context ) ) {
575
+ return $ current_context ? self ::CUSTOMER_CONTEXT_ADD_PAYMENT_METHOD : null ;
576
+ }
577
+
578
+ if ( is_string ( $ current_context ) ) {
579
+ return $ current_context ;
580
+ }
581
+
582
+ return null ;
583
+ }
584
+
539
585
/**
540
586
* Checks to see if error is of invalid request
541
587
* error and it is no such customer.
@@ -978,13 +1024,13 @@ public function delete_id_from_meta() {
978
1024
* Recreates the customer for this user.
979
1025
*
980
1026
* @param array $args Additional arguments for the request (optional).
981
- * @param bool $is_add_payment_method_page Whether the request is for the add payment method page .
1027
+ * @param string|null $current_context The context we are creating the customer in .
982
1028
*
983
1029
* @return string ID of the new Customer object.
984
1030
*/
985
- private function recreate_customer ( $ args = [], $ is_add_payment_method_page = false ) {
1031
+ private function recreate_customer ( $ args = [], ? string $ current_context = null ) {
986
1032
$ this ->delete_id_from_meta ();
987
- return $ this ->create_customer ( $ args , $ is_add_payment_method_page );
1033
+ return $ this ->create_customer ( $ args , $ current_context );
988
1034
}
989
1035
990
1036
/**
0 commit comments