Estou desenvolvendo uma interface para enviar lotes de Recibo Provisório de Serviço (RPS).
Criei o XML na estrutura definida na documentação e já assine ele com o certificado A3.
Criei um “Cliente para WebService” utilizando o Netbeans. A partir disso, tenho as interfaces disponíves (Stubs).
Estou enviando o XML e pelo que entendi do erro abaxo - que recebo ao enviar uma requisoção - eles esperam receber um envelope SOAP. Eu nunca trabalhei com SOAP antes.
com.sun.xml.internal.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: Endpoint {http://homologacao.ginfes.com.br}ServiceGinfesImplPort does not contain operation meta data for: {https://homologacao.ginfes.com.br}ConsultarLoteRpsV3 Please see the server log to find more detail regarding exact cause of the failure.
at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178)
at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:116)
at com.sun.xml.internal.ws.client.sei.StubHandler.readResponse(StubHandler.java:238)
at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:189)
at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:276)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:104)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:77)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:147)
at com.sun.proxy.$Proxy29.consultarLoteRpsV3(Unknown Source)
at nfse.Nfse.main(Nfse.java:373)
Abaixo está o meu código completo e comentado.
Geração do XML; definição de parâmetros de conexão e a comunicação com o WebService.
Gostaria de saber, se alguém por aqui consegue me ajudar.
Desde já agradeço. Obrigado !
try {
/**
* Criação do arquivo XML RPS
*/
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
// cria o nó raís
Document doc = docBuilder.newDocument();
Element enviarLoteRpsEnvio = doc.createElement("EnviarLoteRpsEnvio");
doc.appendChild(enviarLoteRpsEnvio);
// define o atributo xmlns
Attr xmlns = doc.createAttribute("xmlns");
xmlns.setValue("http://www.ginfes.com.br/servico_enviar_lote_rps_envio_v03.xsd");
enviarLoteRpsEnvio.setAttributeNode(xmlns);
// define o atributo xmlns:tipos
Attr xmlnsTipos = doc.createAttribute("xmlns:tipos");
xmlnsTipos.setValue("http://www.ginfes.com.br/tipos_v03.xsd");
enviarLoteRpsEnvio.setAttributeNode(xmlnsTipos);
// define o atributo xmlns:xsi
Attr xmlnsXsi = doc.createAttribute("xmlns:xsi");
// xmlnsXsi.setValue("http://www.w3.org/2000/09/xmldsig#");
xmlnsXsi.setValue("http://www.w3.org/2001/XMLSchema-instance");
enviarLoteRpsEnvio.setAttributeNode(xmlnsXsi);
// cria e adiciona o nó base da NFe no nó raís do XML
Element loteRps = doc.createElement("LoteRps");
enviarLoteRpsEnvio.appendChild(loteRps);
// define o atributo id para "LoteRps"
Attr attr = doc.createAttribute("Id");
attr.setValue("400000013");
loteRps.setAttributeNode(attr);
// Elemento NumeroLote
Element numeroLote = doc.createElement("tipos:NumeroLote");
numeroLote.appendChild(doc.createTextNode("13"));
loteRps.appendChild(numeroLote);
// Elemento Cnpj
Element lrcnpj = doc.createElement("tipos:Cnpj");
lrcnpj.appendChild(doc.createTextNode("07331220000147"));
loteRps.appendChild(lrcnpj);
// Elemento InscricaoMunicipal
Element inscricaoMunicipal = doc.createElement("tipos:InscricaoMunicipal");
inscricaoMunicipal.appendChild(doc.createTextNode("1981905"));
loteRps.appendChild(inscricaoMunicipal);
// Elemento QuantidadeRps
Element quantidadeRps = doc.createElement("tipos:QuantidadeRps");
quantidadeRps.appendChild(doc.createTextNode("1"));
loteRps.appendChild(quantidadeRps);
// Elemento ListaRps
Element listaRps = doc.createElement("tipos:ListaRps");
loteRps.appendChild(listaRps);
// Elemento Rps
Element rps = doc.createElement("tipos:Rps");
listaRps.appendChild(rps);
// Elemento InfRps
Element infRps = doc.createElement("tipos:InfRps");
infRps.appendChild(doc.createTextNode("1"));
rps.appendChild(infRps);
// Elemento IdentificacaoRps
Element identificacaoRps = doc.createElement("tipos:IdentificacaoRps");
infRps.appendChild(identificacaoRps);
// Elemento Numero
Element numero = doc.createElement("tipos:Numero");
numero.appendChild(doc.createTextNode("1"));
identificacaoRps.appendChild(numero);
// Elemento Serie
Element serie = doc.createElement("tipos:Serie");
serie.appendChild(doc.createTextNode("MAQ01"));
identificacaoRps.appendChild(serie);
// Elemento Tipo
Element tipo = doc.createElement("tipos:Tipo");
tipo.appendChild(doc.createTextNode("1"));
identificacaoRps.appendChild(tipo);
// Elemento DataEmissao
Element dataEmissao = doc.createElement("tipos:DataEmissao");
dataEmissao.appendChild(doc.createTextNode("2017-08-10T14:37:00"));
infRps.appendChild(dataEmissao);
// Elemento NaturezaOperacao
Element naturezaOperacao = doc.createElement("tipos:NaturezaOperacao");
naturezaOperacao.appendChild(doc.createTextNode("1"));
infRps.appendChild(naturezaOperacao);
// Elemento OptanteSimplesNacional
Element optanteSimplesNacional = doc.createElement("tipos:OptanteSimplesNacional");
optanteSimplesNacional.appendChild(doc.createTextNode("2"));
infRps.appendChild(optanteSimplesNacional);
// Elemento IncentivadorCultural
Element incentivadorCultural = doc.createElement("tipos:IncentivadorCultural");
incentivadorCultural.appendChild(doc.createTextNode("2"));
infRps.appendChild(optanteSimplesNacional);
// Elemento Status
Element status = doc.createElement("tipos:Status");
status.appendChild(doc.createTextNode("1"));
infRps.appendChild(status);
// Elemento Servico
Element servico = doc.createElement("tipos:Servico");
infRps.appendChild(servico);
// Elemento Valores
Element valores = doc.createElement("tipos:Valores");
servico.appendChild(valores);
// Elemento ValorServicos
Element valorServicos = doc.createElement("tipos:ValorServicos");
valorServicos.appendChild(doc.createTextNode("10.00"));
valores.appendChild(valorServicos);
// Elemento IssRetido
Element issRetido = doc.createElement("tipos:IssRetido");
issRetido.appendChild(doc.createTextNode("2"));
valores.appendChild(issRetido);
// Elemento BaseCalculo
Element baseCalculo = doc.createElement("tipos:BaseCalculo");
baseCalculo.appendChild(doc.createTextNode("10.00"));
valores.appendChild(baseCalculo);
// Elemento ValorLiquidoNfse
Element valorLiquidoNfse = doc.createElement("tipos:ValorLiquidoNfse");
valorLiquidoNfse.appendChild(doc.createTextNode("10.00"));
valores.appendChild(valorLiquidoNfse);
// Elemento ItemListaServico
Element itemListaServico = doc.createElement("tipos:ItemListaServico");
itemListaServico.appendChild(doc.createTextNode("1.0"));
servico.appendChild(itemListaServico);
// Elemento CodigoTributacaoMunicipio
Element codigoTributacaoMunicipio = doc.createElement("tipos:CodigoTributacaoMunicipio");
codigoTributacaoMunicipio.appendChild(doc.createTextNode("1.0"));
servico.appendChild(codigoTributacaoMunicipio);
// Elemento Discriminacao
Element discriminacao = doc.createElement("tipos:Discriminacao");
discriminacao.appendChild(doc.createTextNode("Serviços prestados nas dependências da Dgnet - referente ao plano contratado - gBackup Pro 100GB.\n" +
"INFORMAÇÕES COMPLEMENTARES:\n" +
"Segundo a LC116/2003, art 3º, incisos I a XXII, não haverá retenção legal de ISS pelo fato dos serviços subitem 01.07, não constarem nas exceções previstas\n" +
"nos mesmos."));
servico.appendChild(discriminacao);
// Elemento CodigoMunicipio
Element codigoMunicipio = doc.createElement("tipos:CodigoMunicipio");
codigoMunicipio.appendChild(doc.createTextNode("0000"));
servico.appendChild(codigoMunicipio);
// Elemento Prestador
Element prestador = doc.createElement("tipos:Prestador");
infRps.appendChild(prestador);
// Elemento Cnpj
Element pcnpj = doc.createElement("tipos:Cnpj");
pcnpj.appendChild(doc.createTextNode("7331220000147"));
prestador.appendChild(pcnpj);
// Element Tomador
Element tomador = doc.createElement("tipos:Tomador");
infRps.appendChild(tomador);
// Elemento IdentificacaoTomador
Element identificacaoTomador = doc.createElement("tipos:IdentificacaoTomador");
tomador.appendChild(identificacaoTomador);
// Elemento CpfCnpj
Element cpfCnpj = doc.createElement("tipos:CpfCnpj");
identificacaoTomador.appendChild(cpfCnpj);
// Elemento Cnpj
Element tcnpj = doc.createElement("tipos:Cnpj");
tcnpj.appendChild(doc.createTextNode("17609594000801"));
cpfCnpj.appendChild(tcnpj);
// Elemento RazaoSocial
Element razaoSocial = doc.createElement("tipos:RazaoSocial");
razaoSocial.appendChild(doc.createTextNode("ALJA HOTELARIA E SERVIçOS LTDA - EPP"));
tomador.appendChild(razaoSocial);
// Elemento Endereco
Element tendereco = doc.createElement("tipos:Endereco");
tomador.appendChild(tendereco);
// Elemento Endereco
Element tlendereco = doc.createElement("tipos:Endereco");
tlendereco.appendChild(doc.createTextNode("Avenida 1, 926"));
tendereco.appendChild(tlendereco);
// Elemento Bairro
Element tbairro = doc.createElement("tipos:Bairro");
tbairro.appendChild(doc.createTextNode("CENTRO"));
tendereco.appendChild(tbairro);
// Elemento CodigoMunicipio
Element tcodigoMunicipio = doc.createElement("tipos:CodigoMunicipio");
tcodigoMunicipio.appendChild(doc.createTextNode("000000000"));
tendereco.appendChild(tcodigoMunicipio);
// Elemento Uf
Element tuf = doc.createElement("tipos:Uf");
tuf.appendChild(doc.createTextNode("SP"));
tendereco.appendChild(tuf);
// Elemento Cep
Element tcep = doc.createElement("tipos:Cep");
tcep.appendChild(doc.createTextNode("37800000"));
tendereco.appendChild(tcep);
// Elemento Contato
Element tcontato = doc.createElement("tipos:Contato");
tomador.appendChild(tcontato);
// Elemento Telefone
Element ttelefone = doc.createElement("tipos:Telefone");
ttelefone.appendChild(doc.createTextNode("0000000000"));
tcontato.appendChild(ttelefone);
// Elemento Email
Element temail = doc.createElement("tipos:Email");
temail.appendChild(doc.createTextNode("algum.teste@teste.com"));
tcontato.appendChild(temail);
// escreve o conteúdo dentro do arquivo XML
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("file.xml"));
transformer.transform(source, result);
System.out.println("File Saved!");
String senhaDoCertificadoDoCliente = "1234";
/**
* Assinando o XML
* fileEnviNFe = Caminho do Arquivo XML (EnviNFe) gerado;
*/
String fileEnviNFe = "file.xml";
Nfse nfse = new Nfse();
String xmlEnviNFe = nfse.lerXML(fileEnviNFe);
String xmlEnviNFeAssinado = nfse.assinaEnviNFe(xmlEnviNFe, senhaDoCertificadoDoCliente);
PrintWriter out = new PrintWriter("C:/Users/willian/Documents/NetBeansProjects/Nfse/file3.xml");
out.println(xmlEnviNFeAssinado);
out.close();
info("XML EnviNFe Assinado: " + xmlEnviNFeAssinado);
/**
* Parâmetros de conexão
*/
Provider provider = new sun.security.pkcs11.SunPKCS11(tokenAladdin());
Security.addProvider(provider);
System.clearProperty("javax.net.ssl.keyStore");
System.clearProperty("javax.net.ssl.keyStorePassword");
System.clearProperty("javax.net.ssl.trustStore");
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
System.setProperty("javax.net.ssl.keyStoreType", "PKCS11");
System.setProperty("javax.net.ssl.keyStore","NONE");
System.setProperty("javax.net.ssl.keyStorePassword", senhaDoCertificadoDoCliente);
/**
* Comunicação com o Servidor
*/
System.err.println("a1");
String header = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" +
"<cabecalho xmlns:xsi=\"\" xmlns:xsd=\"\" versao=\"3\" xmlns=\"\">\n" +
"<versaoDados xmlns=\"\">3</versaoDados>\n" +
"</cabecalho>";
System.err.println("a2");
ServiceGinfesImpl sgi = new ServiceGinfesImplService().getServiceGinfesImplPort();
System.err.println("a3");
sgi.consultarLoteRpsV3(header, xmlEnviNFeAssinado);
System.err.println("a4");
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
} catch (TransformerConfigurationException ex) {
ex.printStackTrace();
} catch (TransformerException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}