From c0368e9ada0a9fcf9306ec13199d4f388c519827 Mon Sep 17 00:00:00 2001 From: Alex P Date: Mon, 8 May 2023 09:51:32 +0300 Subject: [PATCH] Do not show broken card button gateway when no checkout location Removes the card button gateway together with the PayPal gateway when disabled in checkout (since it gets broken anyway). Also refactored the admin notice about disabled gateway to add a message about disabled location. --- modules/ppcp-wc-gateway/services.php | 3 +- .../src/Checkout/DisableGateways.php | 7 +- .../GatewayWithoutPayPalAdminNotice.php | 83 ++++++++++++++----- 3 files changed, 68 insertions(+), 25 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 901c1f94a..f5a745076 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -223,7 +223,8 @@ return array( $container->get( 'onboarding.state' ), $container->get( 'wcgateway.settings' ), $container->get( 'wcgateway.is-wc-payments-page' ), - $container->get( 'wcgateway.is-ppcp-settings-page' ) + $container->get( 'wcgateway.is-ppcp-settings-page' ), + $container->get( 'wcgateway.settings.status' ) ); }, 'wcgateway.notice.authorize-order-action' => diff --git a/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php b/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php index bbd9b30d3..a842fc28c 100644 --- a/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php +++ b/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php @@ -82,8 +82,11 @@ class DisableGateways { unset( $methods[ CreditCardGateway::ID ] ); } - if ( ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) && ! $this->session_handler->order() && is_checkout() ) { - unset( $methods[ PayPalGateway::ID ] ); + if ( ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) ) { + unset( $methods[ CardButtonGateway::ID ] ); + if ( ! $this->session_handler->order() && is_checkout() ) { + unset( $methods[ PayPalGateway::ID ] ); + } } if ( ! $this->needs_to_disable_gateways() ) { diff --git a/modules/ppcp-wc-gateway/src/Notice/GatewayWithoutPayPalAdminNotice.php b/modules/ppcp-wc-gateway/src/Notice/GatewayWithoutPayPalAdminNotice.php index 08a20651b..81b057423 100644 --- a/modules/ppcp-wc-gateway/src/Notice/GatewayWithoutPayPalAdminNotice.php +++ b/modules/ppcp-wc-gateway/src/Notice/GatewayWithoutPayPalAdminNotice.php @@ -13,11 +13,16 @@ use WC_Payment_Gateway; use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message; use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; +use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus; /** * Creates the admin message about the gateway being enabled without the PayPal gateway. */ class GatewayWithoutPayPalAdminNotice { + private const NOTICE_OK = ''; + private const NOTICE_DISABLED_GATEWAY = 'disabled_gateway'; + private const NOTICE_DISABLED_LOCATION = 'disabled_location'; + /** * The gateway ID. * @@ -53,27 +58,37 @@ class GatewayWithoutPayPalAdminNotice { */ private $is_ppcp_settings_page; + /** + * The Settings status helper. + * + * @var SettingsStatus|null + */ + protected $settings_status; + /** * ConnectAdminNotice constructor. * - * @param string $id The gateway ID. - * @param State $state The state. - * @param ContainerInterface $settings The settings. - * @param bool $is_payments_page Whether the current page is the WC payment page. - * @param bool $is_ppcp_settings_page Whether the current page is the PPCP settings page. + * @param string $id The gateway ID. + * @param State $state The state. + * @param ContainerInterface $settings The settings. + * @param bool $is_payments_page Whether the current page is the WC payment page. + * @param bool $is_ppcp_settings_page Whether the current page is the PPCP settings page. + * @param SettingsStatus|null $settings_status The Settings status helper. */ public function __construct( string $id, State $state, ContainerInterface $settings, bool $is_payments_page, - bool $is_ppcp_settings_page + bool $is_ppcp_settings_page, + ?SettingsStatus $settings_status = null ) { $this->id = $id; $this->state = $state; $this->settings = $settings; $this->is_payments_page = $is_payments_page; $this->is_ppcp_settings_page = $is_ppcp_settings_page; + $this->settings_status = $settings_status; } /** @@ -82,8 +97,25 @@ class GatewayWithoutPayPalAdminNotice { * @return Message|null */ public function message(): ?Message { - if ( ! $this->should_display() ) { - return null; + $notice_type = $this->check(); + + switch ( $notice_type ) { + case self::NOTICE_DISABLED_GATEWAY: + /* translators: %1$s the gateway name, %2$s URL. */ + $text = __( + '%1$s cannot be used without the PayPal gateway. Enable the PayPal gateway.', + 'woocommerce-paypal-payments' + ); + break; + case self::NOTICE_DISABLED_LOCATION: + /* translators: %1$s the gateway name, %2$s URL. */ + $text = __( + '%1$s cannot be used without enabling the Checkout location for the PayPal gateway. Enable the Checkout location.', + 'woocommerce-paypal-payments' + ); + break; + default: + return null; } $gateway = $this->get_gateway(); @@ -94,11 +126,7 @@ class GatewayWithoutPayPalAdminNotice { $name = $gateway->get_method_title(); $message = sprintf( - /* translators: %1$s the gateway name, %2$s URL. */ - __( - '%1$s cannot be used without the PayPal gateway. Enable the PayPal gateway.', - 'woocommerce-paypal-payments' - ), + $text, $name, admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=ppcp-gateway' ) ); @@ -106,22 +134,33 @@ class GatewayWithoutPayPalAdminNotice { } /** - * Whether the message should be displayed. + * Checks whether one of the messages should be displayed. * - * @return bool + * @return string One of the NOTICE_* constants. */ - protected function should_display(): bool { + protected function check(): string { if ( State::STATE_ONBOARDED !== $this->state->current_state() || ( ! $this->is_payments_page && ! $this->is_ppcp_settings_page ) ) { - return false; - } - if ( $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' ) ) { - return false; + return self::NOTICE_OK; } - $gateway = $this->get_gateway(); + $gateway = $this->get_gateway(); + $gateway_enabled = $gateway && wc_string_to_bool( $gateway->get_option( 'enabled' ) ); - return $gateway && wc_string_to_bool( $gateway->get_option( 'enabled' ) ); + if ( ! $gateway_enabled ) { + return self::NOTICE_OK; + } + + $paypal_enabled = $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' ); + if ( ! $paypal_enabled ) { + return self::NOTICE_DISABLED_GATEWAY; + } + + if ( $this->settings_status && ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) ) { + return self::NOTICE_DISABLED_LOCATION; + } + + return self::NOTICE_OK; } /**