Skip to content

Commit 9860e20

Browse files
authored
Add caching to address autocomplete token (#11062)
1 parent d362d8e commit 9860e20

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

changelog/fix-woopmt-5349

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fix
3+
4+
Add caching to address autocomplete token

includes/class-database-cache.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
* A class for caching data as an option in the database.
1717
*/
1818
class Database_Cache implements MultiCurrencyCacheInterface {
19-
const ACCOUNT_KEY = 'wcpay_account_data';
20-
const ONBOARDING_FIELDS_DATA_KEY = 'wcpay_onboarding_fields_data';
21-
const BUSINESS_TYPES_KEY = 'wcpay_business_types_data';
22-
const PAYMENT_PROCESS_FACTORS_KEY = 'wcpay_payment_process_factors';
23-
const FRAUD_SERVICES_KEY = 'wcpay_fraud_services_data';
24-
const RECOMMENDED_PAYMENT_METHODS = 'wcpay_recommended_payment_methods';
19+
const ACCOUNT_KEY = 'wcpay_account_data';
20+
const ONBOARDING_FIELDS_DATA_KEY = 'wcpay_onboarding_fields_data';
21+
const BUSINESS_TYPES_KEY = 'wcpay_business_types_data';
22+
const PAYMENT_PROCESS_FACTORS_KEY = 'wcpay_payment_process_factors';
23+
const FRAUD_SERVICES_KEY = 'wcpay_fraud_services_data';
24+
const RECOMMENDED_PAYMENT_METHODS = 'wcpay_recommended_payment_methods';
25+
const ADDRESS_AUTOCOMPLETE_JWT_KEY = 'wcpay_address_autocomplete_jwt';
2526

2627
/**
2728
* Refresh during AJAX calls is avoided, but white-listing
@@ -469,6 +470,9 @@ private function get_ttl( string $key, array $cache_contents ): int {
469470
case self::TRACKING_INFO_KEY:
470471
$ttl = $cache_contents['errored'] ? 2 * MINUTE_IN_SECONDS : MONTH_IN_SECONDS;
471472
break;
473+
case self::ADDRESS_AUTOCOMPLETE_JWT_KEY:
474+
$ttl = 12 * HOUR_IN_SECONDS;
475+
break;
472476
default:
473477
// Default to 24h.
474478
$ttl = DAY_IN_SECONDS;

includes/class-wc-payments-address-provider.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
}
1515

1616
use Automattic\WooCommerce\Internal\AddressProvider\AbstractAutomatticAddressProvider;
17+
use WCPay\Database_Cache;
1718
use WCPay\Logger;
1819

1920
/**
@@ -22,22 +23,45 @@
2223
* @psalm-suppress UndefinedClass
2324
*/
2425
class WC_Payments_Address_Provider extends AbstractAutomatticAddressProvider {
26+
/**
27+
* Placeholder value to use in the cache when the token retrieval fails.
28+
*/
29+
const INVALID_TOKEN = 'INVALID_TOKEN';
30+
2531
/**
2632
* Client for making requests to the WooCommerce Payments API
2733
*
2834
* @var WC_Payments_API_Client
2935
*/
3036
protected $payments_api_client;
3137

38+
/**
39+
* Payments account service.
40+
*
41+
* @var WC_Payments_Account
42+
*/
43+
private $account;
44+
45+
/**
46+
* Database cache instance.
47+
*
48+
* @var Database_Cache
49+
*/
50+
private $database_cache;
51+
3252
/**
3353
* Constructor.
3454
*
3555
* @param WC_Payments_API_Client $payments_api_client The API client for making requests.
56+
* @param WC_Payments_Account $account The payments account service.
57+
* @param Database_Cache $database_cache The database cache instance.
3658
*/
37-
public function __construct( WC_Payments_API_Client $payments_api_client ) {
59+
public function __construct( WC_Payments_API_Client $payments_api_client, WC_Payments_Account $account, Database_Cache $database_cache ) {
3860
$this->id = 'woocommerce_payments';
3961
$this->name = __( 'WooCommerce Payments', 'woocommerce-payments' );
4062
$this->payments_api_client = $payments_api_client;
63+
$this->account = $account;
64+
$this->database_cache = $database_cache;
4165
parent::__construct();
4266
}
4367

@@ -53,16 +77,32 @@ public function __construct( WC_Payments_API_Client $payments_api_client ) {
5377
* @return string|WP_Error The JWT token on success, WP_Error on failure.
5478
*/
5579
public function get_address_service_jwt() {
56-
try {
57-
$response = $this->payments_api_client->get_address_autocomplete_token();
58-
return $response['token'];
59-
} catch ( \Exception $e ) {
60-
Logger::error( 'Unexpected error getting address service JWT: ' . $e->getMessage() );
80+
$token = $this->database_cache->get_or_add(
81+
Database_Cache::ADDRESS_AUTOCOMPLETE_JWT_KEY,
82+
function () {
83+
if ( ! $this->account->is_stripe_connected() ) {
84+
return self::INVALID_TOKEN;
85+
}
86+
87+
try {
88+
$response = $this->payments_api_client->get_address_autocomplete_token();
89+
return $response['token'] ?? self::INVALID_TOKEN;
90+
} catch ( \Exception $e ) {
91+
Logger::error( 'Unexpected error getting address service JWT: ' . $e->getMessage() );
92+
return self::INVALID_TOKEN;
93+
}
94+
},
95+
'__return_true'
96+
);
97+
98+
if ( self::INVALID_TOKEN === $token ) {
6199
return new WP_Error(
62100
'wcpay_address_service_error',
63101
'An unexpected error occurred while retrieving the address service token.'
64102
);
65103
}
104+
105+
return $token;
66106
}
67107

68108
/**

includes/class-wc-payments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ public static function add_address_provider( $providers ) {
904904

905905
include_once __DIR__ . '/class-wc-payments-address-provider.php';
906906

907-
$providers[] = new WC_Payments_Address_Provider( self::$api_client );
907+
$providers[] = new WC_Payments_Address_Provider( self::$api_client, self::$account, self::$database_cache );
908908

909909
return $providers;
910910
}

0 commit comments

Comments
 (0)