protected void btnConfirmar_Click(object sender, EventArgs e) { try { var idNf = Conversoes.StrParaUint(Request["idNf"]); var idLoja = Glass.Data.DAL.NotaFiscalDAO.Instance.ObtemIdLoja(idNf); System.Security.Cryptography.X509Certificates.X509Certificate2 cert = Certificado.GetCertificado(idLoja); if (DateTime.Now > cert.NotAfter) { throw new Exception("O certificado digital cadastrado está vencido, insira um novo certificado para emitir esta nota. Data Venc.: " + cert.GetExpirationDateString()); } string msg = EnviaXML.EnviaInutilizacao(idNf, txtMotivo.Text); MensagemAlerta.ShowMsg(msg, Page); Page.ClientScript.RegisterStartupScript(typeof(string), Guid.NewGuid().ToString(), "window.close();", true); } catch (Exception ex) { if (ex.Message.Contains("URI está vazio.") || ex.Message.Contains("URI is empty.")) { var mensagem = "A inutilização não está disponivel na versão 4.00 da NF-e para o seu Estado, assim que possível será disponibilizada."; Glass.MensagemAlerta.ErrorMsg("Info.:", new Exception(mensagem), Page); return; } Glass.MensagemAlerta.ErrorMsg("Falha ao inutilizar NFe.", ex, Page); return; } }
/// <summary> /// Retorna o certificado usado no WebService. /// </summary> /// <param name="idLoja">O id da loja.</param> /// <param name="certPath">O caminho da pasta do certificado.</param> /// <returns></returns> private static X509Certificate2 GetCertificado(uint idLoja, string certPath) { return(!String.IsNullOrEmpty(certPath) ? Certificado.GetCertificado(idLoja, certPath) : Certificado.GetCertificado(idLoja)); }
// TODO TESTE GerarCartaCorrecao private XmlDocument GeraXmlCartaCorrecao(uint idCarta, bool preVisualizar) { CartaCorrecao obj = GetElement(idCarta); NotaFiscal nf = NotaFiscalDAO.Instance.GetElement(obj.IdNf); XmlDocument doc = new XmlDocument(); XmlNode declarationNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(declarationNode); XmlElement evento = doc.CreateElement("evento"); evento.SetAttribute("versao", "1.00"); evento.SetAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe"); doc.AppendChild(evento); XmlElement infEvento = doc.CreateElement("infEvento"); //Identificador da TAG a ser assinada, a regra de formação //do Id é: //“ID” + tpEvento + chave da NF-e + nSeqEvento infEvento.SetAttribute("Id", obj.IdInfEvento); evento.AppendChild(infEvento); // Código do órgão de recepção do Evento. Utilizar a Tabela // do IBGE, utilizar 90 para identificar o Ambiente Nacional. XmlElement cOrgao = doc.CreateElement("cOrgao"); cOrgao.InnerText = Configuracoes.FiscalConfig.NotaFiscalConfig.ContingenciaNFe == DataSources.TipoContingenciaNFe.NaoUtilizar ? obj.Orgao.ToString() : "90"; infEvento.AppendChild(cOrgao); // Identificação do Amb // 1 - Produção // 2 – Homologação XmlElement tpAmb = doc.CreateElement("tpAmb"); tpAmb.InnerText = obj.TipoAmbiente.ToString(); infEvento.AppendChild(tpAmb); if (!string.IsNullOrEmpty(obj.CPF)) { XmlElement CPF = doc.CreateElement("CPF"); CPF.InnerText = obj.CPF.ToString().PadLeft(11, '0'); infEvento.AppendChild(CPF); } else if (!string.IsNullOrEmpty(obj.CNPJ)) { XmlElement CNPJ = doc.CreateElement("CNPJ"); CNPJ.InnerText = obj.CNPJ.ToString().PadLeft(14, '0'); infEvento.AppendChild(CNPJ); } XmlElement chNFe = doc.CreateElement("chNFe"); chNFe.InnerText = obj.ChaveNFe; infEvento.AppendChild(chNFe); XmlElement dhEvento = doc.CreateElement("dhEvento"); dhEvento.InnerText = DateTime.Now.AddMinutes(-10).ToString("yyyy-MM-ddTHH:mm:ssK"); infEvento.AppendChild(dhEvento); //Código do de evento = 110110 XmlElement tpEvento = doc.CreateElement("tpEvento"); tpEvento.InnerText = obj.TipoEvento.ToString(); infEvento.AppendChild(tpEvento); XmlElement nSeqEvento = doc.CreateElement("nSeqEvento"); nSeqEvento.InnerText = obj.NumeroSequencialEvento.ToString(); infEvento.AppendChild(nSeqEvento); XmlElement verEvento = doc.CreateElement("verEvento"); verEvento.InnerText = obj.VersaoEvento; infEvento.AppendChild(verEvento); XmlElement detEvento = doc.CreateElement("detEvento"); detEvento.SetAttribute("versao", "1.00"); ManipulacaoXml.SetNode(doc, detEvento, "descEvento", obj.DescricaoEvento); ManipulacaoXml.SetNode(doc, detEvento, "xCorrecao", obj.Correcao); ManipulacaoXml.SetNode(doc, detEvento, "xCondUso", obj.CondicaoUso); infEvento.AppendChild(detEvento); #region Assina CCe try { if (!preVisualizar) { MemoryStream stream = new MemoryStream(); doc.Save(stream); using (stream) { // Classe responsável por assinar o xml da NFe AssinaturaDigital AD = new AssinaturaDigital(); System.Security.Cryptography.X509Certificates.X509Certificate2 cert = Certificado.GetCertificado(nf.IdLoja.Value); if (DateTime.Now > cert.NotAfter) { throw new Exception("O certificado digital cadastrado está vencido, insira um novo certificado para emitir esta nota. Data Venc.: " + cert.GetExpirationDateString()); } int resultado = AD.Assinar(ref doc, "infEvento", cert); if (resultado > 0) { throw new Exception(AD.mensagemResultado); } } } } catch (Exception ex) { throw new Exception("Falha ao assinar Carta de Correção." + ex.Message); } #endregion #region Valida XML try { if (!preVisualizar) { ValidaXML.Validar(doc, ValidaXML.TipoArquivoXml.CCe); } } catch (Exception ex) { throw new Exception("XML inconsistente." + ex.Message); } #endregion #region Salva arquivo XML da CCe try { string fileName = Utils.GetCartaCorrecaoXmlPath + doc["evento"]["infEvento"].GetAttribute("Id").Remove(0, 3) + "-cce.xml"; if (File.Exists(fileName)) { File.Delete(fileName); } doc.Save(fileName); } catch (Exception ex) { throw new Exception("Falha ao salvar arquivo xml da CCe. " + ex.Message); } #endregion return(doc); }