Ver Mensaje Individual
  #8  
Antiguo 02-10-2023
Avatar de keys
keys keys is offline
Miembro
 
Registrado: sep 2003
Ubicación: Bilbao
Posts: 1.030
Reputación: 22
keys Va por buen camino
Hola a todos. Pongo una función para validar un documento en el servicio VIES de la UE.

Código Delphi [-]
function ComprobarDocumentoVIES(Pais, Documento: string): false;
var
  LRequest: THTTPClient;

  LResponse: TStringStream;
  JSONData: TJSONObject;
  StringStream: TStringStream;
  valido : Boolean;
begin

  result := false;
  LRequest := THTTPClient.Create;
  LResponse := TStringStream.Create;

  //Objeto JSON para enviar los datos
  JSONData := TJSONObject.Create;
  JSONData.AddPair('countryCode', Pais);
  JSONData.AddPair('vatNumber', Documento);
  
  try

    Lrequest.ContentType := 'application/json';

    StringStream := TStringStream.Create(JSONData.ToString, TEncoding.UTF8);
    LRequest.Post('https://ec.europa.eu/taxation_customs/vies/rest-api/check-vat-number', StringStream , LResponse);
   
    JSONData := TJSONObject.ParseJSONValue(LResponse.DataString) as TJSONObject;

    if Assigned(JSONData) then
      begin
        // Obtener el valor del campo deseado
         if JSONData.TryGetValue Boolean ('valid', valido) then //esto lo pongo asi por que el chat no me deja poner los simbolos de mayor y menor delante y detrás de boolean;
          begin
            
             result := valido;
           
          end;

      end;

    JsonData.destroy;
    LResponse.Free;
    LRequest.Free;
    StringStream.Destroy;
  except
    JsonData.destroy;
    LResponse.Free;
    LRequest.Free;
    StringStream.Destroy;
  end;


end;
Responder Con Cita