Skip to content

Commit 672ffdc

Browse files
wjrosaannemirasoldaledupreezmalithsen
authored
Adding a fetch cooldown to PM config retrieval endpoint calls (#4330)
* Adding a fetch cooldown to PM config retrieval endpoint calls * Changelog and readme entries * Moving up the setting of the option value * Fallback to cards when config is not available * Improving the minimal card config * Use legacy settings as fallback * Fix tests * Implement a fallback cache using wp options * Use 'or' in cache logic * Update docblock to reflect new param * Modify logic to only use fallbacks when hitting API too much * Remove minimal config fallback * Add comment about fallthrough to fetching data Co-authored-by: daledupreez <dale.du.preez@automattic.com> * Update test flag logic and naming Co-authored-by: daledupreez <dale.du.preez@automattic.com> * Fix linting issues * Moving the transient key to a new constant * Renaming transient to option * Renaming transient to option * Better fix for unit tests * Removing unnecessary hook in unit tests * Adding log to identify merchants not connected to our platform account * Reverting the new log --------- Co-authored-by: Anne Mirasol <anne.mirasol@automattic.com> Co-authored-by: daledupreez <dale.du.preez@automattic.com> Co-authored-by: Malith Senaweera <6216000+malithsen@users.noreply.github.com>
1 parent 962163c commit 672ffdc

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** Changelog ***
22

33
= 9.6.0 - xxxx-xx-xx =
4+
* Fix - Adds a fetch cooldown to the payment method configuration retrieval endpoint to prevent excessive requests.
45
* Fix - Fixes the payment method title when using the classic checkout with the Optimized Checkout enabled.
56
* Fix - Fix fatal error when checking for a payment method availability using a specific order ID.
67
* Fix - Stop checking for detached subscriptions for admin users, as it was slowing down wp-admin

includes/class-wc-stripe-payment-method-configurations.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,64 @@ class WC_Stripe_Payment_Method_Configurations {
5151
*/
5252
const CONFIGURATION_CACHE_TRANSIENT_EXPIRATION = 10 * MINUTE_IN_SECONDS;
5353

54+
/**
55+
* The payment method configuration fetch cooldown option key.
56+
*/
57+
const FETCH_COOLDOWN_OPTION_KEY = 'wcstripe_payment_method_config_fetch_cooldown';
58+
5459
/**
5560
* Get the merchant payment method configuration in Stripe.
5661
*
5762
* @param bool $force_refresh Whether to force a refresh of the payment method configuration from Stripe.
5863
* @return object|null
5964
*/
6065
private static function get_primary_configuration( $force_refresh = false ) {
61-
if ( ! $force_refresh ) {
66+
// Only allow fetching payment configuration once per minute.
67+
$fetch_cooldown = get_option( self::FETCH_COOLDOWN_OPTION_KEY, 0 );
68+
$is_in_cooldown = $fetch_cooldown > time();
69+
if ( ! $force_refresh || $is_in_cooldown ) {
6270
$cached_primary_configuration = self::get_payment_method_configuration_from_cache();
6371
if ( $cached_primary_configuration ) {
6472
return $cached_primary_configuration;
6573
}
74+
75+
// If we are hitting the API too much, and our main cache is not working, use the fallback cache.
76+
if ( $is_in_cooldown ) {
77+
$fallback_cache = self::get_payment_method_configuration_from_cache( true );
78+
if ( $fallback_cache ) {
79+
return $fallback_cache;
80+
}
81+
}
82+
83+
// Intentionally fall through to fetching the data from Stripe if we don't have it locally,
84+
// even when $force_refresh = false and/or $is_in_cooldown is true.
85+
// We _need_ the payment method configuration for things to work as expected,
86+
// so we will fetch it if we don't have anything locally.
6687
}
6788

89+
update_option( self::FETCH_COOLDOWN_OPTION_KEY, time() + MINUTE_IN_SECONDS );
90+
6891
return self::get_payment_method_configuration_from_stripe();
6992
}
7093

7194
/**
7295
* Get the payment method configuration from cache.
7396
*
97+
* @param bool $use_fallback Whether to use the fallback cache if the transient is not available.
98+
*
7499
* @return object|null
75100
*/
76-
private static function get_payment_method_configuration_from_cache() {
101+
private static function get_payment_method_configuration_from_cache( $use_fallback = false ) {
77102
if ( null !== self::$primary_configuration ) {
78103
return self::$primary_configuration;
79104
}
80105

81-
$cache_key = WC_Stripe_Mode::is_test() ? self::TEST_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY : self::LIVE_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY;
106+
$cache_key = WC_Stripe_Mode::is_test() ? self::TEST_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY : self::LIVE_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY;
82107
$cached_primary_configuration = get_transient( $cache_key );
83108
if ( false === $cached_primary_configuration || null === $cached_primary_configuration ) {
109+
if ( $use_fallback ) {
110+
return get_option( $cache_key );
111+
}
84112
return null;
85113
}
86114

@@ -93,8 +121,9 @@ private static function get_payment_method_configuration_from_cache() {
93121
*/
94122
public static function clear_payment_method_configuration_cache() {
95123
self::$primary_configuration = null;
96-
$cache_key = WC_Stripe_Mode::is_test() ? self::TEST_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY : self::LIVE_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY;
124+
$cache_key = WC_Stripe_Mode::is_test() ? self::TEST_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY : self::LIVE_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY;
97125
delete_transient( $cache_key );
126+
delete_option( $cache_key );
98127
}
99128

100129
/**
@@ -104,8 +133,11 @@ public static function clear_payment_method_configuration_cache() {
104133
*/
105134
private static function set_payment_method_configuration_cache( $configuration ) {
106135
self::$primary_configuration = $configuration;
107-
$cache_key = WC_Stripe_Mode::is_test() ? self::TEST_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY : self::LIVE_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY;
136+
$cache_key = WC_Stripe_Mode::is_test() ? self::TEST_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY : self::LIVE_MODE_CONFIGURATION_CACHE_TRANSIENT_KEY;
108137
set_transient( $cache_key, $configuration, self::CONFIGURATION_CACHE_TRANSIENT_EXPIRATION );
138+
139+
// To be used as fallback if we are in API cooldown and the transient is not available.
140+
update_option( $cache_key, $configuration );
109141
}
110142

111143
/**
@@ -115,11 +147,7 @@ private static function set_payment_method_configuration_cache( $configuration )
115147
*/
116148
private static function get_payment_method_configuration_from_stripe() {
117149
$result = WC_Stripe_API::get_instance()->get_payment_method_configurations();
118-
$configurations = $result->data ?? null;
119-
120-
if ( ! $configurations ) {
121-
return null;
122-
}
150+
$configurations = $result->data ?? [];
123151

124152
// When connecting to the WooCommerce Platform account a new payment method configuration is created for the merchant.
125153
// This new payment method configuration has the WooCommerce Platform payment method configuration as parent, and inherits it's default payment methods.

phpunit.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@
2525
<html outputDirectory="phpunit-html"/>
2626
</report>
2727
</coverage>
28-
2928
</phpunit>

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
112112

113113
= 9.6.0 - xxxx-xx-xx =
114114

115+
* Fix - Adds a fetch cooldown to the payment method configuration retrieval endpoint to prevent excessive requests.
115116
* Fix - Fixes the payment method title when using the classic checkout with the Optimized Checkout enabled.
116117
* Fix - Fix fatal error when checking for a payment method availability using a specific order ID.
117118
* Fix - Stop checking for detached subscriptions for admin users, as it was slowing down wp-admin

0 commit comments

Comments
 (0)