diff --git a/modules/ppcp-api-client/services.php b/modules/ppcp-api-client/services.php index 515968463..21df84fc8 100644 --- a/modules/ppcp-api-client/services.php +++ b/modules/ppcp-api-client/services.php @@ -840,6 +840,9 @@ return array( $container->get( 'wcgateway.settings' ) ); }, + 'api.paypal-bearer-cache' => static function( ContainerInterface $container ): Cache { + return new Cache( 'ppcp-paypal-bearer' ); + }, 'api.client-credentials-cache' => static function( ContainerInterface $container ): Cache { return new Cache( 'ppcp-client-credentials-cache' ); }, diff --git a/modules/ppcp-api-client/src/ApiModule.php b/modules/ppcp-api-client/src/ApiModule.php index c75c63e6b..a2880761c 100644 --- a/modules/ppcp-api-client/src/ApiModule.php +++ b/modules/ppcp-api-client/src/ApiModule.php @@ -20,6 +20,8 @@ use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameI use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ServiceModule; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; +use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer; +use WooCommerce\PayPalCommerce\ApiClient\Authentication\SdkClientToken; /** * Class ApiModule @@ -103,6 +105,34 @@ class ApiModule implements ServiceModule, ExtendingModule, ExecutableModule { 2 ); + /** + * Flushes the API client caches. + */ + add_action( + 'woocommerce_paypal_payments_flush_api_cache', + static function () use ( $c ) { + $caches = array( + 'api.paypal-bearer-cache' => array( + PayPalBearer::CACHE_KEY, + ), + 'api.client-credentials-cache' => array( + SdkClientToken::CACHE_KEY, + ), + ); + + foreach ( $caches as $cache_id => $keys ) { + $cache = $c->get( $cache_id ); + assert( $cache instanceof Cache ); + + foreach ( $keys as $key ) { + if ( $cache->has( $key ) ) { + $cache->delete( $key ); + } + } + } + } + ); + return true; } }