diff --git a/changelog.txt b/changelog.txt index 92071250d5..e8d0a9b360 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ *** Changelog *** = 8.4.0 - xxxx-xx-xx = +* Fix - Removes the list of saved payment methods when the setting is disabled. * Tweak - Update WooCommerce.com docs links. * Fix - Correctly setting the preferred card brand when creating and updating a payment intent. * Tweak - Update WordPress.org screenshots and captions. diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 5e5258c5c5..3c80daa223 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -222,6 +222,22 @@ public function __construct() { add_action( 'set_logged_in_cookie', [ $this, 'set_cookie_on_current_request' ] ); add_action( 'wc_ajax_wc_stripe_save_appearance', [ $this, 'save_appearance_ajax' ] ); + + add_filter( 'woocommerce_saved_payment_methods_list', [ $this, 'filter_saved_payment_methods_list' ], 10, 2 ); + } + + /** + * Removes all saved payment methods when the setting to save cards is disabled. + * + * @param array $list List of payment methods passed from wc_get_customer_saved_methods_list(). + * @param int $customer_id The customer to fetch payment methods for. + * @return array Filtered list of customers payment methods. + */ + public function filter_saved_payment_methods_list( $list, $customer_id ) { + if ( ! $this->saved_cards ) { + return []; + } + return $list; } /** diff --git a/readme.txt b/readme.txt index 166b197aa4..e8f357b212 100644 --- a/readme.txt +++ b/readme.txt @@ -129,6 +129,7 @@ If you get stuck, you can ask for help in the Plugin Forum. == Changelog == = 8.4.0 - xxxx-xx-xx = +* Fix - Removes the list of saved payment methods when the setting is disabled. * Tweak - Update WooCommerce.com docs links. * Fix - Correctly setting the preferred card brand when creating and updating a payment intent. * Fix - Added a feedback message + redirection back to cart when a Cash App payment fails. diff --git a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php index 80cda27eba..f06c89663e 100644 --- a/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php +++ b/tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php @@ -321,11 +321,11 @@ public function test_process_payment_returns_valid_response() { list( $amount, $description, $metadata ) = $this->get_order_details( $order ); $expected_request = [ - 'amount' => $amount, - 'currency' => $currency, - 'description' => $description, - 'customer' => $customer_id, - 'metadata' => $metadata, + 'amount' => $amount, + 'currency' => $currency, + 'description' => $description, + 'customer' => $customer_id, + 'metadata' => $metadata, ]; $_POST = [ 'wc_payment_intent_id' => $payment_intent_id ]; @@ -428,7 +428,7 @@ public function test_process_payment_deferred_intent_with_required_action_return ], ], 'payment_method' => 'pm_mock', - 'charges' => (object) [ + 'charges' => (object) [ 'total_count' => 0, // Intents requiring SCA verification respond with no charges. 'data' => [], ], @@ -484,17 +484,17 @@ public function test_process_payment_deferred_intent_with_required_action_for_wa $mock_intent = (object) wp_parse_args( [ - 'status' => 'requires_action', - 'data' => [ + 'status' => 'requires_action', + 'data' => [ (object) [ 'id' => $order_id, 'captured' => 'yes', 'status' => 'succeeded', ], ], - 'payment_method' => 'pm_mock', + 'payment_method' => 'pm_mock', 'payment_method_types' => [ 'wechat_pay' ], - 'charges' => (object) [ + 'charges' => (object) [ 'total_count' => 0, // Intents requiring SCA verification respond with no charges. 'data' => [], ], @@ -1538,12 +1538,12 @@ public function test_if_order_has_subscription_payment_method_will_be_saved() { list( $amount, $description, $metadata ) = $this->get_order_details( $order ); $expected_request = [ - 'amount' => $amount, - 'currency' => $currency, - 'description' => $description, - 'customer' => $customer_id, - 'metadata' => $metadata, - 'setup_future_usage' => 'off_session', + 'amount' => $amount, + 'currency' => $currency, + 'description' => $description, + 'customer' => $customer_id, + 'metadata' => $metadata, + 'setup_future_usage' => 'off_session', ]; $_POST = [ 'wc_payment_intent_id' => $payment_intent_id ]; @@ -2093,6 +2093,50 @@ public function test_process_payment_deferred_intent_with_co_branded_cc_and_pref $this->assertMatchesRegularExpression( '/Charge ID: ch_mock/', $note->content ); } + /** + * Test for `filter_saved_payment_methods_list` + * + * @param bool $saved_cards Whether saved cards are enabled. + * @param array $item The list of saved payment methods. + * @param array $expected The expected list of saved payment methods. + * @return void + * @dataProvider provide_test_filter_saved_payment_methods_list + */ + public function test_filter_saved_payment_methods_list( $saved_cards, $item, $expected ) { + $payment_token = $this->getMockBuilder( 'WC_Payment_Token_CC' ) + ->disableOriginalConstructor() + ->getMock(); + $this->mock_gateway->saved_cards = $saved_cards; + $list = $this->mock_gateway->filter_saved_payment_methods_list( $item, $payment_token ); + $this->assertSame( $expected, $list ); + } + + /** + * Provider for `test_filter_saved_payment_methods_list` + * + * @return array + */ + public function provide_test_filter_saved_payment_methods_list() { + $item = [ + 'brand' => 'visa', + 'exp_month' => '7', + 'exp_year' => '2099', + 'last4' => '4242', + ]; + return [ + 'Saved cards enabled' => [ + 'saved cards' => true, + 'item' => $item, + 'expected' => $item, + ], + 'Saved cards disabled' => [ + 'saved cards' => false, + 'item' => $item, + 'expected' => [], + ], + ]; + } + /** * @param array $account_data *