id = 'woocommerce_payments'; $this->name = __( 'WooCommerce Payments', 'woocommerce-payments' ); $this->payments_api_client = $payments_api_client; $this->account = $account; $this->database_cache = $database_cache; parent::__construct(); } /** * Checks if the core setting is enabled before loading scripts. * The parent method does not check this (will be patched and this override can be removed when WC 10.4 is released) */ public function load_scripts() { if ( wc_string_to_bool( get_option( 'woocommerce_address_autocomplete_enabled', 'no' ) ) === true ) { parent::load_scripts(); } } /** * Get address service JWT token from the WCPay server. * * This method calls the address-autocomplete-token endpoint to retrieve * a JWT token for address autocomplete services. * * Caching and retries and backoff logic is handled by the parent class, if you must override that without fixing it upstream, you should also override `load_jwt`. * * @return string|WP_Error The JWT token on success, WP_Error on failure. */ public function get_address_service_jwt() { $token = $this->database_cache->get_or_add( Database_Cache::ADDRESS_AUTOCOMPLETE_JWT_KEY, function () { if ( ! $this->account->is_stripe_connected() ) { return self::INVALID_TOKEN; } try { $response = $this->payments_api_client->get_address_autocomplete_token(); return $response['token'] ?? self::INVALID_TOKEN; } catch ( \Exception $e ) { Logger::error( 'Unexpected error getting address service JWT: ' . $e->getMessage() ); return self::INVALID_TOKEN; } }, '__return_true' ); if ( self::INVALID_TOKEN === $token ) { return new WP_Error( 'wcpay_address_service_error', 'An unexpected error occurred while retrieving the address service token.' ); } return $token; } /** * Whether the address provider can send frontend telemetry data. * * @return bool True if telemetry is allowed, false otherwise. */ public function can_telemetry() { // We defer to the global Woo setting. return WC_Site_Tracking::is_tracking_enabled(); } }