/var/www/html/wp-content/plugins/woocommerce/src/Internal/Admin/Settings/PaymentsRestController.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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
<?php
declare( strict_types=);

namespace 
Automattic\WooCommerce\Internal\Admin\Settings;

use 
Automattic\WooCommerce\Internal\RestApiControllerBase;
use 
Exception;
use 
WP_Error;
use 
WP_REST_Request;
use 
WP_REST_Response;

/**
 * Controller for the REST endpoints to service the Payments settings page.
 */
class PaymentsRestController extends RestApiControllerBase {

    
/**
     * The root namespace for the JSON REST API endpoints.
     *
     * @var string
     */
    
protected string $route_namespace 'wc-admin';

    
/**
     * Route base.
     *
     * @var string
     */
    
protected string $rest_base 'settings/payments';

    
/**
     * The payments settings page service.
     *
     * @var Payments
     */
    
private Payments $payments;

    
/**
     * Get the WooCommerce REST API namespace for the class.
     *
     * @return string
     */
    
protected function get_rest_api_namespace(): string {
        return 
'wc-admin';
    }

    
/**
     * Register the REST API endpoints handled by this controller.
     *
     * @param bool $override Whether to override the existing routes. Useful for testing.
     */
    
public function register_routesbool $override false ) {
        
register_rest_route(
            
$this->route_namespace,
            
'/' $this->rest_base '/country',
            array(
                array(
                    
'methods'             => \WP_REST_Server::EDITABLE,
                    
'callback'            => fn( $request ) => $this->run$request'set_country' ),
                    
'validation_callback' => 'rest_validate_request_arg',
                    
'permission_callback' => fn( $request ) => $this->check_permissions$request ),
                    
'args'                => array(
                        
'location' => array(
                            
'description'       => esc_html__'The ISO3166 alpha-2 country code to save for the current user.''woocommerce' ),
                            
'type'              => 'string',
                            
'pattern'           => '[a-zA-Z]{2}'// Two alpha characters.
                            
'required'          => true,
                            
'validate_callback' => fn( $value$request ) => $this->check_location_arg$value$request ),
                        ),
                    ),
                ),
            ),
            
$override
        
);
        
register_rest_route(
            
$this->route_namespace,
            
'/' $this->rest_base '/providers',
            array(
                array(
                    
'methods'             => \WP_REST_Server::READABLE,
                    
'callback'            => fn( $request ) => $this->run$request'get_providers' ),
                    
'validation_callback' => 'rest_validate_request_arg',
                    
'permission_callback' => fn( $request ) => $this->check_permissions$request ),
                    
'args'                => array(
                        
'location' => array(
                            
'description'       => __'ISO3166 alpha-2 country code. Defaults to WooCommerce\'s base location country.''woocommerce' ),
                            
'type'              => 'string',
                            
'pattern'           => '[a-zA-Z]{2}'// Two alpha characters.
                            
'required'          => false,
                            
'validate_callback' => fn( $value$request ) => $this->check_location_arg$value$request ),
                        ),
                    ),
                ),
                
'schema' => fn() => $this->get_schema_for_get_payment_providers(),
            ),
            
$override
        
);
        
register_rest_route(
            
$this->route_namespace,
            
'/' $this->rest_base '/providers/order',
            array(
                array(
                    
'methods'             => \WP_REST_Server::EDITABLE,
                    
'callback'            => fn( $request ) => $this->run$request'update_providers_order' ),
                    
'permission_callback' => fn( $request ) => $this->check_permissions$request ),
                    
'args'                => array(
                        
'order_map' => array(
                            
'description'       => __'A map of provider ID to integer values representing the sort order.''woocommerce' ),
                            
'type'              => 'object',
                            
'required'          => true,
                            
'validate_callback' => fn( $value ) => $this->check_providers_order_map_arg$value ),
                            
'sanitize_callback' => fn( $value ) => $this->sanitize_providers_order_arg$value ),
                        ),
                    ),
                ),
            ),
            
$override
        
);
        
register_rest_route(
            
$this->route_namespace,
            
'/' $this->rest_base '/suggestion/(?P<id>[\w\d\-]+)/hide',
            array(
                array(
                    
'methods'             => \WP_REST_Server::EDITABLE,
                    
'callback'            => fn( $request ) => $this->run$request'hide_payment_extension_suggestion' ),
                    
'permission_callback' => fn( $request ) => $this->check_permissions$request ),
                ),
            ),
            
$override
        
);
        
