WP rest api endpoint protection using jwt token

Once user hit the api endpoint will check the token valid or not and process the function send information to wordpress api end point with success details.

 function update_payment_history(WP_REST_Request $request) {      
   if(!$request->get_header('authorization')){
 return new WP_Error(
            'jwt_auth_no_auth_header',
            'Authorization header not found.',
            array(
                'status' => 403,
            )
        );
        exit;
 } else if($request->get_header('authorization')){ 

  $response = wp_remote_post( 'https://example.com/wp-json/jwt- 
   auth/v1/token/validate', array(

'headers'     =>array(
                        'Authorization' => $request->get_header('authorization'),
                            "Accept: application/json")
    
)
);

$check_code = json_decode( wp_remote_retrieve_body( $response ) );

   if($check_code->data->status==200){

 print_r( $request->get_param('status')); // get body parameters
 exit;
}else{
    return new WP_Error(
           $check_code->code,
            array(
                'status' => $check_code->data->status,
            )
        );
        exit;
}  
  }  
    }