host = $host; $this->bearer = $bearer; $this->logger = $logger; } /** * Returns `id_token` which uniquely identifies the payer. * * @throws PayPalApiException If the request fails. * @throws RuntimeException If something unexpected happens. */ public function id_token(): string { $bearer = $this->bearer->bearer(); $url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token'; $args = array( 'method' => 'POST', 'headers' => array( 'Authorization' => 'Bearer ' . $bearer->token(), 'Content-Type' => 'application/x-www-form-urlencoded', ), ); $response = $this->request( $url, $args ); if ( $response instanceof WP_Error ) { throw new RuntimeException( $response->get_error_message() ); } $json = json_decode( $response['body'] ); $status_code = (int) wp_remote_retrieve_response_code( $response ); if ( 200 !== $status_code ) { throw new PayPalApiException( $json, $status_code ); } return $json->id_token; } }