Skip to content

Commit

Permalink
Revert "Disable Direct Checkout if WooPayments is disabled (#9159)" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
asumaran committed Jul 31, 2024
1 parent 591857f commit 9897488
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Reverts changes related to Direct Checkout that broke the PayPal extension.
6 changes: 1 addition & 5 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,10 +825,6 @@ public function needs_https_setup() {
* @return bool Whether the gateway is enabled and ready to accept payments.
*/
public function is_available() {
if ( ! $this->payment_method ) {
return false;
}

$processing_payment_method = $this->payment_methods[ $this->payment_method->get_id() ];
if ( ! $processing_payment_method->is_enabled_at_checkout( $this->get_account_country() ) ) {
return false;
Expand Down Expand Up @@ -908,7 +904,7 @@ public function is_available_for_current_currency() {
$supported_currencies = $this->account->get_account_customer_supported_currencies();
$current_currency = strtolower( get_woocommerce_currency() );

if ( is_null( $supported_currencies ) || ( is_array( $supported_currencies ) && count( $supported_currencies ) === 0 ) ) {
if ( count( $supported_currencies ) === 0 ) {
// If we don't have info related to the supported currencies
// of the country, we won't disable the gateway.
return true;
Expand Down
13 changes: 0 additions & 13 deletions includes/class-wc-payments-features.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,19 +266,6 @@ public static function is_payment_overview_widget_ui_enabled(): bool {
* @return bool True if Direct Checkout is enabled, false otherwise.
*/
public static function is_woopay_direct_checkout_enabled() {
// If WooPayments is not enabled then disable Direct checkout.
// TODO: Change to WC()->payment_gateways->get_available_payment_gateways() once issue with WC Subscriptions is sorted out.
$wc_payment_gateways = WC_Payment_Gateways::instance();
$wc_payment_gateways->init();
$enabled_gateways = $wc_payment_gateways->payment_gateways();

// Only used for tests.
$enabled_gateways = apply_filters( 'woocommerce_payments_enabled_gateways_for_woopay', $enabled_gateways );

if ( ! isset( $enabled_gateways['woocommerce_payments'] ) || ! $enabled_gateways['woocommerce_payments']->is_available() ) {
return false;
}

$account_cache = WC_Payments::get_database_cache()->get( WCPay\Database_Cache::ACCOUNT_KEY, true );
$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' );
Expand Down
70 changes: 0 additions & 70 deletions tests/unit/test-class-wc-payments-features.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ function ( $key ) {

// Restore the cache service in the main class.
WC_Payments::set_database_cache( $this->_cache );

remove_all_filters( 'woocommerce_payments_enabled_gateways_for_woopay' );

parent::tear_down();
}

Expand Down Expand Up @@ -184,57 +181,33 @@ public function test_are_payments_enabled_returns_false_when_flag_not_set() {
}

public function test_is_woopay_enabled_returns_true() {
add_filter( 'woocommerce_payments_enabled_gateways_for_woopay', [ $this, 'enable_woopayments' ] );
$this->set_feature_flag_option( WC_Payments_Features::WOOPAY_EXPRESS_CHECKOUT_FLAG_NAME, '1' );
WC_Payments::get_gateway()->update_option( 'platform_checkout', 'yes' );
$this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => true ] );
$this->assertTrue( WC_Payments_Features::is_woopay_enabled() );
}

public function test_is_woopay_enabled_returns_false_if_only_woopayments_is_enabled() {
add_filter( 'woocommerce_payments_enabled_gateways_for_woopay', [ $this, 'enable_woopayments' ] );
$this->assertFalse( WC_Payments_Features::is_woopay_enabled() );
}

// We are disabling only DC for now. We should disable WooPay if WooPayments is disabled though.
// Leaving this test case so we can catch that later.
public function test_is_woopay_enabled_returns_true_if_woopayments_is_disabled_and_woopay_is_enabled() {
add_filter( 'woocommerce_payments_enabled_gateways_for_woopay', [ $this, 'disable_woopayments' ] );
$this->set_feature_flag_option( WC_Payments_Features::WOOPAY_EXPRESS_CHECKOUT_FLAG_NAME, '1' );
WC_Payments::get_gateway()->update_option( 'platform_checkout', 'yes' );
$this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => true ] );
$this->assertTrue( WC_Payments_Features::is_woopay_enabled() );
}

public function test_is_woopay_enabled_returns_false_when_express_checkout_flag_is_false() {
add_filter( 'woocommerce_payments_enabled_gateways_for_woopay', [ $this, 'enable_woopayments' ] );
$this->set_feature_flag_option( WC_Payments_Features::WOOPAY_EXPRESS_CHECKOUT_FLAG_NAME, '0' );
WC_Payments::get_gateway()->update_option( 'platform_checkout', 'yes' );
$this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => true ] );
$this->assertFalse( WC_Payments_Features::is_woopay_enabled() );
}

public function test_is_woopay_enabled_returns_false_when_platform_checkout_flag_is_false() {
add_filter( 'woocommerce_payments_enabled_gateways_for_woopay', [ $this, 'enable_woopayments' ] );
$this->set_feature_flag_option( WC_Payments_Features::WOOPAY_EXPRESS_CHECKOUT_FLAG_NAME, '1' );
WC_Payments::get_gateway()->update_option( 'platform_checkout', 'no' );
$this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => true ] );
$this->assertFalse( WC_Payments_Features::is_woopay_enabled() );
}

