예제 #1
0
        public Retorno.Recepcao Enviar(long numeroLote)
        {
            if (_producao)
            {
                var nfeRecepcao2 = new nfeRecepcaoEvento.RecepcaoEvento();
                var nfeCabecalho = new nfeRecepcaoEvento.nfeCabecMsg
                {
                    cUF         = _uf,
                    versaoDados = NFeContexto.Versao.VersaoString
                };

                //Informa dados no WS de cabecalho

                nfeRecepcao2.nfeCabecMsgValue = nfeCabecalho;
                nfeRecepcao2.ClientCertificates.Add(NFeContexto.Certificado);

                //Envia para o webservice e recebe a resposta
                var xmlResposta = nfeRecepcao2.nfeRecepcaoEvento(MontarXml(numeroLote).DocumentElement);

                var recibo = xmlResposta["infRec"]["nRec"].InnerText;
                var motivo = xmlResposta["xMotivo"].InnerText;

                return(new Retorno.Recepcao(recibo, "", motivo));
            }
            else
            {
                var nfeRecepcao2 = new NfeRecepcaoEvento1.RecepcaoEvento();
                var nfeCabecalho = new NfeRecepcaoEvento1.nfeCabecMsg
                {
                    cUF         = _uf,
                    versaoDados = NFeContexto.Versao.VersaoString
                };

                //Informa dados no WS de cabecalho

                nfeRecepcao2.nfeCabecMsgValue = nfeCabecalho;
                nfeRecepcao2.ClientCertificates.Add(NFeContexto.Certificado);

                //Envia para o webservice e recebe a resposta
                var xmlResposta = nfeRecepcao2.nfeRecepcaoEvento(MontarXml(numeroLote).DocumentElement);

                var recibo = xmlResposta["infRec"]["nRec"].InnerText;
                var motivo = xmlResposta["xMotivo"].InnerText;

                return(new Retorno.Recepcao(recibo, "", motivo));
            }
        }