register_rest_route(
            
$this->route_namespace,
            
'/' $this->rest_base '/suggestion/(?P<suggestion_id>[\w\d\-]+)/incentive/(?P<incentive_id>[\w\d\-]+)/dismiss',
            array(
                array(
                    
'methods'             => \WP_REST_Server::EDITABLE,
                    
'callback'            => fn( $request ) => $this->run$request'dismiss_payment_extension_suggestion_incentive' ),
                    
'permission_callback' => fn( $request ) => $this->check_permissions$request ),
                    
'args'                => array(
                        
'context' => array(
                            
'description'       => __'The context ID for which to dismiss the incentive. If not provided, will dismiss the incentive for all contexts.''woocommerce' ),
                            
'type'              => 'string',
                            
'required'          => false,
                            
'sanitize_callback' => 'sanitize_key',
                        ),
                    ),
                ),
            ),
            
$override
        
);
    }

    
/**
     * Initialize the class instance.
     *
     * @param Payments $payments The payments settings page service.
     *
     * @internal
     */
    
final public function initPayments $payments ): void {
        
$this->payments $payments;
    }

    
/**
     * Get the payment providers for the given location.
     *
     * @param WP_REST_Request $request The request object.
     * @return WP_Error|WP_REST_Response
     */
    
protected function get_providersWP_REST_Request $request ) {
        
$location $request->get_param'location' );
        if ( empty( 
$location ) ) {
            
// Fall back to the providers country if no location is provided.
            
$location $this->payments->get_country();
        }

        try {
            
$providers $this->payments->get_payment_providers$location );
        } catch ( 
Exception $e ) {
            return new 
WP_Error'woocommerce_rest_payment_providers_error'$e->getMessage(), array( 'status' => 500 ) );
        }

        try {
            
$suggestions $this->get_extension_suggestions$location );
        } catch ( 
Exception $e ) {
            return new 
WP_Error'woocommerce_rest_payment_providers_error'$e->getMessage(), array( 'status' => 500 ) );
        }

        
// Separate the offline PMs from the main providers list.
        
$offline_payment_providers array_values(
            
array_filter(
                
$providers,
                fn( 
$provider ) => PaymentProviders::TYPE_OFFLINE_PM === $provider['_type']
            )
        );
        
$providers                 array_values(
            
array_filter(
                
$providers,
                fn( 
$provider ) => PaymentProviders::TYPE_OFFLINE_PM !== $provider['_type']
            )
        );

        
$response = array(
            
'providers'               => $providers,
            
'offline_payment_methods' => $offline_payment_providers,
            
'suggestions'             => $suggestions,
            
'suggestion_categories'   => $this->payments->get_payment_extension_suggestion_categories(),
        );

        return 
rest_ensure_response$this->prepare_payment_providers_response$response ) );
    }

    
/**
     * Set the country for the payment providers.
     *
     * @param WP_REST_Request $request The request object.
     *
     * @return WP_Error|WP_REST_Response
     */
    
protected function set_countryWP_REST_Request $request ) {
        
$location $request->get_param'location' );

        
$result $this->payments->set_country$location );

        return 
rest_ensure_response( array( 'success' => $result ) );
    }

    
/**
     * Update the payment providers order.
     *
     * @param WP_REST_Request $request The request object.
     *
     * @return WP_Error|WP_REST_Response
     */
    
protected function update_providers_orderWP_REST_Request $request ) {
        
$order_map $request->get_param'order_map' );

        
$result $this->payments->update_payment_providers_order_map$order_map );

        return 
rest_ensure_response( array( 'success' => $result ) );
    }

    
/**
     * Hide a payment extension suggestion.
     *
     * @param WP_REST_Request $request The request object.
     *
     * @return WP_Error|WP_REST_Response
     */
    
protected function hide_payment_extension_suggestionWP_REST_Request $request ) {
        
$suggestion_id $request->get_param'id' );

        try {
            
$result $this->payments->hide_payment_extension_suggestion$suggestion_id );
        } catch ( 
Exception $e ) {
            return new 
WP_Error'woocommerce_rest_payment_extension_suggestion_error'$e->getMessage(), array( 'status' => 400 ) );
        }

        return 
rest_ensure_response( array( 'success' => $result ) );
    }

    
/**
     * Dismiss a payment extension suggestion incentive.
     *
     * @param WP_REST_Request $request The request object.
     *
     * @return WP_Error|WP_REST_Response
     */
    
