/var/www/html_us/wp-content/plugins/woocommerce/src/Internal/Admin/Settings/Payments.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
<?php
declare( strict_types=);

namespace 
Automattic\WooCommerce\Internal\Admin\Settings;

use 
Automattic\WooCommerce\Internal\Admin\Suggestions\PaymentExtensionSuggestions as ExtensionSuggestions;
use 
Exception;

defined'ABSPATH' ) || exit;
/**
 * Payments settings service class.
 */
class Payments {

    const 
USER_PAYMENTS_NOX_PROFILE_KEY 'woocommerce_payments_nox_profile';

    const 
SUGGESTIONS_CONTEXT 'wc_settings_payments';

    
/**
     * The payment providers service.
     *
     * @var PaymentProviders
     */
    
private PaymentProviders $providers;

    
/**
     * The payment extension suggestions service.
     *
     * @var ExtensionSuggestions
     */
    
private ExtensionSuggestions $extension_suggestions;

    
/**
     * Initialize the class instance.
     *
     * @param PaymentProviders     $payment_providers             The payment providers service.
     * @param ExtensionSuggestions $payment_extension_suggestions The payment extension suggestions service.
     *
     * @internal
     */
    
final public function initPaymentProviders $payment_providersExtensionSuggestions $payment_extension_suggestions ): void {
        
$this->providers             $payment_providers;
        
$this->extension_suggestions $payment_extension_suggestions;
    }

    
/**
     * Get the payment provider details list for the settings page.
     *
     * @param string $location The location for which the providers are being determined.
     *                         This is a ISO 3166-1 alpha-2 country code.
     *
     * @return array The payment providers details list.
     * @throws Exception If there are malformed or invalid suggestions.
     */
    
public function get_payment_providersstring $location ): array {
        
$payment_gateways $this->providers->get_payment_gateways();
        
$suggestions      = array();

        
$providers_order_map $this->providers->get_order_map();

        
$payment_providers = array();

        
// Only include suggestions if the requesting user can install plugins.
        
if ( current_user_can'install_plugins' ) ) {
            
$suggestions $this->providers->get_extension_suggestions$locationself::SUGGESTIONS_CONTEXT );
        }
        
// If we have preferred suggestions, add them to the providers list.
        
if ( ! empty( $suggestions['preferred'] ) ) {
            
// Sort them by priority, ASC.
            
usort(
                
$suggestions['preferred'],
                function ( 
$a$b ) {
                    return 
$a['_priority'] <=> $b['_priority'];
                }
            );
            
$added_to_top 0;
            foreach ( 
$suggestions['preferred'] as $suggestion ) {
                
$suggestion_order_map_id $this->providers->get_suggestion_order_map_id$suggestion['id'] );
                
// Determine the suggestion's order value.
                // If we don't have an order for it, add it to the top but keep the relative order (PSP first, APM second).
                
if ( ! isset( $providers_order_map$suggestion_order_map_id ] ) ) {
                    
$providers_order_map Utils::order_map_add_at_order$providers_order_map$suggestion_order_map_id$added_to_top );
                    ++
$added_to_top;
                }

                
// Change suggestion details to align it with a regular payment gateway.
                
$suggestion['_suggestion_id'] = $suggestion['id'];
                
$suggestion['id']             = $suggestion_order_map_id;
                
$suggestion['_type']          = PaymentProviders::TYPE_SUGGESTION;
                
$suggestion['_order']         = $providers_order_map$suggestion_order_map_id ];
                unset( 
$suggestion['_priority'] );

                
$payment_providers[] = $suggestion;
            }
        }

        foreach ( 
$payment_gateways as $payment_gateway ) {
            
// Determine the gateway's order value.
            // If we don't have an order for it, add it to the end.
            
if ( ! isset( $providers_order_map$payment_gateway->id ] ) ) {
                
$providers_order_map Utils::order_map_add_at_order$providers_order_map$payment_gateway->idcount$payment_providers ) );
            }

            
$payment_providers[] = $this->providers->get_payment_gateway_details(
                
$payment_gateway,
                
$providers_order_map$payment_gateway->id ],
                
$location
            
);
        }

        
// Add offline payment methods group entry if we have offline payment methods.
        
if ( in_arrayPaymentProviders::TYPE_OFFLINE_PMarray_column$payment_providers'_type' ), true ) ) {
            
// Determine the item's order value.
            // If we don't have an order for it, add it to the end.
            
if ( ! isset( $providers_order_mapPaymentProviders::OFFLINE_METHODS_ORDERING_GROUP ] ) ) {
                
$providers_order_map Utils::order_map_add_at_order$providers_order_mapPaymentProviders::OFFLINE_METHODS_ORDERING_GROUPcount$payment_providers ) );
            }

            
$payment_providers[] = array(
                
'id'          => PaymentProviders::OFFLINE_METHODS_ORDERING_GROUP,
                
'_type'       => PaymentProviders::TYPE_OFFLINE_PMS_GROUP,
                
'_order'      => $providers_order_mapPaymentProviders::OFFLINE_METHODS_ORDERING_GROUP ],
                