예제 #2
0
        public Retorno.RetRecepcao Enviar(string numeroRecibo)
        {
            //Monta corpo do xml de envio
            var xmlString = new StringBuilder();

            xmlString.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            xmlString.Append("<consReciNFe xmlns=\"http://www.portalfiscal.inf.br/nfe\" versao=\"" +
                             NFeContexto.Versao.VersaoString + "\">");
            xmlString.Append("    <tpAmb>" + (NFeContexto.Producao ? "1" : "2") + "</tpAmb>");
            xmlString.Append("    <nRec>" + numeroRecibo + "</nRec>");
            xmlString.Append("</consReciNFe>");

            XmlNode consultaXml = Xml.StringToXml(xmlString.ToString());

            if (_producao)
            {
                var nfeRetRecepcao2 = new nfeRecepcaoEvento.RecepcaoEvento();
                var nfeCabecalho    = new nfeCabecMsg
                {
                    cUF         = _uf,
                    versaoDados = NFeContexto.Versao.VersaoString
                };

                //Informa dados no WS de cabecalho

                nfeRetRecepcao2.nfeCabecMsgValue = nfeCabecalho;
                nfeRetRecepcao2.ClientCertificates.Add(NFeContexto.Certificado);


                Retorno.RetRecepcao retorno;
                XmlNode             respostaXml;

                var isEmProcessamento = true;

                //Verifica a resposta de envio da sefaz e aguarda até quando estiver processado
                do
                {
                    respostaXml = nfeRetRecepcao2.nfeRecepcaoEvento(consultaXml);

                    //Esse e o resultado só do lote (cabeçalho)
                    var status = respostaXml["cStat"].InnerText;
                    var motivo = respostaXml["xMotivo"].InnerText;
                    retorno = new Retorno.RetRecepcao("", "", status, motivo);

                    if (retorno.Status != "105")
                    {
                        isEmProcessamento = false;
                    }
                    else
                    {
                        Thread.Sleep(5000);
                    }
                } while (isEmProcessamento);


                if (retorno.Status != "225")
                {
                    //Isso aqui é o resultado de CADA NFe, mas como por enquanto pra cada lote só manda 1 nota, entao segue assim por enquanto #todo
                    if ((retorno.Status != "100") && (retorno.Status != "104"))
                    {
                        throw new Exception("Lote não processado: " + retorno.Status + " - " + retorno.Motivo);
                    }
                    var protocolo = "";
                    var status    = "";
                    var motivo    = "";

                    try
                    {
                        status    = respostaXml["protNFe"]["infProt"]["cStat"].InnerText;
                        protocolo = respostaXml["protNFe"]["infProt"]["nProt"].InnerText;
                        motivo    = respostaXml["protNFe"]["infProt"]["xMotivo"].InnerText;
                    }
                    catch
                    {
                        //ignora o erro
                    }

                    //Caso deu algum problema e nao veio o protocolo, mas veio a descrição do problema
                    if (string.IsNullOrEmpty(protocolo) && !string.IsNullOrEmpty(status) &&
                        !string.IsNullOrEmpty(motivo))
                    {
                        throw new Exception("Erro de retorno: " + status + " - " + motivo);
                    }

                    try
                    {
                        var numeroNota = respostaXml["protNFe"]["infProt"]["chNFe"].InnerText;
                        protocolo = respostaXml["protNFe"]["infProt"]["nProt"].InnerText;
                        status    = respostaXml["protNFe"]["infProt"]["cStat"].InnerText;
                        motivo    = respostaXml["protNFe"]["infProt"]["xMotivo"].InnerText;

                        return(new Retorno.RetRecepcao(numeroNota, protocolo, status, motivo));
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Erro ler resposta de envio: " + e.Message);
                    }
                }
                throw new Exception("Erro ao enviar lote XML: " + retorno.Motivo);
            }
            else
            {
                var nfeRetRecepcao2 = new NfeRecepcaoEvento1.RecepcaoEvento();
                var nfeCabecalho    = new NfeRecepcaoEvento1.nfeCabecMsg
                {
                    cUF         = _uf,
                    versaoDados = NFeContexto.Versao.VersaoString
                };

                //Informa dados no WS de cabecalho

                nfeRetRecepcao2.nfeCabecMsgValue = nfeCabecalho;
                nfeRetRecepcao2.ClientCertificates.Add(NFeContexto.Certificado);


                Retorno.RetRecepcao retorno;
                XmlNode             respostaXml = null;

                var isEmProcessamento = true;

                //Verifica a resposta de envio da sefaz e aguarda até quando estiver processado
                do
                {
                    respostaXml = nfeRetRecepcao2.nfeRecepcaoEvento(consultaXml);

                    //Esse e o resultado só do lote (cabeçalho)
                    var status = respostaXml["cStat"].InnerText;
                    var motivo = respostaXml["xMotivo"].InnerText;
                    retorno = new Retorno.RetRecepcao("", "", status, motivo);

                    if (retorno.Status != "105")
                    {
                        isEmProcessamento = false;
                    }
                    else
                    {
                        Thread.Sleep(5000);
                    }
                } while (isEmProcessamento);


                if (retorno.Status != "225")
                {
                    //Isso aqui é o resultado de CADA NFe, mas como por enquanto pra cada lote só manda 1 nota, entao segue assim por enquanto #todo
                    if ((retorno.Status != "100") && (retorno.Status != "104"))
                    {
                        throw new Exception("Lote não processado: " + retorno.Status + " - " + retorno.Motivo);
                    }
                    var protocolo = "";
                    var status    = "";
                    var motivo    = "";

                    try
                    {
                        status    = respostaXml["protNFe"]["infProt"]["cStat"].InnerText;
                        protocolo = respostaXml["protNFe"]["infProt"]["nProt"].InnerText;
                        motivo    = respostaXml["protNFe"]["infProt"]["xMotivo"].InnerText;
                    }
                    catch
                    {
                        //ignora o erro
                    }

                    //Caso deu algum problema e nao veio o protocolo, mas veio a descrição do problema
                    if (string.IsNullOrEmpty(protocolo) && !string.IsNullOrEmpty(status) &&
                        !string.IsNullOrEmpty(motivo))
                    {
                        throw new Exception("Erro de retorno: " + status + " - " + motivo);
                    }

                    try
                    {
                        var numeroNota = respostaXml["protNFe"]["infProt"]["chNFe"].InnerText;
                        protocolo = respostaXml["protNFe"]["infProt"]["nProt"].InnerText;
                        status    = respostaXml["protNFe"]["infProt"]["cStat"].InnerText;
                        motivo    = respostaXml["protNFe"]["infProt"]["xMotivo"].InnerText;

                        return(new Retorno.RetRecepcao(numeroNota, protocolo, status, motivo));
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Erro ler resposta de envio: " + e.Message);
                    }
                }
                throw new Exception("Erro ao enviar lote XML: " + retorno.Motivo);
            }
        }
