AddressRequirements.php
7 months ago
Buttons.php
4 months ago
Constants.php
2 months ago
Helper.php
2 months ago
Notices.php
4 months ago
Request.php
1 month ago
TransactAccountManager.php
4 months ago
WebhookHandler.php
2 months ago
TransactAccountManager.php
421 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Gateways\PayPal; |
| 6 | |
| 7 | use Automattic\Jetpack\Connection\Client as Jetpack_Connection_Client; |
| 8 | use Automattic\WooCommerce\Gateways\PayPal\Constants as PayPalConstants; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * PayPal TransactAccountManager Class |
| 16 | * |
| 17 | * Handles Transact account management. |
| 18 | * |
| 19 | * @since 10.5.0 |
| 20 | */ |
| 21 | final class TransactAccountManager { |
| 22 | /** |
| 23 | * The API version for the proxy endpoint. |
| 24 | * |
| 25 | * @var int |
| 26 | * |
| 27 | * @since 10.5.0 |
| 28 | */ |
| 29 | private const WPCOM_PROXY_ENDPOINT_API_VERSION = 2; |
| 30 | |
| 31 | /** |
| 32 | * Transact provider type, for provider onboarding. |
| 33 | * |
| 34 | * @var string |
| 35 | * |
| 36 | * @since 10.5.0 |
| 37 | */ |
| 38 | private const TRANSACT_PROVIDER_TYPE = 'paypal_standard'; |
| 39 | |
| 40 | /** |
| 41 | * Cache key for the merchant account in live mode. |
| 42 | * |
| 43 | * @var string |
| 44 | * |
| 45 | * @since 10.5.0 |
| 46 | */ |
| 47 | private const TRANSACT_MERCHANT_ACCOUNT_CACHE_KEY_LIVE = 'woocommerce_paypal_transact_merchant_account_live'; |
| 48 | |
| 49 | /** |
| 50 | * Cache key for the merchant account in test mode. |
| 51 | * |
| 52 | * @var string |
| 53 | * |
| 54 | * @since 10.5.0 |
| 55 | */ |
| 56 | private const TRANSACT_MERCHANT_ACCOUNT_CACHE_KEY_TEST = 'woocommerce_paypal_transact_merchant_account_test'; |
| 57 | |
| 58 | /** |
| 59 | * Cache key for the provider account in live mode. |
| 60 | * |
| 61 | * @var string |
| 62 | * |
| 63 | * @since 10.5.0 |
| 64 | */ |
| 65 | private const TRANSACT_PROVIDER_ACCOUNT_CACHE_KEY_LIVE = 'woocommerce_paypal_transact_provider_account_live'; |
| 66 | |
| 67 | /** |
| 68 | * Cache key for the provider account in test mode. |
| 69 | * |
| 70 | * @var string |
| 71 | * |
| 72 | * @since 10.5.0 |
| 73 | */ |
| 74 | private const TRANSACT_PROVIDER_ACCOUNT_CACHE_KEY_TEST = 'woocommerce_paypal_transact_provider_account_test'; |
| 75 | |
| 76 | /** |
| 77 | * The expiry time for the Transact account cache. |
| 78 | * |
| 79 | * @var int |
| 80 | * |
| 81 | * @since 10.5.0 |
| 82 | */ |
| 83 | private const TRANSACT_ACCOUNT_CACHE_EXPIRY = 60 * 60 * 24; // 24 hours. |
| 84 | |
| 85 | /** |
| 86 | * Paypal gateway object. |
| 87 | * |
| 88 | * @var \WC_Gateway_Paypal |
| 89 | */ |
| 90 | private \WC_Gateway_Paypal $gateway; |
| 91 | |
| 92 | /** |
| 93 | * Constructor. |
| 94 | * |
| 95 | * @param \WC_Gateway_Paypal $gateway Paypal gateway object. |
| 96 | */ |
| 97 | public function __construct( \WC_Gateway_Paypal $gateway ) { |
| 98 | $this->gateway = $gateway; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Onboard the merchant with the Transact platform. |
| 103 | * |
| 104 | * @return void |
| 105 | * |
| 106 | * @since 10.5.0 |
| 107 | */ |
| 108 | public function do_onboarding(): void { |
| 109 | // Check that we have a PayPal email. This is required for processing payments -- |
| 110 | // used as the payee email. Only begin onboarding if this minimum requirement is met. |
| 111 | if ( empty( $this->gateway->email ) ) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | // Register with Jetpack if not already connected. |
| 116 | $jetpack_connection_manager = $this->gateway->get_jetpack_connection_manager(); |
| 117 | if ( ! $jetpack_connection_manager ) { |
| 118 | \WC_Gateway_Paypal::log( 'Jetpack connection manager not found.', 'error' ); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | if ( ! $jetpack_connection_manager->is_connected() ) { |
| 123 | $result = $jetpack_connection_manager->try_registration(); |
| 124 | if ( is_wp_error( $result ) ) { |
| 125 | \WC_Gateway_Paypal::log( 'Jetpack registration failed: ' . $result->get_error_message(), 'error' ); |
| 126 | return; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Fetch (cached) or create the Transact merchant and provider accounts. |
| 131 | $merchant_account_data = $this->get_transact_account_data( 'merchant' ); |
| 132 | if ( empty( $merchant_account_data ) ) { |
| 133 | $merchant_account = $this->create_merchant_account(); |
| 134 | if ( empty( $merchant_account ) ) { |
| 135 | \WC_Gateway_Paypal::log( 'Transact merchant onboarding failed.', 'error' ); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | // Cache the merchant account data. |
| 140 | $this->update_transact_account_cache( |
| 141 | $this->get_cache_key( 'merchant' ), |
| 142 | $merchant_account |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | $provider_account_data = $this->get_transact_account_data( 'provider' ); |
| 147 | if ( empty( $provider_account_data ) ) { |
| 148 | $provider_account = $this->create_provider_account(); |
| 149 | if ( ! $provider_account ) { |
| 150 | \WC_Gateway_Paypal::log( 'Transact provider onboarding failed.', 'error' ); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | // Cache the provider account data. |
| 155 | $this->update_transact_account_cache( |
| 156 | $this->get_cache_key( 'provider' ), |
| 157 | $provider_account |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | // Set an extra flag to indicate that we've completed onboarding, |
| 162 | // so we can do inexpensive early returns for checkers like |
| 163 | // WC_Gateway_Paypal::should_use_orders_v2(). |
| 164 | $this->gateway->set_transact_onboarding_complete(); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get the Transact account (merchant or provider) data. Performs a fetch if the account |
| 169 | * is not in cache or expired. |
| 170 | * |
| 171 | * @since 10.5.0 |
| 172 | * |
| 173 | * @param string $account_type The type of account to get (merchant or provider). |
| 174 | * @return mixed Returns null if the transact account cannot be retrieved. |
| 175 | */ |
| 176 | public function get_transact_account_data( string $account_type ) { |
| 177 | $cache_key = $this->get_cache_key( $account_type ); |
| 178 | |
| 179 | // Get transact account from cache. If not found, fetch/create it. |
| 180 | $transact_account = $this->get_transact_account_from_cache( $cache_key ); |
| 181 | if ( empty( $transact_account ) ) { |
| 182 | $transact_account = 'merchant' === $account_type ? $this->fetch_merchant_account() : $this->fetch_provider_account(); |
| 183 | |
| 184 | // Fetch failed. |
| 185 | if ( empty( $transact_account ) ) { |
| 186 | return null; |
| 187 | } |
| 188 | |
| 189 | // Update cache. |
| 190 | $this->update_transact_account_cache( $cache_key, $transact_account ); |
| 191 | } |
| 192 | |
| 193 | return $transact_account; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Get the cache key for the transact account. |
| 198 | * |
| 199 | * @since 10.5.0 |
| 200 | * |
| 201 | * @param string $account_type The type of account to get (merchant or provider). |
| 202 | * @return string|null The cache key, or null if the account type is invalid. |
| 203 | */ |
| 204 | private function get_cache_key( string $account_type ): ?string { |
| 205 | if ( 'merchant' === $account_type ) { |
| 206 | return $this->gateway->testmode ? self::TRANSACT_MERCHANT_ACCOUNT_CACHE_KEY_TEST : self::TRANSACT_MERCHANT_ACCOUNT_CACHE_KEY_LIVE; |
| 207 | } |
| 208 | |
| 209 | if ( 'provider' === $account_type ) { |
| 210 | return $this->gateway->testmode ? self::TRANSACT_PROVIDER_ACCOUNT_CACHE_KEY_TEST : self::TRANSACT_PROVIDER_ACCOUNT_CACHE_KEY_LIVE; |
| 211 | } |
| 212 | |
| 213 | return null; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Fetch the merchant account from the Transact platform. |
| 218 | * |
| 219 | * @since 10.5.0 |
| 220 | * |
| 221 | * @return array|null The API response body, or null if the request fails. |
| 222 | */ |
| 223 | private function fetch_merchant_account(): ?array { |
| 224 | $site_id = \Jetpack_Options::get_option( 'id' ); |
| 225 | if ( ! $site_id ) { |
| 226 | return null; |
| 227 | } |
| 228 | |
| 229 | $request_body = array( |
| 230 | 'test_mode' => $this->gateway->testmode, |
| 231 | ); |
| 232 | |
| 233 | $response = $this->send_transact_api_request( |
| 234 | 'GET', |
| 235 | sprintf( '/sites/%d/transact/account', $site_id ), |
| 236 | $request_body |
| 237 | ); |
| 238 | |
| 239 | if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 240 | return null; |
| 241 | } |
| 242 | |
| 243 | $response_data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 244 | if ( empty( $response_data['public_id'] ) ) { |
| 245 | return null; |
| 246 | } |
| 247 | |
| 248 | return array( 'public_id' => $response_data['public_id'] ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Fetch the provider account from the Transact platform. |
| 253 | * |
| 254 | * @since 10.5.0 |
| 255 | * |
| 256 | * @return bool True if the provider account exists, false otherwise. |
| 257 | */ |
| 258 | private function fetch_provider_account(): bool { |
| 259 | $site_id = \Jetpack_Options::get_option( 'id' ); |
| 260 | if ( ! $site_id ) { |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | $request_body = array( |
| 265 | 'test_mode' => $this->gateway->testmode, |
| 266 | 'provider_type' => self::TRANSACT_PROVIDER_TYPE, |
| 267 | ); |
| 268 | |
| 269 | $response = $this->send_transact_api_request( |
| 270 | 'GET', |
| 271 | sprintf( '/sites/%d/transact/account/%s', $site_id, self::TRANSACT_PROVIDER_TYPE ), |
| 272 | $request_body |
| 273 | ); |
| 274 | |
| 275 | if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | // Provider account response only returns an empty onboarding link, |
| 280 | // which we do not need. |
| 281 | return true; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Create the merchant account with the Transact platform. |
| 286 | * |
| 287 | * @since 10.5.0 |
| 288 | * |
| 289 | * @return array|null The API response body, or null if the request fails. |
| 290 | */ |
| 291 | private function create_merchant_account(): ?array { |
| 292 | $site_id = \Jetpack_Options::get_option( 'id' ); |
| 293 | if ( ! $site_id ) { |
| 294 | return null; |
| 295 | } |
| 296 | |
| 297 | $request_body = array( 'test_mode' => $this->gateway->testmode ); |
| 298 | |
| 299 | $response = $this->send_transact_api_request( |
| 300 | 'POST', |
| 301 | sprintf( '/sites/%d/transact/account', $site_id ), |
| 302 | $request_body |
| 303 | ); |
| 304 | |
| 305 | if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 306 | return null; |
| 307 | } |
| 308 | |
| 309 | $response_data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 310 | if ( empty( $response_data['public_id'] ) ) { |
| 311 | \WC_Gateway_Paypal::log( 'Transact merchant account creation failed. Response body: ' . wc_print_r( $response_data, true ) ); |
| 312 | return null; |
| 313 | } |
| 314 | |
| 315 | return array( 'public_id' => $response_data['public_id'] ); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Create the provider account with the Transact platform. |
| 320 | * |
| 321 | * @since 10.5.0 |
| 322 | * |
| 323 | * @return bool True if the provider account creation was successful, false otherwise. |
| 324 | */ |
| 325 | private function create_provider_account(): bool { |
| 326 | $site_id = \Jetpack_Options::get_option( 'id' ); |
| 327 | if ( ! $site_id ) { |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | $request_body = array( |
| 332 | 'test_mode' => $this->gateway->testmode, |
| 333 | 'provider_type' => self::TRANSACT_PROVIDER_TYPE, |
| 334 | ); |
| 335 | $response = $this->send_transact_api_request( |
| 336 | 'POST', |
| 337 | sprintf( '/sites/%d/transact/account/%s/onboard', $site_id, self::TRANSACT_PROVIDER_TYPE ), |
| 338 | $request_body |
| 339 | ); |
| 340 | |
| 341 | if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | // Provider account response only returns an empty onboarding link, |
| 346 | // which we do not need. |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Update the transact account (merchant or provider) cache. |
| 352 | * |
| 353 | * @since 10.5.0 |
| 354 | * |
| 355 | * @param string|null $cache_key The cache key to update. |
| 356 | * @param array|bool $account_data The transact account data. |
| 357 | * |
| 358 | * @return void |
| 359 | */ |
| 360 | private function update_transact_account_cache( string $cache_key, $account_data ): void { |
| 361 | $expires = time() + self::TRANSACT_ACCOUNT_CACHE_EXPIRY; |
| 362 | update_option( |
| 363 | $cache_key, |
| 364 | array( |
| 365 | 'account' => $account_data, |
| 366 | 'expiry' => $expires, |
| 367 | ) |
| 368 | ); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Get the transact account (merchant or provider) from the database cache. |
| 373 | * |
| 374 | * @since 10.5.0 |
| 375 | * |
| 376 | * @param string $cache_key The cache key to get the account. |
| 377 | * @return mixed The transact account data, or null if the cache is |
| 378 | * empty or expired. |
| 379 | */ |
| 380 | private function get_transact_account_from_cache( string $cache_key ) { |
| 381 | $transact_account = get_option( $cache_key, null ); |
| 382 | |
| 383 | if ( empty( $transact_account ) || ( isset( $transact_account['expiry'] ) && $transact_account['expiry'] < time() ) ) { |
| 384 | return null; |
| 385 | } |
| 386 | |
| 387 | return $transact_account['account'] ?? null; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Send a request to the Transact platform. |
| 392 | * |
| 393 | * @since 10.5.0 |
| 394 | * |
| 395 | * @param string $method The HTTP method to use. |
| 396 | * @param string $endpoint The endpoint to request. |
| 397 | * @param array $request_body The request body. |
| 398 | * |
| 399 | * @return array|\WP_Error The API response body, or null if the request fails. |
| 400 | */ |
| 401 | private function send_transact_api_request( string $method, string $endpoint, array $request_body ) { |
| 402 | if ( 'GET' === $method ) { |
| 403 | $endpoint .= '?' . http_build_query( $request_body ); |
| 404 | } |
| 405 | |
| 406 | $response = Jetpack_Connection_Client::wpcom_json_api_request_as_blog( |
| 407 | $endpoint, |
| 408 | (string) self::WPCOM_PROXY_ENDPOINT_API_VERSION, |
| 409 | array( |
| 410 | 'headers' => array( 'Content-Type' => 'application/json' ), |
| 411 | 'method' => $method, |
| 412 | 'timeout' => PayPalConstants::WPCOM_PROXY_REQUEST_TIMEOUT, |
| 413 | ), |
| 414 | 'GET' === $method ? null : wp_json_encode( $request_body ), |
| 415 | 'wpcom' |
| 416 | ); |
| 417 | |
| 418 | return $response; |
| 419 | } |
| 420 | } |
| 421 |