AdditionalPayments.php
1 year ago
Appearance.php
2 years ago
CustomizeStore.php
5 months ago
ExperimentalShippingRecommendation.php
5 months ago
ExtendStore.php
1 year ago
GetMobileApp.php
3 years ago
LaunchYourStore.php
2 years ago
Marketing.php
7 months ago
Payments.php
10 months ago
Products.php
1 month ago
ReviewShippingOptions.php
3 years ago
Shipping.php
3 months ago
StoreCreation.php
4 years ago
StoreDetails.php
3 years ago
Tax.php
1 year ago
TourInAppMarketplace.php
2 years ago
WooCommercePayments.php
5 months ago
Payments.php
483 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks; |
| 5 | |
| 6 | use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task; |
| 7 | use Automattic\WooCommerce\Internal\Admin\Settings\Payments as SettingsPaymentsService; |
| 8 | use Automattic\WooCommerce\Admin\Features\PaymentGatewaySuggestions\DefaultPaymentGateways; |
| 9 | use Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders; |
| 10 | use Automattic\WooCommerce\Internal\Admin\Suggestions\PaymentsExtensionSuggestions; |
| 11 | use WC_Gateway_BACS; |
| 12 | use WC_Gateway_Cheque; |
| 13 | use WC_Gateway_COD; |
| 14 | |
| 15 | /** |
| 16 | * Payments Task |
| 17 | */ |
| 18 | class Payments extends Task { |
| 19 | |
| 20 | /** |
| 21 | * Used to cache is_complete() method result. |
| 22 | * |
| 23 | * @var null |
| 24 | */ |
| 25 | private $is_complete_result = null; |
| 26 | |
| 27 | /** |
| 28 | * ID. |
| 29 | * |
| 30 | * @return string |
| 31 | */ |
| 32 | public function get_id() { |
| 33 | return 'payments'; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Title. |
| 38 | * |
| 39 | * @return string |
| 40 | */ |
| 41 | public function get_title() { |
| 42 | return __( 'Set up payments', 'woocommerce' ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Content. |
| 47 | * |
| 48 | * @return string |
| 49 | */ |
| 50 | public function get_content() { |
| 51 | return __( |
| 52 | 'Choose payment providers and enable payment methods at checkout.', |
| 53 | 'woocommerce' |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Time. |
| 59 | * |
| 60 | * @return string |
| 61 | */ |
| 62 | public function get_time() { |
| 63 | return __( '5 minutes', 'woocommerce' ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Task completion. |
| 68 | * |
| 69 | * @return bool |
| 70 | */ |
| 71 | public function is_complete() { |
| 72 | if ( null === $this->is_complete_result ) { |
| 73 | if ( $this->is_woopayments_active() ) { |
| 74 | // If WooPayments is active, check if it is fully onboarded with a live account. |
| 75 | $this->is_complete_result = $this->is_woopayments_onboarded() && ! $this->has_woopayments_test_account(); |
| 76 | } else { |
| 77 | // If WooPayments is not active, check if there are any enabled gateways. |
| 78 | $this->is_complete_result = self::has_gateways(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return $this->is_complete_result; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Task visibility. |
| 87 | * |
| 88 | * @return bool |
| 89 | */ |
| 90 | public function can_view() { |
| 91 | // The task is always visible. |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Check if the store has any enabled gateways. |
| 97 | * |
| 98 | * @return bool |
| 99 | */ |
| 100 | public static function has_gateways() { |
| 101 | $gateways = WC()->payment_gateways()->payment_gateways; |
| 102 | $enabled_gateways = array_filter( |
| 103 | $gateways, |
| 104 | function ( $gateway ) { |
| 105 | return 'yes' === $gateway->enabled; |
| 106 | } |
| 107 | ); |
| 108 | |
| 109 | return ! empty( $enabled_gateways ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Check if the task is in progress. |
| 114 | * |
| 115 | * @return bool |
| 116 | */ |
| 117 | public function is_in_progress() { |
| 118 | // If the task is already complete, it's not in progress. |
| 119 | if ( $this->is_complete() ) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | return ( $this->has_woopayments_live_account_in_progress() || $this->has_woopayments_test_account() ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * The task in progress label. |
| 128 | * |
| 129 | * @return string |
| 130 | */ |
| 131 | public function in_progress_label() { |
| 132 | // If WooPayments live account onboarding is in progress, show "Action needed" label. |
| 133 | if ( $this->has_woopayments_live_account_in_progress() ) { |
| 134 | return esc_html__( 'Action needed', 'woocommerce' ); |
| 135 | } |
| 136 | |
| 137 | return esc_html__( 'Test account', 'woocommerce' ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * The task action URL. |
| 142 | * |
| 143 | * Empty string means the JS logic will handle the task linking. |
| 144 | * |
| 145 | * @return string |
| 146 | */ |
| 147 | public function get_action_url() { |
| 148 | // Link to the Payments settings page. |
| 149 | return admin_url( 'admin.php?page=wc-settings&tab=checkout&from=' . SettingsPaymentsService::FROM_PAYMENTS_TASK ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Additional data to be passed to the front-end JS logic. |
| 154 | * |
| 155 | * Primarily used to inform the behavior of the Payments task in the LYS context. |
| 156 | * |
| 157 | * @return array |
| 158 | */ |
| 159 | public function get_additional_data() { |
| 160 | return array( |
| 161 | 'wooPaymentsIsActive' => $this->is_woopayments_active(), |
| 162 | 'wooPaymentsIsInstalled' => $this->is_woopayments_installed(), |
| 163 | 'wooPaymentsSettingsCountryIsSupported' => $this->is_woopayments_supported_country( $this->get_payments_settings_country() ), |
| 164 | 'wooPaymentsIsOnboarded' => $this->is_woopayments_onboarded(), |
| 165 | 'wooPaymentsHasTestAccount' => $this->has_woopayments_test_account(), |
| 166 | 'wooPaymentsHasOtherProvidersEnabled' => $this->has_providers_enabled_other_than_woopayments(), |
| 167 | 'wooPaymentsHasOtherProvidersNeedSetup' => $this->has_providers_needing_setup_other_than_woopayments(), |
| 168 | 'wooPaymentsHasOnlineGatewaysEnabled' => $this->has_online_gateways(), |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Check if the WooPayments plugin is active. |
| 174 | * |
| 175 | * @return bool |
| 176 | */ |
| 177 | private function is_woopayments_active(): bool { |
| 178 | return class_exists( '\WC_Payments' ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Check if the WooPayments plugin is installed. |
| 183 | * |
| 184 | * @return bool |
| 185 | */ |
| 186 | private function is_woopayments_installed(): bool { |
| 187 | if ( $this->is_woopayments_active() ) { |
| 188 | // If it is active, it is also installed. |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | $woopayments_suggestion = $this->get_woopayments_suggestion(); |
| 193 | // We should have the WooPayments suggestion, but if not, return false. |
| 194 | if ( ! $woopayments_suggestion ) { |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | // Check if the suggestion has its plugin installed. |
| 199 | if ( ! empty( $woopayments_suggestion['plugin']['status'] ) && |
| 200 | PaymentsProviders::EXTENSION_INSTALLED === $woopayments_suggestion['plugin']['status'] ) { |
| 201 | |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Check if WooPayments is completely onboarded. |
| 210 | * |
| 211 | * @return bool |
| 212 | */ |
| 213 | private function is_woopayments_onboarded(): bool { |
| 214 | if ( ! $this->is_woopayments_active() ) { |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | $woopayments_provider = $this->get_woopayments_provider(); |
| 219 | // We should have the WooPayments provider, but if not, return false. |
| 220 | if ( ! $woopayments_provider ) { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | // Check the provider's state to determine if it is onboarded. |
| 225 | if ( ! empty( $woopayments_provider['onboarding']['state']['completed'] ) ) { |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Check if WooPayments has a live account onboarding in progress. |
| 234 | * |
| 235 | * @return bool |
| 236 | */ |
| 237 | private function has_woopayments_live_account_in_progress() { |
| 238 | if ( $this->is_woopayments_onboarded() ) { |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | $woopayments_provider = $this->get_woopayments_provider(); |
| 243 | // We should have the WooPayments provider, but if not, return false. |
| 244 | if ( ! $woopayments_provider ) { |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | // If we have a test account, we are not in live account onboarding. |
| 249 | if ( $this->has_woopayments_test_account() ) { |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | // Check the provider's state to determine if a live account onboarding is started. |
| 254 | if ( ! empty( $woopayments_provider['onboarding']['state']['started'] ) ) { |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Check if WooPayments is onboarded and has a test [drive] account. |
| 263 | * |
| 264 | * @return bool |
| 265 | */ |
| 266 | private function has_woopayments_test_account(): bool { |
| 267 | if ( ! $this->is_woopayments_onboarded() ) { |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | $woopayments_provider = $this->get_woopayments_provider(); |
| 272 | // We should have the WooPayments provider, but if not, return false. |
| 273 | if ( ! $woopayments_provider ) { |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | // Check the provider's state to determine if a test [drive] account is in use. |
| 278 | if ( ! empty( $woopayments_provider['onboarding']['state']['test_drive_account'] ) ) { |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Check if the store is in a WooPayments-supported geography. |
| 287 | * |
| 288 | * @param string $country_code Country code to check. If not provided, uses store base country. |
| 289 | * |
| 290 | * @return bool Whether the country is supported by WooPayments. |
| 291 | */ |
| 292 | private function is_woopayments_supported_country( string $country_code ): bool { |
| 293 | if ( class_exists( '\WC_Payments_Utils' ) && is_callable( array( '\WC_Payments_Utils', 'supported_countries' ) ) ) { |
| 294 | $supported_countries = array_keys( \WC_Payments_Utils::supported_countries() ); |
| 295 | return in_array( $country_code, $supported_countries, true ); |
| 296 | } |
| 297 | |
| 298 | // WooPayments is not installed and active, use core's list of supported countries. |
| 299 | $supported_countries = DefaultPaymentGateways::get_wcpay_countries(); |
| 300 | return in_array( $country_code, $supported_countries, true ); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Check if the store has any enabled providers other than WooPayments. |
| 305 | * |
| 306 | * @return bool |
| 307 | */ |
| 308 | public function has_providers_enabled_other_than_woopayments(): bool { |
| 309 | $providers = $this->get_payments_providers(); |
| 310 | |
| 311 | foreach ( $providers as $provider ) { |
| 312 | // Check if the provider is enabled and is not WooPayments. |
| 313 | if ( |
| 314 | ! empty( $provider['state']['enabled'] ) && |
| 315 | ! empty( $provider['id'] ) && |
| 316 | 'woocommerce_payments' !== $provider['id'] |
| 317 | ) { |
| 318 | return true; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Check if any non-WooPayments providers need setup. |
| 327 | * |
| 328 | * @return bool |
| 329 | */ |
| 330 | private function has_providers_needing_setup_other_than_woopayments(): bool { |
| 331 | $providers = $this->get_payments_providers(); |
| 332 | |
| 333 | foreach ( $providers as $provider ) { |
| 334 | // Check if the provider needs setup and is not WooPayments. |
| 335 | if ( |
| 336 | ! empty( $provider['state']['needs_setup'] ) && |
| 337 | ! empty( $provider['id'] ) && |
| 338 | 'woocommerce_payments' !== $provider['id'] |
| 339 | ) { |
| 340 | return true; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Check if the store has any enabled online gateways. |
| 349 | * |
| 350 | * @return bool |
| 351 | */ |
| 352 | private function has_online_gateways(): bool { |
| 353 | $providers = $this->get_payments_providers(); |
| 354 | |
| 355 | foreach ( $providers as $provider ) { |
| 356 | // Check if the provider is enabled and is not an offline payment method. |
| 357 | if ( |
| 358 | ! empty( $provider['state']['enabled'] ) && |
| 359 | ! empty( $provider['id'] ) && |
| 360 | ! in_array( $provider['id'], array( WC_Gateway_BACS::ID, WC_Gateway_Cheque::ID, WC_Gateway_COD::ID ), true ) |
| 361 | ) { |
| 362 | return true; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Get the store's business registration country/location as it is used on the Payments Settings page. |
| 371 | * |
| 372 | * @return string The business registration country/location code. |
| 373 | */ |
| 374 | private function get_payments_settings_country(): string { |
| 375 | try { |
| 376 | /** |
| 377 | * The Payments Settings [page] service. |
| 378 | * |
| 379 | * @var SettingsPaymentsService $settings_payments_service |
| 380 | */ |
| 381 | $settings_payments_service = wc_get_container()->get( SettingsPaymentsService::class ); |
| 382 | |
| 383 | return $settings_payments_service->get_country(); |
| 384 | } catch ( \Throwable $e ) { |
| 385 | // In case of any error, return the WooCommerce base country. |
| 386 | return WC()->countries->get_base_country(); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Get the list of payments providers as it is used on the Payments Settings page. |
| 392 | * |
| 393 | * The list can include payments extension suggestions, the same as on the Payments Settings page. |
| 394 | * |
| 395 | * @return array The list of payments providers. |
| 396 | */ |
| 397 | private function get_payments_providers(): array { |
| 398 | try { |
| 399 | /** |
| 400 | * The Payments Settings [page] service. |
| 401 | * |
| 402 | * @var SettingsPaymentsService $settings_payments_service |
| 403 | */ |
| 404 | $settings_payments_service = wc_get_container()->get( SettingsPaymentsService::class ); |
| 405 | |
| 406 | // Get the raw list of payment providers, including suggestions, but remove shells. |
| 407 | // This way we prevent shell gateways that are (wrongly) reported as enabled from affecting the task completion. |
| 408 | return $settings_payments_service->get_payment_providers( $settings_payments_service->get_country(), false, true ); |
| 409 | } catch ( \Throwable $e ) { |
| 410 | // In case of any error, return an empty array. |
| 411 | return array(); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Get the list of payments extension suggestions as it is used on the Payments Settings page. |
| 417 | * |
| 418 | * @return array The list of payments extension suggestions. |
| 419 | */ |
| 420 | private function get_payments_extension_suggestions(): array { |
| 421 | try { |
| 422 | /** |
| 423 | * The Payments Settings [page] service. |
| 424 | * |
| 425 | * @var SettingsPaymentsService $settings_payments_service |
| 426 | */ |
| 427 | $settings_payments_service = wc_get_container()->get( SettingsPaymentsService::class ); |
| 428 | |
| 429 | return $settings_payments_service->get_payment_extension_suggestions( $settings_payments_service->get_country() ); |
| 430 | } catch ( \Throwable $e ) { |
| 431 | // In case of any error, return an empty array. |
| 432 | return array(); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Get the WooPayments provider details from the list used on the Payments Settings page. |
| 438 | * |
| 439 | * @return array|null The WooPayments provider details or null if not found. |
| 440 | */ |
| 441 | private function get_woopayments_provider(): ?array { |
| 442 | $providers = $this->get_payments_providers(); |
| 443 | foreach ( $providers as $provider ) { |
| 444 | if ( ! empty( $provider['id'] ) && PaymentsProviders\WooPayments\WooPaymentsService::GATEWAY_ID === $provider['id'] ) { |
| 445 | return $provider; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | return null; |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Get the WooPayments payments extension suggestion details from the lists used on the Payments Settings page. |
| 454 | * |
| 455 | * @return array|null The WooPayments suggestion details or null if not found. |
| 456 | */ |
| 457 | private function get_woopayments_suggestion(): ?array { |
| 458 | // First, check the payments providers list. |
| 459 | $providers = $this->get_payments_providers(); |
| 460 | foreach ( $providers as $provider ) { |
| 461 | if ( ! empty( $provider['_type'] ) && |
| 462 | PaymentsProviders::TYPE_SUGGESTION === $provider['_type'] && |
| 463 | ! empty( $provider['_suggestion_id'] ) && |
| 464 | PaymentsExtensionSuggestions::WOOPAYMENTS === $provider['_suggestion_id'] ) { |
| 465 | |
| 466 | return $provider; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | // If not found in the main list, check the payments extension suggestions list. |
| 471 | $suggestions = $this->get_payments_extension_suggestions(); |
| 472 | foreach ( $suggestions as $suggestion ) { |
| 473 | if ( ! empty( $suggestion['id'] ) && |
| 474 | PaymentsExtensionSuggestions::WOOPAYMENTS === $suggestion['id'] ) { |
| 475 | |
| 476 | return $suggestion; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return null; |
| 481 | } |
| 482 | } |
| 483 |