-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathclass-wc-rest-stripe-settings-controller.php
751 lines (657 loc) · 27.6 KB
/
class-wc-rest-stripe-settings-controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
<?php
/**
* Class WC_REST_Stripe_Settings_Controller
*/
defined( 'ABSPATH' ) || exit;
/**
* REST controller for settings.
*/
class WC_REST_Stripe_Settings_Controller extends WC_Stripe_REST_Base_Controller {
/**
* Endpoint path.
*
* @var string
*/
protected $rest_base = 'wc_stripe/settings';
/**
* Stripe payment gateway.
*
* @var WC_Gateway_Stripe
*/
private $gateway;
/**
* Constructor.
*
* @param WC_Gateway_Stripe $gateway Stripe payment gateway.
*/
public function __construct( WC_Gateway_Stripe $gateway ) {
$this->gateway = $gateway;
}
/**
* Configure REST API routes.
*/
public function register_routes() {
$form_fields = $this->gateway->get_form_fields();
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
[
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_settings' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ $this, 'update_settings' ],
'permission_callback' => [ $this, 'check_permission' ],
'args' => [
'is_stripe_enabled' => [
'description' => __( 'If Stripe should be enabled.', 'woocommerce-gateway-stripe' ),
'type' => 'boolean',
'validate_callback' => 'rest_validate_request_arg',
],
'is_test_mode_enabled' => [
'description' => __( 'Stripe test mode setting.', 'woocommerce-gateway-stripe' ),
'type' => 'boolean',
'validate_callback' => 'rest_validate_request_arg',
],
'enabled_payment_method_ids' => [
'description' => __( 'Payment method IDs that should be enabled. Other methods will be disabled.', 'woocommerce-gateway-stripe' ),
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => array_merge( $this->gateway->get_upe_available_payment_methods(), WC_Stripe_Helper::get_legacy_available_payment_method_ids() ),
],
'validate_callback' => 'rest_validate_request_arg',
],
'individual_payment_method_settings' => [
'description' => __( 'Individual payment method settings.', 'woocommerce-gateway-stripe' ),
'type' => 'object',
'items' => [
'type' => 'object',
],
'validate_callback' => 'rest_validate_request_arg',
],
'is_spe_enabled' => [
'description' => __( 'If Single Payment Element should be enabled.', 'woocommerce-gateway-stripe' ),
'type' => 'boolean',
'validate_callback' => 'rest_validate_request_arg',
],
'amazon_pay_button_size' => [
'description' => __( 'Express checkout button sizes.', 'woocommerce-gateway-stripe' ),
'type' => 'string',
'enum' => array_keys( $form_fields['amazon_pay_button_size']['options'] ?? [] ),
'validate_callback' => 'rest_validate_request_arg',
],
'amazon_pay_button_locations' => [
'description' => __( 'Express checkout locations that should be enabled.', 'woocommerce-gateway-stripe' ),
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => array_keys( $form_fields['amazon_pay_button_locations']['options'] ?? [] ),
],
'validate_callback' => 'rest_validate_request_arg',
],
'is_payment_request_enabled' => [
'description' => __( 'If Stripe express checkouts should be enabled.', 'woocommerce-gateway-stripe' ),
'type' => 'boolean',
'validate_callback' => 'rest_validate_request_arg',
],
'payment_request_button_type' => [
'description' => __( 'Express checkout button types.', 'woocommerce-gateway-stripe' ),
'type' => 'string',
'enum' => array_keys( $form_fields['payment_request_button_type']['options'] ),
'validate_callback' => 'rest_validate_request_arg',
],
'payment_request_button_theme' => [
'description' => __( 'Express checkout button themes.', 'woocommerce-gateway-stripe' ),
'type' => 'string',
'enum' => array_keys( $form_fields['payment_request_button_theme']['options'] ),
'validate_callback' => 'rest_validate_request_arg',
],
'payment_request_button_size' => [
'description' => __( 'Express checkout button sizes.', 'woocommerce-gateway-stripe' ),
'type' => 'string',
// it can happen that `$form_fields['payment_request_button_size']` is empty (in tests) - fixing temporarily.
'enum' => array_keys( isset( $form_fields['payment_request_button_size']['options'] ) ? $form_fields['payment_request_button_size']['options'] : [] ),
'validate_callback' => 'rest_validate_request_arg',
],
'payment_request_button_locations' => [
'description' => __( 'Express checkout locations that should be enabled.', 'woocommerce-gateway-stripe' ),
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => array_keys( $form_fields['payment_request_button_locations']['options'] ),
],
'validate_callback' => 'rest_validate_request_arg',
],
'is_manual_capture_enabled' => [
'description' => __( 'If manual capture of charges should be enabled.', 'woocommerce-gateway-stripe' ),
'type' => 'boolean',
'validate_callback' => 'rest_validate_request_arg',
],
'is_saved_cards_enabled' => [
'description' => __( 'If "Saved cards" should be enabled.', 'woocommerce-gateway-stripe' ),
'type' => 'boolean',
'validate_callback' => 'rest_validate_request_arg',
],
'is_sepa_tokens_for_other_methods_enabled' => [
'description' => __( 'If "SEPA tokens for other methods" should be enabled.', 'woocommerce-gateway-stripe' ),
'type' => 'boolean',
'validate_callback' => 'rest_validate_request_arg',
],
'is_separate_card_form_enabled' => [
'description' => __( 'If credit card number field, expiry date field, and CVC field should be separate.', 'woocommerce-gateway-stripe' ),
'type' => 'boolean',
'validate_callback' => 'rest_validate_request_arg',
],
'is_short_statement_descriptor_enabled' => [
'description' => __( 'When enabled, we\'ll include the order number for card and express checkout transactions.', 'woocommerce-gateway-stripe' ),
'type' => 'boolean',
'validate_callback' => 'rest_validate_request_arg',
],
'is_debug_log_enabled' => [
'description' => __( 'When enabled, payment error logs will be saved to WooCommerce > Status > Logs.', 'woocommerce-gateway-stripe' ),
'type' => 'boolean',
'validate_callback' => 'rest_validate_request_arg',
],
],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/payment_method_order',
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ $this, 'update_payment_methods_order' ],
'permission_callback' => [ $this, 'check_permission' ],
'args' => [
'ordered_payment_method_ids' => [
'description' => __( 'The order for the payment method IDs to be saved.', 'woocommerce-gateway-stripe' ),
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => array_merge( $this->gateway->get_upe_available_payment_methods(), WC_Stripe_Helper::get_legacy_available_payment_method_ids() ),
],
'validate_callback' => 'rest_validate_request_arg',
],
],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/payment_method',
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ $this, 'update_individual_payment_method_settings' ],
'permission_callback' => [ $this, 'check_permission' ],
'args' => [
'payment_method' => [
'description' => __( 'Payment method id.', 'woocommerce-gateway-stripe' ),
'type' => 'string',
'enum' => WC_Stripe_Helper::get_legacy_available_payment_method_ids(),
'validate_callback' => 'rest_validate_request_arg',
],
'is_enabled' => [
'description' => __( 'If payment method should be enabled.', 'woocommerce-gateway-stripe' ),
'type' => 'boolean',
'validate_callback' => 'rest_validate_request_arg',
],
'title' => [
'description' => __( 'Payment method title.', 'woocommerce-gateway-stripe' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
],
'description' => [
'description' => __( 'Payment method description.', 'woocommerce-gateway-stripe' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
],
],
]
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/notice',
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [ $this, 'dismiss_customization_notice' ],
'permission_callback' => [ $this, 'check_permission' ],
]
);
}
/**
* Retrieve settings.
*
* @return WP_REST_Response
*/
public function get_settings() {
$is_upe_enabled = WC_Stripe_Feature_Flags::is_upe_checkout_enabled();
$available_payment_method_ids = $is_upe_enabled ? $this->gateway->get_upe_available_payment_methods() : WC_Stripe_Helper::get_legacy_available_payment_method_ids();
$ordered_payment_method_ids = $is_upe_enabled ? WC_Stripe_Helper::get_upe_ordered_payment_method_ids( $this->gateway ) : $available_payment_method_ids;
$enabled_payment_method_ids = $is_upe_enabled ? $this->gateway->get_upe_enabled_payment_method_ids() : WC_Stripe_Helper::get_legacy_enabled_payment_method_ids();
return new WP_REST_Response(
[
/* Settings > General */
'is_stripe_enabled' => $this->gateway->is_enabled(),
'is_test_mode_enabled' => $this->gateway->is_in_test_mode(),
/* Settings > Payments accepted on checkout */
'enabled_payment_method_ids' => array_values( array_intersect( $enabled_payment_method_ids, $available_payment_method_ids ) ), // only fetch enabled payment methods that are available.
'available_payment_method_ids' => $available_payment_method_ids,
'ordered_payment_method_ids' => array_values(
array_diff(
$ordered_payment_method_ids,
[ WC_Stripe_Payment_Methods::AMAZON_PAY, WC_Stripe_Payment_Methods::LINK ]
)
), // exclude Amazon Pay and Link from this list as they are express methods only.
'individual_payment_method_settings' => $is_upe_enabled ? WC_Stripe_Helper::get_upe_individual_payment_method_settings( $this->gateway ) : WC_Stripe_Helper::get_legacy_individual_payment_method_settings(),
/* Settings > Express checkouts */
'amazon_pay_button_size' => $this->gateway->get_validated_option( 'amazon_pay_button_size' ),
'amazon_pay_button_locations' => $this->gateway->get_validated_option( 'amazon_pay_button_locations' ),
'is_payment_request_enabled' => 'yes' === $this->gateway->get_option( 'payment_request' ),
'payment_request_button_type' => $this->gateway->get_validated_option( 'payment_request_button_type' ),
'payment_request_button_theme' => $this->gateway->get_validated_option( 'payment_request_button_theme' ),
'payment_request_button_size' => $this->gateway->get_validated_option( 'payment_request_button_size' ),
'payment_request_button_locations' => $this->gateway->get_validated_option( 'payment_request_button_locations' ),
/* Settings > Payments & transactions */
'is_manual_capture_enabled' => ! $this->gateway->is_automatic_capture_enabled(),
'is_saved_cards_enabled' => 'yes' === $this->gateway->get_option( 'saved_cards' ),
'is_sepa_tokens_for_other_methods_enabled' => 'yes' === $this->gateway->get_option( 'sepa_tokens_for_other_methods' ),
'is_separate_card_form_enabled' => 'no' === $this->gateway->get_option( 'inline_cc_form' ),
'is_short_statement_descriptor_enabled' => 'yes' === $this->gateway->get_option( 'is_short_statement_descriptor_enabled' ),
/* Settings > Advanced settings */
'is_debug_log_enabled' => 'yes' === $this->gateway->get_option( 'logging' ),
'is_upe_enabled' => $is_upe_enabled,
'is_spe_enabled' => 'yes' === $this->gateway->get_option( 'single_payment_element' ),
]
);
}
/**
* Update settings.
*
* @param WP_REST_Request $request Full data about the request.
*/
public function update_settings( WP_REST_Request $request ) {
/* Settings > General */
$this->update_is_stripe_enabled( $request );
$this->update_is_test_mode_enabled( $request );
/* Settings > Payments accepted on checkout */
$this->update_enabled_payment_methods( $request );
$this->update_individual_payment_method_settings( $request );
/* Settings > Express checkouts */
$this->update_is_payment_request_enabled( $request );
$this->update_payment_request_settings( $request );
$this->update_amazon_pay_settings( $request );
/* Settings > Payments & transactions */
$this->update_is_manual_capture_enabled( $request );
$this->update_is_saved_cards_enabled( $request );
$this->update_is_sepa_tokens_for_other_methods_enabled( $request );
$this->update_is_separate_card_form_enabled( $request );
$this->update_is_short_account_statement_enabled( $request );
/* Settings > Advanced settings */
$this->update_is_debug_log_enabled( $request );
$this->update_is_upe_enabled( $request );
$this->update_is_spe_enabled( $request );
return new WP_REST_Response( [], 200 );
}
/**
* Updates the order of available payment methods in settings.
*
* @param WP_REST_Request $request Request object.
*
* @return WP_REST_Response
*/
public function update_payment_methods_order( WP_REST_Request $request ) {
$is_upe_enabled = WC_Stripe_Feature_Flags::is_upe_checkout_enabled();
$ordered_payment_method_ids = $request->get_param( 'ordered_payment_method_ids' );
if ( empty( $ordered_payment_method_ids ) ) {
return new WP_REST_Response( [], 403 );
}
if ( $is_upe_enabled ) {
$this->gateway->update_option( 'stripe_upe_payment_method_order', $ordered_payment_method_ids );
} else {
$ordered_payment_method_ids = array_map(
function ( $id ) {
if ( WC_Stripe_Payment_Methods::CARD === $id ) {
return 'stripe';
}
return 'stripe_' . $id;
},
$ordered_payment_method_ids
);
$this->gateway->update_option( 'stripe_legacy_method_order', $ordered_payment_method_ids );
}
WC_Stripe_Helper::add_stripe_methods_in_woocommerce_gateway_order( $ordered_payment_method_ids );
return new WP_REST_Response( [], 200 );
}
/**
* Updates Stripe enabled status.
*
* @param WP_REST_Request $request Request object.
*/
private function update_is_stripe_enabled( WP_REST_Request $request ) {
$is_stripe_enabled = $request->get_param( 'is_stripe_enabled' );
if ( null === $is_stripe_enabled ) {
return;
}
if ( $is_stripe_enabled ) {
$this->gateway->enable();
} else {
$this->gateway->disable();
}
}
/**
* Updates Stripe test mode.
*
* @param WP_REST_Request $request Request object.
*/
private function update_is_test_mode_enabled( WP_REST_Request $request ) {
$is_test_mode_enabled = $request->get_param( 'is_test_mode_enabled' );
if ( null === $is_test_mode_enabled ) {
return;
}
$this->gateway->update_option( 'testmode', $is_test_mode_enabled ? 'yes' : 'no' );
}
/**
* Updates the "payment request" enable/disable settings.
*
* @param WP_REST_Request $request Request object.
*/
private function update_is_payment_request_enabled( WP_REST_Request $request ) {
$is_payment_request_enabled = $request->get_param( 'is_payment_request_enabled' );
if ( null === $is_payment_request_enabled ) {
return;
}
$this->gateway->update_option( 'payment_request', $is_payment_request_enabled ? 'yes' : 'no' );
}
/**
* Updates manual capture.
*
* @param WP_REST_Request $request Request object.
*/
private function update_is_manual_capture_enabled( WP_REST_Request $request ) {
$is_manual_capture_enabled = $request->get_param( 'is_manual_capture_enabled' );
if ( null === $is_manual_capture_enabled ) {
return;
}
$this->gateway->update_option( 'capture', $is_manual_capture_enabled ? 'no' : 'yes' );
}
/**
* Updates "saved cards" feature.
*
* @param WP_REST_Request $request Request object.
*/
private function update_is_saved_cards_enabled( WP_REST_Request $request ) {
$is_saved_cards_enabled = $request->get_param( 'is_saved_cards_enabled' );
if ( null === $is_saved_cards_enabled ) {
return;
}
$this->gateway->update_option( 'saved_cards', $is_saved_cards_enabled ? 'yes' : 'no' );
}
/**
* Updates "SEPA tokens for other methods" feature.
*
* @param WP_REST_Request $request Request object.
*/
private function update_is_sepa_tokens_for_other_methods_enabled( WP_REST_Request $request ) {
$is_sepa_tokens_for_other_methods_enabled = $request->get_param( 'is_sepa_tokens_for_other_methods_enabled' );
if ( null === $is_sepa_tokens_for_other_methods_enabled ) {
return;
}
$this->gateway->update_option( 'sepa_tokens_for_other_methods', $is_sepa_tokens_for_other_methods_enabled ? 'yes' : 'no' );
}
/**
* Updates "saved cards" feature.
*
* @param WP_REST_Request $request Request object.
*/
private function update_is_separate_card_form_enabled( WP_REST_Request $request ) {
$is_separate_card_form_enabled = $request->get_param( 'is_separate_card_form_enabled' );
if ( null === $is_separate_card_form_enabled ) {
return;
}
$this->gateway->update_option( 'inline_cc_form', $is_separate_card_form_enabled ? 'no' : 'yes' );
}
/**
* Updates whether short account statement should be used.
*
* @param WP_REST_Request $request Request object.
*/
private function update_is_short_account_statement_enabled( WP_REST_Request $request ) {
$is_short_account_statement_enabled = $request->get_param( 'is_short_statement_descriptor_enabled' );
if ( null === $is_short_account_statement_enabled ) {
return;
}
$this->gateway->update_option( 'is_short_statement_descriptor_enabled', $is_short_account_statement_enabled ? 'yes' : 'no' );
}
/**
* Updates whether debug logging is enabled.
*
* @param WP_REST_Request $request Request object.
*/
private function update_is_debug_log_enabled( WP_REST_Request $request ) {
$is_debug_log_enabled = $request->get_param( 'is_debug_log_enabled' );
if ( null === $is_debug_log_enabled ) {
return;
}
$this->gateway->update_option( 'logging', $is_debug_log_enabled ? 'yes' : 'no' );
}
/**
* Updates whether debug logging is enabled.
*
* @param WP_REST_Request $request Request object.
*/
private function update_is_upe_enabled( WP_REST_Request $request ) {
$is_upe_enabled = $request->get_param( 'is_upe_enabled' );
if ( null === $is_upe_enabled ) {
return;
}
// If the new UPE is enabled, we need to remove the flag to ensure legacy SEPA tokens are updated flag.
if ( $is_upe_enabled && ! WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) {
delete_option( 'woocommerce_stripe_subscriptions_legacy_sepa_tokens_updated' );
}
$this->gateway->update_option( WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME, $is_upe_enabled ? 'yes' : 'disabled' );
// including the class again because otherwise it's not present.
if ( WC_Stripe_Inbox_Notes::are_inbox_notes_supported() ) {
require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-availability-note.php';
WC_Stripe_UPE_Availability_Note::possibly_delete_note();
require_once WC_STRIPE_PLUGIN_PATH . '/includes/notes/class-wc-stripe-upe-stripelink-note.php';
WC_Stripe_UPE_StripeLink_Note::possibly_delete_note();
}
WC_Stripe_Helper::add_stripe_methods_in_woocommerce_gateway_order();
}
/**
* Updates appearance attributes of the Amazon Pay button.
*
* @param WP_REST_Request $request Request object.
*/
private function update_amazon_pay_settings( WP_REST_Request $request ) {
$attributes = [
'amazon_pay_button_size' => 'amazon_pay_button_size',
'amazon_pay_button_locations' => 'amazon_pay_button_locations',
];
foreach ( $attributes as $request_key => $attribute ) {
if ( null === $request->get_param( $request_key ) ) {
continue;
}
$value = $request->get_param( $request_key );
$this->gateway->update_validated_option( $attribute, $value );
}
}
/**
* Updates appearance attributes of the payment request button.
*
* @param WP_REST_Request $request Request object.
*/
private function update_payment_request_settings( WP_REST_Request $request ) {
$attributes = [
'payment_request_button_type' => 'payment_request_button_type',
'payment_request_button_size' => 'payment_request_button_size',
'payment_request_button_theme' => 'payment_request_button_theme',
'payment_request_button_locations' => 'payment_request_button_locations',
];
foreach ( $attributes as $request_key => $attribute ) {
if ( null === $request->get_param( $request_key ) ) {
continue;
}
$value = $request->get_param( $request_key );
$this->gateway->update_validated_option( $attribute, $value );
}
}
/**
* Updates the "Single Payment Element" enable/disable settings.
*
* @param WP_REST_Request $request Request object.
*/
private function update_is_spe_enabled( WP_REST_Request $request ) {
$is_spe_enabled = $request->get_param( 'is_spe_enabled' );
$current_spe_enabled = $this->gateway->get_option( 'single_payment_element' );
if ( null === $is_spe_enabled ) {
return;
}
if ( $is_spe_enabled !== $current_spe_enabled ) {
$this->gateway->update_option( 'single_payment_element', $is_spe_enabled ? 'yes' : 'no' );
wc_admin_record_tracks_event(
$is_spe_enabled ? 'wcstripe_spe_enabled' : 'wcstripe_spe_disabled',
[ 'test_mode' => WC_Stripe_Mode::is_test() ? 1 : 0 ]
);
}
}
/**
* Updates the list of enabled payment methods.
*
* @param WP_REST_Request $request Request object.
*/
private function update_enabled_payment_methods( WP_REST_Request $request ) {
$payment_method_ids_to_enable = $request->get_param( 'enabled_payment_method_ids' );
$is_upe_enabled = $request->get_param( 'is_upe_enabled' );
if ( null === $is_upe_enabled ) {
return;
}
if ( null === $payment_method_ids_to_enable ) {
return;
}
if ( ! $is_upe_enabled ) {
$currently_enabled_payment_method_ids = WC_Stripe_Helper::get_legacy_enabled_payment_method_ids();
$payment_gateways = WC_Stripe_Helper::get_legacy_payment_methods();
foreach ( $payment_gateways as $gateway ) {
$gateway_id = str_replace( 'stripe_', '', $gateway->id );
if ( ! in_array( $gateway_id, $payment_method_ids_to_enable, true ) && in_array( $gateway_id, $currently_enabled_payment_method_ids, true ) ) {
$gateway->update_option( 'enabled', 'no' );
} elseif ( in_array( $gateway_id, $payment_method_ids_to_enable, true ) ) {
$gateway->update_option( 'enabled', 'yes' );
}
}
return;
}
$this->gateway->update_enabled_payment_methods( $payment_method_ids_to_enable );
}
/**
* Update individual payment gateway settings.
*
* @param WP_REST_Request $request Request object.
*
* @return WP_REST_Response
*/
public function update_individual_payment_method_settings( WP_REST_Request $request ) {
$payment_method_id = $request->get_param( 'payment_method_id' );
$is_enabled = $request->get_param( 'is_enabled' );
$title = sanitize_text_field( $request->get_param( 'title' ) );
$description = sanitize_text_field( $request->get_param( 'description' ) );
$is_upe_enabled = WC_Stripe_Feature_Flags::is_upe_checkout_enabled();
if ( $is_upe_enabled ) {
$available_payment_methods = $this->gateway->get_upe_available_payment_methods();
if ( ! in_array( $payment_method_id, $available_payment_methods, true ) ) {
return new WP_REST_Response( [ 'result' => 'payment method not found' ], 404 );
}
$settings = [
'title' => $title,
'description' => $description,
];
if ( in_array( $payment_method_id, [ WC_Stripe_Payment_Methods::BOLETO ], true ) ) {
$settings['expiration'] = sanitize_text_field( $request->get_param( 'expiration' ) );
}
update_option(
'woocommerce_stripe_' . $payment_method_id . '_settings',
$settings
);
return new WP_REST_Response( [], 200 );
}
// Map the ids used in the frontend to the legacy gateway class ids.
$mapped_legacy_method_id = ( 'stripe_' . $payment_method_id );
// In legacy mode (when UPE is disabled), Stripe gateway refers to card as payment method id.
if ( WC_Stripe_Payment_Methods::CARD === $payment_method_id ) {
$this->gateway->update_option( 'title', $title );
$this->gateway->update_option( 'description', $description );
return new WP_REST_Response( [], 200 );
}
$payment_gateway = WC_Stripe_Helper::get_legacy_payment_method( $mapped_legacy_method_id );
if ( ! $payment_gateway ) {
return new WP_REST_Response( [ 'result' => 'payment method not found' ], 404 );
}
$payment_gateway->update_option( 'title', $title );
$payment_gateway->update_option( 'description', $description );
if ( $request->get_param( 'expiration' ) && method_exists( $payment_gateway, 'update_unique_settings' ) ) {
$request->set_param( $mapped_legacy_method_id . '_expiration', $request->get_param( 'expiration' ) );
$payment_gateway->update_unique_settings( $request );
}
return new WP_REST_Response( [], 200 );
}
/**
* Set `wc_stripe_show_customization_notice` as `no` to dismiss notice.
*
* @param WP_REST_Request $request Request object.
*
* @return WP_REST_Response
*/
public function dismiss_customization_notice( WP_REST_Request $request ) {
if ( null === $request->get_param( 'wc_stripe_show_customization_notice' ) ) {
return new WP_REST_Response( [], 200 );
}
update_option( 'wc_stripe_show_customization_notice', 'no' );
return new WP_REST_Response( [ 'result' => 'notice dismissed' ], 200 );
}
/**
* Record tracks events for each payment method that was enabled or disabled.
*
* @param array $enabled_methods An array of payment method ids that were enabled.
* @param array $disabled_methods An array of payment method ids that were disabled.
*
* @return void
*/
private function record_payment_method_settings_event( $enabled_methods, $disabled_methods ) {
if ( ! function_exists( 'wc_admin_record_tracks_event' ) ) {
return;
}
$is_test_mode = WC_Stripe_Mode::is_test();
// Track the events for both arrays.
array_map(
function ( $id ) use ( $is_test_mode ) {
wc_admin_record_tracks_event(
'wcstripe_payment_method_settings_enabled',
[
'is_test_mode' => $is_test_mode,
'payment_method' => $id,
]
);
},
$enabled_methods
);
array_map(
function ( $id ) use ( $is_test_mode ) {
wc_admin_record_tracks_event(
'wcstripe_payment_method_settings_disabled',
[
'is_test_mode' => $is_test_mode,
'payment_method' => $id,
]
);
},
$disabled_methods
);
}
}