Skip to content

Commit

Permalink
Disable WooPay for suspended and rejected accounts - Take 2 (#8942)
Browse files Browse the repository at this point in the history
  • Loading branch information
asumaran committed Jun 17, 2024
1 parent b946daf commit 602e466
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
4 changes: 4 additions & 0 deletions changelog/as-disable-woopay-rejected-suspended-accounts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: fix

Disable WooPay for suspended and rejected accounts.
10 changes: 9 additions & 1 deletion includes/class-wc-payments-features.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,15 @@ public static function is_woopay_eligible() {

// read directly from cache, ignore cache expiration check.
$account = WC_Payments::get_database_cache()->get( WCPay\Database_Cache::ACCOUNT_KEY, true );
return is_array( $account ) && ( $account['platform_checkout_eligible'] ?? false );

$is_account_rejected = WC_Payments::get_account_service()->is_account_rejected();

$is_account_under_review = WC_Payments::get_account_service()->is_account_under_review();

return is_array( $account )
&& ( $account['platform_checkout_eligible'] ?? false )
&& ! $is_account_rejected
&& ! $is_account_under_review;
}

/**
Expand Down
22 changes: 15 additions & 7 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,19 +570,26 @@ public static function init() {
// To avoid register the same hooks twice.
wcpay_get_container()->get( \WCPay\Internal\Service\DuplicatePaymentPreventionService::class )->init_hooks();

self::maybe_register_woopay_hooks();

self::$apple_pay_registration = new WC_Payments_Apple_Pay_Registration( self::$api_client, self::$account, self::get_gateway() );
self::$apple_pay_registration->init_hooks();

$express_checkout_helper = new WC_Payments_Express_Checkout_Button_Helper( self::get_gateway(), self::$account );
self::set_express_checkout_helper( $express_checkout_helper );

self::maybe_display_express_checkout_buttons();

self::maybe_init_woopay_direct_checkout();

self::maybe_enqueue_woopay_common_config_script( WC_Payments_Features::is_woopay_direct_checkout_enabled() );
// Delay registering hooks that could end up in a fatal error due to expired account cache.
// The `woocommerce_payments_account_refreshed` action will result in a fatal error if it's fired before the `$wp_rewrite` is defined.
// See #8942 for more details.
add_action(
'setup_theme',
function () {
add_action( 'woocommerce_payments_account_refreshed', [ WooPay_Order_Status_Sync::class, 'remove_webhook' ] );

self::maybe_register_woopay_hooks();
self::maybe_display_express_checkout_buttons();
self::maybe_init_woopay_direct_checkout();
self::maybe_enqueue_woopay_common_config_script( WC_Payments_Features::is_woopay_direct_checkout_enabled() );
}
);

// Insert the Stripe Payment Messaging Element only if there is at least one BNPL method enabled.
$enabled_bnpl_payment_methods = array_intersect(
Expand Down Expand Up @@ -1502,6 +1509,7 @@ public static function is_network_saved_cards_enabled() {

/**
* Registers woopay hooks if the woopay feature flag is enabled.
* Removes WooPay webhooks if the merchant is not eligible.
*
* @return void
*/
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/test-class-wc-payments-features.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class WC_Payments_Features_Test extends WCPAY_UnitTestCase {
*/
protected $mock_cache;

/**
* Mock WC_Payments_Account.
*
* @var WC_Payments_Account|MockObject
*/
private $mock_wcpay_account;

const FLAG_OPTION_NAME_TO_FRONTEND_KEY_MAPPING = [
'_wcpay_feature_customer_multi_currency' => 'multiCurrency',
'_wcpay_feature_documents' => 'documents',
Expand All @@ -29,6 +36,17 @@ public function set_up() {
$this->_cache = WC_Payments::get_database_cache();
$this->mock_cache = $this->createMock( WCPay\Database_Cache::class );
WC_Payments::set_database_cache( $this->mock_cache );

// Mock the WCPay Account class to make sure the account is not restricted by default.
$this->mock_wcpay_account = $this->createMock( WC_Payments_Account::class );
$this->mock_wcpay_account
->method( 'is_account_rejected' )
->willReturn( false );
$this->mock_wcpay_account
->method( 'is_account_under_review' )
->willReturn( false );

WC_Payments::set_account_service( $this->mock_wcpay_account );
}

public function tear_down() {
Expand Down Expand Up @@ -91,6 +109,32 @@ public function test_is_woopay_eligible_returns_false() {
$this->assertFalse( WC_Payments_Features::is_woopay_eligible() );
}

public function test_is_woopay_eligible_when_account_is_suspended_returns_false() {
$mock_wcpay_account = $this->createMock( WC_Payments_Account::class );
$mock_wcpay_account
->method( 'is_account_under_review' )
->willReturn( true );

WC_Payments::set_account_service( $mock_wcpay_account );

$this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => true ] );

$this->assertFalse( WC_Payments_Features::is_woopay_eligible() );
}

public function test_is_woopay_eligible_when_account_is_rejected_returns_false() {
$mock_wcpay_account = $this->createMock( WC_Payments_Account::class );
$mock_wcpay_account
->method( 'is_account_rejected' )
->willReturn( true );

WC_Payments::set_account_service( $mock_wcpay_account );

$this->mock_cache->method( 'get' )->willReturn( [ 'platform_checkout_eligible' => true ] );

$this->assertFalse( WC_Payments_Features::is_woopay_eligible() );
}

public function test_is_documents_section_enabled_returns_true_when_flag_is_true() {
$this->mock_cache->method( 'get' )->willReturn( [ 'is_documents_enabled' => true ] );
$this->assertTrue( WC_Payments_Features::is_documents_section_enabled() );
Expand Down

0 comments on commit 602e466

Please sign in to comment.