protected function dismiss_payment_extension_suggestion_incentiveWP_REST_Request $request ) {
        
$suggestion_id $request->get_param'suggestion_id' );
        
$incentive_id  $request->get_param'incentive_id' );
        
$context       $request->get_param'context' ) ?? 'all';

        try {
            
$result $this->payments->dismiss_extension_suggestion_incentive$suggestion_id$incentive_id$context );
        } catch ( 
Exception $e ) {
            return new 
WP_Error'woocommerce_rest_payment_extension_suggestion_incentive_error'$e->getMessage(), array( 'status' => 400 ) );
        }

        return 
rest_ensure_response( array( 'success' => $result ) );
    }

    
/**
     * Get the payment extension suggestions (other) for the given location.
     *
     * @param string $location The location for which the suggestions are being fetched.
     *
     * @return array[]   The payment extension suggestions for the given location,
     *                   excluding the ones part of the main providers list.
     * @throws Exception If there are malformed or invalid suggestions.
     */
    
private function get_extension_suggestionsstring $location ): array {
        
// If the requesting user can't install plugins, we don't suggest any extensions.
        
if ( ! current_user_can'install_plugins' ) ) {
            return array();
        }

        
$suggestions $this->payments->get_payment_extension_suggestions$location );

        return 
$suggestions['other'] ?? array();
    }

    
/**
     * General permissions check for payments settings REST API endpoint.
     *
     * @param WP_REST_Request $request The request for which the permission is checked.
     * @return bool|WP_Error True if the current user has the capability, otherwise an "Unauthorized" error or False if no error is available for the request method.
     */
    
private function check_permissionsWP_REST_Request $request ) {
        
$context 'read';
        if ( 
'POST' === $request->get_method() ) {
            
$context 'edit';
        } elseif ( 
'DELETE' === $request->get_method() ) {
            
$context 'delete';
        }

        if ( 
wc_rest_check_manager_permissions'payment_gateways'$context ) ) {
            return 
true;
        }

        
$error_information $this->get_authentication_error_by_method$request->get_method() );
        if ( 
is_null$error_information ) ) {
            return 
false;
        }

        return new 
WP_Error(
            
$error_information['code'],
            
$error_information['message'],
            array( 
'status' => rest_authorization_required_code() )
        );
    }

    
/**
     * Validate the location argument.
     *
     * @param mixed           $value   Value of the argument.
     * @param WP_REST_Request $request The current request object.
     *
     * @return WP_Error|true True if the location argument is valid, otherwise a WP_Error object.
     */
    
private function check_location_arg$valueWP_REST_Request $request ) {
        
// If the 'location' argument is not a string return an error.
        
if ( ! is_string$value ) ) {
            return new 
WP_Error'rest_invalid_param'esc_html__'The location argument must be a string.''woocommerce' ), array( 'status' => 400 ) );
        }

        
// Get the registered attributes for this endpoint request.
        
$attributes $request->get_attributes();

        
// Grab the location param schema.
        
$args $attributes['args']['location'];

        
// If the location param doesn't match the regex pattern then we should return an error as well.
        
if ( ! preg_match'/^' $args['pattern'] . '$/'$value ) ) {
            return new 
WP_Error'rest_invalid_param'esc_html__'The location argument must be a valid ISO3166 alpha-2 country code.''woocommerce' ), array( 'status' => 400 ) );
        }

        return 
true;
    }

    
/**
     * Validate the providers order map argument.
     *
     * @param mixed $value Value of the argument.
     *
     * @return WP_Error|true True if the providers order map argument is valid, otherwise a WP_Error object.
     */
    
private function check_providers_order_map_arg$value ) {
        if ( ! 
is_array$value ) ) {
            return new 
WP_Error'rest_invalid_param'esc_html__'The ordering argument must be an object.''woocommerce' ), array( 'status' => 400 ) );
        }

        foreach ( 
$value as $provider_id => $order ) {
            if ( ! 
is_string$provider_id ) || ! is_numeric$order ) ) {
                return new 
WP_Error'rest_invalid_param'esc_html__'The ordering argument must be an object with provider IDs as keys and numeric values as values.''woocommerce' ), array( 'status' => 400 ) );
            }

            if ( 
sanitize_key$provider_id ) !== $provider_id ) {
                return new 
WP_Error'rest_invalid_param'esc_html__'The provider ID must be a valid string.''woocommerce' ), array( 'status' => 400 ) );
            }

            if ( 
false === filter_var$orderFILTER_VALIDATE_INT ) ) {
                return new 
WP_Error'rest_invalid_param'esc_html__'The order value must be an integer.''woocommerce' ), array( 'status' => 400 ) );
            }
        }

        return 
