1414}
1515
1616use Automattic \WooCommerce \Internal \AddressProvider \AbstractAutomatticAddressProvider ;
17+ use WCPay \Database_Cache ;
1718use WCPay \Logger ;
1819
1920/**
2223 * @psalm-suppress UndefinedClass
2324 */
2425class 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 /**
0 commit comments