From b241ced9d84f5d1ec6e76f6aff9fd46790ef4dbc Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 7 Jun 2024 14:24:44 -0300 Subject: [PATCH 1/4] Removing saved cards when setting is disabled --- .../class-wc-stripe-upe-payment-gateway.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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..512da63f29 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,20 @@ 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. + * + * @return array An empty list of payment methods + */ + public function filter_saved_payment_methods_list( $item, $payment_token ) { + if ( ! $this->saved_cards ) { + return []; + } + return $item; } /** From cc59ab8ba0312af0a9efebde392f313fdc9b1559 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 7 Jun 2024 17:15:20 -0300 Subject: [PATCH 2/4] Changelog and readme updates --- changelog.txt | 1 + readme.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/changelog.txt b/changelog.txt index 9417e13a56..738150e549 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. * Fix - Correctly setting the preferred card brand when creating and updating a payment intent. * Tweak - Update WordPress.org screenshots and captions. * Fix - Added a feedback message + redirection back to cart when a Cash App payment fails. diff --git a/readme.txt b/readme.txt index 865023ea9a..e4db650787 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. * 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. * Tweak - Update WordPress.org screenshots and captions. From 0c948e4a2ce563a2eb03237517432c5fab3aa183 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Fri, 7 Jun 2024 17:56:49 -0300 Subject: [PATCH 3/4] Adding specific unit tests --- ...st-class-wc-stripe-upe-payment-gateway.php | 76 +++++++++++++++---- 1 file changed, 60 insertions(+), 16 deletions(-) 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 * From 8351c7065029aff3a44cd86fc0b0a20381e7d890 Mon Sep 17 00:00:00 2001 From: Wesley Rosa Date: Mon, 10 Jun 2024 10:46:21 -0300 Subject: [PATCH 4/4] Update includes/payment-methods/class-wc-stripe-upe-payment-gateway.php Co-authored-by: Diego Curbelo --- .../class-wc-stripe-upe-payment-gateway.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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 512da63f29..3c80daa223 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -229,13 +229,15 @@ public function __construct() { /** * Removes all saved payment methods when the setting to save cards is disabled. * - * @return array An empty list of payment methods + * @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( $item, $payment_token ) { + public function filter_saved_payment_methods_list( $list, $customer_id ) { if ( ! $this->saved_cards ) { return []; } - return $item; + return $list; } /**