true;
    }

    
/**
     * Sanitize the providers ordering argument.
     *
     * @param array $value Value of the argument.
     *
     * @return array
     */
    
private function sanitize_providers_order_arg( array $value ): array {
        
// Sanitize the ordering object to ensure that the order values are integers and the provider IDs are safe strings.
        
foreach ( $value as $provider_id => $order ) {
            
$id           sanitize_key$provider_id );
            
$value$id ] = intval$order );
        }

        return 
$value;
    }

    
/**
     * Prepare the response for the GET payment providers request.
     *
     * @param array $response The response to prepare.
     *
     * @return array The prepared response.
     */
    
private function prepare_payment_providers_response( array $response ): array {
        
$response $this->prepare_payment_providers_response_recursive$response$this->get_schema_for_get_payment_providers() );

        
$response['providers'] = $this->add_provider_links$response['providers'] );

        return 
$response;
    }

    
/**
     * Recursively prepare the response items for the GET payment providers request.
     *
     * @param mixed $response_item The response item to prepare.
     * @param array $schema        The schema to use for preparing the response.
     *
     * @return mixed The prepared response item.
     */
    
private function prepare_payment_providers_response_recursive$response_item, array $schema ) {
        if ( 
is_null$response_item ) ||
            ! 
array_key_exists'properties'$schema ) ||
            ! 
is_array$schema['properties'] ) ) {
            return 
$response_item;
        }

        
$prepared_response = array();
        foreach ( 
$schema['properties'] as $key => $property_schema ) {
            if ( 
is_array$response_item ) && array_key_exists$key$response_item ) ) {
                if ( 
is_array$property_schema ) && array_key_exists'properties'$property_schema ) ) {
                    
$prepared_response$key ] = $this->prepare_payment_providers_response_recursive$response_item$key ], $property_schema );
                } elseif ( 
is_array$property_schema ) && array_key_exists'items'$property_schema ) ) {
                    
$prepared_response$key ] = array_map(
                        fn( 
$item ) => $this->prepare_payment_providers_response_recursive$item$property_schema['items'] ),
                        
$response_item$key ]
                    );
                } else {
                    
$prepared_response$key ] = $response_item$key ];
                }
            }
        }

        
// Ensure the order is the same as in the schema.
        
$prepared_response array_mergearray_fill_keysarray_keys$schema['properties'] ), null ), $prepared_response );

        
// Remove any null values from the response.
        
$prepared_response array_filter$prepared_response, fn( $value ) => ! is_null$value ) );

        return 
$prepared_response;
    }

    
/**
     * Add links to providers list items.
     *
     * @param array $providers The providers list.
     *
     * @return array The providers list with added links.
     */
    
private function add_provider_links( array $providers ): array {
        foreach ( 
$providers as $key => $provider ) {
            if ( empty( 
$provider['_links'] ) ) {
                
$providers$key ]['_links'] = array();
            }

            
// If this is a suggestion, add a link to hide it.
            
if ( ! empty( $provider['_type'] ) &&
                
PaymentProviders::TYPE_SUGGESTION === $provider['_type'] &&
                ! empty( 
$provider['_suggestion_id'] )
                ) {
                
$providers$key ]['_links']['hide'] = array(
                    
'href' => rest_urlsprintf'/%s/%s/suggestion/%s/hide'$this->route_namespace$this->rest_base$provider['_suggestion_id'] ) ),
                );
            }

            
// If we have an incentive, add a link to dismiss it.
            
if ( ! empty( $provider['_incentive'] ) && ! empty( $provider['_suggestion_id'] ) ) {
                if ( empty( 
$provider['_incentive']['_links'] ) ) {
                    
$providers$key ]['_incentive']['_links'] = array();
                }

                
$providers$key ]['_incentive']['_links']['dismiss'] = array(
                    
'href' => rest_urlsprintf'/%s/%s/suggestion/%s/incentive/%s/dismiss'$this->route_namespace$this->rest_base$provider['_suggestion_id'], $provider['_incentive']['id'] ) ),
                );
            }
        }

        return 
$providers;
    }

    
/**
     * Get the schema for the GET payment providers request.
     *
     * @return array[]
     */
    
