Ejemplo n.º 1
0
        /// <summary>
        /// Retorna el ACK de estado de los certificados de un sobre recibido
        /// </summary>
        /// <param name="token"></param>
        /// <param name="idRespuesta"></param>
        /// <returns></returns>
        public void ObtenerEstadoCFE(string token, string idRespuesta, string destinatario, string nombreArchivoEntrada)
        {
            string xmlACK;
            string nombreArchivo;

            try
            {
                ArrayList listaCertificados = manteUdoSobreRecibido.ConsultarCFEProcesado(token, idRespuesta);

                if (listaCertificados.Count > 0)
                {
                    foreach (CertificadosRecProcesados certificadoRecProcesado in listaCertificados)
                    {
                        certificadoRecProcesado.MotivosRechazo = manteUdoEstadoSobreRecibido.ConsultarCFEProcesado(certificadoRecProcesado.DocEntry);
                    }

                    xmlACK  = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                    xmlACK += "<ACKCFE xmlns=\"http://cfe.dgi.gub.uy\" version=\"1.0\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";

                    xmlACK += ObtenerTagCaratula(idRespuesta, listaCertificados, nombreArchivoEntrada);
                    xmlACK += ObtenerTagCertificados(listaCertificados);

                    xmlACK += "</ACKCFE>";

                    ObtenerFirmaDigital();

                    nombreArchivo = ObtenerNombre(idRespuesta);
                    ProcTransformacion.GuardarACKCertificadoPrevio(nombreArchivo, xmlACK);
                    ProcTransformacion.FirmarACKCertificado(RUTA_CERTIFICADO, nombreArchivo, CLAVE_CERTIFICADO);

                    ManteUdoCorreos manteUdo = new ManteUdoCorreos();
                    Correo          correo   = manteUdo.Consultar();

                    if (correo != null)
                    {
                        string[] adjuntos = new string[1];
                        //Se agregan las rutas de los archivos adjuntos
                        adjuntos[0] = RutasCarpetas.RutaCarpetaACKCFEReceptor + nombreArchivo + ".xml";

                        Mail mail = new Mail(destinatario, correo.Cuenta, Mensaje.cACKAsunto,
                                             Mensaje.cACKMensaje, Mensaje.pdfServidorGmail, correo.Cuenta, correo.Clave, adjuntos, 587);

                        //Envia correo con acuse de respuesta
                        if (mail.enviarCorreoGmail())
                        {
                            if (!adjuntos[0].Equals(""))
                            {
                                //Borra el archivo de sobre copiado para enviar en el correo
                                System.IO.File.Delete(adjuntos[0]);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                //SAPbouiCOM.Framework.Application.SBO_Application.MessageBox("RespuestaConsultaCFE/Error: " + ex.ToString());
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Valida la firma electronica en un CFE
        /// </summary>
        /// <param name="cfe"></param>
        /// <returns></returns>
        public bool ValidarFimaElectronica(string cfe)
        {
            XmlDocument xmlDocumento = new XmlDocument();

            xmlDocumento.LoadXml(cfe);

            ObtenerFirmaDigital();

            return(ProcTransformacion.ValidarFirmaElectronica(xmlDocumento, RUTA_CERTIFICADO, CLAVE_CERTIFICADO));
        }
Ejemplo n.º 3
0
        public XmlDocument refirmarDocumentos(XmlDocument XmlDocument)
        {
            string nombre = String.Empty;

            ManteUdoCertificadoDigital cerDigital = new ManteUdoCertificadoDigital();

            //Se obtiene informacion del certificado digital
            string rutaCertificado = cerDigital.ObtenerRutaCertificado();
            string clave           = cerDigital.ObtenerPassCertificado();

            DateTime fecha      = DateTime.Now;
            string   fechaFirma = fecha.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK");

            string strRutReceptor, strFecha, strFechaFirma, strInfoCertificado, strFechaEmision, strCodSucursal;

            XmlDocument documento = new XmlDocument();

            strRutReceptor     = "DGICFE:RutReceptor";
            strFecha           = "DGICFE:Fecha";
            strInfoCertificado = "DGICFE:X509Certificate";
            strFechaFirma      = "ns1:TmstFirma";
            strFechaEmision    = "ns1:FchEmis";
            strCodSucursal     = "ns1:CdgDGISucur";

            nombre = String.Empty;

            documento = new XmlDocument();
            documento.PreserveWhitespace = true;
            documento = XmlDocument;

            FirmaDigital firma           = new FirmaDigital();
            string       infoCertificado = ProcTransformacion.ObtenerCadenaCertificado();

            XmlNode nodoCertificado = documento.GetElementsByTagName(strInfoCertificado).Item(0);

            nodoCertificado.InnerXml = infoCertificado;
            // End Caratula /

            // Begin CFE /
            XmlNode nodoFechaFirma = documento.GetElementsByTagName(strFechaFirma).Item(0);

            nodoFechaFirma.InnerXml = fechaFirma;

            // End CFE /

            #region CFE_FIRMA
            // Elimino el nodo Firma del template
            XmlNode nodoFirma = documento.GetElementsByTagName("Signature").Item(0);
            documento.GetElementsByTagName("ns1:CFE").Item(0).RemoveChild(nodoFirma);

            //Crea nuevo documento con el CFE para firmar
            XmlDocument docFirma = new XmlDocument();
            docFirma.PreserveWhitespace = true;

            XmlNode cfe   = documento.DocumentElement.GetElementsByTagName("ns1:CFE").Item(0);
            XmlNode nuevo = docFirma.ImportNode(cfe, true);
            docFirma.AppendChild(nuevo);

            try
            {
                // Create a new CspParameters object to specify
                // a key container.
                CspParameters cspParams = new CspParameters();
                cspParams.KeyContainerName = rutaCertificado;

                // Create a new RSA signing key and save it in the container.
                RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);

                // Obtengo cadena certificado /
                byte[] pfxBlob = File.ReadAllBytes(rutaCertificado);

                // Sign the XML document.
                firma.SignXml(docFirma, pfxBlob, rsaKey, clave);


                // Elimino el CFE del template //
                documento.DocumentElement.RemoveChild(cfe);

                // Agrego el CFE firmado //
                XmlNode cfeFirmado = docFirma.GetElementsByTagName("ns1:CFE").Item(0);
                nuevo = documento.ImportNode(cfeFirmado, true);
                documento.DocumentElement.AppendChild(nuevo);
                #endregion CFE_FIRMA
            }
            catch (Exception e)
            {
            }


            return(documento);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Firma el ACK generado
        /// </summary>
        /// <param name="nombreAlmacenClaves"></param>
        /// <param name="numeroCertificado"></param>
        /// <returns></returns>
        public bool FirmarACK(string nombreACKsf, string nombreACKf)
        {
            string rutaCertificado = "", passCertificado = "";
            bool   resultado = true;

            try
            {
                ManteUdoCertificadoDigital cerDigital = new ManteUdoCertificadoDigital();

                //Se obtiene informacion del certificado digital
                rutaCertificado = cerDigital.ObtenerRutaCertificado();
                passCertificado = cerDigital.ObtenerPassCertificado();

                //Crea un nuevo objeto CspParameters para especificar el contenedor del certificado
                CspParameters cspParams = new CspParameters();
                cspParams.KeyContainerName = rutaCertificado;

                //Crea un nuevo RSA.
                RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);

                //Crea un nuevo documento xml
                XmlDocument xmlDocumento = new XmlDocument();

                //Carga el xml no firmado.
                xmlDocumento.PreserveWhitespace = true;

                xmlDocumento.Load(nombreACKsf);


                //CENTENARIO BUENO
                //xmlDocumento.LoadXml("<?xml version=\"1.0\"?><ACKSobre xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" version=\"1.0\" xmlns=\"http://cfe.dgi.gub.uy\"><Caratula><RUCReceptor>210308350014</RUCReceptor><RUCEmisor>219999830019</RUCEmisor><IDRespuesta>100</IDRespuesta><NomArch>sob_219999830019_20141211_2.xml</NomArch><FecHRecibido>2014-12-11T09:57:16.4132781-02:00</FecHRecibido><IDEmisor>2</IDEmisor><IDReceptor>100</IDReceptor><CantidadCFE>8</CantidadCFE><Tmst>2014-12-11T09:58:26.4133782-02:00</Tmst></Caratula><Detalle><Estado>AS</Estado><ParamConsulta><Token>8iLdRqMvLC3m/5dxX3sjsFuQ9qs4vkVUVZl6QqDE0/5rgMl/rs8CSUoPSDeG4QhL5S3OP+6fCHJMDIQwIJlXlQ==</Token><Fechahora>2014-12-11T09:59:16.4132781-02:00</Fechahora></ParamConsulta></Detalle></ACKSobre>");

                //CENTENARIO MALO
                // xmlDocumento.LoadXml("<?xml version=\"1.0\"?><ACKSobre xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" version=\"1.0\" xmlns=\"http://cfe.dgi.gub.uy\"><Caratula><RUCReceptor>210308350014</RUCReceptor><RUCEmisor>219999830019</RUCEmisor><IDRespuesta>101</IDRespuesta><NomArch>sob_219999830019_20141211_1.xml</NomArch><FecHRecibido>2014-12-11T09:57:43.5371631-02:00</FecHRecibido><IDEmisor>1</IDEmisor><IDReceptor>101</IDReceptor><CantidadCFE>15</CantidadCFE><Tmst>2014-12-11T09:57:43.5371631-02:00</Tmst></Caratula><Detalle><Estado>BS</Estado><MotivosRechazo><Motivo>S05</Motivo><Glosa>No coinciden cantidad CFE de carátula y contenido</Glosa></MotivosRechazo></Detalle></ACKSobre>");

                //LUSA BUENO
                //xmlDocumento.LoadXml("<?xml version=\"1.0\"?><ACKSobre xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" version=\"1.0\" xmlns=\"http://cfe.dgi.gub.uy\"><Caratula><RUCReceptor>210160990017</RUCReceptor><RUCEmisor>219999830019</RUCEmisor><IDRespuesta>35</IDRespuesta><NomArch>sob_219999830019_20141218_2.xml</NomArch><FecHRecibido>2014-12-18T15:08:46.4642461-02:00</FecHRecibido><IDEmisor>2</IDEmisor><IDReceptor>35</IDReceptor><CantidadCFE>8</CantidadCFE><Tmst>2014-12-18T15:08:50.4642461-02:00</Tmst></Caratula><Detalle><Estado>AS</Estado><ParamConsulta><Token>hmbh5ghNyOIEQ95B9oJtE9Tjsys=</Token><Fechahora>2014-12-18T15:08:52.4642461-02:00</Fechahora></ParamConsulta></Detalle></ACKSobre>");

                //LUSA MALO
                //xmlDocumento.LoadXml("<?xml version=\"1.0\"?><ACKSobre xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" version=\"1.0\" xmlns=\"http://cfe.dgi.gub.uy\"><Caratula><RUCReceptor>210160990017</RUCReceptor><RUCEmisor>219999830019</RUCEmisor><IDRespuesta>34</IDRespuesta><NomArch>sob_219999830019_20141218_1.xml</NomArch><FecHRecibido>2014-12-18T15:08:52.7576766-02:00</FecHRecibido><IDEmisor>1</IDEmisor><IDReceptor>34</IDReceptor><CantidadCFE>5</CantidadCFE><Tmst>2014-12-18T15:08:54.4642461-02:00</Tmst></Caratula><Detalle><Estado>BS</Estado><MotivosRechazo><Motivo>S05</Motivo><Glosa>No coinciden cantidad CFE de carátula y contenido</Glosa></MotivosRechazo></Detalle></ACKSobre>");

                byte[] pfxBlob = File.ReadAllBytes(rutaCertificado);

                //Firma el documento
                ProcTransformacion.SignXml(xmlDocumento, pfxBlob, passCertificado);

                //Guarda el certificado firmado
                xmlDocumento.Save(nombreACKf);

                //BUENO
                //xmlDocumento.Save(@"C:\Users\greivinsalas\Desktop\M_35_Sob_219999830019_20141218_2.xml");

                //MALO
                // xmlDocumento.Save(@"C:\Users\greivinsalas\Desktop\M_34_Sob_219999830019_20141218_1.xml");
            }
            catch (Exception)
            {
                resultado = false;
            }

            return(resultado);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Metodo para crear sobre para el comprobante
        /// </summary>
        /// <param name="cfe"></param>
        private Sobre CrearSobre(CFE cfe, bool sobreDgi)
        {
            Sobre  sobre           = new Sobre(cfe);
            string infoCertificado = "";

            try
            {
                infoCertificado = ProcTransformacion.ObtenerCadenaCertificado();

                if (infoCertificado.Equals(""))
                {
                    sobre = null;
                }
                else
                {
                    if (sobreDgi)
                    {
                        ManteUdoDocumento manteUdoDocumento = new ManteUdoDocumento();
                        string            rutConfigurado    = manteUdoDocumento.ObtenerRut();
                        //Proceso para DGI
                        if (rutConfigurado != null)
                        {
                            sobre.RucReceptor = rutConfigurado;//214844360018;//219999830019
                        }
                        else
                        {
                            sobre.RucReceptor = "214844360018";//219999830019
                        }
                        sobre.X509Certificate = infoCertificado;
                        sobre.ObtenerCertificadosCreados(listaCertificadosCreados);

                        string xmlSobreDGI = ProcSerializacion.CrearXmlSobre(sobre);

                        ProcTransformacion.GuardarSobrePrevio(sobre.NombrePrev, xmlSobreDGI, true);
                        ProcTransformacion.TransformarSobre(sobre.NombrePrev, sobre.Nombre, sobre.ListaCertificados, "", true);
                    }
                    else
                    {
                        //Proceso para Tercero

                        sobre.X509Certificate = infoCertificado;
                        sobre.ObtenerCertificadosCreados(listaCertificadosCreados);

                        string xmlSobreCliente = ProcSerializacion.CrearXmlSobre(sobre);

                        ProcTransformacion.GuardarSobrePrevio(sobre.NombrePrev, xmlSobreCliente, false);

                        if (!cfe.TextoLibreAdenda.Equals(""))
                        {
                            ProcTransformacion.TransformarSobre(sobre.NombrePrev, sobre.Nombre, sobre.ListaCertificados, cfe.TextoLibreAdenda, false);
                        }
                        else
                        {
                            ProcTransformacion.TransformarSobre(sobre.NombrePrev, sobre.Nombre, sobre.ListaCertificados, "", false);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                SAPbouiCOM.Framework.Application.SBO_Application.MessageBox("ERROR: " + ex.ToString());
            }

            return(sobre);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Metodo para obtener el codigo de seguridad
 /// </summary>
 /// <param name="numeroCertificado"></param>
 /// <returns></returns>
 private string ObtenerCodigoSeguridad(string numeroCertificado)
 {
     return(ProcTransformacion.ObtenerCodigoSegurdad(numeroCertificado));
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Crea los archivos de xml para los certificados
        /// </summary>
        /// <param name="cfe"></param>
        private string CrearCertificado(CFE cfe)
        {
            string codSeguridad = "";

            try
            {
                int  i            = 0;
                bool estadoAdenda = false;

                if (cfe.Items != null)
                {
                    while (i < cfe.Items.Count)
                    {
                        if (cfe.Items[i].UnidadMedida.Length > 4)
                        {
                            cfe.Items[i].UnidadMedida = "N/A";
                        }
                        i++;
                    }
                }

                //Limpia la lista de certificados creados
                listaCertificadosCreados.Clear();

                //Agregar certificado a lista de certificados creados
                listaCertificadosCreados.Add(cfe);

                String xmlCertificado = ProcSerializacion.CrearXmlCFE(cfe);
                string adenda         = ProcTransformacion.ObtenerAdenda(cfe);
                ProcTransformacion.GuardarCertificadoPrevio(cfe.TipoCFEInt, cfe.SerieComprobante, cfe.NumeroComprobante.ToString(), xmlCertificado);

                if (cfe.InfoReferencia.Count > 0)
                {
                    //Valida que la referencia sea global o especifica
                    if (cfe.InfoReferencia[0].IndicadorReferenciaGlobal == CFEInfoReferencia.ESIndicadorReferenciaGlobal.ReferenciaGlobal)
                    {
                        ProcTransformacion.TransformarCertificado(cfe.TipoCFEInt, cfe.SerieComprobante, cfe.NumeroComprobante.ToString(), cfe.TipoCFE, cfe.TipoDocumentoReceptor, true);
                    }
                    else
                    {
                        ProcTransformacion.TransformarCertificado(cfe.TipoCFEInt, cfe.SerieComprobante, cfe.NumeroComprobante.ToString(), cfe.TipoCFE, cfe.TipoDocumentoReceptor);
                    }
                }
                else
                {
                    ProcTransformacion.TransformarCertificado(cfe.TipoCFEInt, cfe.SerieComprobante, cfe.NumeroComprobante.ToString(), cfe.TipoCFE, cfe.TipoDocumentoReceptor);
                }

                if (!adenda.Equals(""))
                {
                    ProcTransformacion.GenerarCFEAdenda(cfe.TipoCFEInt, cfe.SerieComprobante, cfe.NumeroComprobante.ToString(), adenda);
                    estadoAdenda = true;
                }

                ProcTransformacion.FirmarCertificado(RUTA_CERTIFICADO, cfe.TipoCFEInt, cfe.SerieComprobante, cfe.NumeroComprobante.ToString(), CLAVE_CERTIFICADO, false, ref codSeguridad);

                if (estadoAdenda)
                {
                    ProcTransformacion.FirmarCertificado(RUTA_CERTIFICADO, cfe.TipoCFEInt, cfe.SerieComprobante, cfe.NumeroComprobante.ToString(), CLAVE_CERTIFICADO, estadoAdenda, ref codSeguridad);
                }
            }
            catch (Exception ex)
            {
                SAPbouiCOM.Framework.Application.SBO_Application.MessageBox("ERROR: " + ex.ToString());
            }
            return(codSeguridad);
        }