Title: Caching client_token
Last modified: March 1, 2021

---

# Caching client_token

 *  Resolved [David Marín Carreño](https://wordpress.org/support/users/davefx/)
 * (@davefx)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/caching-client_token/)
 * Hi!
 * We are using your plugin on our site and I’ve observed that every call to the/
   checkout page generates a call to the Braintree API to obtain the client_token.
   This request is somewhat time-expensive, compared to the time the checkout screen
   takes to render.
 * Have you considered the possibility of caching the returned value so we are not
   constantly asking Braintree for the client_token again and again?
 * In our site we are using Redis as a persistent object cache, so this change would
   represent a big improvement in the server performance.
 * I’ve carried out a local test replacing the contents of the wc_braintree_generated_client_token()
   function with the following ones:
 *     ```
       function wc_braintree_generate_client_token( $env = '' ) {
       	$client_token = '';
       	try {
       		$args = array();
       		if ( ( $merchant_account = wc_braintree_get_merchant_account( wc_braintree_get_currency() ) ) ) {
       			$args['merchantAccountId'] = $merchant_account;
       		}
       		$cache_key = 'wc_braintree_client_token_' . md5(json_encode($args) . json_encode( $env ) );
       		$client_token = wp_cache_get( $cache_key, 'woo-payment-gateway' );
   
       		if ( $client_token === false ) {
       			$gateway      = new \Braintree\Gateway( wc_braintree_connection_settings( $env ) );
       			$client_token = $gateway->clientToken()->generate( $args );
       			wp_cache_set( $cache_key, $client_token, 'woo-payment-gateway', 60 );
       		}
       	} catch ( \Braintree\Exception $e ) {
       		wc_braintree_log_error( sprintf( __( 'Error creating client token. Exception: %1$s', 'woo-payment-gateway' ), get_class( $e ) ) );
       	}
   
       	return $client_token;
       }
       ```
   
 * and things seem to keep working, but now much faster.
 * Am I missing anything?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Clayton R](https://wordpress.org/support/users/mrclayton/)
 * (@mrclayton)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/caching-client_token/#post-14119326)
 * Hi [@davefx](https://wordpress.org/support/users/davefx/),
 * The issue with caching the client token is it has an expiration time and a max
   consumption count.
 * Braintree doesn’t provide an exact number but you can’t consume the client token
   too many times before it throws an error due to security reasons.
 * That is why I have coded it to generate a new client token during each page load.
 * Kind regards,
 *  Plugin Author [Clayton R](https://wordpress.org/support/users/mrclayton/)
 * (@mrclayton)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/caching-client_token/#post-14119330)
 * I could add a hook in the next version that allows merchants to controls this
   client token generation. But it will be at your own risk for the reasons outlined
   in the previous response.
 * Kind regards,
 *  Thread Starter [David Marín Carreño](https://wordpress.org/support/users/davefx/)
 * (@davefx)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/caching-client_token/#post-14121203)
 * After contacting Braintree support they’ve told me:
 * >  Client tokens are only valid for 24 hours. However, we still recommend generating
   > one for each customer for a couple of reasons:
   > * If too many payment methods are made in short succession using the same client
   > token, it will be invalidated (we do not provide specific figures for this 
   > to mitigate fraud risk)
   > * There are additional parameters you can pass in client token generation that
   > may be customer or currency-specific (customer ID, or merchant account ID respectively).
   > A token will be invalidated after its first use if these parameters are present.
   > For these 2 reasons, it is a best practice to generate a new client token as
   > frequently as possible, if not for each unique customer session.
 * As the code is providing the merchant account ID, I’m afraid we won’t be able
   to activate this caching unless we are also able to invalidate any existing cached
   value if the client token is used.
    -  This reply was modified 5 years, 2 months ago by [David Marín Carreño](https://wordpress.org/support/users/davefx/).
    -  This reply was modified 5 years, 2 months ago by [David Marín Carreño](https://wordpress.org/support/users/davefx/).

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Caching client_token’ is closed to new replies.

 * ![](https://ps.w.org/woo-payment-gateway/assets/icon-256x256.png?rev=2142799)
 * [Payment Plugins Braintree For WooCommerce](https://wordpress.org/plugins/woo-payment-gateway/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woo-payment-gateway/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woo-payment-gateway/)
 * [Active Topics](https://wordpress.org/support/plugin/woo-payment-gateway/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woo-payment-gateway/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woo-payment-gateway/reviews/)

## Tags

 * [braintree](https://wordpress.org/support/topic-tag/braintree/)
 * [performance](https://wordpress.org/support/topic-tag/performance/)

 * 3 replies
 * 2 participants
 * Last reply from: [David Marín Carreño](https://wordpress.org/support/users/davefx/)
 * Last activity: [5 years, 2 months ago](https://wordpress.org/support/topic/caching-client_token/#post-14121203)
 * Status: resolved