private function get_schema_for_get_payment_providers(): array {
        
$schema               = array(
            
'$schema' => 'http://json-schema.org/draft-04/schema#',
            
'title'   => 'WooCommerce Settings Payments providers for the given location.',
            
'type'    => 'object',
        );
        
$schema['properties'] = array(
            
'providers'               => array(
                
'type'        => 'array',
                
'description' => esc_html__'The ordered providers list. This includes registered payment gateways, suggestions, and offline payment methods group entry. The individual offline payment methods are separate.''woocommerce' ),
                
'context'     => array( 'view''edit' ),
                
'readonly'    => true,
                
'items'       => $this->get_schema_for_payment_provider(),
            ),
            
'offline_payment_methods' => array(
                
'type'        => 'array',
                
'description' => esc_html__'The ordered offline payment methods providers list.''woocommerce' ),
                
'context'     => array( 'view''edit' ),
                
'readonly'    => true,
                
'items'       => $this->get_schema_for_payment_provider(),
            ),
            
'suggestions'             => array(
                
'type'        => 'array',
                
'description' => esc_html__'The list of suggestions, excluding the ones part of the providers list.''woocommerce' ),
                
'context'     => array( 'view''edit' ),
                
'readonly'    => true,
                
'items'       => $this->get_schema_for_suggestion(),
            ),
            
'suggestion_categories'   => array(
                
'type'        => 'array',
                
'description' => esc_html__'The suggestion categories.''woocommerce' ),
                
'context'     => array( 'view''edit' ),
                
'readonly'    => true,
                
'items'       => array(
                    
'type'        => 'object',
                    
'description' => esc_html__'A suggestion category.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                    
'properties'  => array(
                        
'id'          => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The unique identifier for the category.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'_priority'   => array(
                            
'type'        => 'integer',
                            
'description' => esc_html__'The priority of the category.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'title'       => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The title of the category.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'description' => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The description of the category.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),

                    ),
                ),
            ),
        );

        return 
$schema;
    }

    
/**
     * Get the schema for a payment provider.
     *
     * @return array The schema for a payment provider.
     */
    
private function get_schema_for_payment_provider(): array {
        return array(
            
'type'        => 'object',
            
'description' => esc_html__'A payment provider in the context of the main Payments Settings page list.''woocommerce' ),
            
'properties'  => array(
                
'id'             => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The unique identifier for the provider.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
                
'_order'         => array(
                    
'type'        => 'integer',
                    
'description' => esc_html__'The sort order of the provider.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
                
'_type'          => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The type of payment provider. Use this to differentiate between the various items in the list and determine their intended use.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
                
'title'          => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The title of the provider.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
                
'description'    => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The description of the provider.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
                
'supports'       => array(
                    
'description' => __'Supported features for this provider.''woocommerce' ),
                    
'type'        => 'array',
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                    
'items'       => array(
                        
'type' => 'string',
                    ),
                ),
                
'plugin'         => array(
                    
'type'        => 'object',
                    
'description' => esc_html__'The corresponding plugin details of the provider.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                    
'properties'  => array(
                        
'_type'  => array(
                            
'type'        => 'string',
                            
'enum'        => array( PaymentProviders::EXTENSION_TYPE_WPORG ),
                            
'description' => esc_html__'The type of the plugin.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'slug'   => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The slug of the plugin.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'file'   => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The plugin main file. This is a relative path to the plugins directory.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'status' => array(
                            
'type'        => 'string',
                            
'enum'        => array(
                                
PaymentProviders::EXTENSION_NOT_INSTALLED,
                                
PaymentProviders::EXTENSION_INSTALLED,
                                
PaymentProviders::EXTENSION_ACTIVE,
                            ),
                            
'description' => esc_html__'The status of the plugin.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                    ),
                ),
                
'image'          => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The URL of the provider image.''woocommerce' ),
                    
'readonly'    => true,
                ),
                
'icon'           => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The URL of the provider icon (square aspect ratio - 72px by 72px).''woocommerce' ),
                    
'readonly'    => true,
                ),
                
'links'          => array(
                    
'type'        => 'array',
                    
'description' => __'Links for the provider.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                    
'items'       => array(
                        
'type'       => 'object',
                        
'properties' => array(
                            
'_type' => array(
                                
'type'        => 'string',
                                
'description' => esc_html__'The type of the link.''woocommerce' ),
                                
'context'     => array( 'view''edit' ),
                                
'readonly'    => true,
                            ),
                            
'url'   => array(
                                
'type'        => 'string',
                                
'description' => esc_html__'The URL of the link.''woocommerce' ),
                                
'context'     => array( 'view''edit' ),
                                
'readonly'    => true,
                            ),
                        ),
                    ),
                ),
                