'title'       => __'Take offline payments''woocommerce' ),
                
'description' => __'Accept payments offline using multiple different methods. These can also be used to test purchases.''woocommerce' ),
                
'icon'        => plugins_url'assets/images/payment_methods/cod.svg'WC_PLUGIN_FILE ),
                
// The offline PMs (and their group) are obviously from WooCommerce, and WC is always active.
                
'plugin'      => array(
                    
'_type'  => 'wporg',
                    
'slug'   => 'woocommerce',
                    
'file'   => ''// This pseudo-provider should have no use for the plugin file.
                    
'status' => PaymentProviders::EXTENSION_ACTIVE,
                ),
                
'management'  => array(
                    
'_links' => array(
                        
'settings' => array(
                            
'href' => admin_url'admin.php?page=wc-settings&tab=checkout&section=offline' ),
                        ),
                    ),
                ),
            );
        }

        
// Determine the final, standardized providers order map.
        
$providers_order_map $this->providers->enhance_order_map$providers_order_map );
        
// Enforce the order map on all providers, just in case.
        
foreach ( $payment_providers as $key => $provider ) {
            
$payment_providers$key ]['_order'] = $providers_order_map$provider['id'] ];
        }
        
// NOTE: For now, save it back to the DB. This is temporary until we have a better way to handle this!
        
$this->providers->save_order_map$providers_order_map );

        
// Sort the payment providers by order, ASC.
        
usort(
            
$payment_providers,
            function ( 
$a$b ) {
                return 
$a['_order'] <=> $b['_order'];
            }
        );

        return 
$payment_providers;
    }

    
/**
     * Get the payment extension suggestions 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, split into preferred and other.
     * @throws Exception If there are malformed or invalid suggestions.
     */
    
public function get_payment_extension_suggestionsstring $location ): array {
        return 
$this->providers->get_extension_suggestions$locationself::SUGGESTIONS_CONTEXT );
    }

    
/**
     * Get the payment extension suggestions categories details.
     *
     * @return array The payment extension suggestions categories.
     */
    
public function get_payment_extension_suggestion_categories(): array {
        return 
$this->providers->get_extension_suggestion_categories();
    }

    
/**
     * Get the business location country code for the Payments settings.
     *
     * @return string The ISO 3166-1 alpha-2 country code to use for the overall business location.
     *                If the user didn't set a location, the WC base location country code is used.
     */
    
public function get_country(): string {
        
$user_nox_meta get_user_metaget_current_user_id(), self::USER_PAYMENTS_NOX_PROFILE_KEYtrue );
        if ( ! empty( 
$user_nox_meta['business_country_code'] ) ) {
            return 
$user_nox_meta['business_country_code'];
        }

        return 
WC()->countries->get_base_country();
    }

    
/**
     * Set the business location country for the Payments settings.
     *
     * @param string $location The country code. This should be a ISO 3166-1 alpha-2 country code.
     */
    
public function set_countrystring $location ): bool {
        
$user_payments_nox_profile get_user_metaget_current_user_id(), self::USER_PAYMENTS_NOX_PROFILE_KEYtrue );

        if ( empty( 
$user_payments_nox_profile ) ) {
            
$user_payments_nox_profile = array();
        } else {
            
$user_payments_nox_profile maybe_unserialize$user_payments_nox_profile );
        }
        
$user_payments_nox_profile['business_country_code'] = $location;

        return 
false !== update_user_metaget_current_user_id(), self::USER_PAYMENTS_NOX_PROFILE_KEY$user_payments_nox_profile );
    }

    
/**
     * Update the payment providers order map.
     *
     * @param array $order_map The new order for payment providers.
     *
     * @return bool True if the payment providers ordering was successfully updated, false otherwise.
     */
    
public function update_payment_providers_order_map( array $order_map ): bool {
        return 
$this->providers->update_payment_providers_order_map$order_map );
    }

    
/**
     * Hide a payment extension suggestion.
     *
     * @param string $id The ID of the payment extension suggestion to hide.
     *
     * @return bool True if the suggestion was successfully hidden, false otherwise.
     * @throws Exception If the suggestion ID is invalid.
     */
    
public function hide_payment_extension_suggestionstring $id ): bool {
        return 
$this->providers->hide_extension_suggestion$id );
    }

    
/**
     * Dismiss a payment extension suggestion incentive.
     *
     * @param string $suggestion_id The suggestion ID.
     * @param string $incentive_id  The incentive ID.
     * @param string $context       Optional. The context in which the incentive should be dismissed.
     *                              Default is to dismiss the incentive in all contexts.
     *
     * @return bool True if the incentive was not previously dismissed and now it is.
     *              False if the incentive was already dismissed or could not be dismissed.
     * @throws Exception If the incentive could not be dismissed due to an error.
     */
    
public function dismiss_extension_suggestion_incentivestring $suggestion_idstring $incentive_idstring $context 'all' ): bool {
        return 
$this->extension_suggestions->dismiss_incentive$incentive_id$suggestion_id$context );
    }
}