Ver Mensaje Individual
  #3656  
Antiguo 23-01-2025
Avatar de YellowStone
YellowStone YellowStone is offline
Miembro
 
Registrado: feb 2007
Ubicación: Adeje
Posts: 170
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 Windows, Dialogs, SysUtils, Messages, Classes, xmldom, XMLDoc, XMLIntf, DateUtils, StrUtils, IdHTTP, IdSSLOpenSSL;

type

  
{ Forward Decls }

  
IXMLContribuyenteType = interface;

{ 
IXMLContribuyenteType }

  
IXMLContribuyenteType = interface(IXMLNode)
    [
'{501E2A2E-11EB-42A7-A9D1-D1D7FA481B5B}']
    { 
Property Accessors }
    function 
Get_Nif: WideString;
    function 
Get_Nombre: WideString;
    function 
Get_Resultado: WideString;
    
procedure Set_Nif(Value: WideString);
    
procedure Set_Nombre(Value: WideString);
    
procedure Set_Resultado(Value: WideString);
    { 
Methods & Properties }
    
property Nif: WideString read Get_Nif write Set_Nif;
    
property Nombre: WideString read Get_Nombre write Set_Nombre;
    
property Resultado: WideString read Get_Resultado write Set_Resultado;
  
end;

{ 
Forward Decls }

  
TXMLContribuyenteType = class;

{ 
TXMLContribuyenteType }

  
TXMLContribuyenteType = class(TXMLNode, IXMLContribuyenteType)
  protected
    { 
IXMLContribuyenteType }
    function 
Get_Nif: WideString;
    function 
Get_Nombre: WideString;
    function 
Get_Resultado: WideString;
    
procedure Set_Nif(Value: WideString);
    
procedure Set_Nombre(Value: WideString);
    
procedure Set_Resultado(Value: WideString);
  
end;

  
TGetCertificado = class(Tobject)
  
procedure hServidorGetPassword(var Password: string);
  
end;

{ Global 
Functions }

function 
GetContribuyente(Doc: IXMLDocument): IXMLContribuyenteType;
function 
NewContribuyente: IXMLContribuyenteType;
function 
VerificaNIFAEAT(var nif: string; var nombre: string): boolean; // versión indy
function LimpiaRespuesta(respuesta: string): string;

const

  
TargetNamespace = '';

implementation

var rDatosEmisor: TDatosEmisor;
    
bCertificado: boolean;

{ Global 
Functions }

function 
GetContribuyente(Doc: IXMLDocument): IXMLContribuyenteType;
begin
  Result 
:= Doc.GetDocBinding('Contribuyente', TXMLContribuyenteType, TargetNamespace) as IXMLContribuyenteType;
end;

function 
NewContribuyente: IXMLContribuyenteType;
begin
  Result 
:= NewXMLDocument.GetDocBinding('Contribuyente', TXMLContribuyenteType, TargetNamespace) as IXMLContribuyenteType;
end;

{ 
TXMLContribuyenteType }

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

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

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

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

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

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

//Versión Indy
function VerificaNIFAEAT(var nif: string; var nombre: string): boolean;
var 
Respuesta: TStringList;
    
ixmlDoc: IXMLDocument;
    
hServidor: TIdHTTP;
    
LHandler: TIdSSLIOHandlerSocketOpenSSL;
    
stRequest: TStringStream;
    
sXML, sURL: string;
    
salida: IXMLContribuyenteType;
    
rGetCertificado: TGetCertificado;
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(respuesta: string): string;
var 
iIni, iLon: integer;
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(result, iIni, iLon);
  
result := StringReplace(result, 'VNifV2Sal:', '', [rfReplaceAll]);
end;

procedure TGetCertificado.hServidorGetPassword(var Password: string);
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