Commit 6735982c21 for woocommerce
commit 6735982c21142f033158a4ce64221a4169411d87
Author: Wesley Rosa <wesleyjrosa@gmail.com>
Date: Mon Jan 5 08:32:45 2026 -0300
PayPal Standard Refactor 4: Moving the TransactAccountManager class to the src folder (#62624)
* Moving the TransactAccountManager class to the src folder
* Removing private methods
* Add missing namespace
* Add changefile(s) from automation for the following project(s): woocommerce
* Adding deprecated function calls
* Fix docblock for test class
* Adding param and return types
* Adding param and return types
* Adding param and return types
* Fix get_transact_account_data return type
* Fix param type
* Fix return type
* Fix PHPStan issues
* Updating PHPStan baseline file
---------
Co-authored-by: github-actions <github-actions@github.com>
diff --git a/plugins/woocommerce/changelog/62624-refactor-paypal-standard-4-transact-account-manager-class b/plugins/woocommerce/changelog/62624-refactor-paypal-standard-4-transact-account-manager-class
new file mode 100644
index 0000000000..a7bdfe0c80
--- /dev/null
+++ b/plugins/woocommerce/changelog/62624-refactor-paypal-standard-4-transact-account-manager-class
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+
+Deprecate WC_Gateway_Paypal_Transact_Account_Manager class in favor of Automattic\WooCommerce\Gateways\PayPal\TransactAccountManager class.
\ No newline at end of file
diff --git a/plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.php b/plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.php
index 7a352b2455..6c3c3205ea 100644
--- a/plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.php
+++ b/plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.php
@@ -16,6 +16,7 @@ use Automattic\Jetpack\Connection\Manager as Jetpack_Connection_Manager;
use Automattic\WooCommerce\Gateways\PayPal\Constants as PayPalConstants;
use Automattic\WooCommerce\Gateways\PayPal\Helper as PayPalHelper;
use Automattic\WooCommerce\Gateways\PayPal\Notices as PayPalNotices;
+use Automattic\WooCommerce\Gateways\PayPal\TransactAccountManager as PayPalTransactAccountManager;
if ( ! defined( 'ABSPATH' ) ) {
exit;
@@ -313,8 +314,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
return;
}
- include_once __DIR__ . '/includes/class-wc-gateway-paypal-transact-account-manager.php';
- $transact_account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this );
+ $transact_account_manager = new PayPalTransactAccountManager( $this );
$transact_account_manager->do_onboarding();
}
@@ -948,8 +948,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
}
// We need merchant and provider accounts with Transact to be able to use the proxy.
- include_once __DIR__ . '/includes/class-wc-gateway-paypal-transact-account-manager.php';
- $transact_account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this );
+ $transact_account_manager = new PayPalTransactAccountManager( $this );
$merchant_account_data = $transact_account_manager->get_transact_account_data( 'merchant' );
if ( empty( $merchant_account_data ) ) {
return false;
diff --git a/plugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php b/plugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
index 63ad7fc59e..49ec94d2c3 100644
--- a/plugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
+++ b/plugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
@@ -3,11 +3,13 @@
* Class WC_Gateway_Paypal_Transact_Account_Manager file.
*
* @package WooCommerce\Gateways
+ *
+ * @deprecated 10.5.0 Use Automattic\WooCommerce\Gateways\PayPal\TransactAccountManager instead. This class will be removed in 11.0.0.
*/
declare(strict_types=1);
-use Automattic\Jetpack\Connection\Client as Jetpack_Connection_Client;
+use Automattic\WooCommerce\Gateways\PayPal\TransactAccountManager as PayPalTransactAccountManager;
if ( ! defined( 'ABSPATH' ) ) {
exit;
@@ -15,45 +17,16 @@ if ( ! defined( 'ABSPATH' ) ) {
/**
* Handles Transact account management.
+ *
+ * @deprecated 10.5.0 Use Automattic\WooCommerce\Gateways\PayPal\TransactAccountManager instead. This class will be removed in 11.0.0.
*/
final class WC_Gateway_Paypal_Transact_Account_Manager {
/**
- * The API version for the proxy endpoint.
- *
- * @var int
- */
- private const WPCOM_PROXY_ENDPOINT_API_VERSION = 2;
-
- /**
- * Transact provider type, for provider onboarding.
- *
- * @var string
- */
- private const TRANSACT_PROVIDER_TYPE = 'paypal_standard';
-
- /**
- * Cache keys for the merchant and provider accounts.
- *
- * @var string
- */
- private const TRANSACT_MERCHANT_ACCOUNT_CACHE_KEY_LIVE = 'woocommerce_paypal_transact_merchant_account_live';
- private const TRANSACT_MERCHANT_ACCOUNT_CACHE_KEY_TEST = 'woocommerce_paypal_transact_merchant_account_test';
- private const TRANSACT_PROVIDER_ACCOUNT_CACHE_KEY_LIVE = 'woocommerce_paypal_transact_provider_account_live';
- private const TRANSACT_PROVIDER_ACCOUNT_CACHE_KEY_TEST = 'woocommerce_paypal_transact_provider_account_test';
-
- /**
- * The expiry time for the Transact account cache.
+ * The delegated TransactAccountManager instance.
*
- * @var int
+ * @var PayPalTransactAccountManager
*/
- private const TRANSACT_ACCOUNT_CACHE_EXPIRY = 60 * 60 * 24; // 24 hours.
-
- /**
- * Paypal gateway object.
- *
- * @var WC_Gateway_Paypal
- */
- private $gateway;
+ private $transact_account_manager;
/**
* Constructor.
@@ -61,71 +34,24 @@ final class WC_Gateway_Paypal_Transact_Account_Manager {
* @param WC_Gateway_Paypal $gateway Paypal gateway object.
*/
public function __construct( WC_Gateway_Paypal $gateway ) {
- $this->gateway = $gateway;
+ $this->transact_account_manager = new PayPalTransactAccountManager( $gateway );
}
/**
* Onboard the merchant with the Transact platform.
*
* @return void
+ *
+ * @deprecated 10.5.0 Use Automattic\WooCommerce\Gateways\PayPal\TransactAccountManager::do_onboarding() instead. This method will be removed in 11.0.0.
*/
public function do_onboarding() {
- // Check that we have a PayPal email. This is required for processing payments --
- // used as the payee email. Only begin onboarding if this minimum requirement is met.
- if ( empty( $this->gateway->email ) ) {
- return;
- }
-
- // Register with Jetpack if not already connected.
- $jetpack_connection_manager = $this->gateway->get_jetpack_connection_manager();
- if ( ! $jetpack_connection_manager ) {
- WC_Gateway_Paypal::log( 'Jetpack connection manager not found.', 'error' );
- return;
- }
-
- if ( ! $jetpack_connection_manager->is_connected() ) {
- $result = $jetpack_connection_manager->try_registration();
- if ( is_wp_error( $result ) ) {
- WC_Gateway_Paypal::log( 'Jetpack registration failed: ' . $result->get_error_message(), 'error' );
- return;
- }
- }
-
- // Fetch (cached) or create the Transact merchant and provider accounts.
- $merchant_account_data = $this->get_transact_account_data( 'merchant' );
- if ( empty( $merchant_account_data ) ) {
- $merchant_account = $this->create_merchant_account();
- if ( empty( $merchant_account ) ) {
- WC_Gateway_Paypal::log( 'Transact merchant onboarding failed.', 'error' );
- return;
- }
-
- // Cache the merchant account data.
- $this->update_transact_account_cache(
- $this->get_cache_key( 'merchant' ),
- $merchant_account
- );
- }
-
- $provider_account_data = $this->get_transact_account_data( 'provider' );
- if ( empty( $provider_account_data ) ) {
- $provider_account = $this->create_provider_account();
- if ( ! $provider_account ) {
- WC_Gateway_Paypal::log( 'Transact provider onboarding failed.', 'error' );
- return;
- }
-
- // Cache the provider account data.
- $this->update_transact_account_cache(
- $this->get_cache_key( 'provider' ),
- $provider_account
- );
- }
+ wc_deprecated_function(
+ __METHOD__,
+ '10.5.0',
+ PayPalTransactAccountManager::class . '::do_onboarding()'
+ );
- // Set an extra flag to indicate that we've completed onboarding,
- // so we can do inexpensive early returns for checkers like
- // WC_Gateway_Paypal::should_use_orders_v2().
- $this->gateway->set_transact_onboarding_complete();
+ $this->transact_account_manager->do_onboarding();
}
/**
@@ -134,231 +60,16 @@ final class WC_Gateway_Paypal_Transact_Account_Manager {
*
* @param string $account_type The type of account to get (merchant or provider).
* @return array|null Returns null if the transact account cannot be retrieved.
- */
- public function get_transact_account_data( $account_type ) {
- $cache_key = $this->get_cache_key( $account_type );
-
- // Get transact account from cache. If not found, fetch/create it.
- $transact_account = $this->get_transact_account_from_cache( $cache_key );
- if ( empty( $transact_account ) ) {
- $transact_account = 'merchant' === $account_type ? $this->fetch_merchant_account() : $this->fetch_provider_account();
-
- // Fetch failed.
- if ( empty( $transact_account ) ) {
- return null;
- }
-
- // Update cache.
- $this->update_transact_account_cache( $cache_key, $transact_account );
- }
-
- return $transact_account;
- }
-
- /**
- * Get the cache key for the transact account.
- *
- * @param string $account_type The type of account to get (merchant or provider).
- * @return string|null The cache key, or null if the account type is invalid.
- */
- private function get_cache_key( $account_type ) {
- if ( 'merchant' === $account_type ) {
- return $this->gateway->testmode ? self::TRANSACT_MERCHANT_ACCOUNT_CACHE_KEY_TEST : self::TRANSACT_MERCHANT_ACCOUNT_CACHE_KEY_LIVE;
- }
-
- if ( 'provider' === $account_type ) {
- return $this->gateway->testmode ? self::TRANSACT_PROVIDER_ACCOUNT_CACHE_KEY_TEST : self::TRANSACT_PROVIDER_ACCOUNT_CACHE_KEY_LIVE;
- }
-
- return null;
- }
-
- /**
- * Fetch the merchant account from the Transact platform.
*
- * @return array|null The API response body, or null if the request fails.
+ * @deprecated 10.5.0 Use Automattic\WooCommerce\Gateways\PayPal\TransactAccountManager::get_transact_account_data() instead. This method will be removed in 11.0.0.
*/
- private function fetch_merchant_account() {
- $site_id = \Jetpack_Options::get_option( 'id' );
- if ( ! $site_id ) {
- return null;
- }
-
- $request_body = array(
- 'test_mode' => $this->gateway->testmode,
- );
-
- $response = $this->send_transact_api_request(
- 'GET',
- sprintf( '/sites/%d/transact/account', $site_id ),
- $request_body
- );
-
- if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
- return null;
- }
-
- $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
- if ( empty( $response_data['public_id'] ) ) {
- return null;
- }
-
- return array( 'public_id' => $response_data['public_id'] );
- }
-
- /**
- * Fetch the provider account from the Transact platform.
- *
- * @return bool True if the provider account exists, false otherwise.
- */
- private function fetch_provider_account() {
- $site_id = \Jetpack_Options::get_option( 'id' );
- if ( ! $site_id ) {
- return false;
- }
-
- $request_body = array(
- 'test_mode' => $this->gateway->testmode,
- 'provider_type' => self::TRANSACT_PROVIDER_TYPE,
- );
-
- $response = $this->send_transact_api_request(
- 'GET',
- sprintf( '/sites/%d/transact/account/%s', $site_id, self::TRANSACT_PROVIDER_TYPE ),
- $request_body
- );
-
- if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
- return false;
- }
-
- // Provider account response only returns an empty onboarding link,
- // which we do not need.
- return true;
- }
-
- /**
- * Create the merchant account with the Transact platform.
- *
- * @return array|null The API response body, or null if the request fails.
- */
- private function create_merchant_account() {
- $site_id = \Jetpack_Options::get_option( 'id' );
- if ( ! $site_id ) {
- return null;
- }
-
- $request_body = array( 'test_mode' => $this->gateway->testmode );
-
- $response = $this->send_transact_api_request(
- 'POST',
- sprintf( '/sites/%d/transact/account', $site_id ),
- $request_body
- );
-
- if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
- return null;
- }
-
- $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
- if ( empty( $response_data['public_id'] ) ) {
- WC_Gateway_Paypal::log( 'Transact merchant account creation failed. Response body: ' . wc_print_r( $response_data, true ) );
- return null;
- }
-
- return array( 'public_id' => $response_data['public_id'] );
- }
-
- /**
- * Create the provider account with the Transact platform.
- *
- * @return bool True if the provider account creation was successful, false otherwise.
- */
- private function create_provider_account() {
- $site_id = \Jetpack_Options::get_option( 'id' );
- if ( ! $site_id ) {
- return false;
- }
-
- $request_body = array(
- 'test_mode' => $this->gateway->testmode,
- 'provider_type' => self::TRANSACT_PROVIDER_TYPE,
- );
- $response = $this->send_transact_api_request(
- 'POST',
- sprintf( '/sites/%d/transact/account/%s/onboard', $site_id, self::TRANSACT_PROVIDER_TYPE ),
- $request_body
- );
-
- if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
- return false;
- }
-
- // Provider account response only returns an empty onboarding link,
- // which we do not need.
- return true;
- }
-
- /**
- * Update the transact account (merchant or provider) cache.
- *
- * @param string $cache_key The cache key to update.
- * @param array $account_data The transact account data.
- */
- private function update_transact_account_cache( $cache_key, $account_data ) {
- $expires = time() + self::TRANSACT_ACCOUNT_CACHE_EXPIRY;
- update_option(
- $cache_key,
- array(
- 'account' => $account_data,
- 'expiry' => $expires,
- )
- );
- }
-
- /**
- * Get the transact account (merchant or provider) from the database cache.
- *
- * @param string $cache_key The cache key to get the account.
- * @return bool|null The transact account data, or null if the cache is
- * empty or expired.
- */
- private function get_transact_account_from_cache( $cache_key ) {
- $transact_account = get_option( $cache_key, null );
-
- if ( empty( $transact_account ) || ( isset( $transact_account['expiry'] ) && $transact_account['expiry'] < time() ) ) {
- return null;
- }
-
- return $transact_account['account'] ?? null;
- }
-
- /**
- * Send a request to the Transact platform.
- *
- * @param string $method The HTTP method to use.
- * @param string $endpoint The endpoint to request.
- * @param array $request_body The request body.
- *
- * @return array|null The API response body, or null if the request fails.
- */
- private function send_transact_api_request( $method, $endpoint, $request_body ) {
- if ( 'GET' === $method ) {
- $endpoint .= '?' . http_build_query( $request_body );
- }
-
- $response = Jetpack_Connection_Client::wpcom_json_api_request_as_blog(
- $endpoint,
- self::WPCOM_PROXY_ENDPOINT_API_VERSION,
- array(
- 'headers' => array( 'Content-Type' => 'application/json' ),
- 'method' => $method,
- 'timeout' => WC_Gateway_Paypal_Constants::WPCOM_PROXY_REQUEST_TIMEOUT,
- ),
- 'GET' === $method ? null : wp_json_encode( $request_body ),
- 'wpcom'
+ public function get_transact_account_data( $account_type ) {
+ wc_deprecated_function(
+ __METHOD__,
+ '10.5.0',
+ PayPalTransactAccountManager::class . '::get_transact_account_data()'
);
- return $response;
+ return $this->transact_account_manager->get_transact_account_data( $account_type );
}
}
diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index 4499632727..64c0c612f5 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -25164,84 +25164,6 @@ parameters:
count: 2
path: includes/gateways/paypal/includes/class-wc-gateway-paypal-response.php
- -
- message: '#^Method WC_Gateway_Paypal_Transact_Account_Manager\:\:get_transact_account_data\(\) should return array\|null but returns array\|true\.$#'
- identifier: return.type
- count: 1
- path: includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
-
- -
- message: '#^Method WC_Gateway_Paypal_Transact_Account_Manager\:\:send_transact_api_request\(\) never returns array so it can be removed from the return type\.$#'
- identifier: return.unusedType
- count: 1
- path: includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
-
- -
- message: '#^Method WC_Gateway_Paypal_Transact_Account_Manager\:\:send_transact_api_request\(\) never returns null so it can be removed from the return type\.$#'
- identifier: return.unusedType
- count: 1
- path: includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
-
- -
- message: '#^Method WC_Gateway_Paypal_Transact_Account_Manager\:\:send_transact_api_request\(\) should return array\|null but returns Automattic\\Jetpack\\Connection\\_WP_Remote_Response_Array\|WP_Error\.$#'
- identifier: return.type
- count: 1
- path: includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
-
- -
- message: '#^Method WC_Gateway_Paypal_Transact_Account_Manager\:\:update_transact_account_cache\(\) has no return type specified\.$#'
- identifier: missingType.return
- count: 1
- path: includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
-
- -
- message: '#^Parameter \#1 \$cache_key of method WC_Gateway_Paypal_Transact_Account_Manager\:\:get_transact_account_from_cache\(\) expects string, string\|null given\.$#'
- identifier: argument.type
- count: 1
- path: includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
-
- -
- message: '#^Parameter \#1 \$cache_key of method WC_Gateway_Paypal_Transact_Account_Manager\:\:update_transact_account_cache\(\) expects string, string\|null given\.$#'
- identifier: argument.type
- count: 3
- path: includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
-
- -
- message: '#^Parameter \#1 \$response of function wp_remote_retrieve_body expects array\|WP_Error, array\|null given\.$#'
- identifier: argument.type
- count: 2
- path: includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
-
- -
- message: '#^Parameter \#1 \$response of function wp_remote_retrieve_response_code expects array\|WP_Error, array\|null given\.$#'
- identifier: argument.type
- count: 4
- path: includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
-
- -
- message: '#^Parameter \#2 \$account_data of method WC_Gateway_Paypal_Transact_Account_Manager\:\:update_transact_account_cache\(\) expects array, array\|true given\.$#'
- identifier: argument.type
- count: 1
- path: includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
-
- -
- message: '#^Parameter \#2 \$account_data of method WC_Gateway_Paypal_Transact_Account_Manager\:\:update_transact_account_cache\(\) expects array, true given\.$#'
- identifier: argument.type
- count: 1
- path: includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
-
- -
- message: '#^Parameter \#2 \$version of static method Automattic\\Jetpack\\Connection\\Client\:\:wpcom_json_api_request_as_blog\(\) expects string, int given\.$#'
- identifier: argument.type
- count: 1
- path: includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
-
- -
- message: '#^Parameter \#4 \$body of static method Automattic\\Jetpack\\Connection\\Client\:\:wpcom_json_api_request_as_blog\(\) expects array\|string\|null, string\|false\|null given\.$#'
- identifier: argument.type
- count: 1
- path: includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php
-
-
message: '#^Method WC_Gateway_Paypal_Webhook_Handler\:\:process_checkout_order_approved\(\) has no return type specified\.$#'
identifier: missingType.return
@@ -64275,6 +64197,42 @@ parameters:
count: 1
path: src/Gateways/PayPal/AddressRequirements.php
+ -
+ message: '#^Method Automattic\\WooCommerce\\Gateways\\PayPal\\TransactAccountManager\:\:send_transact_api_request\(\) never returns array so it can be removed from the return type\.$#'
+ identifier: return.unusedType
+ count: 1
+ path: src/Gateways/PayPal/TransactAccountManager.php
+
+ -
+ message: '#^Method Automattic\\WooCommerce\\Gateways\\PayPal\\TransactAccountManager\:\:send_transact_api_request\(\) should return array\|WP_Error but returns Automattic\\Jetpack\\Connection\\_WP_Remote_Response_Array\|WP_Error\.$#'
+ identifier: return.type
+ count: 1
+ path: src/Gateways/PayPal/TransactAccountManager.php
+
+ -
+ message: '#^PHPDoc tag @param for parameter \$cache_key with type string\|null is not subtype of native type string\.$#'
+ identifier: parameter.phpDocType
+ count: 1
+ path: src/Gateways/PayPal/TransactAccountManager.php
+
+ -
+ message: '#^Parameter \#1 \$cache_key of method Automattic\\WooCommerce\\Gateways\\PayPal\\TransactAccountManager\:\:get_transact_account_from_cache\(\) expects string, string\|null given\.$#'
+ identifier: argument.type
+ count: 1
+ path: src/Gateways/PayPal/TransactAccountManager.php
+
+ -
+ message: '#^Parameter \#1 \$cache_key of method Automattic\\WooCommerce\\Gateways\\PayPal\\TransactAccountManager\:\:update_transact_account_cache\(\) expects string, string\|null given\.$#'
+ identifier: argument.type
+ count: 3
+ path: src/Gateways/PayPal/TransactAccountManager.php
+
+ -
+ message: '#^Parameter \#4 \$body of static method Automattic\\Jetpack\\Connection\\Client\:\:wpcom_json_api_request_as_blog\(\) expects array\|string\|null, string\|false\|null given\.$#'
+ identifier: argument.type
+ count: 1
+ path: src/Gateways/PayPal/TransactAccountManager.php
+
-
message: '#^Access to an undefined property object\:\:\$payload\.$#'
identifier: property.notFound
diff --git a/plugins/woocommerce/src/Gateways/PayPal/TransactAccountManager.php b/plugins/woocommerce/src/Gateways/PayPal/TransactAccountManager.php
new file mode 100644
index 0000000000..aca501cfdb
--- /dev/null
+++ b/plugins/woocommerce/src/Gateways/PayPal/TransactAccountManager.php
@@ -0,0 +1,420 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Automattic\WooCommerce\Gateways\PayPal;
+
+use Automattic\Jetpack\Connection\Client as Jetpack_Connection_Client;
+use Automattic\WooCommerce\Gateways\PayPal\Constants as PayPalConstants;
+
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+/**
+ * PayPal TransactAccountManager Class
+ *
+ * Handles Transact account management.
+ *
+ * @since 10.5.0
+ */
+final class TransactAccountManager {
+ /**
+ * The API version for the proxy endpoint.
+ *
+ * @var int
+ *
+ * @since 10.5.0
+ */
+ private const WPCOM_PROXY_ENDPOINT_API_VERSION = 2;
+
+ /**
+ * Transact provider type, for provider onboarding.
+ *
+ * @var string
+ *
+ * @since 10.5.0
+ */
+ private const TRANSACT_PROVIDER_TYPE = 'paypal_standard';
+
+ /**
+ * Cache key for the merchant account in live mode.
+ *
+ * @var string
+ *
+ * @since 10.5.0
+ */
+ private const TRANSACT_MERCHANT_ACCOUNT_CACHE_KEY_LIVE = 'woocommerce_paypal_transact_merchant_account_live';
+
+ /**
+ * Cache key for the merchant account in test mode.
+ *
+ * @var string
+ *
+ * @since 10.5.0
+ */
+ private const TRANSACT_MERCHANT_ACCOUNT_CACHE_KEY_TEST = 'woocommerce_paypal_transact_merchant_account_test';
+
+ /**
+ * Cache key for the provider account in live mode.
+ *
+ * @var string
+ *
+ * @since 10.5.0
+ */
+ private const TRANSACT_PROVIDER_ACCOUNT_CACHE_KEY_LIVE = 'woocommerce_paypal_transact_provider_account_live';
+
+ /**
+ * Cache key for the provider account in test mode.
+ *
+ * @var string
+ *
+ * @since 10.5.0
+ */
+ private const TRANSACT_PROVIDER_ACCOUNT_CACHE_KEY_TEST = 'woocommerce_paypal_transact_provider_account_test';
+
+ /**
+ * The expiry time for the Transact account cache.
+ *
+ * @var int
+ *
+ * @since 10.5.0
+ */
+ private const TRANSACT_ACCOUNT_CACHE_EXPIRY = 60 * 60 * 24; // 24 hours.
+
+ /**
+ * Paypal gateway object.
+ *
+ * @var \WC_Gateway_Paypal
+ */
+ private \WC_Gateway_Paypal $gateway;
+
+ /**
+ * Constructor.
+ *
+ * @param \WC_Gateway_Paypal $gateway Paypal gateway object.
+ */
+ public function __construct( \WC_Gateway_Paypal $gateway ) {
+ $this->gateway = $gateway;
+ }
+
+ /**
+ * Onboard the merchant with the Transact platform.
+ *
+ * @return void
+ *
+ * @since 10.5.0
+ */
+ public function do_onboarding(): void {
+ // Check that we have a PayPal email. This is required for processing payments --
+ // used as the payee email. Only begin onboarding if this minimum requirement is met.
+ if ( empty( $this->gateway->email ) ) {
+ return;
+ }
+
+ // Register with Jetpack if not already connected.
+ $jetpack_connection_manager = $this->gateway->get_jetpack_connection_manager();
+ if ( ! $jetpack_connection_manager ) {
+ \WC_Gateway_Paypal::log( 'Jetpack connection manager not found.', 'error' );
+ return;
+ }
+
+ if ( ! $jetpack_connection_manager->is_connected() ) {
+ $result = $jetpack_connection_manager->try_registration();
+ if ( is_wp_error( $result ) ) {
+ \WC_Gateway_Paypal::log( 'Jetpack registration failed: ' . $result->get_error_message(), 'error' );
+ return;
+ }
+ }
+
+ // Fetch (cached) or create the Transact merchant and provider accounts.
+ $merchant_account_data = $this->get_transact_account_data( 'merchant' );
+ if ( empty( $merchant_account_data ) ) {
+ $merchant_account = $this->create_merchant_account();
+ if ( empty( $merchant_account ) ) {
+ \WC_Gateway_Paypal::log( 'Transact merchant onboarding failed.', 'error' );
+ return;
+ }
+
+ // Cache the merchant account data.
+ $this->update_transact_account_cache(
+ $this->get_cache_key( 'merchant' ),
+ $merchant_account
+ );
+ }
+
+ $provider_account_data = $this->get_transact_account_data( 'provider' );
+ if ( empty( $provider_account_data ) ) {
+ $provider_account = $this->create_provider_account();
+ if ( ! $provider_account ) {
+ \WC_Gateway_Paypal::log( 'Transact provider onboarding failed.', 'error' );
+ return;
+ }
+
+ // Cache the provider account data.
+ $this->update_transact_account_cache(
+ $this->get_cache_key( 'provider' ),
+ $provider_account
+ );
+ }
+
+ // Set an extra flag to indicate that we've completed onboarding,
+ // so we can do inexpensive early returns for checkers like
+ // WC_Gateway_Paypal::should_use_orders_v2().
+ $this->gateway->set_transact_onboarding_complete();
+ }
+
+ /**
+ * Get the Transact account (merchant or provider) data. Performs a fetch if the account
+ * is not in cache or expired.
+ *
+ * @since 10.5.0
+ *
+ * @param string $account_type The type of account to get (merchant or provider).
+ * @return mixed Returns null if the transact account cannot be retrieved.
+ */
+ public function get_transact_account_data( string $account_type ) {
+ $cache_key = $this->get_cache_key( $account_type );
+
+ // Get transact account from cache. If not found, fetch/create it.
+ $transact_account = $this->get_transact_account_from_cache( $cache_key );
+ if ( empty( $transact_account ) ) {
+ $transact_account = 'merchant' === $account_type ? $this->fetch_merchant_account() : $this->fetch_provider_account();
+
+ // Fetch failed.
+ if ( empty( $transact_account ) ) {
+ return null;
+ }
+
+ // Update cache.
+ $this->update_transact_account_cache( $cache_key, $transact_account );
+ }
+
+ return $transact_account;
+ }
+
+ /**
+ * Get the cache key for the transact account.
+ *
+ * @since 10.5.0
+ *
+ * @param string $account_type The type of account to get (merchant or provider).
+ * @return string|null The cache key, or null if the account type is invalid.
+ */
+ private function get_cache_key( string $account_type ): ?string {
+ if ( 'merchant' === $account_type ) {
+ return $this->gateway->testmode ? self::TRANSACT_MERCHANT_ACCOUNT_CACHE_KEY_TEST : self::TRANSACT_MERCHANT_ACCOUNT_CACHE_KEY_LIVE;
+ }
+
+ if ( 'provider' === $account_type ) {
+ return $this->gateway->testmode ? self::TRANSACT_PROVIDER_ACCOUNT_CACHE_KEY_TEST : self::TRANSACT_PROVIDER_ACCOUNT_CACHE_KEY_LIVE;
+ }
+
+ return null;
+ }
+
+ /**
+ * Fetch the merchant account from the Transact platform.
+ *
+ * @since 10.5.0
+ *
+ * @return array|null The API response body, or null if the request fails.
+ */
+ private function fetch_merchant_account(): ?array {
+ $site_id = \Jetpack_Options::get_option( 'id' );
+ if ( ! $site_id ) {
+ return null;
+ }
+
+ $request_body = array(
+ 'test_mode' => $this->gateway->testmode,
+ );
+
+ $response = $this->send_transact_api_request(
+ 'GET',
+ sprintf( '/sites/%d/transact/account', $site_id ),
+ $request_body
+ );
+
+ if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
+ return null;
+ }
+
+ $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
+ if ( empty( $response_data['public_id'] ) ) {
+ return null;
+ }
+
+ return array( 'public_id' => $response_data['public_id'] );
+ }
+
+ /**
+ * Fetch the provider account from the Transact platform.
+ *
+ * @since 10.5.0
+ *
+ * @return bool True if the provider account exists, false otherwise.
+ */
+ private function fetch_provider_account(): bool {
+ $site_id = \Jetpack_Options::get_option( 'id' );
+ if ( ! $site_id ) {
+ return false;
+ }
+
+ $request_body = array(
+ 'test_mode' => $this->gateway->testmode,
+ 'provider_type' => self::TRANSACT_PROVIDER_TYPE,
+ );
+
+ $response = $this->send_transact_api_request(
+ 'GET',
+ sprintf( '/sites/%d/transact/account/%s', $site_id, self::TRANSACT_PROVIDER_TYPE ),
+ $request_body
+ );
+
+ if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
+ return false;
+ }
+
+ // Provider account response only returns an empty onboarding link,
+ // which we do not need.
+ return true;
+ }
+
+ /**
+ * Create the merchant account with the Transact platform.
+ *
+ * @since 10.5.0
+ *
+ * @return array|null The API response body, or null if the request fails.
+ */
+ private function create_merchant_account(): ?array {
+ $site_id = \Jetpack_Options::get_option( 'id' );
+ if ( ! $site_id ) {
+ return null;
+ }
+
+ $request_body = array( 'test_mode' => $this->gateway->testmode );
+
+ $response = $this->send_transact_api_request(
+ 'POST',
+ sprintf( '/sites/%d/transact/account', $site_id ),
+ $request_body
+ );
+
+ if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
+ return null;
+ }
+
+ $response_data = json_decode( wp_remote_retrieve_body( $response ), true );
+ if ( empty( $response_data['public_id'] ) ) {
+ \WC_Gateway_Paypal::log( 'Transact merchant account creation failed. Response body: ' . wc_print_r( $response_data, true ) );
+ return null;
+ }
+
+ return array( 'public_id' => $response_data['public_id'] );
+ }
+
+ /**
+ * Create the provider account with the Transact platform.
+ *
+ * @since 10.5.0
+ *
+ * @return bool True if the provider account creation was successful, false otherwise.
+ */
+ private function create_provider_account(): bool {
+ $site_id = \Jetpack_Options::get_option( 'id' );
+ if ( ! $site_id ) {
+ return false;
+ }
+
+ $request_body = array(
+ 'test_mode' => $this->gateway->testmode,
+ 'provider_type' => self::TRANSACT_PROVIDER_TYPE,
+ );
+ $response = $this->send_transact_api_request(
+ 'POST',
+ sprintf( '/sites/%d/transact/account/%s/onboard', $site_id, self::TRANSACT_PROVIDER_TYPE ),
+ $request_body
+ );
+
+ if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
+ return false;
+ }
+
+ // Provider account response only returns an empty onboarding link,
+ // which we do not need.
+ return true;
+ }
+
+ /**
+ * Update the transact account (merchant or provider) cache.
+ *
+ * @since 10.5.0
+ *
+ * @param string|null $cache_key The cache key to update.
+ * @param array|bool $account_data The transact account data.
+ *
+ * @return void
+ */
+ private function update_transact_account_cache( string $cache_key, $account_data ): void {
+ $expires = time() + self::TRANSACT_ACCOUNT_CACHE_EXPIRY;
+ update_option(
+ $cache_key,
+ array(
+ 'account' => $account_data,
+ 'expiry' => $expires,
+ )
+ );
+ }
+
+ /**
+ * Get the transact account (merchant or provider) from the database cache.
+ *
+ * @since 10.5.0
+ *
+ * @param string $cache_key The cache key to get the account.
+ * @return mixed The transact account data, or null if the cache is
+ * empty or expired.
+ */
+ private function get_transact_account_from_cache( string $cache_key ) {
+ $transact_account = get_option( $cache_key, null );
+
+ if ( empty( $transact_account ) || ( isset( $transact_account['expiry'] ) && $transact_account['expiry'] < time() ) ) {
+ return null;
+ }
+
+ return $transact_account['account'] ?? null;
+ }
+
+ /**
+ * Send a request to the Transact platform.
+ *
+ * @since 10.5.0
+ *
+ * @param string $method The HTTP method to use.
+ * @param string $endpoint The endpoint to request.
+ * @param array $request_body The request body.
+ *
+ * @return array|\WP_Error The API response body, or null if the request fails.
+ */
+ private function send_transact_api_request( string $method, string $endpoint, array $request_body ) {
+ if ( 'GET' === $method ) {
+ $endpoint .= '?' . http_build_query( $request_body );
+ }
+
+ $response = Jetpack_Connection_Client::wpcom_json_api_request_as_blog(
+ $endpoint,
+ (string) self::WPCOM_PROXY_ENDPOINT_API_VERSION,
+ array(
+ 'headers' => array( 'Content-Type' => 'application/json' ),
+ 'method' => $method,
+ 'timeout' => PayPalConstants::WPCOM_PROXY_REQUEST_TIMEOUT,
+ ),
+ 'GET' === $method ? null : wp_json_encode( $request_body ),
+ 'wpcom'
+ );
+
+ return $response;
+ }
+}
diff --git a/plugins/woocommerce/tests/php/includes/gateways/paypal/class-wc-gateway-paypal-transact-manager-account-test.php b/plugins/woocommerce/tests/php/src/Gateways/PayPal/TransactAccountManagerTest.php
similarity index 85%
rename from plugins/woocommerce/tests/php/includes/gateways/paypal/class-wc-gateway-paypal-transact-manager-account-test.php
rename to plugins/woocommerce/tests/php/src/Gateways/PayPal/TransactAccountManagerTest.php
index fcce85105d..fcf4f50a40 100644
--- a/plugins/woocommerce/tests/php/includes/gateways/paypal/class-wc-gateway-paypal-transact-manager-account-test.php
+++ b/plugins/woocommerce/tests/php/src/Gateways/PayPal/TransactAccountManagerTest.php
@@ -1,38 +1,39 @@
<?php
/**
- * Class WC_Gateway_Paypal_Transact_Account_Manager_Test file.
+ * Class TransactAccountManagerTest file.
*
* @package WooCommerce\Tests\Gateways\PayPal
*/
declare(strict_types=1);
+namespace Automattic\WooCommerce\Tests\Gateways\PayPal;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
-require_once WC_ABSPATH . 'includes/gateways/paypal/includes/class-wc-gateway-paypal-transact-account-manager.php';
+use Automattic\WooCommerce\Gateways\PayPal\TransactAccountManager as PayPalTransactAccountManager;
/**
- * WC_Gateway_Paypal_Transact_Account_Manager_Test class.
+ * TransactAccountManagerTest class.
*
* @package WooCommerce\Tests\Gateways\PayPal
*/
-class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case {
+class TransactAccountManagerTest extends \WC_Unit_Test_Case {
/**
* Mock PayPal gateway.
*
- * @var WC_Gateway_Paypal
+ * @var \WC_Gateway_Paypal
*/
- private $gateway;
+ private \WC_Gateway_Paypal $gateway;
/**
* Account manager instance.
*
- * @var WC_Gateway_Paypal_Transact_Account_Manager
+ * @var PayPalTransactAccountManager
*/
- private $account_manager;
+ private PayPalTransactAccountManager $account_manager;
/**
* Set up test fixtures.
@@ -41,7 +42,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
parent::setUp();
// Create mock PayPal gateway.
- $this->gateway = $this->getMockBuilder( WC_Gateway_Paypal::class )
+ $this->gateway = $this->getMockBuilder( \WC_Gateway_Paypal::class )
->disableOriginalConstructor()
->getMock();
@@ -50,17 +51,19 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
$this->gateway->testmode = true;
// Create account manager instance.
- $this->account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
+ $this->account_manager = new PayPalTransactAccountManager( $this->gateway );
}
/**
* Test constructor sets gateway.
+ *
+ * @return void
*/
- public function test_constructor_sets_gateway() {
- $account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
+ public function test_constructor_sets_gateway(): void {
+ $account_manager = new PayPalTransactAccountManager( $this->gateway );
// Use reflection to access the private gateway property.
- $reflection = new ReflectionClass( $account_manager );
+ $reflection = new \ReflectionClass( $account_manager );
$gateway_property = $reflection->getProperty( 'gateway' );
$gateway_property->setAccessible( true );
@@ -69,8 +72,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test do_onboarding when email is empty.
+ *
+ * @return void
*/
- public function test_do_onboarding_when_email_empty() {
+ public function test_do_onboarding_when_email_empty(): void {
$this->gateway->email = '';
// Should not throw any errors and should return early.
@@ -81,8 +86,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test do_onboarding when Jetpack registration fails.
+ *
+ * @return void
*/
- public function test_do_onboarding_when_jetpack_registration_fails() {
+ public function test_do_onboarding_when_jetpack_registration_fails(): void {
// Mock the gateway to return a mock Jetpack connection manager.
$jetpack_manager = $this->getMockBuilder( 'stdClass' )
->addMethods( array( 'is_connected', 'try_registration' ) )
@@ -92,7 +99,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
->willReturn( false );
$jetpack_manager->method( 'try_registration' )
- ->willReturn( new WP_Error( 'registration_failed', 'Registration failed' ) );
+ ->willReturn( new \WP_Error( 'registration_failed', 'Registration failed' ) );
$this->gateway->method( 'get_jetpack_connection_manager' )
->willReturn( $jetpack_manager );
@@ -105,8 +112,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test do_onboarding when merchant account creation fails.
+ *
+ * @return void
*/
- public function test_do_onboarding_when_merchant_account_creation_fails() {
+ public function test_do_onboarding_when_merchant_account_creation_fails(): void {
// Mock the gateway to return a mock Jetpack connection manager.
$jetpack_manager = $this->getMockBuilder( 'stdClass' )
->addMethods( array( 'is_connected' ) )
@@ -128,7 +137,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
add_filter( 'pre_http_request', array( $this, 'return_api_error' ) );
// Should do nothing. Should not throw any errors.
- $account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
+ $account_manager = new PayPalTransactAccountManager( $this->gateway );
$account_manager->do_onboarding();
// Clean up the filters.
@@ -141,8 +150,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test do_onboarding when provider account creation fails.
+ *
+ * @return void
*/
- public function test_do_onboarding_when_provider_account_creation_fails() {
+ public function test_do_onboarding_when_provider_account_creation_fails(): void {
// Mock the gateway to return a mock Jetpack connection manager.
$jetpack_manager = $this->getMockBuilder( 'stdClass' )
->addMethods( array( 'is_connected' ) )
@@ -164,7 +175,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
add_filter( 'pre_http_request', array( $this, 'return_api_error' ) );
// Should do nothing. Should not throw any errors.
- $account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
+ $account_manager = new PayPalTransactAccountManager( $this->gateway );
$account_manager->do_onboarding();
// Check that it returns true.
@@ -177,8 +188,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test get_merchant_account_data returns cached data when available.
+ *
+ * @return void
*/
- public function test_get_merchant_account_data_returns_cached_data() {
+ public function test_get_merchant_account_data_returns_cached_data(): void {
// Return valid cache data.
add_filter(
'pre_option_woocommerce_paypal_transact_merchant_account_test',
@@ -199,8 +212,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test get_merchant_account_data returns null when cache is expired.
+ *
+ * @return void
*/
- public function test_get_merchant_account_data_returns_null_when_cache_expired() {
+ public function test_get_merchant_account_data_returns_null_when_cache_expired(): void {
// Mock cache to return expired data.
add_filter(
'pre_option_woocommerce_paypal_transact_merchant_account_test',
@@ -220,8 +235,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test get_merchant_account_data fetches when cache is empty and caches fetched data.
+ *
+ * @return void
*/
- public function test_get_merchant_account_data_fetches_and_caches_data() {
+ public function test_get_merchant_account_data_fetches_and_caches_data(): void {
// Return empty cache.
add_filter(
'pre_option_woocommerce_paypal_transact_merchant_account_test',
@@ -237,7 +254,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
// Return a successful response, with the merchant account data.
add_filter( 'pre_http_request', array( $this, 'return_merchant_account_api_success' ) );
- $account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
+ $account_manager = new PayPalTransactAccountManager( $this->gateway );
$result = $account_manager->get_transact_account_data( 'merchant' );
// Clean up the filters.
@@ -260,8 +277,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test get_provider_account_data returns cached data when available.
+ *
+ * @return void
*/
- public function test_get_provider_account_data_returns_cached_data() {
+ public function test_get_provider_account_data_returns_cached_data(): void {
// Return valid cache data.
add_filter(
'pre_option_woocommerce_paypal_transact_provider_account_test',
@@ -282,8 +301,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test get_provider_account_data returns null when cache is expired.
+ *
+ * @return void
*/
- public function test_get_provider_account_data_returns_null_when_cache_expired() {
+ public function test_get_provider_account_data_returns_null_when_cache_expired(): void {
// Mock cache to return expired data.
add_filter(
'pre_option_woocommerce_paypal_transact_provider_account_test',
@@ -303,8 +324,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test get_provider_account_data fetches when cache is empty and caches fetched data.
+ *
+ * @return void
*/
- public function test_get_provider_account_data_fetches_and_caches_data() {
+ public function test_get_provider_account_data_fetches_and_caches_data(): void {
// Return empty cache.
add_filter(
'pre_option_woocommerce_paypal_transact_provider_account_test',
@@ -320,7 +343,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
// Return a successful response, with the provider account data.
add_filter( 'pre_http_request', array( $this, 'return_provider_account_api_success' ) );
- $account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
+ $account_manager = new PayPalTransactAccountManager( $this->gateway );
$result = $account_manager->get_transact_account_data( 'provider' );
// Clean up the filters.
@@ -340,8 +363,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test fetch_merchant_account when API request fails.
+ *
+ * @return void
*/
- public function test_fetch_merchant_account_when_api_request_fails() {
+ public function test_fetch_merchant_account_when_api_request_fails(): void {
// Mock Jetpack options to return a valid site ID.
add_filter( 'pre_option_jetpack_options', array( $this, 'return_valid_site_id' ) );
@@ -351,8 +376,8 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
// Mock the HTTP request to return an error.
add_filter( 'pre_http_request', array( $this, 'return_api_error' ) );
- $account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
- $reflection = new ReflectionClass( $account_manager );
+ $account_manager = new PayPalTransactAccountManager( $this->gateway );
+ $reflection = new \ReflectionClass( $account_manager );
$method = $reflection->getMethod( 'fetch_merchant_account' );
$method->setAccessible( true );
@@ -368,8 +393,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test fetch_merchant_account when API response is successful.
+ *
+ * @return void
*/
- public function test_fetch_merchant_account_when_api_response_successful() {
+ public function test_fetch_merchant_account_when_api_response_successful(): void {
// Mock Jetpack options to return a valid site ID.
add_filter( 'pre_option_jetpack_options', array( $this, 'return_valid_site_id' ) );
@@ -379,8 +406,8 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
// Mock the HTTP request to return a successful response.
add_filter( 'pre_http_request', array( $this, 'return_merchant_account_api_success' ) );
- $account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
- $reflection = new ReflectionClass( $account_manager );
+ $account_manager = new PayPalTransactAccountManager( $this->gateway );
+ $reflection = new \ReflectionClass( $account_manager );
$method = $reflection->getMethod( 'fetch_merchant_account' );
$method->setAccessible( true );
@@ -396,8 +423,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test fetch_provider_account when API request fails.
+ *
+ * @return void
*/
- public function test_fetch_provider_account_when_api_request_fails() {
+ public function test_fetch_provider_account_when_api_request_fails(): void {
// Mock Jetpack options to return a valid site ID.
add_filter( 'pre_option_jetpack_options', array( $this, 'return_valid_site_id' ) );
@@ -408,10 +437,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
add_filter( 'pre_http_request', array( $this, 'return_api_error' ) );
// Create a real account manager instance.
- $account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
+ $account_manager = new PayPalTransactAccountManager( $this->gateway );
// Use reflection to access the private fetch_provider_account method.
- $reflection = new ReflectionClass( $account_manager );
+ $reflection = new \ReflectionClass( $account_manager );
$method = $reflection->getMethod( 'fetch_provider_account' );
$method->setAccessible( true );
$result = $method->invoke( $account_manager );
@@ -426,8 +455,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test fetch_provider_account when API response is successful.
+ *
+ * @return void
*/
- public function test_fetch_provider_account_when_api_response_successful() {
+ public function test_fetch_provider_account_when_api_response_successful(): void {
// Mock Jetpack options to return a valid site ID.
add_filter( 'pre_option_jetpack_options', array( $this, 'return_valid_site_id' ) );
@@ -437,8 +468,8 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
// Mock the HTTP request to return a successful response.
add_filter( 'pre_http_request', array( $this, 'return_provider_account_api_success' ) );
- $account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
- $reflection = new ReflectionClass( $account_manager );
+ $account_manager = new PayPalTransactAccountManager( $this->gateway );
+ $reflection = new \ReflectionClass( $account_manager );
$method = $reflection->getMethod( 'fetch_provider_account' );
$method->setAccessible( true );
$result = $method->invoke( $account_manager );
@@ -454,8 +485,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test create_merchant_account when API request fails.
+ *
+ * @return void
*/
- public function test_create_merchant_account_when_api_request_fails() {
+ public function test_create_merchant_account_when_api_request_fails(): void {
// Mock Jetpack options to return a valid site ID.
add_filter( 'pre_option_jetpack_options', array( $this, 'return_valid_site_id' ) );
@@ -466,10 +499,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
add_filter( 'pre_http_request', array( $this, 'return_api_error' ) );
// Create a real account manager instance.
- $account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
+ $account_manager = new PayPalTransactAccountManager( $this->gateway );
// Use reflection to access the private create_merchant_account method.
- $reflection = new ReflectionClass( $account_manager );
+ $reflection = new \ReflectionClass( $account_manager );
$create_method = $reflection->getMethod( 'create_merchant_account' );
$create_method->setAccessible( true );
@@ -486,8 +519,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test create_merchant_account when API response is successful.
+ *
+ * @return void
*/
- public function test_create_merchant_account_when_api_response_successful() {
+ public function test_create_merchant_account_when_api_response_successful(): void {
// Mock Jetpack options to return a valid site ID.
add_filter( 'pre_option_jetpack_options', array( $this, 'return_valid_site_id' ) );
@@ -498,10 +533,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
add_filter( 'pre_http_request', array( $this, 'return_merchant_account_api_success' ) );
// Create a real account manager instance.
- $account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
+ $account_manager = new PayPalTransactAccountManager( $this->gateway );
// Use reflection to access the private create_merchant_account method.
- $reflection = new ReflectionClass( $account_manager );
+ $reflection = new \ReflectionClass( $account_manager );
$method = $reflection->getMethod( 'create_merchant_account' );
$method->setAccessible( true );
@@ -518,8 +553,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test create_provider_account when API request fails.
+ *
+ * @return void
*/
- public function test_create_provider_account_when_api_request_fails() {
+ public function test_create_provider_account_when_api_request_fails(): void {
// Mock Jetpack options to return a valid site ID.
add_filter( 'pre_option_jetpack_options', array( $this, 'return_valid_site_id' ) );
@@ -529,8 +566,8 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
// Mock the HTTP request to return an error.
add_filter( 'pre_http_request', array( $this, 'return_api_error' ) );
- $account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
- $reflection = new ReflectionClass( $account_manager );
+ $account_manager = new PayPalTransactAccountManager( $this->gateway );
+ $reflection = new \ReflectionClass( $account_manager );
$method = $reflection->getMethod( 'create_provider_account' );
$method->setAccessible( true );
$result = $method->invoke( $account_manager );
@@ -546,8 +583,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Test create_provider_account when API response is successful.
+ *
+ * @return void
*/
- public function test_create_provider_account_when_api_response_successful() {
+ public function test_create_provider_account_when_api_response_successful(): void {
// Mock Jetpack options to return a valid site ID.
add_filter( 'pre_option_jetpack_options', array( $this, 'return_valid_site_id' ) );
@@ -557,8 +596,8 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
// Mock the HTTP request to return a successful response.
add_filter( 'pre_http_request', array( $this, 'return_provider_account_api_success' ) );
- $account_manager = new WC_Gateway_Paypal_Transact_Account_Manager( $this->gateway );
- $reflection = new ReflectionClass( $account_manager );
+ $account_manager = new PayPalTransactAccountManager( $this->gateway );
+ $reflection = new \ReflectionClass( $account_manager );
$method = $reflection->getMethod( 'create_provider_account' );
$method->setAccessible( true );
$result = $method->invoke( $account_manager );
@@ -575,10 +614,10 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
/**
* Helper method to return API error response.
*
- * @return WP_Error Error response.
+ * @return \WP_Error Error response.
*/
- public function return_api_error() {
- return new WP_Error( 'api_error', 'API request failed' );
+ public function return_api_error(): \WP_Error {
+ return new \WP_Error( 'api_error', 'API request failed' );
}
/**
@@ -586,7 +625,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
*
* @return array Success response.
*/
- public function return_merchant_account_api_success() {
+ public function return_merchant_account_api_success(): array {
return array(
'response' => array( 'code' => 200 ),
'body' => wp_json_encode(
@@ -602,7 +641,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
*
* @return array Success response.
*/
- public function return_provider_account_api_success() {
+ public function return_provider_account_api_success(): array {
return array( 'response' => array( 'code' => 200 ) );
}
@@ -611,9 +650,9 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
*
* @param mixed $value The option value.
*
- * @return null
+ * @return array
*/
- public function return_null_site_id( $value ) {
+ public function return_null_site_id( $value ): array {
return array( 'id' => null );
}
@@ -622,9 +661,9 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
*
* @param mixed $value The option value.
*
- * @return int
+ * @return array
*/
- public function return_valid_site_id( $value ) {
+ public function return_valid_site_id( $value ): array {
return array( 'id' => 12345 );
}
@@ -633,7 +672,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
*
* @return false
*/
- public function return_empty_merchant_account_cache() {
+ public function return_empty_merchant_account_cache(): bool {
return false;
}
@@ -642,7 +681,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
*
* @return array
*/
- public function return_expired_merchant_account_cache() {
+ public function return_expired_merchant_account_cache(): array {
return array(
'account' => array( 'public_id' => 'test_public_id' ),
'expiry' => time() - 3600, // Expired 1 hour ago.
@@ -654,7 +693,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
*
* @return array
*/
- public function return_valid_merchant_account_cache() {
+ public function return_valid_merchant_account_cache(): array {
return array(
'account' => array( 'public_id' => 'test_public_id' ),
'expiry' => time() + 3600, // Expires in 1 hour.
@@ -666,7 +705,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
*
* @return false
*/
- public function return_empty_provider_account_cache() {
+ public function return_empty_provider_account_cache(): bool {
return false;
}
@@ -675,7 +714,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
*
* @return array
*/
- public function return_expired_provider_account_cache() {
+ public function return_expired_provider_account_cache(): array {
return array(
'account' => true,
'expiry' => time() - 3600, // Expired 1 hour ago.
@@ -687,7 +726,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
*
* @return array
*/
- public function return_valid_provider_account_cache() {
+ public function return_valid_provider_account_cache(): array {
return array(
'account' => true,
'expiry' => time() + 3600, // Expires in 1 hour.
@@ -701,7 +740,7 @@ class WC_Gateway_Paypal_Transact_Account_Manager_Test extends \WC_Unit_Test_Case
*
* @return array
*/
- public function return_blog_token( $value ) {
+ public function return_blog_token( $value ): array {
return array( 'blog_token' => 'IAM.AJETPACKBLOGTOKEN' );
}