From dbfd6b692838152bf3376344fd04261d36e99b95 Mon Sep 17 00:00:00 2001 From: Hector Lovo Date: Fri, 2 Aug 2024 12:05:47 -0400 Subject: [PATCH] Disable Direct Checkout When WooPayments Gateway Is Disabled (#9203) --- ...-disable-woopay-dc-when-woopayments-disabled | 4 ++++ includes/class-wc-payments-features.php | 14 +++++++++++++- tests/unit/test-class-wc-payments-features.php | 17 ++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 changelog/fix-disable-woopay-dc-when-woopayments-disabled diff --git a/changelog/fix-disable-woopay-dc-when-woopayments-disabled b/changelog/fix-disable-woopay-dc-when-woopayments-disabled new file mode 100644 index 00000000000..2e01821e936 --- /dev/null +++ b/changelog/fix-disable-woopay-dc-when-woopayments-disabled @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Only enable Direct Checkout when WooPayments gateway is enabled. diff --git a/includes/class-wc-payments-features.php b/includes/class-wc-payments-features.php index 2be033177ca..7d1a2316b19 100644 --- a/includes/class-wc-payments-features.php +++ b/includes/class-wc-payments-features.php @@ -270,7 +270,7 @@ public static function is_woopay_direct_checkout_enabled() { $is_direct_checkout_eligible = is_array( $account_cache ) && ( $account_cache['platform_direct_checkout_eligible'] ?? false ); $is_direct_checkout_flag_enabled = '1' === get_option( self::WOOPAY_DIRECT_CHECKOUT_FLAG_NAME, '1' ); - return $is_direct_checkout_eligible && $is_direct_checkout_flag_enabled && self::is_woopay_enabled(); + return $is_direct_checkout_eligible && $is_direct_checkout_flag_enabled && self::is_woopayments_gateway_enabled() && self::is_woopay_enabled(); } /** @@ -392,4 +392,16 @@ public static function to_array() { ] ); } + + /** + * Checks if WooCommerce Payments gateway is enabled. + * + * @return bool True if WooCommerce Payments gateway is enabled, false otherwise. + */ + private static function is_woopayments_gateway_enabled() { + $woopayments_settings = get_option( 'woocommerce_woocommerce_payments_settings' ); + $woopayments_enabled_setting = $woopayments_settings['enabled'] ?? 'no'; + + return 'yes' === $woopayments_enabled_setting; + } } diff --git a/tests/unit/test-class-wc-payments-features.php b/tests/unit/test-class-wc-payments-features.php index 0229e014001..217bb7be513 100644 --- a/tests/unit/test-class-wc-payments-features.php +++ b/tests/unit/test-class-wc-payments-features.php @@ -226,7 +226,8 @@ public function test_is_woopay_express_checkout_enabled_returns_false_when_woopa $this->assertFalse( WC_Payments_Features::is_woopay_express_checkout_enabled() ); } - public function test_is_woopay_direct_checkout_enabled_returns_true() { + public function test_is_woopay_direct_checkout_enabled_returns_true_when_woopayments_gateway_enabled() { + update_option( 'woocommerce_woocommerce_payments_settings', [ 'enabled' => 'yes' ] ); $this->set_feature_flag_option( WC_Payments_Features::WOOPAY_EXPRESS_CHECKOUT_FLAG_NAME, '1' ); $this->set_feature_flag_option( WC_Payments_Features::WOOPAY_DIRECT_CHECKOUT_FLAG_NAME, '1' ); $this->mock_cache->method( 'get' )->willReturn( @@ -238,6 +239,19 @@ public function test_is_woopay_direct_checkout_enabled_returns_true() { $this->assertTrue( WC_Payments_Features::is_woopay_direct_checkout_enabled() ); } + public function test_is_woopay_direct_checkout_enabled_returns_false_when_woopayments_gateway_disabled() { + update_option( 'woocommerce_woocommerce_payments_settings', [ 'enabled' => 'no' ] ); + $this->set_feature_flag_option( WC_Payments_Features::WOOPAY_EXPRESS_CHECKOUT_FLAG_NAME, '1' ); + $this->set_feature_flag_option( WC_Payments_Features::WOOPAY_DIRECT_CHECKOUT_FLAG_NAME, '1' ); + $this->mock_cache->method( 'get' )->willReturn( + [ + 'platform_checkout_eligible' => true, + 'platform_direct_checkout_eligible' => true, + ] + ); + $this->assertFalse( WC_Payments_Features::is_woopay_direct_checkout_enabled() ); + } + public function test_is_woopay_direct_checkout_enabled_returns_false_when_flag_is_false() { $this->set_feature_flag_option( WC_Payments_Features::WOOPAY_EXPRESS_CHECKOUT_FLAG_NAME, '1' ); $this->set_feature_flag_option( WC_Payments_Features::WOOPAY_DIRECT_CHECKOUT_FLAG_NAME, '0' ); @@ -263,6 +277,7 @@ public function test_is_woopay_direct_checkout_enabled_returns_false_when_woopay } public function test_is_woopay_direct_checkout_enabled_returns_true_when_first_party_auth_is_disabled() { + update_option( 'woocommerce_woocommerce_payments_settings', [ 'enabled' => 'yes' ] ); $this->set_feature_flag_option( WC_Payments_Features::WOOPAY_EXPRESS_CHECKOUT_FLAG_NAME, '1' ); $this->set_feature_flag_option( WC_Payments_Features::WOOPAY_FIRST_PARTY_AUTH_FLAG_NAME, '0' ); $this->set_feature_flag_option( WC_Payments_Features::WOOPAY_DIRECT_CHECKOUT_FLAG_NAME, '1' );