mended_pms Optional. The recommended payment methods to use. * * @return array The onboarding payment methods state. */ private function get_onboarding_payment_methods_state( string $location, ?array $recommended_pms ): array { // First, get the recommended payment methods details from the provider. // We will use their enablement state as the default. // Note: The list is validated and standardized by the provider, so we don't need to do it here. if ( null === $recommended_pms ) { $recommended_pms = $this->get_onboarding_recommended_payment_methods( $location ); } if ( empty( $recommended_pms ) ) { // If there are no recommended payment methods, return an empty array. return array(); } // Grab the stored payment methods state // (a key-value array of payment method IDs and if they should be automatically enabled or not). $step_pms_data = (array) $this->get_nox_profile_onboarding_step_data_entry( self::ONBOARDING_STEP_PAYMENT_METHODS, $location, 'payment_methods' ); $payment_methods_state = array(); $apple_pay_enabled = false; $google_pay_enabled = false; foreach ( $recommended_pms as $recommended_pm ) { $pm_id = $recommended_pm['id']; /** * We need to handle Apple Pay and Google Pay separately. * They are not stored in the same way as the other payment methods. */ if ( 'apple_pay' === $pm_id ) { $apple_pay_enabled = $recommended_pm['enabled']; continue; } if ( 'google_pay' === $pm_id ) { $google_pay_enabled = $recommended_pm['enabled']; continue; } // Start with the recommended enabled state. $payment_methods_state[ $pm_id ] = $recommended_pm['enabled']; // Force enable if required. if ( $recommended_pm['required'] ) { $payment_methods_state[ $pm_id ] = true; continue; } // Check the stored state, if any. if ( isset( $step_pms_data[ $pm_id ] ) ) { $payment_methods_state[ $pm_id ] = wc_string_to_bool( $step_pms_data[ $pm_id ] ); } } // Combine Apple Pay and Google Pay into a single `apple_google` entry. // First check if apple_google is explicitly stored, otherwise fallback to combining individual states. if ( isset( $step_pms_data['apple_google'] ) ) { $apple_google_enabled = wc_string_to_bool( $step_pms_data['apple_google'] ); } else { // Fallback to OR logic for backward compatibility. $apple_google_enabled = $apple_pay_enabled || $google_pay_enabled; } $payment_methods_state['apple_google'] = $apple_google_enabled; return $payment_methods_state; } /** * Get the WPCOM (Jetpack) connection authorization details. * * @param string $return_url The URL to redirect to after the connection is set up. * * @return array The WPCOM connection authorization details. */ private function get_wpcom_connection_authorization( string $return_url ): array { return $this->proxy->call_static( Utils::class, 'get_wpcom_connection_authorization', $return_url ); } /** * Get the store's WPCOM (Jetpack) connection state. * * @return array The WPCOM connection state. */ private function get_wpcom_connection_state(): array { $is_connected = $this->wpcom_connection_manager->is_connected(); $has_connected_owner = $this->wpcom_connection_manager->has_connected_owner(); return array( 'has_working_connection' => $this->has_working_wpcom_connection(), 'is_store_connected' => $is_connected, 'has_connected_owner' => $has_connected_owner, 'is_connection_owner' => $has_connected_owner && $this->wpcom_connection_manager->is_connection_owner(), ); } /** * Check if the store has a working WPCOM connection. * * The store is considered to have a working WPCOM connection if: * - The store is connected to WPCOM (blog ID and tokens are set). * - The store connection has a connected owner (connection owner is set). * * @return bool Whether the store has a working WPCOM connection. */ private function has_working_wpcom_connection(): bool { return $this->wpcom_connection_manager->is_connected() && $this->wpcom_connection_manager->has_connected_owner(); } /** * Check if the WooPayments plugin is active. * * @return boolean */ private function is_extension_active(): bool { return $this->proxy->call_function( 'class_exists', '\WC_Payments' ); } /** * Get the main payment gateway instance. * * @return \WC_Payment_Gateway The main payment gateway instance. */ private function get_payment_gateway(): \WC_Payment_Gateway { return $this->proxy->call_static( '\WC_Payments', 'get_gateway' ); } /** * Determine if WooPayments has an account set up. * * @return bool Whether WooPayments has an account set up. */ private function has_account(): bool { return $this->provider->is_account_connected( $this->get_payment_gateway() ); } /** * Determine if WooPayments has a valid, fully onboarded account set up. * * @return bool Whether WooPayments has a valid, fully onboarded account set up. */ private function has_valid_account(): bool { if ( ! $this->has_account() ) { return false; } $account_service = $this->proxy->call_static( '\WC_Payments', 'get_account_service' ); return $account_service->is_stripe_account_valid(); } /** * Determine if WooPayments has a working account set up. * * This is a more specific check than has_valid_account() and checks if payments are enabled for the account. * * @return bool Whether WooPayments has a working account set up. */ private function has_working_account(): bool { if ( ! $this->has_account() ) { return false; } $account_service = $this->proxy->call_static( '\WC_Payments', 'get_account_service' ); $account_status = $account_service->get_account_status_data(); return ! empty( $account_status['paymentsEnabled'] ); } /** * Determine if WooPayments has a test account set up. * * @return bool Whether WooPayments has a test account set up. */ private function has_test_account(): bool { if ( ! $this->has_account() ) { return false; } $account_service = $this->proxy->call_static( '\WC_Payments', 'get_account_service' ); $account_status = $account_service->get_account_status_data(); return ! empty( $account_status['testDrive'] ); } /** * Determine if WooPayments has a sandbox account set up. * * @return bool Whether WooPayments has a sandbox account set up. */ private function has_sandbox_account(): bool { if ( ! $this->has_account() ) { return false; } $account_service = $this->proxy->call_static( '\WC_Payments', 'get_account_service' ); $account_status = $account_service->get_account_status_data(); return empty( $account_status['isLive'] ) && empty( $account_status['testDrive'] ); } /** * Determine if WooPayments has a live account set up. * * @return bool Whether WooPayments has a test account set up. */ private function has_live_account(): bool { if ( ! $this->has_account() ) { return false; } $account_service = $this->proxy->call_static( '\WC_Payments', 'get_account_service' ); $account_status = $account_service->get_account_status_data(); return ! empty( $account_status['isLive'] ); } /** * Get the onboarding fields data for the KYC business verification. * * @param string $location The location for which we are onboarding. * This is an ISO 3166-1 alpha-2 country code. * * @return array The onboarding fields data. * @throws Exception If the onboarding fields data could not be retrieved or there was an error. */ private function get_onboarding_kyc_fields( string $location ): array { // Call the WooPayments API to get the onboarding fields. $response = $this->proxy->call_static( Utils::class, 'rest_endpoint_get_request', '/wc/v3/payments/onboarding/fields' ); if ( is_wp_error( $response ) ) { throw new Exception( esc_html( $response->get_error_message() ) ); } if ( ! is_array( $response ) || ! isset( $response['data'] ) ) { throw new Exception( esc_html__( 'Failed to get onboarding fields data.', 'woocommerce' ) ); } $fields = $response['data']; // If there is no available_countries entry, add it. if ( ! isset( $fields['available_countries'] ) && class_exists( '\WC_Payments_Utils' ) && $this->proxy->call_function( 'is_callable', '\WC_Payments_Utils::supported_countries' ) ) { $fields['available_countries'] = $this->proxy->call_static( '\WC_Payments_Utils', 'supported_countries' ); } $fields['location'] = $location; return $fields; } /** * Get the fallback URL for the embedded KYC flow. * * @return string The fallback URL for the embedded KYC flow. */ private function get_onboarding_kyc_fallback_url(): string { if ( $this->proxy->call_function( 'is_callable', '\WC_Payments_Account::get_connect_url' ) ) { return $this->proxy->call_static( '\WC_Payments_Account', 'get_connect_url', self::FROM_NOX_IN_CONTEXT ); } // Fall back to the provider onboarding URL. return $this->provider->get_onboarding_url( $this->get_payment_gateway(), Utils::wc_payments_settings_url( self::ONBOARDING_PATH_BASE, array( 'from' => self::FROM_KYC ) ) ); } /** * Get the WooPayments Overview page URL. * * @return string The WooPayments Overview page URL. */ private function get_overview_page_url(): string { if ( $this->proxy->call_function( 'is_callable', '\WC_Payments_Account::get_overview_page_url' ) ) { return add_query_arg( array( 'from' => self::FROM_NOX_IN_CONTEXT, ), $this->proxy->call_static( '\WC_Payments_Account', 'get_overview_page_url' ) ); } // Fall back to the known WooPayments Overview page URL. return add_query_arg( array( 'page' => 'wc-admin', 'path' => '/payments/overview', 'from' => self::FROM_NOX_IN_CONTEXT, ), admin_url( 'admin.php' ) ); } /** * Check the onboarding source and ensure it is a valid value. * * @param string|null $source The source of the onboarding request. * * @return string The validated onboarding source. */ private function validate_onboarding_source( ?string $source ): string { if ( empty( $source ) ) { return self::SESSION_ENTRY_DEFAULT; } $valid_sources = array( self::SESSION_ENTRY_DEFAULT, self::SESSION_ENTRY_LYS, ); return in_array( $source, $valid_sources, true ) ? $source : self::SESSION_ENTRY_DEFAULT; } }