Skip to content

Use database cache for all customer payment methods #4570

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

Draft
wants to merge 19 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
91d638b
Basic get_all_payment_methods() implementation
daledupreez Aug 7, 2025
35837db
Ensure we clear the new _all payment methods cache
daledupreez Aug 8, 2025
9cfccaf
Always send expand flags for SEPA
daledupreez Aug 8, 2025
68bee81
Refactor logic to get customer UPE tokens
daledupreez Aug 8, 2025
c622d76
Refactor WC_Stripe_Subscriptions_Trait->maybe_render_subscription_pay…
daledupreez Aug 8, 2025
2e20c28
Revert "Refactor WC_Stripe_Subscriptions_Trait->maybe_render_subscrip…
daledupreez Aug 8, 2025
b733cd3
Reinstate SEPA logic
daledupreez Aug 8, 2025
243d766
Merge branch 'develop' into refactor/fetching-all-customer-payment-me…
daledupreez Aug 8, 2025
341f885
Remove unnecessary array_merge() call
daledupreez Aug 8, 2025
20ade01
Changelog
daledupreez Aug 8, 2025
6c9eb2f
Use Payment Method Configuration to identify active payment methods
daledupreez Aug 8, 2025
0b12464
Merge branch 'develop' into refactor/fetching-all-customer-payment-me…
daledupreez Aug 11, 2025
b6e1e68
Add link to Stripe docs for limit argument
daledupreez Aug 11, 2025
aaa7ba7
Use one-line isset() for missing customer checks
daledupreez Aug 11, 2025
ef842a2
Use database cache for all payment methods cache
daledupreez Aug 11, 2025
247b333
Changelog
daledupreez Aug 11, 2025
4dc2b3c
Remove double-period
daledupreez Aug 11, 2025
6028240
Merge branch 'develop' into add/database-caching-for-all-payment-methods
daledupreez Aug 13, 2025
5e314fa
Merge branch 'develop' into add/database-caching-for-all-payment-methods
daledupreez Aug 18, 2025
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
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Tweak - Small improvements to e2e tests
* Fix - Prevent the PMC migration to run when the plugin is not connected to Stripe
* Fix - Fixes a fatal error in the OC inbox note when the new checkout is disabled
* Tweak - Improve how we cache saved payment methods for customers

= 9.8.0 - 2025-08-11 =
* Add - Adds the current setting value for the Optimized Checkout to the Stripe System Status Report data
Expand Down Expand Up @@ -46,6 +47,7 @@
* Update - Copy for the Optimized Checkout settings and notices
* Update - Removes the ability to change the title for the Optimized Checkout payment element, as it is now set to "Stripe" by default
* Fix - Handle missing customer when calling payment_methods API
* Dev - Fix some e2e issues: timing, optional flows, and WooCommerce RC support
* Fix - Reduce number of calls to Stripe payment_methods API
* Fix - Add `get_icon_url()` to Payment Method base class

Expand Down
15 changes: 10 additions & 5 deletions includes/class-wc-stripe-customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class WC_Stripe_Customer {
*/
const PAYMENT_METHODS_TRANSIENT_KEY = 'stripe_payment_methods_';

/**
* Cache prefix for all saved payment methods per customer.
*/
protected const ALL_PAYMENT_METHODS_CACHE_PREFIX = 'all_payment_methods_';

/**
* Queryable Stripe payment method types.
*/
Expand Down Expand Up @@ -806,8 +811,8 @@ public function get_all_payment_methods( array $payment_method_types = [], int $
return [];
}

$cache_key = self::PAYMENT_METHODS_TRANSIENT_KEY . '__all_' . $this->get_id();
$all_payment_methods = get_transient( $cache_key );
$cache_key = self::ALL_PAYMENT_METHODS_CACHE_PREFIX . $this->get_id();
$all_payment_methods = WC_Stripe_Database_Cache::get( $cache_key );

if ( false === $all_payment_methods || ! is_array( $all_payment_methods ) ) {
$all_payment_methods = [];
Expand All @@ -832,7 +837,7 @@ public function get_all_payment_methods( array $payment_method_types = [], int $
&& 'resource_missing' === $response->error->code
) {
// If the customer doesn't exist, cache an empty array.
set_transient( $cache_key, [], DAY_IN_SECONDS );
WC_Stripe_Database_Cache::set( $cache_key, [], DAY_IN_SECONDS );
}
return [];
}
Expand All @@ -855,7 +860,7 @@ public function get_all_payment_methods( array $payment_method_types = [], int $
} while ( null !== $last_payment_method_id );

// Always cache the result without any filters applied.
set_transient( $cache_key, $all_payment_methods, DAY_IN_SECONDS );
WC_Stripe_Database_Cache::set( $cache_key, $all_payment_methods, DAY_IN_SECONDS );
}

// If there are no payment methods, no need to apply any filters below.
Expand Down Expand Up @@ -986,7 +991,7 @@ public function clear_cache( $payment_method_id = null ) {
foreach ( self::STRIPE_PAYMENT_METHODS as $payment_method_type ) {
delete_transient( self::PAYMENT_METHODS_TRANSIENT_KEY . $payment_method_type . $this->get_id() );
Copy link
Preview

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The clear_cache method has inconsistent caching mechanisms - it still uses delete_transient for individual payment method types but WC_Stripe_Database_Cache::delete for all payment methods. Consider updating all cache clearing to use the same mechanism for consistency.

Suggested change
delete_transient( self::PAYMENT_METHODS_TRANSIENT_KEY . $payment_method_type . $this->get_id() );
WC_Stripe_Database_Cache::delete( self::PAYMENT_METHODS_TRANSIENT_KEY . $payment_method_type . $this->get_id() );

Copilot uses AI. Check for mistakes.

}
delete_transient( self::PAYMENT_METHODS_TRANSIENT_KEY . '__all_' . $this->get_id() );
WC_Stripe_Database_Cache::delete( self::ALL_PAYMENT_METHODS_CACHE_PREFIX . $this->get_id() );
// Clear cache for the specific payment method if provided.
if ( $payment_method_id ) {
WC_Stripe_Database_Cache::delete( 'payment_method_for_source_' . $payment_method_id );
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,6 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
* Tweak - Small improvements to e2e tests
* Fix - Prevent the PMC migration to run when the plugin is not connected to Stripe
* Fix - Fixes a fatal error in the OC inbox note when the new checkout is disabled
* Tweak - Improve how we cache saved payment methods for customers

[See changelog for full details across versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).
Loading