public function test_is_woopay_enabled_returns_false_when_ineligible() {
add_filter( 'woocommerce_payments_enabled_gateways_for_woopay', [ $this, 'enable_woopayments' ] );
$this->set_feature_flag_option( WC_Payments_Features::WOOPAY_EXPRESS_CHECKOUT_FLAG_NAME, '1' );
WC_Payments::get_gateway()->update_option( 'platform_checkout', 'yes' );
$this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => false ] );
$this->assertFalse( WC_Payments_Features::is_woopay_enabled() );
}

public function test_is_woopay_enabled_returns_false_when_woopayments_is_disabled() {
add_filter( 'woocommerce_payments_enabled_gateways_for_woopay', [ $this, 'disable_woopayments' ] );
$this->assertFalse( WC_Payments_Features::is_woopay_enabled() );
}

public function test_is_woopay_express_checkout_enabled_returns_true() {
$this->set_feature_flag_option( WC_Payments_Features::WOOPAY_EXPRESS_CHECKOUT_FLAG_NAME, '1' );
$this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => true ] );
Expand All @@ -254,7 +227,6 @@ public function test_is_woopay_express_checkout_enabled_returns_false_when_woopa
}

public function test_is_woopay_direct_checkout_enabled_returns_true() {
add_filter( 'woocommerce_payments_enabled_gateways_for_woopay', [ $this, 'enable_woopayments' ] );
$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(
Expand All @@ -263,17 +235,10 @@ public function test_is_woopay_direct_checkout_enabled_returns_true() {
'platform_direct_checkout_eligible' => true,
]
);
WC_Payments::get_gateway()->update_option( 'platform_checkout', 'yes' );
$this->assertTrue( WC_Payments_Features::is_woopay_direct_checkout_enabled() );
}

public function test_is_woopay_direct_checkout_enabled_returns_false_when_woopayments_is_disabled() {
add_filter( 'woocommerce_payments_enabled_gateways_for_woopay', [ $this, 'disable_woopayments' ] );
$this->assertFalse( WC_Payments_Features::is_woopay_direct_checkout_enabled() );
}

public function test_is_woopay_direct_checkout_enabled_returns_false_when_flag_is_false() {
add_filter( 'woocommerce_payments_enabled_gateways_for_woopay', [ $this, 'enable_woopayments' ] );
$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' );
$this->mock_cache->method( 'get' )->willReturn(
Expand All @@ -286,7 +251,6 @@ public function test_is_woopay_direct_checkout_enabled_returns_false_when_flag_i
}

public function test_is_woopay_direct_checkout_enabled_returns_false_when_woopay_eligible_is_false() {
add_filter( 'woocommerce_payments_enabled_gateways_for_woopay', [ $this, 'enable_woopayments' ] );
$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(
Expand All @@ -299,7 +263,6 @@ 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() {
add_filter( 'woocommerce_payments_enabled_gateways_for_woopay', [ $this, 'enable_woopayments' ] );
$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' );
Expand All @@ -309,7 +272,6 @@ public function test_is_woopay_direct_checkout_enabled_returns_true_when_first_p
'platform_direct_checkout_eligible' => true,
]
);
WC_Payments::get_gateway()->update_option( 'platform_checkout', 'yes' );
$this->assertTrue( WC_Payments_Features::is_woopay_direct_checkout_enabled() );
}

Expand All @@ -323,38 +285,6 @@ public function test_is_frt_review_feature_active_returns_false_when_flag_is_not
$this->assertFalse( WC_Payments_Features::is_frt_review_feature_active() );
}

public function enable_woopayments( $gateways ) {
return array_map(
function ( $gateway ) {
if ( is_a( $gateway, 'WC_Payment_Gateway' ) && 'woocommerce_payments' === $gateway->id ) {
// Simple class to replace the WC_Payment_Gateway_WCPay instance. With this the `is_available` method will return `true` enabling the payment gateway.
return new class() extends WC_Payment_Gateway_WCPay {
public function __construct() {
}

public function is_available() {
return true;
}
};
}
return $gateway;
},
$gateways
);
}

public function disable_woopayments( $gateways ) {
return array_filter(
$gateways,
function ( $gateway ) {
if ( is_object( $gateway ) && 'woocommerce_payments' === $gateway->id ) {
return false;
}
return true;
}
);
}

private function setup_enabled_flags( array $enabled_flags ) {
foreach ( array_keys( self::FLAG_OPTION_NAME_TO_FRONTEND_KEY_MAPPING ) as $flag ) {
add_filter(
Expand Down

0 comments on commit 9897488

Please sign in to comment.