Ajuda com erros de retorno NFSe em produção

0 respostas
I

Colegas!

Preciso muito da ajuda, eu terminei todas rotinas pra homologação, estava funcionando tudo 100%.

Quando coloquei em produção está dando erro de assinatura... estranho que homologação funcionava e produção agora não funfa.

Meu maior problema é que não posso setar os Id das tag LoteRps e InfRps.

Retorno que estou tendo:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<EnviarLoteRpsResposta xmlns="http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd">
    <ListaMensagemRetorno>
        <MensagemRetorno>
            <Codigo>E515</Codigo>
            <Mensagem>Erro ao validar assinatura. - Remessa adulterada após a assinatura.</Mensagem>
        </MensagemRetorno>
    </ListaMensagemRetorno>
</EnviarLoteRpsResposta>

Segue código atual que estou trabalhando pra tentar solucionar o problema:

/**
     * Assinatura do XML de Envio de Lote da NFS-e utilizando Certificado
     * Digital A1.
     */
    protected String assinaEnviNFSe(String xml, String certificado, String senha) throws Exception {
        Document document = documentFactory(xml);
        XMLSignatureFactory signatureFactory = XMLSignatureFactory.getInstance("DOM");
        ArrayList<Transform> transformList = signatureFactory(signatureFactory);
        loadCertificates(certificado, senha, signatureFactory);

        
        
        for (int i = 0; i < document.getDocumentElement().getElementsByTagName(Rps).getLength(); i++) {
            assinarNFSeInfRps(signatureFactory, transformList, privateKey, keyInfo, document, i);
        }
        
        //Assina o lote RPS
        assinarNFSeLoteRps(signatureFactory, transformList, privateKey, keyInfo, document, 0);

        return outputXML(document);
    }

    private void assinarNFSeInfRps(XMLSignatureFactory fac, ArrayList<Transform> transformList, PrivateKey privateKey, KeyInfo ki,
            Document document, int indexNFse) throws Exception {

        NodeList elements = document.getElementsByTagName(InfRps);
        org.w3c.dom.Element el = (org.w3c.dom.Element) elements.item(indexNFse);

        String idAttName = el.hasAttribute("Id") ? "Id" : "id";
        String id = el.getAttribute(idAttName);
        if (!id.equals("")) {
            try {
                el.setIdAttribute(idAttName, true);
            } catch (Exception localException) {
                localException.printStackTrace();
            }
        } else {
           // id = String.valueOf(indexNFse + 1);
            //el.setAttribute("Id", id);
        }

        Reference ref = fac.newReference("" +id, fac.newDigestMethod(DigestMethod.SHA1, null), transformList, null, null);

        SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE,
                (C14NMethodParameterSpec) null),
                fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null),
                Collections.singletonList(ref));

        XMLSignature signature = fac.newXMLSignature(si, ki);

        DOMSignContext dsc = new DOMSignContext(privateKey, document.getDocumentElement().getElementsByTagName(Rps).item(indexNFse));
        signature.sign(dsc);
    }

    private void assinarNFSeLoteRps(XMLSignatureFactory fac, ArrayList<Transform> transformList, PrivateKey privateKey, KeyInfo ki,
            Document document, int indexNFse) throws Exception {

        NodeList elements = document.getElementsByTagName(LoteRps);
        org.w3c.dom.Element el = (org.w3c.dom.Element) elements.item(indexNFse);

        String idAttName = el.hasAttribute("Id") ? "Id" : "id";
        String id = el.getAttribute(idAttName);
        if (!id.equals("")) {
            try {
                el.setIdAttribute(idAttName, true);
            } catch (Exception localException) {
                localException.printStackTrace();
            }
        } else {
           // id = String.valueOf(indexNFse + 1);
            //el.setAttribute("Id", id);
        }

        Reference ref = fac.newReference(""+id, fac.newDigestMethod(DigestMethod.SHA1, null), transformList, null, null);

        SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE,
                (C14NMethodParameterSpec) null),
                fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null),
                Collections.singletonList(ref));

        XMLSignature signature = fac.newXMLSignature(si, ki);

        DOMSignContext dsc = new DOMSignContext(privateKey, document.getFirstChild());
        signature.sign(dsc);
    }
Criado 2 de julho de 2014
Respostas 0
Participantes 1