'state'          => array(
                    
'type'        => 'object',
                    
'description' => esc_html__'The general state of the provider with regards to it\'s payments processing.''woocommerce' ),
                    
'properties'  => array(
                        
'enabled'           => array(
                            
'type'        => 'boolean',
                            
'description' => esc_html__'Whether the provider is enabled for use on checkout.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'account_connected' => array(
                            
'type'        => 'boolean',
                            
'description' => esc_html__'Whether the provider has a payments processing account connected.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'needs_setup'       => array(
                            
'type'        => 'boolean',
                            
'description' => esc_html__'Whether the provider needs setup.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'test_mode'         => array(
                            
'type'        => 'boolean',
                            
'description' => esc_html__'Whether the provider is in test mode for payments processing.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'dev_mode'          => array(
                            
'type'        => 'boolean',
                            
'description' => esc_html__'Whether the provider is in dev mode. Having this true usually leads to forcing test payments. ''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                    ),
                ),
                
'management'     => array(
                    
'type'        => 'object',
                    
'description' => esc_html__'The management details of the provider.''woocommerce' ),
                    
'properties'  => array(
                        
'_links' => array(
                            
'type'       => 'object',
                            
'context'    => array( 'view''edit' ),
                            
'readonly'   => true,
                            
'properties' => array(
                                
'settings' => array(
                                    
'type'        => 'object',
                                    
'description' => esc_html__'The link to the settings page for the payment gateway.''woocommerce' ),
                                    
'context'     => array( 'view''edit' ),
                                    
'readonly'    => true,
                                    
'properties'  => array(
                                        
'href' => array(
                                            
'type'        => 'string',
                                            
'description' => esc_html__'The URL to the settings page for the payment gateway.''woocommerce' ),
                                            
'context'     => array( 'view''edit' ),
                                            
'readonly'    => true,
                                        ),
                                    ),
                                ),
                            ),
                        ),
                    ),
                ),
                
'onboarding'     => array(
                    
'type'        => 'object',
                    
'description' => esc_html__'Onboarding-related details for the provider.''woocommerce' ),
                    
'properties'  => array(
                        
'state'                       => array(
                            
'type'        => 'object',
                            
'description' => esc_html__'The state of the onboarding process.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'properties'  => array(
                                
'started'   => array(
                                    
'type'        => 'boolean',
                                    
'description' => esc_html__'Whether the onboarding process has been started.''woocommerce' ),
                                    
'context'     => array( 'view''edit' ),
                                    
'readonly'    => true,
                                ),
                                
'completed' => array(
                                    
'type'        => 'boolean',
                                    
'description' => esc_html__'Whether the onboarding process has been completed.''woocommerce' ),
                                    
'context'     => array( 'view''edit' ),
                                    
'readonly'    => true,
                                ),
                                
'test_mode' => array(
                                    
'type'        => 'boolean',
                                    
'description' => esc_html__'Whether the onboarding process happens in test mode (aka sandbox or test-drive).''woocommerce' ),
                                    
'context'     => array( 'view''edit' ),
                                    
'readonly'    => true,
                                ),
                            ),
                        ),
                        
'_links'                      => array(
                            
'type'       => 'object',
                            
'context'    => array( 'view''edit' ),
                            
'readonly'   => true,
                            
'properties' => array(
                                
'onboard' => array(
                                    
'type'        => 'object',
                                    
'description' => esc_html__'The start/continue onboarding link for the payment gateway.''woocommerce' ),
                                    
'context'     => array( 'view''edit' ),
                                    
'readonly'    => true,
                                    
'properties'  => array(
                                        
'href' => array(
                                            
'type'        => 'string',
                                            
'description' => esc_html__'The URL to start/continue onboarding for the payment gateway.''woocommerce' ),
                                            
'context'     => array( 'view''edit' ),
                                            
'readonly'    => true,
                                        ),
                                    ),
                                ),
                            ),
                        ),
                        
