Ver Mensaje Individual
  #3  
Antiguo 23-01-2025
Avatar de YellowStone
YellowStone YellowStone is offline
Miembro
 
Registrado: feb 2007
Ubicación: Adeje
Posts: 165
Reputación: 20
YellowStone Va por buen camino
Este código de DELPHI 2007 Funciona en 2008 Y 2012 en la consulta de NIFS, sin IISCrypto ni tocar el registro. (Y no necesita CAPICOM)

Código PHP:
unit VNifV2;

interface

uses WindowsDialogsSysUtilsMessagesClassesxmldomXMLDocXMLIntfDateUtilsStrUtilsIdHTTPIdSSLOpenSSL;

type

  
Forward Decls }

  
IXMLContribuyenteType = interface;

IXMLContribuyenteType }

  
IXMLContribuyenteType = interface(IXMLNode)
    [
'{501E2A2E-11EB-42A7-A9D1-D1D7FA481B5B}']
    { 
Property Accessors }
    function 
Get_NifWideString;
    function 
Get_NombreWideString;
    function 
Get_ResultadoWideString;
    
procedure Set_Nif(ValueWideString);
    
procedure Set_Nombre(ValueWideString);
    
procedure Set_Resultado(ValueWideString);
    { 
Methods Properties }
    
property NifWideString read Get_Nif write Set_Nif;
    
property NombreWideString read Get_Nombre write Set_Nombre;
    
property ResultadoWideString read Get_Resultado write Set_Resultado;
  
end;

Forward Decls }

  
TXMLContribuyenteType = class;

TXMLContribuyenteType }

  
TXMLContribuyenteType = class(TXMLNodeIXMLContribuyenteType)
  protected
    { 
IXMLContribuyenteType }
    function 
Get_NifWideString;
    function 
Get_NombreWideString;
    function 
Get_ResultadoWideString;
    
procedure Set_Nif(ValueWideString);
    
procedure Set_Nombre(ValueWideString);
    
procedure Set_Resultado(ValueWideString);
  
end;

  
TGetCertificado = class(Tobject)
  
procedure hServidorGetPassword(var Passwordstring);
  
end;

{ Global 
Functions }

function 
GetContribuyente(DocIXMLDocument): IXMLContribuyenteType;
function 
NewContribuyenteIXMLContribuyenteType;
function 
VerificaNIFAEAT(var nifstring; var nombrestring): boolean// versión indy
function LimpiaRespuesta(respuestastring): string;

const

  
TargetNamespace '';

implementation

var rDatosEmisorTDatosEmisor;
    
bCertificadoboolean;

{ Global 
Functions }

function 
GetContribuyente(DocIXMLDocument): IXMLContribuyenteType;
begin
  Result 
:= Doc.GetDocBinding('Contribuyente'TXMLContribuyenteTypeTargetNamespace) as IXMLContribuyenteType;
end;

function 
NewContribuyenteIXMLContribuyenteType;
begin
  Result 
:= NewXMLDocument.GetDocBinding('Contribuyente'TXMLContribuyenteTypeTargetNamespace) as IXMLContribuyenteType;
end;

TXMLContribuyenteType }

function 
TXMLContribuyenteType.Get_NifWideString;
begin
  Result 
:= ChildNodes['Nif'].Text;
end;

procedure TXMLContribuyenteType.Set_Nif(ValueWideString);
begin
  ChildNodes
['Nif'].NodeValue := Value;
end;

function 
TXMLContribuyenteType.Get_NombreWideString;
begin
  Result 
:= ChildNodes['Nombre'].Text;
end;

procedure TXMLContribuyenteType.Set_Nombre(ValueWideString);
begin
  ChildNodes
['Nombre'].NodeValue := Value;
end;

function 
TXMLContribuyenteType.Get_ResultadoWideString;
begin
  Result 
:= ChildNodes['Resultado'].Text;
end;

procedure TXMLContribuyenteType.Set_Resultado(ValueWideString);
begin
  ChildNodes
['Resultado'].NodeValue := Value;
end;

//Versión Indy
function VerificaNIFAEAT(var nifstring; var nombrestring): boolean;
var 
RespuestaTStringList;
    
ixmlDocIXMLDocument;
    
hServidorTIdHTTP;
    
LHandlerTIdSSLIOHandlerSocketOpenSSL;
    
stRequestTStringStream;
    
sXMLsURLstring;
    
salidaIXMLContribuyenteType;
    
rGetCertificadoTGetCertificado;
begin
  
// Creamos la petición para comprobar
  
