コード例 #1
0
        private static async Task DownloadXml()
        {
            try
            {
                Console.WriteLine("Informe a chave da NFe para download:");
                string chave = Console.ReadLine();

                Console.WriteLine("Deseja manifestar a NFe? (S/N)");
                bool manifestar = string.Equals(Console.ReadLine().Trim().ToLower(), "s");


                using (var _certificado = CertificadoDigital.ObterCertificado(_configuracoes.CfgServico.Certificado))
                    using (var servicoNFe = new ServicosNFe(_configuracoes.CfgServico, _certificado))
                    {
                        if (manifestar)
                        {
                            try
                            {
                                var retornoManifestacao = servicoNFe.RecepcaoEventoManifestacaoDestinatario(idlote: 1,
                                                                                                            sequenciaEvento: 1,
                                                                                                            chavesNFe: new string[] { chave },
                                                                                                            nFeTipoEventoManifestacaoDestinatario: NFeTipoEvento.TeMdCienciaDaOperacao,
                                                                                                            cpfcnpj: _configuracoes.Emitente.CNPJ,
                                                                                                            justificativa: null);

                                Console.WriteLine($"Retorno da manifestação: {retornoManifestacao.RetornoStr}");
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine($"Manifestação: {ex.Message}");
                            }
                        }

                        var retornoNFeDistDFe = servicoNFe.NfeDistDFeInteresse(ufAutor: _configuracoes.EnderecoEmitente.UF.ToString(), documento: _configuracoes.Emitente.CNPJ, chNFE: chave);
                        if (retornoNFeDistDFe.Retorno.loteDistDFeInt == null)
                        {
                            await Task.Delay(2000); //https://github.com/ZeusAutomacao/DFe.NET/issues/568#issuecomment-339862458

                            retornoNFeDistDFe = servicoNFe.NfeDistDFeInteresse(ufAutor: _configuracoes.EnderecoEmitente.UF.ToString(), documento: _configuracoes.Emitente.CNPJ, chNFE: chave);

                            if (retornoNFeDistDFe.Retorno.loteDistDFeInt == null)
                            {
                                throw new Exception(retornoNFeDistDFe.Retorno.xMotivo);
                            }
                        }

                        if ((retornoNFeDistDFe.Retorno.loteDistDFeInt.Count()) > 0)
                        {
                            var    xmlBytes = retornoNFeDistDFe.Retorno.loteDistDFeInt[0].XmlNfe;
                            string xmlStr   = Compressao.Unzip(xmlBytes);

                            Console.WriteLine($"Xml: {xmlStr}");
                        }
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #2
0
        private static async Task CarregarNSUs()
        {
            try
            {
                Console.WriteLine("Informe o ultimo número de NSU que você tem. Se não tiver, informe 0 (zero):");
                string ultimoNsu = Console.ReadLine();

                Console.WriteLine("Informe a UF do autor");
                string uf = Console.ReadLine();

                do
                {
                    RetornoNfeDistDFeInt retornoNFeDistDFe = null;
                    using (var _certificado = CertificadoDigital.ObterCertificado(_configuracoes.CfgServico.Certificado))
                        using (var servicoNFe = new ServicosNFe(_configuracoes.CfgServico, _certificado))
                            retornoNFeDistDFe = servicoNFe.NfeDistDFeInteresse(ufAutor: _configuracoes.Emitente.enderEmit.UF.ToString(),
                                                                               documento: _configuracoes.Emitente.CNPJ,
                                                                               ultNSU: ultimoNsu.ToString());

                    var lote = retornoNFeDistDFe.Retorno.loteDistDFeInt;
                    if (lote == null || !lote.Any())
                    {
                        break;
                    }

                    Console.WriteLine($"{"NSU".PadRight(44, ' ')} | Xml");

                    foreach (var item in lote)
                    {
                        string linha = string.Empty;

                        string xmlStr = string.Empty;

                        if (item.XmlNfe != null)
                        {
                            xmlStr = Compressao.Unzip(item.XmlNfe);

                            Console.WriteLine($"{item.NSU.ToString().PadRight(44, ' ')} | {xmlStr}");
                        }
                    }

                    await Task.Delay(2000); //https://github.com/ZeusAutomacao/DFe.NET/issues/568#issuecomment-339862458
                } while (true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #3
0
        public Document SearchForDocumentDistribution(Company company, string accessKey = "")
        {
            var document = new Document();

            // Normalize CNPJ number
            var companyCnpj = company.Cnpj.Replace(".", "").Replace("/", "").Replace("-", "").Trim();

            #region Essential Validation

            //if (!Validation.ValidaCpfCnpj(companyCnpj))
            //    throw new ArgumentException("CNPJ inválido!", companyCnpj);

            if (string.IsNullOrEmpty(company.Estate.Trim()))
            {
                throw new ArgumentException("UF não informada!", company.Estate);
            }

            if (string.IsNullOrEmpty(company.CertificateSerialNumber.Trim()))
            {
                throw new ArgumentException("Nenhum certificado foi informado no cadastro da empresa!",
                                            company.CertificateSerialNumber);
            }

            if (string.IsNullOrEmpty(company.CertificatePassword.Trim()))
            {
                throw new ArgumentException("A senha do certificado não foi informada no cadastro da empresa!",
                                            company.CertificatePassword);
            }

            #endregion Essential Validation

            var certConfiguration = GetCertificateForConfig(company.CertificateSerialNumber, company.CertificatePassword);

            ConfiguraServico(GetEstateForConfig(company.Estate), ModeloDocumento.NFe,
                             NFe.Classes.Informacoes.Identificacao.Tipos.TipoAmbiente.taProducao, TipoEmissao.teNormal, 10000,
                             SecurityProtocolType.Ssl3, certConfiguration);

            using (var servicoNFe = new ServicosNFe(_configServico))
            {
                // Get response from SEFAZ WS
                var response = servicoNFe.NfeDistDFeInteresse(company.Estate, companyCnpj, chNFE: accessKey);

                document = TurnIntoDFeDocument(response.Retorno.loteDistDFeInt);
            }

            return(document);
        }