PluginProbe
WooCommerce / 11.1.0
WooCommerce v11.1.0
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Internal / Admin / Settings / PaymentsProviders / WooPayments.php
WooPayments.php
694 lines 27.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare( strict_types=1 );
3
4 namespace Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders;
5
6 use Automattic\Jetpack\Connection\Manager as WPCOM_Connection_Manager;
7 use Automattic\Jetpack\Constants;
8 use Automattic\WooCommerce\Admin\PluginsHelper;
9 use Automattic\WooCommerce\Admin\WCAdminHelper;
10 use Automattic\WooCommerce\Enums\OrderInternalStatus;
11 use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
12 use Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders;
13 use Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders\WooPayments\WooPaymentsRestController;
14 use Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders\WooPayments\WooPaymentsService;
15 use Automattic\WooCommerce\Internal\Admin\Settings\Payments;
16 use Automattic\WooCommerce\Internal\Admin\Settings\Utils;
17 use Automattic\WooCommerce\Internal\Logging\SafeGlobalFunctionProxy;
18 use Throwable;
19 use WC_Abstract_Order;
20 use WC_Payment_Gateway;
21
22 defined( 'ABSPATH' ) || exit;
23
24 /**
25 * WooPayments payment gateway provider class.
26 *
27 * This class handles all the custom logic for the WooPayments payment gateway provider.
28 */
29 class WooPayments extends PaymentGateway {
30
31 const PREFIX = 'woocommerce_admin_settings_payments__woopayments__';
32
33 /**
34 * Extract the payment gateway provider details from the object.
35 *
36 * @param WC_Payment_Gateway $gateway The payment gateway object.
37 * @param int $order Optional. The order to assign.
38 * Defaults to 0 if not provided.
39 * @param string $country_code Optional. The country code for which the details are being gathered.
40 * This should be an ISO 3166-1 alpha-2 country code.
41 *
42 * @return array The payment gateway provider details.
43 *
44 * phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.Missing -- We wrap the throw in a try/catch.
45 */
46 public function get_details( WC_Payment_Gateway $gateway, int $order = 0, string $country_code = '' ): array {
47 $details = parent::get_details( $gateway, $order, $country_code );
48
49 $has_test_account = $this->has_test_account();
50 $has_sandbox_account = $this->has_sandbox_account();
51
52 // Switch the onboarding type to native.
53 $details['onboarding']['type'] = self::ONBOARDING_TYPE_NATIVE;
54
55 // Add the test [drive] account details to the onboarding state.
56 $details['onboarding']['state']['test_drive_account'] = $has_test_account;
57
58 // Add WPCOM/Jetpack connection details to the onboarding state.
59 $details['onboarding']['state'] = array_merge( $details['onboarding']['state'], $this->get_wpcom_connection_state() );
60
61 // If the WooPayments installed version is less than minimum required version,
62 // we can't use the in-context onboarding flows.
63 if ( Constants::is_defined( 'WCPAY_VERSION_NUMBER' ) &&
64 version_compare( Constants::get_constant( 'WCPAY_VERSION_NUMBER' ), WooPaymentsService::EXTENSION_MINIMUM_VERSION, '<' ) ) {
65
66 return $details;
67 }
68
69 // Switch the onboarding type to native in-context.
70 $details['onboarding']['type'] = self::ONBOARDING_TYPE_NATIVE_IN_CONTEXT;
71
72 // Provide the native, in-context onboarding URL instead of the external one.
73 // This is a catch-all URL that should start or continue the onboarding process.
74 $details['onboarding']['_links']['onboard'] = array(
75 'href' => Utils::wc_payments_settings_url( '/woopayments/onboarding', array( 'from' => Payments::FROM_PAYMENTS_SETTINGS ) ),
76 );
77
78 try {
79 /**
80 * The WooPayments REST controller instance.
81 *
82 * @var WooPaymentsRestController $rest_controller
83 */
84 $rest_controller = wc_get_container()->get( WooPaymentsRestController::class );
85
86 // Add disable test account URL to onboarding links, if the current account is a test or sandbox account.
87 if ( $has_test_account || $has_sandbox_account ) {
88 $details['onboarding']['_links']['disable_test_account'] = array(
89 'href' => rest_url( $rest_controller->get_rest_url_path( 'onboarding/test_account/disable' ) ),
90 );
91 }
92
93 // Add reset account/onboarding URL to onboarding links.
94 $details['onboarding']['_links']['reset'] = array(
95 'href' => rest_url( $rest_controller->get_rest_url_path( 'onboarding/reset' ) ),
96 );
97 } catch ( \Throwable $e ) {
98 // If the REST controller is not available, we can't generate the REST API endpoint URLs.
99 // This is not a critical error, so we just ignore it.
100 // Log so we can investigate.
101 SafeGlobalFunctionProxy::wc_get_logger()->error(
102 'Failed to get the WooPayments REST controller instance: ' . $e->getMessage(),
103 array(
104 'source' => 'settings-payments',
105 )
106 );
107 }
108
109 // Override the onboarding state with the entries provided by the WooPayments service.
110 if ( ! empty( $country_code ) ) {
111 try {
112 /**
113 * The WooPayments service instance.
114 *
115 * @var WooPaymentsService $service
116 */
117 $service = wc_get_container()->get( WooPaymentsService::class );
118
119 // Ensure we have a valid rest_controller from the earlier try block.
120 if ( ! isset( $rest_controller ) ) {
121 throw new \RuntimeException( 'WooPayments REST controller not available' );
122 }
123
124 $onboarding_details = $service->get_onboarding_details( $country_code, $rest_controller->get_rest_url_path( 'onboarding' ) );
125 // Merge the onboarding state with the one provided by the service.
126 if ( ! empty( $onboarding_details['state'] ) && is_array( $onboarding_details['state'] ) ) {
127 $details['onboarding']['state'] = array_merge(
128 $details['onboarding']['state'],
129 $onboarding_details['state']
130 );
131 }
132 // Merge any messages provided by the service.
133 if ( ! empty( $onboarding_details['messages'] ) && is_array( $onboarding_details['messages'] ) ) {
134 if ( ! isset( $details['onboarding']['messages'] ) || ! is_array( $details['onboarding']['messages'] ) ) {
135 $details['onboarding']['messages'] = array();
136 }
137 $details['onboarding']['messages'] = array_merge(
138 $details['onboarding']['messages'],
139 $onboarding_details['messages']
140 );
141 }
142 // The steps provided by the service override any existing steps.
143 if ( ! empty( $onboarding_details['steps'] ) && is_array( $onboarding_details['steps'] ) ) {
144 $details['onboarding']['steps'] = $onboarding_details['steps'];
145 }
146 // Merge any context provided by the service.
147 if ( ! empty( $onboarding_details['context'] ) && is_array( $onboarding_details['context'] ) ) {
148 if ( ! isset( $details['onboarding']['context'] ) || ! is_array( $details['onboarding']['context'] ) ) {
149 $details['onboarding']['context'] = array();
150 }
151 $details['onboarding']['context'] = array_merge(
152 $details['onboarding']['context'],
153 $onboarding_details['context']
154 );
155 }
156 } catch ( \Throwable $e ) {
157 // If the service is not available, we can't impose the more specific logic.
158 // This is not a critical error, so we just ignore it.
159 // Log so we can investigate.
160 SafeGlobalFunctionProxy::wc_get_logger()->error(
161 'Failed to get the WooPayments service instance: ' . $e->getMessage(),
162 array(
163 'source' => 'settings-payments',
164 )
165 );
166 }
167 }
168
169 return $details;
170 }
171
172 /**
173 * Enhance this provider's payment extension suggestion with additional information.
174 *
175 * The details added do not require the payment extension to be active or a gateway instance.
176 *
177 * @param array $extension_suggestion The extension suggestion details.
178 *
179 * @return array The enhanced payment extension suggestion details.
180 */
181 public function enhance_extension_suggestion( array $extension_suggestion ): array {
182 $extension_suggestion = parent::enhance_extension_suggestion( $extension_suggestion );
183
184 // If the extension is installed, we can get the plugin data and act upon it.
185 if ( ! empty( $extension_suggestion['plugin']['file'] ) &&
186 isset( $extension_suggestion['plugin']['status'] ) &&
187 in_array( $extension_suggestion['plugin']['status'], array( PaymentsProviders::EXTENSION_INSTALLED, PaymentsProviders::EXTENSION_ACTIVE ), true ) ) {
188
189 // Switch to the native in-context onboarding type if the WooPayments extension its version is compatible.
190 // We need to put back the '.php' extension to construct the plugin filename.
191 $plugin_data = $this->proxy->call_static( PluginsHelper::class, 'get_plugin_data', $extension_suggestion['plugin']['file'] . '.php' );
192 if ( $plugin_data && ! empty( $plugin_data['Version'] ) &&
193 version_compare( $plugin_data['Version'], PaymentsProviders\WooPayments\WooPaymentsService::EXTENSION_MINIMUM_VERSION, '>=' ) ) {
194
195 $extension_suggestion['onboarding']['type'] = self::ONBOARDING_TYPE_NATIVE_IN_CONTEXT;
196 }
197 } else {
198 // We assume the latest version of the WooPayments extension will be installed.
199 $extension_suggestion['onboarding']['type'] = self::ONBOARDING_TYPE_NATIVE_IN_CONTEXT;
200 }
201
202 // Add onboarding state.
203 if ( ! isset( $extension_suggestion['onboarding']['state'] ) || ! is_array( $extension_suggestion['onboarding']['state'] ) ) {
204 $extension_suggestion['onboarding']['state'] = array();
205 }
206 // Add the store's WPCOM/Jetpack connection state to the onboarding state.
207 $extension_suggestion['onboarding']['state'] = array_merge(
208 $extension_suggestion['onboarding']['state'],
209 $this->get_wpcom_connection_state()
210 );
211
212 // Add onboarding links.
213 if ( empty( $extension_suggestion['onboarding']['_links'] ) || ! is_array( $extension_suggestion['onboarding']['_links'] ) ) {
214 $extension_suggestion['onboarding']['_links'] = array();
215 }
216
217 // We only add the preload link if we don't have a working WPCOM connection.
218 // This is because WooPayments onboarding preloading focuses on hydrating the WPCOM connection.
219 if ( ! $extension_suggestion['onboarding']['state']['wpcom_has_working_connection'] ) {
220 try {
221 /**
222 * The WooPayments REST controller instance.
223 *
224 * @var WooPaymentsRestController $rest_controller
225 */
226 $rest_controller = wc_get_container()->get( WooPaymentsRestController::class );
227
228 // Add the onboarding preload URL.
229 $extension_suggestion['onboarding']['_links']['preload'] = array(
230 'href' => rest_url( $rest_controller->get_rest_url_path( 'onboarding/preload' ) ),
231 );
232 } catch ( Throwable $e ) {
233 // If the REST controller is not available, we can't preload the onboarding data.
234 // This is not a critical error, so we just ignore it.
235 // Log so we can investigate.
236 SafeGlobalFunctionProxy::wc_get_logger()->error(
237 'Failed to get the WooPayments REST controller instance: ' . $e->getMessage(),
238 array(
239 'source' => 'settings-payments',
240 )
241 );
242 }
243 }
244
245 return $extension_suggestion;
246 }
247
248 /**
249 * Check if the payment gateway needs setup.
250 *
251 * @param WC_Payment_Gateway $payment_gateway The payment gateway object.
252 *
253 * @return bool True if the payment gateway needs setup, false otherwise.
254 */
255 public function needs_setup( WC_Payment_Gateway $payment_gateway ): bool {
256 // No account means we need setup.
257 if ( ! $this->is_account_connected( $payment_gateway ) ) {
258 return true;
259 }
260
261 // Test-drive accounts don't need setup.
262 if ( $this->has_test_account() ) {
263 return false;
264 }
265
266 return parent::needs_setup( $payment_gateway );
267 }
268
269 /**
270 * Try to determine if the payment gateway is in test mode.
271 *
272 * This is a best-effort attempt, as there is no standard way to determine this.
273 * Trust the true value, but don't consider a false value as definitive.
274 *
275 * @param WC_Payment_Gateway $payment_gateway The payment gateway object.
276 *
277 * @return bool True if the payment gateway is in test mode, false otherwise.
278 */
279 public function is_in_test_mode( WC_Payment_Gateway $payment_gateway ): bool {
280 if ( $this->proxy->call_function( 'class_exists', 'WC_Payments' ) &&
281 $this->proxy->call_function( 'is_callable', 'WC_Payments::mode' ) ) {
282
283 $woopayments_mode = $this->proxy->call_static( 'WC_Payments', 'mode' );
284 if ( $this->proxy->call_function( 'method_exists', $woopayments_mode, 'is_test' ) &&
285 $this->proxy->call_function( 'is_callable', array( $woopayments_mode, 'is_test' ) ) ) {
286
287 return $woopayments_mode->is_test();
288 }
289 }
290
291 return parent::is_in_test_mode( $payment_gateway );
292 }
293
294 /**
295 * Try to determine if the payment gateway is in dev mode.
296 *
297 * This is a best-effort attempt, as there is no standard way to determine this.
298 * Trust the true value, but don't consider a false value as definitive.
299 *
300 * @param WC_Payment_Gateway $payment_gateway The payment gateway object.
301 *
302 * @return bool True if the payment gateway is in dev mode, false otherwise.
303 */
304 public function is_in_dev_mode( WC_Payment_Gateway $payment_gateway ): bool {
305 if ( $this->proxy->call_function( 'class_exists', 'WC_Payments' ) &&
306 $this->proxy->call_function( 'is_callable', 'WC_Payments::mode' ) ) {
307
308 $woopayments_mode = $this->proxy->call_static( 'WC_Payments', 'mode' );
309 if ( $this->proxy->call_function( 'method_exists', $woopayments_mode, 'is_dev' ) &&
310 $this->proxy->call_function( 'is_callable', array( $woopayments_mode, 'is_dev' ) ) ) {
311
312 return $woopayments_mode->is_dev();
313 }
314 }
315
316 return parent::is_in_dev_mode( $payment_gateway );
317 }
318
319 /**
320 * Check if the payment gateway supports the current store state for onboarding.
321 *
322 * Most of the time the current business location should be the main factor, but could also
323 * consider other store settings like currency.
324 *
325 * @param WC_Payment_Gateway $payment_gateway The payment gateway object.
326 * @param string $country_code Optional. The country code for which to check.
327 * This should be an ISO 3166-1 alpha-2 country code.
328 *
329 * @return bool|null True if the payment gateway supports onboarding, false otherwise.
330 * If the payment gateway does not provide the information,
331 * we will return null to indicate that we don't know.
332 */
333 public function is_onboarding_supported( WC_Payment_Gateway $payment_gateway, string $country_code = '' ): ?bool {
334 $is_onboarding_supported = parent::is_onboarding_supported( $payment_gateway, $country_code );
335 if ( ! is_null( $is_onboarding_supported ) ) {
336 return $is_onboarding_supported;
337 }
338
339 // Without a country code to check against, we assume onboarding is supported to avoid blocking the user.
340 if ( empty( $country_code ) ) {
341 return true;
342 }
343
344 // Normalize the country code.
345 $country_code = strtoupper( $country_code );
346
347 // The payment gateway didn't provide the information. We will do it the hard way.
348 $supported_country_codes = $this->get_supported_country_codes();
349 // If we can't get the supported countries, we assume onboarding supported to avoid blocking the user.
350 if ( is_null( $supported_country_codes ) ) {
351 return true;
352 }
353
354 return in_array( $country_code, $supported_country_codes, true );
355 }
356
357 /**
358 * Get the message to show when the payment gateway does not support onboarding.
359 *
360 * @see self::is_onboarding_supported()
361 *
362 * @param WC_Payment_Gateway $payment_gateway The payment gateway object.
363 * @param string $country_code Optional. The country code for which to check.
364 * This should be an ISO 3166-1 alpha-2 country code.
365 *
366 * @return string|null The message to show when the payment gateway does not support onboarding,
367 * or null if no specific message should be provided.
368 */
369 public function get_onboarding_not_supported_message( WC_Payment_Gateway $payment_gateway, string $country_code = '' ): ?string {
370 $message = parent::get_onboarding_not_supported_message( $payment_gateway, $country_code );
371 if ( ! is_null( $message ) ) {
372 return $message;
373 }
374
375 return sprintf(
376 /* translators: %s: WooPayments. */
377 esc_html__( '%s is not supported in the selected business location.', 'woocommerce' ),
378 'WooPayments'
379 );
380 }
381
382 /**
383 * Try to determine if the payment gateway is in test mode onboarding (aka sandbox or test-drive).
384 *
385 * This is a best-effort attempt, as there is no standard way to determine this.
386 * Trust the true value, but don't consider a false value as definitive.
387 *
388 * @param WC_Payment_Gateway $payment_gateway The payment gateway object.
389 *
390 * @return bool True if the payment gateway is in test mode onboarding, false otherwise.
391 */
392 public function is_in_test_mode_onboarding( WC_Payment_Gateway $payment_gateway ): bool {
393 if ( $this->proxy->call_function( 'class_exists', 'WC_Payments' ) &&
394 $this->proxy->call_function( 'is_callable', 'WC_Payments::mode' ) ) {
395
396 $woopayments_mode = $this->proxy->call_static( 'WC_Payments', 'mode' );
397 if ( $this->proxy->call_function( 'method_exists', $woopayments_mode, 'is_test_mode_onboarding' ) &&
398 $this->proxy->call_function( 'is_callable', array( $woopayments_mode, 'is_test_mode_onboarding' ) ) ) {
399
400 return $woopayments_mode->is_test_mode_onboarding();
401 }
402 }
403
404 return parent::is_in_test_mode_onboarding( $payment_gateway );
405 }
406
407 /**
408 * Get the onboarding URL for the payment gateway.
409 *
410 * This URL should start or continue the onboarding process.
411 *
412 * @param WC_Payment_Gateway $payment_gateway The payment gateway object.
413 * @param string $return_url Optional. The URL to return to after onboarding.
414 * This will likely get attached to the onboarding URL.
415 *
416 * @return string The onboarding URL for the payment gateway.
417 */
418 public function get_onboarding_url( WC_Payment_Gateway $payment_gateway, string $return_url = '' ): string {
419 if ( $this->proxy->call_function( 'class_exists', 'WC_Payments_Account' ) &&
420 $this->proxy->call_function( 'is_callable', 'WC_Payments_Account::get_connect_url' ) ) {
421
422 $connect_url = $this->proxy->call_static( 'WC_Payments_Account', 'get_connect_url' );
423 } else {
424 $connect_url = parent::get_onboarding_url( $payment_gateway, $return_url );
425 }
426
427 // Default URL params to set, regardless if they exist.
428 $params = array(
429 'from' => Constants::is_defined( 'WC_Payments_Onboarding_Service::FROM_WCADMIN_PAYMENTS_SETTINGS' ) ? (string) Constants::get_constant( 'WC_Payments_Onboarding_Service::FROM_WCADMIN_PAYMENTS_SETTINGS' ) : 'WCADMIN_PAYMENT_SETTINGS',
430 'source' => Constants::is_defined( 'WC_Payments_Onboarding_Service::SOURCE_WCADMIN_SETTINGS_PAGE' ) ? (string) Constants::get_constant( 'WC_Payments_Onboarding_Service::SOURCE_WCADMIN_SETTINGS_PAGE' ) : 'wcadmin-settings-page',
431 'redirect_to_settings_page' => 'true',
432 );
433
434 // First, sanity check to handle existing accounts.
435 // Such accounts should keep their current onboarding mode.
436 // Do not force things either way.
437 if ( $this->is_account_connected( $payment_gateway ) ) {
438 return add_query_arg( $params, $connect_url );
439 }
440
441 // We don't have an account yet, so the onboarding link is used to kickstart the process.
442
443 // Default to test-account-first onboarding.
444 $live_onboarding = false;
445
446 /*
447 * Apply our routing logic to determine if we should do a live onboarding/account.
448 *
449 * For new stores (not yet launched aka in Coming Soon mode),
450 * based on the answers provided in the onboarding profile, we will do live onboarding if:
451 * - Merchant selected “I’m already selling” AND answered either:
452 * - Yes, I’m selling online.
453 * - I’m selling both online and offline.
454 *
455 * For launched stores, we will only consider live onboarding if all are true:
456 * - Store is at least 90 days old.
457 * - Store has an active payments gateway (other than WooPayments).
458 * - Store has processed a live electronic payment in the past 90 days (any gateway).
459 *
460 * @see plugins/woocommerce/client/admin/client/core-profiler/pages/UserProfile.tsx for the values.
461 */
462 if ( filter_var( get_option( 'woocommerce_coming_soon' ), FILTER_VALIDATE_BOOLEAN ) ) {
463 $onboarding_profile = get_option( OnboardingProfile::DATA_OPTION, array() );
464 if (
465 isset( $onboarding_profile['business_choice'] ) && 'im_already_selling' === $onboarding_profile['business_choice'] &&
466 isset( $onboarding_profile['selling_online_answer'] ) && (
467 'yes_im_selling_online' === $onboarding_profile['selling_online_answer'] ||
468 'im_selling_both_online_and_offline' === $onboarding_profile['selling_online_answer']
469 )
470 ) {
471 $live_onboarding = true;
472 }
473 } elseif (
474 WCAdminHelper::is_wc_admin_active_for( 90 * DAY_IN_SECONDS ) &&
475 $this->has_enabled_other_ecommerce_gateways() &&
476 $this->has_orders()
477 ) {
478 $live_onboarding = true;
479 }
480
481 // If we are doing live onboarding, we don't need to add more to the URL.
482 // But for test-drive/sandbox mode, we have work to do.
483 if ( ! $live_onboarding ) {
484 $params['test_drive'] = 'true';
485 $params['auto_start_test_drive_onboarding'] = 'true';
486 }
487
488 return add_query_arg( $params, $connect_url );
489 }
490
491 /**
492 * Check if the store has any paid orders.
493 *
494 * Currently, we look at the past 90 days and only consider orders
495 * with status `wc-completed`, `wc-processing`, or `wc-refunded`.
496 *
497 * @return boolean Whether the store has any paid orders.
498 */
499 private function has_orders(): bool {
500 $store_has_orders_transient_name = self::PREFIX . 'store_has_orders';
501
502 // First, get the stored value, if it exists.
503 // This way we avoid costly DB queries and API calls.
504 $has_orders = get_transient( $store_has_orders_transient_name );
505 if ( false !== $has_orders ) {
506 return wc_string_to_bool( $has_orders );
507 }
508
509 // We need to determine the value.
510 // Start with the assumption that the store doesn't have orders in the timeframe we look at.
511 $has_orders = false;
512 // By default, we will check for new orders every 6 hours.
513 $expiration = 6 * HOUR_IN_SECONDS;
514
515 // Get the latest completed, processing, or refunded order.
516 $latest_order = wc_get_orders(
517 array(
518 'status' => array( OrderInternalStatus::COMPLETED, OrderInternalStatus::PROCESSING, OrderInternalStatus::REFUNDED ),
519 'limit' => 1,
520 'orderby' => 'date',
521 'order' => 'DESC',
522 )
523 );
524 if ( ! empty( $latest_order ) ) {
525 $latest_order = reset( $latest_order );
526 // If the latest order is within the timeframe we look at, we consider the store to have orders.
527 // Otherwise, it clearly doesn't have orders.
528 if ( $latest_order instanceof WC_Abstract_Order
529 && strtotime( (string) $latest_order->get_date_created() ) >= strtotime( '-90 days' ) ) {
530
531 $has_orders = true;
532
533 // For ultimate efficiency, we will check again after 90 days from the latest order
534 // because in all that time we will consider the store to have orders regardless of new orders.
535 $expiration = strtotime( (string) $latest_order->get_date_created() ) + 90 * DAY_IN_SECONDS - time();
536 }
537 }
538
539 // Store the value for future use.
540 set_transient( $store_has_orders_transient_name, $has_orders ? 'yes' : 'no', $expiration );
541
542 return $has_orders;
543 }
544
545 /**
546 * Check if the store has any other enabled ecommerce gateways.
547 *
548 * We exclude offline payment methods from this check.
549 *
550 * @return bool True if the store has any enabled ecommerce gateways, false otherwise.
551 */
552 private function has_enabled_other_ecommerce_gateways(): bool {
553 $gateways = WC()->payment_gateways()->payment_gateways;
554 $other_ecommerce_gateways = array_filter(
555 $gateways,
556 function ( $gateway ) {
557 // Filter out offline gateways and WooPayments.
558 return 'yes' === $gateway->enabled &&
559 ! in_array(
560 $gateway->id,
561 array( 'woocommerce_payments', ...PaymentsProviders::OFFLINE_METHODS ),
562 true
563 );
564 }
565 );
566
567 return ! empty( $other_ecommerce_gateways );
568 }
569
570 /**
571 * Determines if the current account is a test account.
572 *
573 * Test accounts are test-drive accounts.
574 * They are different from sandbox accounts (i.e. accounts onboarded in test mode).
575 *
576 * @return bool True if the account is a test account, false otherwise.
577 */
578 private function has_test_account(): bool {
579 if ( $this->proxy->call_function( 'function_exists', 'wcpay_get_container' ) &&
580 $this->proxy->call_function( 'class_exists', 'WC_Payments_Account' ) ) {
581
582 $woopayments_container = $this->proxy->call_function( 'wcpay_get_container' );
583 $account_service = $woopayments_container->get( 'WC_Payments_Account' );
584 if ( ! empty( $account_service ) &&
585 $this->proxy->call_function( 'method_exists', $account_service, 'get_account_status_data' ) &&
586 $this->proxy->call_function( 'is_callable', array( $account_service, 'get_account_status_data' ) ) ) {
587
588 $account_status = $account_service->get_account_status_data();
589
590 return ! empty( $account_status['testDrive'] );
591 }
592 }
593
594 return false;
595 }
596
597 /**
598 * Determines if the current account is a sandbox account.
599 *
600 * Sandbox accounts are accounts that were onboarded in test mode.
601 * They are different from test accounts (i.e. test-drive accounts).
602 *
603 * Sandbox accounts are generally created in development or staging environments when simulating live onboarding.
604 *
605 * @return bool True if the account is a sandbox account, false otherwise.
606 */
607 private function has_sandbox_account(): bool {
608 if ( $this->proxy->call_function( 'function_exists', 'wcpay_get_container' ) &&
609 $this->proxy->call_function( 'class_exists', 'WC_Payments_Account' ) ) {
610
611 $woopayments_container = $this->proxy->call_function( 'wcpay_get_container' );
612 $account_service = $woopayments_container->get( 'WC_Payments_Account' );
613 if ( ! empty( $account_service ) &&
614 $this->proxy->call_function( 'method_exists', $account_service, 'get_account_status_data' ) &&
615 $this->proxy->call_function( 'is_callable', array( $account_service, 'get_account_status_data' ) ) ) {
616
617 $account_status = $account_service->get_account_status_data();
618
619 return empty( $account_status['isLive'] ) && empty( $account_status['testDrive'] );
620 }
621 }
622
623 return false;
624 }
625
626 /**
627 * Get the list of supported country codes for WooPayments.
628 *
629 * @return array|null The list of supported countries as ISO 3166-1 alpha-2 country codes.
630 * The country codes are normalized in uppercase.
631 * If the list cannot be retrieved, null is returned.
632 */
633 private function get_supported_country_codes(): ?array {
634 try {
635 if ( $this->proxy->call_function( 'class_exists', 'WC_Payments_Utils' ) &&
636 $this->proxy->call_function( 'is_callable', 'WC_Payments_Utils::supported_countries' ) ) {
637
638 $supported_country_codes = $this->proxy->call_static( 'WC_Payments_Utils', 'supported_countries' );
639 if ( is_array( $supported_country_codes ) ) {
640 return array_unique( array_map( 'strtoupper', array_keys( $supported_country_codes ) ) );
641 }
642 }
643 } catch ( Throwable $e ) {
644 // This is not a critical error, so we just ignore it.
645 // Log so we can investigate.
646 SafeGlobalFunctionProxy::wc_get_logger()->error(
647 'Failed to get the WooPayments supported country codes list: ' . $e->getMessage(),
648 array(
649 'source' => 'settings-payments',
650 )
651 );
652 }
653
654 return null;
655 }
656
657 /**
658 * Get the current state of the store's WPCOM/Jetpack connection.
659 *
660 * @return array The store's WPCOM/Jetpack connection state.
661 */
662 private function get_wpcom_connection_state(): array {
663 try {
664 $wpcom_connection_manager = $this->proxy->get_instance_of( WPCOM_Connection_Manager::class, 'woocommerce' );
665 } catch ( \Throwable $e ) {
666 // Log so we can investigate.
667 SafeGlobalFunctionProxy::wc_get_logger()->error(
668 'Failed to get the WPCOM/Jetpack Connection Manager instance: ' . $e->getMessage(),
669 array(
670 'source' => 'settings-payments',
671 )
672 );
673
674 // Assume no connection.
675 return array(
676 'wpcom_has_working_connection' => false,
677 'wpcom_is_store_connected' => false,
678 'wpcom_has_connected_owner' => false,
679 'wpcom_is_connection_owner' => false,
680 );
681 }
682
683 $is_connected = $wpcom_connection_manager->is_connected();
684 $has_connected_owner = $wpcom_connection_manager->has_connected_owner();
685
686 return array(
687 'wpcom_has_working_connection' => $is_connected && $has_connected_owner,
688 'wpcom_is_store_connected' => $is_connected,
689 'wpcom_has_connected_owner' => $has_connected_owner,
690 'wpcom_is_connection_owner' => $has_connected_owner && $wpcom_connection_manager->is_connection_owner(),
691 );
692 }
693 }
694