예제 #3
0
        private RetornoSimples EnviarEvento(StringBuilder eventoXml, string id, string arquivoEvento, string schema)
        {
            var documentXml = Assinar(eventoXml, id);
            var xmlDoc      = new XmlDocument();

            xmlDoc.Load(arquivoEvento);
            var conteudoXml = xmlDoc.OuterXml;
            var nota        = new Nota(NFeContexto)
            {
                CaminhoFisico = arquivoEvento
            };
            var bllXml    = new Xml();
            var xmlString = new StringBuilder();

            xmlString.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            xmlString.Append("<envEvento versao=\"1.00\" xmlns=\"http://www.portalfiscal.inf.br/nfe\">");
            xmlString.Append("	<idLote>0131318</idLote>");
            xmlString.Append(conteudoXml);
            xmlString.Append("</envEvento>");

            var sw2 = File.CreateText(arquivoEvento);

            sw2.Write(xmlString.ToString());
            sw2.Close();

            //Verifica se a nota está de acordo com o schema, se não estiver vai disparar um erro
            try
            {
                bllXml.ValidaSchema(arquivoEvento,
                                    Util.ContentFolderSchemaValidacao + "\\" + NFeContexto.Versao.PastaXml + "\\" + schema);
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao validar Nota: " + e.Message);
            }

            if (_producao)
            {
                var recepcao  = new nfeRecepcaoEvento.RecepcaoEvento();
                var cabecalho = new nfeCabecMsg
                {
                    cUF         = _uf,
                    versaoDados = "1.00"
                };

                recepcao.nfeCabecMsgValue = cabecalho;
                recepcao.ClientCertificates.Add(NFeContexto.Certificado);

                var resposta = recepcao.nfeRecepcaoEvento(Xml.StringToXml(xmlString.ToString()));

                var status = resposta["retEvento"]["infEvento"]["xMotivo"].InnerText;
                var motivo = resposta["retEvento"]["infEvento"]["cStat"].InnerText;
                return(new RetornoSimples(status, motivo));
            }
            else
            {
                var recepcao  = new NfeRecepcaoEvento1.RecepcaoEvento();
                var cabecalho = new NfeRecepcaoEvento1.nfeCabecMsg
                {
                    cUF         = _uf,
                    versaoDados = "1.00"
                };

                recepcao.nfeCabecMsgValue = cabecalho;
                recepcao.ClientCertificates.Add(NFeContexto.Certificado);

                var resposta = recepcao.nfeRecepcaoEvento(Xml.StringToXml(xmlString.ToString()));

                var status = resposta["retEvento"]["infEvento"]["xMotivo"].InnerText;
                var motivo = resposta["retEvento"]["infEvento"]["cStat"].InnerText;
                return(new RetornoSimples(status, motivo));
            }
        }