Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing saved cards when setting is disabled #3189

Merged
merged 5 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
16 changes: 16 additions & 0 deletions includes/payment-methods/class-wc-stripe-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
76 changes: 60 additions & 16 deletions tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Expand Down Expand Up @@ -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' => [],
],
Expand Down Expand Up @@ -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' => [],
],
Expand Down Expand Up @@ -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 ];
Expand Down Expand Up @@ -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
*
Expand Down
Loading