salida := NewContribuyente;
  
result := false;
  
Respuesta := TStringList.Create();
  
hServidor := TIdHTTP.Create(nil);
  
rGetCertificado := TGetCertificado.Create;
  try
    try
      
sURL := 'https://www1.agenciatributaria.gob.es/wlpl/BURT-JDIT/ws/VNifV2SOAP';
      
sXML := '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" '+
              
'xmlns:vnif="http://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/burt/jdit/ws/VNifV2Ent.xsd">';
      
sXML := sXML '<soapenv:Body><vnif:VNifV2Ent><vnif:Contribuyente><vnif:Nif>'+Trim(nif)+'</vnif:Nif>';
      
sXML := sXML '<vnif:Nombre>'+Trim(nombre)+'</vnif:Nombre></vnif:Contribuyente></vnif:VNifV2Ent></soapenv:Body></soapenv:Envelope>';

      
stRequest := TStringStream.Create(Utf8Encode(sXML));

      
hServidor.Request.CustomHeaders.AddValue('Accept''*/*');
      
hServidor.Request.CustomHeaders.AddValue('Accept-Encoding''gzip, deflate');
      
hServidor.Request.CustomHeaders.AddValue('Content-Type''text/xml; charset=utf-8');
      
hServidor.Request.CustomHeaders.AddValue('Connection''keep-alive');
      
hServidor.Request.CustomHeaders.AddValue('User-Agent''gVisualRec (Language=Delphi/2007)');
      
hServidor.Request.CustomHeaders.AddValue('SOAPAction''VNifV2Service');

      
LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
      
LHandler.sslOptions.Method := sslvTLSv1_2;
      
LHandler.sslOptions.SSLVersions := [sslvTLSv1_2];
      
LHandler.SSLOptions.CertFile := 'CERTIFICADO.PFX'//<<<--- Nombre del certificado PFX / P12
      
LHandler.SSLOptions.KeyFile := 'CERTIFICADO.PFX';  //<<<--- Nombre del certificado PFX / P12  // Certificado de dispositivo a usar (el mismo que certfile)
      
LHandler.OnGetPassword := rGetCertificado.hServidorGetPassword;    // Cuando se solicite la clave, el evento se encarga de aportarla
      
hServidor.IOHandler := LHandler;

      
Respuesta.Append(hServidor.Post(Trim(sURL), stRequest));

      if 
hServidor.ResponseCode 200 then
        begin
              Respuesta
.Text := LimpiaRespuesta(Respuesta.Text);
              
ixmlDoc := TXMLDocument.Create(nil);
              
ixmlDoc.XML.Add(Respuesta.Text);
              
ixmlDoc.Active := True;
              
ixmlDoc.Encoding := 'UTF-8';
              try
                
salida := GetContribuyente(ixmlDoc);
                
result := ((salida.Resultado 'IDENTIFICADO') and (salida.Nif Nif));
                if 
result then
                  begin
                    Nif 
:= salida.Nif;
                    
Nombre := salida.Nombre;
                  
end
                
else
                  
begin
                    Nif 
:= '';
                    
Nombre := '';
                  
end;
              
except
                on E
:Exception do
                  
begin
                    ShowMessage
(E.Message);
                    
Nif := '';
                    
Nombre := '';
                  
end;
              
end;
        
end
      
else
        
begin
          Nif 
:= '';
          
Nombre := '';
        
end;
    
except
      on e
exception do
        
begin
          ShowMessage
(e.message);
          
Nif := '';
          
Nombre := '';
        
end
    end
;
  
finally
    Respuesta
.Free;
    
stRequest.Free;
    
hServidor.Free;
  
end;
end;

function 
LimpiaRespuesta(respuestastring): string;
var 
iIniiLoninteger;
begin
  result 
:= StringReplace(respuesta'
'
'', [rfReplaceAll]);
  
result := StringReplace(result'&lt;''<', [rfReplaceAll]);
  
result := StringReplace(result'&gt;''>', [rfReplaceAll]);
  
iIni := Pos('<VNifV2Sal:Contribuyente'result);
  
iLon := Pos('</VNifV2Sal:Contribuyente>'result) +26 iIni;
  
result := Copy(resultiIniiLon);
  
result := StringReplace(result'VNifV2Sal:''', [rfReplaceAll]);
end;

procedure TGetCertificado.hServidorGetPassword(var Passwordstring);
begin
  Password 
:= Clave// Password del certificado pfx
end;

end

Última edición por YellowStone fecha: 23-01-2025 a las 17:54:20.
Responder Con Cita