|
Le he pedido a la IA que me pase el código y me saca ésto.. Ni idea de si es correcto.
uses
System.Net.HttpClient, System.Net.HttpClientComponent, System.JSON, System.SysUtils, System.Classes, Vcl.Dialogs;
procedure CheckVAT;
var
CountryCode, VatNumber, Url: string;
HttpClient: THTTPClient;
Response: IHTTPResponse;
JsonValue: TJSONValue;
JSONObject: TJSONObject;
IsValid: Boolean;
Name, Address, ErrorMsg: string;
begin
CountryCode := 'LU'; // Ej. 'ES', 'DE', etc.
VatNumber := '20260743';
Url := Format('https://ec.europa.eu/taxation_customs/vies/rest-api/ms/%s/vat/%s', [CountryCode, VatNumber]);
HttpClient := THTTPClient.Create;
try
Response := HttpClient.Get(Url);
if Response.StatusCode = 200 then
begin
JsonValue := TJSONObject.ParseJSONValue(Response.ContentAsString);
if Assigned(JsonValue) then
begin
JSONObject := JsonValue as TJSONObject;
IsValid := JSONObject.GetValue<Boolean>('isValid');
if IsValid then
begin
Name := JSONObject.GetValue<string>('name');
Address := JSONObject.GetValue<string>('address').Replace('\n', sLineBreak);
ShowMessage('VAT válido.' + sLineBreak +
'Nombre: ' + Name + sLineBreak +
'Dirección: ' + Address);
end
else
begin
ErrorMsg := JSONObject.GetValue<string>('userError');
ShowMessage('VAT no válido. Mensaje: ' + ErrorMsg);
end;
end;
end
else
ShowMessage('Error HTTP: ' + Response.StatusCode.ToString);
except
on E: Exception do
ShowMessage('Excepción: ' + E.Message);
end;
HttpClient.Free;
end;
|