'recommended_payment_methods' => array(
                            
'type'        => 'array',
                            
'description' => esc_html__'The list of recommended payment methods details for the payment gateway.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                            
'items'       => array(
                                
'type'        => 'object',
                                
'description' => esc_html__'The details for a recommended payment method.''woocommerce' ),
                                
'context'     => array( 'view''edit' ),
                                
'readonly'    => true,
                                
'properties'  => array(
                                    
'id'          => array(
                                        
'type'        => 'string',
                                        
'description' => esc_html__'The unique identifier for the payment method.''woocommerce' ),
                                        
'context'     => array( 'view''edit' ),
                                        
'readonly'    => true,
                                    ),
                                    
'_order'      => array(
                                        
'type'        => 'integer',
                                        
'description' => esc_html__'The sort order of the payment method.''woocommerce' ),
                                        
'context'     => array( 'view''edit' ),
                                        
'readonly'    => true,
                                    ),
                                    
'enabled'     => array(
                                        
'type'        => 'boolean',
                                        
'description' => esc_html__'Whether the payment method should be recommended as enabled or not.''woocommerce' ),
                                        
'context'     => array( 'view''edit' ),
                                        
'readonly'    => true,
                                    ),
                                    
'required'    => array(
                                        
'type'        => 'boolean',
                                        
'description' => esc_html__'Whether the payment method should be required (and force-enabled) or not.''woocommerce' ),
                                        
'context'     => array( 'view''edit' ),
                                        
'readonly'    => true,
                                    ),
                                    
'title'       => array(
                                        
'type'        => 'string',
                                        
'description' => esc_html__'The title of the payment method. Does not include HTML tags.''woocommerce' ),
                                        
'context'     => array( 'view''edit' ),
                                        
'readonly'    => true,
                                    ),
                                    
'description' => array(
                                        
'type'        => 'string',
                                        
'description' => esc_html__'The description of the payment method. It can contain basic HTML.''woocommerce' ),
                                        
'context'     => array( 'view''edit' ),
                                        
'readonly'    => true,
                                    ),
                                    
'icon'        => array(
                                        
'type'        => 'string',
                                        
'description' => esc_html__'The URL of the payment method icon or a base64-encoded SVG image.''woocommerce' ),
                                        
'context'     => array( 'view''edit' ),
                                        
'readonly'    => true,
                                    ),
                                ),
                            ),
                        ),
                    ),
                ),
                
'tags'           => array(
                    
'type'        => 'array',
                    
'description' => esc_html__'The tags associated with the provider.''woocommerce' ),
                    
'uniqueItems' => true,
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                    
'items'       => array(
                        
'type'        => 'string',
                        
'description' => esc_html__'Tag associated with the provider.''woocommerce' ),
                        
'readonly'    => true,
                    ),
                ),
                
'_suggestion_id' => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The suggestion ID matching this provider.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
                
'_incentive'     => array(
                    
'type'        => 'object',
                    
'description' => esc_html__'The active incentive for the provider.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                    
'properties'  => array(
                        
'id'                => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The incentive unique ID. This ID needs to be used for incentive dismissals.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'promo_id'          => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The incentive promo ID. This ID need to be fed into the onboarding flow.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'title'             => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The incentive title. It can contain stylistic HTML.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'description'       => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The incentive description. It can contain stylistic HTML.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'short_description' => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The short description of the incentive. It can contain stylistic HTML.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'cta_label'         => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The call to action label for the incentive.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'tc_url'            => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The URL to the terms and conditions for the incentive.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'badge'             => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The badge label for the incentive.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'_dismissals'       => array(
                            
'type'        => 'array',
                            
'description' => esc_html__'The dismissals list for the incentive. The `all` entry means the incentive was dismissed for all contexts.''woocommerce' ),
                            
'uniqueItems' => true,
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                            
'items'       => array(
                                
'type'        => 'string',
                                
'description' => esc_html__'Context ID in which the incentive was dismissed.''woocommerce' ),
                                
'readonly'    => true,
                            ),
                        ),
                        
'_links'            => array(
                            
'type'       => 'object',
                            
'context'    => array( 'view''edit' ),
                            
'readonly'   => true,
                            
'properties' => array(
                                
'dismiss' => array(
                                    
'type'        => 'object',
                                    
'description' => esc_html__'The link to dismiss the incentive.''woocommerce' ),
                                    
'context'     => array( 'view''edit' ),
                                    
'readonly'    => true,
                                    
'properties'  => array(
                                        
'href' => array(
                                            
'type'        => 'string',
                                            
'description' => esc_html__'The URL to dismiss the incentive.''woocommerce' ),
                                            
'context'     => array( 'view''edit' ),
                                            
'readonly'    => true,
                                        ),
                                    ),
                                ),
                                
'onboard' => array(
                                    
'type'        => 'object',
                                    
'description' => esc_html__'The start/continue onboarding link for the payment gateway.''woocommerce' ),
                                    
