@@ -203,55 +203,18 @@ protected function generate_customer_request( $args = [] ) {
203
203
* Validate that we have valid data before we try to create a customer.
204
204
*
205
205
* @param array $create_customer_request
206
+ * @param bool $is_add_payment_method_page
206
207
*
207
208
* @throws WC_Stripe_Exception
208
209
*/
209
- private function validate_create_customer_request ( $ create_customer_request ) {
210
- $ checkout_billing_fields = WC_Checkout::instance ()->get_checkout_fields ( 'billing ' );
211
- $ required_billing_fields = array_filter (
212
- $ checkout_billing_fields ,
213
- function ( $ field_data ) {
214
- return $ field_data ['required ' ] ?? false ;
215
- }
216
- );
217
-
218
- $ required_fields = [];
219
-
220
- if ( isset ( $ required_billing_fields ['billing_email ' ] ) ) {
221
- $ required_fields ['email ' ] = true ;
222
- }
223
-
224
- if ( isset ( $ required_billing_fields ['billing_first_name ' ] ) || isset ( $ required_billing_fields ['billing_last_name ' ] ) ) {
225
- $ required_fields ['name ' ] = true ;
226
- }
227
-
228
- $ required_address_fields = [];
229
- $ address_field_mapping = [
230
- 'billing_address_1 ' => 'line1 ' ,
231
- 'billing_address_2 ' => 'line2 ' ,
232
- 'billing_city ' => 'city ' ,
233
- 'billing_country ' => 'country ' ,
234
- 'billing_postcode ' => 'postal_code ' ,
235
- 'billing_state ' => 'state ' ,
236
- ];
237
-
238
- foreach ( $ address_field_mapping as $ field => $ stripe_field_name ) {
239
- if ( isset ( $ required_billing_fields [ $ field ] ) ) {
240
- $ required_address_fields [ $ stripe_field_name ] = true ;
241
- }
242
- }
243
-
244
- if ( [] !== $ required_address_fields ) {
245
- $ required_fields ['address ' ] = $ required_address_fields ;
246
- }
247
-
210
+ private function validate_create_customer_request ( $ create_customer_request , $ is_add_payment_method_page = false ) {
248
211
/**
249
212
* Filters the required customer fields when creating a customer in Stripe.
250
213
*
251
214
* @since 9.7.0
252
215
* @param array $required_fields The required customer fields as derived from the required billing fields in checkout.
253
216
*/
254
- $ required_fields = apply_filters ( 'wc_stripe_create_customer_required_fields ' , $ required_fields );
217
+ $ required_fields = apply_filters ( 'wc_stripe_create_customer_required_fields ' , $ this -> get_create_customer_required_fields ( $ is_add_payment_method_page ) );
255
218
256
219
foreach ( $ required_fields as $ field => $ field_requirements ) {
257
220
if ( true === $ field_requirements ) {
@@ -285,6 +248,62 @@ function ( $field_data ) {
285
248
}
286
249
}
287
250
251
+ /**
252
+ * Get the list of required fields for the create customer request.
253
+ *
254
+ * @param bool $is_add_payment_method_page
255
+ *
256
+ * @return array
257
+ */
258
+ private function get_create_customer_required_fields ( $ is_add_payment_method_page = false ) {
259
+ // If we are on the add payment method page, we need to check just for the email field.
260
+ if ( $ is_add_payment_method_page ) {
261
+ return [
262
+ 'email ' => true ,
263
+ ];
264
+ }
265
+
266
+ $ checkout_billing_fields = WC_Checkout::instance ()->get_checkout_fields ( 'billing ' );
267
+ $ required_billing_fields = array_filter (
268
+ $ checkout_billing_fields ,
269
+ function ( $ field_data ) {
270
+ return $ field_data ['required ' ] ?? false ;
271
+ }
272
+ );
273
+
274
+ $ required_fields = [];
275
+
276
+ if ( isset ( $ required_billing_fields ['billing_email ' ] ) ) {
277
+ $ required_fields ['email ' ] = true ;
278
+ }
279
+
280
+ if ( isset ( $ required_billing_fields ['billing_first_name ' ] ) || isset ( $ required_billing_fields ['billing_last_name ' ] ) ) {
281
+ $ required_fields ['name ' ] = true ;
282
+ }
283
+
284
+ $ required_address_fields = [];
285
+ $ address_field_mapping = [
286
+ 'billing_address_1 ' => 'line1 ' ,
287
+ 'billing_address_2 ' => 'line2 ' ,
288
+ 'billing_city ' => 'city ' ,
289
+ 'billing_country ' => 'country ' ,
290
+ 'billing_postcode ' => 'postal_code ' ,
291
+ 'billing_state ' => 'state ' ,
292
+ ];
293
+
294
+ foreach ( $ address_field_mapping as $ field => $ stripe_field_name ) {
295
+ if ( isset ( $ required_billing_fields [ $ field ] ) ) {
296
+ $ required_address_fields [ $ stripe_field_name ] = true ;
297
+ }
298
+ }
299
+
300
+ if ( [] !== $ required_address_fields ) {
301
+ $ required_fields ['address ' ] = $ required_address_fields ;
302
+ }
303
+
304
+ return $ required_fields ;
305
+ }
306
+
288
307
/**
289
308
* Get value of billing data field, either from POST or order object.
290
309
*
@@ -396,11 +415,12 @@ public function get_existing_customer( $email, $name ) {
396
415
* Create a customer via API.
397
416
*
398
417
* @param array $args
418
+ * @param bool $is_add_payment_method_page Whether the request is for the add payment method page.
399
419
* @return WP_Error|int
400
420
*
401
421
* @throws WC_Stripe_Exception
402
422
*/
403
- public function create_customer ( $ args = [] ) {
423
+ public function create_customer ( $ args = [], $ is_add_payment_method_page = false ) {
404
424
$ args = $ this ->generate_customer_request ( $ args );
405
425
406
426
// For guest users, check if a customer already exists with the same email and name in Stripe account before creating a new one.
@@ -418,15 +438,15 @@ public function create_customer( $args = [] ) {
418
438
*/
419
439
$ create_customer_args = apply_filters ( 'wc_stripe_create_customer_args ' , $ args );
420
440
421
- $ this ->validate_create_customer_request ( $ create_customer_args );
441
+ $ this ->validate_create_customer_request ( $ create_customer_args, $ is_add_payment_method_page );
422
442
423
443
$ response = WC_Stripe_API::request ( $ create_customer_args , 'customers ' );
424
444
} else {
425
445
/**
426
446
* This filter is documented in includes/class-wc-stripe-customer.php.
427
447
*/
428
448
$ update_customer_args = apply_filters ( 'wc_stripe_update_customer_args ' , $ args );
429
- $ response = WC_Stripe_API::request ( $ update_customer_args , 'customers/ ' . $ response ->id );
449
+ $ response = WC_Stripe_API::request ( $ update_customer_args , 'customers/ ' . $ response ->id );
430
450
}
431
451
432
452
if ( ! empty ( $ response ->error ) ) {
@@ -501,9 +521,9 @@ public function update_customer( $args = [], $is_retry = false ) {
501
521
*
502
522
* @throws WC_Stripe_Exception
503
523
*/
504
- public function update_or_create_customer ( $ args = [] ) {
524
+ public function update_or_create_customer ( $ args = [], $ is_add_payment_method_page = false ) {
505
525
if ( empty ( $ this ->get_id () ) ) {
506
- return $ this ->recreate_customer ( $ args );
526
+ return $ this ->recreate_customer ( $ args, $ is_add_payment_method_page );
507
527
} else {
508
528
return $ this ->update_customer ( $ args );
509
529
}
@@ -858,12 +878,13 @@ public function delete_id_from_meta() {
858
878
* Recreates the customer for this user.
859
879
*
860
880
* @param array $args Additional arguments for the request (optional).
881
+ * @param bool $is_add_payment_method_page Whether the request is for the add payment method page.
861
882
*
862
883
* @return string ID of the new Customer object.
863
884
*/
864
- private function recreate_customer ( $ args = [] ) {
885
+ private function recreate_customer ( $ args = [], $ is_add_payment_method_page = false ) {
865
886
$ this ->delete_id_from_meta ();
866
- return $ this ->create_customer ( $ args );
887
+ return $ this ->create_customer ( $ args, $ is_add_payment_method_page );
867
888
}
868
889
869
890
/**
0 commit comments