'context'     => array( 'view''edit' ),
                                    
'readonly'    => true,
                                    
'properties'  => array(
                                        
'href' => array(
                                            
'type'        => 'string',
                                            
'description' => esc_html__'The URL to start/continue onboarding for the payment gateway.''woocommerce' ),
                                            
'context'     => array( 'view''edit' ),
                                            
'readonly'    => true,
                                        ),
                                    ),
                                ),
                            ),
                        ),
                    ),
                ),
                
'_links'         => array(
                    
'type'       => 'object',
                    
'context'    => array( 'view''edit' ),
                    
'readonly'   => true,
                    
'properties' => array(
                        
'hide' => array(
                            
'type'        => 'object',
                            
'description' => esc_html__'The link to hide the suggestion.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                            
'properties'  => array(
                                
'href' => array(
                                    
'type'        => 'string',
                                    
'description' => esc_html__'The URL to hide the suggestion.''woocommerce' ),
                                    
'context'     => array( 'view''edit' ),
                                    
'readonly'    => true,
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        );
    }

    
/**
     * Get the schema for a suggestion.
     *
     * @return array The schema for a suggestion.
     */
    
private function get_schema_for_suggestion(): array {
        return array(
            
'type'        => 'object',
            
'description' => esc_html__'A suggestion with full details.''woocommerce' ),
            
'context'     => array( 'view''edit' ),
            
'readonly'    => true,
            
'properties'  => array(
                
'id'          => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The unique identifier for the suggestion.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
                
'_priority'   => array(
                    
'type'        => 'integer',
                    
'description' => esc_html__'The priority of the suggestion.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
                
'_type'       => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The type of the suggestion.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
                
'title'       => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The title of the suggestion.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
                
'description' => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The description of the suggestion.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
                
'plugin'      => array(
                    
'type'       => 'object',
                    
'context'    => array( 'view''edit' ),
                    
'readonly'   => true,
                    
'properties' => array(
                        
'_type'  => array(
                            
'type'        => 'string',
                            
'enum'        => array( PaymentProviders::EXTENSION_TYPE_WPORG ),
                            
'description' => esc_html__'The type of the plugin.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'slug'   => array(
                            
'type'        => 'string',
                            
'description' => esc_html__'The slug of the plugin.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                        
'status' => array(
                            
'type'        => 'string',
                            
'enum'        => array(
                                
PaymentProviders::EXTENSION_NOT_INSTALLED,
                                
PaymentProviders::EXTENSION_INSTALLED,
                                
PaymentProviders::EXTENSION_ACTIVE,
                            ),
                            
'description' => esc_html__'The status of the plugin.''woocommerce' ),
                            
'context'     => array( 'view''edit' ),
                            
'readonly'    => true,
                        ),
                    ),
                ),
                
'image'       => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The URL of the image.''woocommerce' ),
                    
'readonly'    => true,
                ),
                
'icon'        => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The URL of the icon (square aspect ratio).''woocommerce' ),
                    
'readonly'    => true,
                ),
                
'links'       => array(
                    
'type'     => 'array',
                    
'context'  => array( 'view''edit' ),
                    
'readonly' => true,
                    
'items'    => array(
                        
'type'       => 'object',
                        
'properties' => array(
                            
'_type' => array(
                                
'type'        => 'string',
                                
'description' => esc_html__'The type of the link.''woocommerce' ),
                                
'context'     => array( 'view''edit' ),
                                
'readonly'    => true,
                            ),
                            
'url'   => array(
                                
'type'        => 'string',
                                
'description' => esc_html__'The URL of the link.''woocommerce' ),
                                
'context'     => array( 'view''edit' ),
                                
'readonly'    => true,
                            ),
                        ),
                    ),
                ),
                
'tags'        => array(
                    
'description' => esc_html__'The tags associated with the suggestion.''woocommerce' ),
                    
'type'        => 'array',
                    
'uniqueItems' => true,
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                    
'items'       => array(
                        
'type'        => 'string',
                        
'description' => esc_html__'The tags associated with the suggestion.''woocommerce' ),
                        
'readonly'    => true,
                    ),
                ),
                
'category'    => array(
                    
'type'        => 'string',
                    
'description' => esc_html__'The category of the suggestion.''woocommerce' ),
                    
'context'     => array( 'view''edit' ),
                    
'readonly'    => true,
                ),
            ),
        );
    }
}