Example #1
0
        public ResAcc <string> CancelarFactura(string sUuid, string sRfc, byte[] ArchivoPfx, string sContraseniaPfx)
        {
            var Res = new ResAcc <string>();

            try
            {
                var oServicio = new Edicom.CFDiService();

                // Se manda cancelar la factura, prueba o normal
                if (this.Prueba)
                {
                    Res.Respuesta = "Resultado de prueba. No hay mecanismo de cancelación en Edicom.";
                }
                else
                {
                    CancelaResponse oCancel = oServicio.cancelaCFDi(this.Usuario, this.Contrasenia, sRfc, new string[] { sUuid }, ArchivoPfx, sContraseniaPfx);
                    Res.Respuesta = oCancel.ack;
                }
                Res.Exito = true;
            }
            catch (Exception e)
            {
                Res.Mensaje = e.Message;
            }
            return(Res);
        }
Example #2
0
        public ResAcc <string> TimbrarFactura(byte[] XmlFactura)
        {
            var Res = new ResAcc <string>();

            try
            {
                var    oServicio = new Edicom.CFDiService();
                byte[] oCfdiZip;

                // Se llama el servico de Edicom, prueba o normal
                if (this.Prueba)
                {
                    oCfdiZip = oServicio.getCfdiTest(this.Usuario, this.Contrasenia, XmlFactura);
                }
                else
                {
                    oCfdiZip = oServicio.getCfdi(this.Usuario, this.Contrasenia, XmlFactura);
                }

                var oZip  = new AccesoZip(oCfdiZip);
                var oCfdi = oZip.ObtenerArchivo(FacturaPac.CfdiNombre);
                Res.Respuesta = Encoding.UTF8.GetString(oCfdi);
                Res.Exito     = true;
            }
            catch (Exception e)
            {
                Res.Mensaje = e.Message;
            }

            return(Res);
        }
Example #3
0
        public ResAcc <string> GenerarSelloDigital(string sArchivoKey, string sContrasenia, string sCadenaOriginal)
        {
            var Res = new ResAcc <string>(true);

            try
            {
                byte[] encryptedPrivateKeyInfoData = File.ReadAllBytes(sArchivoKey);
                AsymmetricKeyParameter parameter   = PrivateKeyFactory.DecryptKey(sContrasenia.ToCharArray(), encryptedPrivateKeyInfoData);
                MemoryStream           stream      = new MemoryStream();
                new StreamWriter(stream);
                StringWriter writer = new StringWriter();
                new PemWriter(writer).WriteObject(parameter);
                writer.Close();
                ISigner signer = SignerUtilities.GetSigner("SHA1WithRSA");
                byte[]  bytes  = Encoding.UTF8.GetBytes(sCadenaOriginal);
                signer.Init(true, parameter);
                signer.BlockUpdate(bytes, 0, bytes.Length);
                Res.Respuesta = Convert.ToBase64String(signer.GenerateSignature()).ToString();
            } catch (Exception e) {
                Res.Exito    = false;
                Res.Mensaje  = "Error al generar el sello digital\n\n";
                Res.Mensaje += (e.InnerException == null ? e.Message : e.InnerException.Message);
            }

            return(Res);
        }
Example #4
0
        public static ResAcc <string> ValidarXml(string sCadenaXml, string sCadenaXsd)
        {
            var Res = new ResAcc <string>();// { Mensaje = "" };

            var EsquemaXsd           = XmlSchema.Read(new StringReader(sCadenaXsd), null);
            XmlReaderSettings Config = new XmlReaderSettings();

            Config.Schemas.Add(EsquemaXsd);
            Config.ValidationType          = ValidationType.Schema;
            Config.ValidationEventHandler += new ValidationEventHandler((o, e) =>
            {
                if (e.Severity == XmlSeverityType.Warning)
                {
                    Res.Mensaje += ("Warning: " + e.Message + "\n");
                }
                else if (e.Severity == XmlSeverityType.Error)
                {
                    Res.Mensaje += ("Error: " + e.Message + "\n");
                }
            });

            XmlReader LectorXml = XmlReader.Create(new StringReader(sCadenaXml), Config);

            FacturaXml.ResultadoValidacion = "";
            while (LectorXml.Read())
            {
            }

            Res.Exito = (Res.Mensaje == null);

            return(Res);
        }
Example #5
0
        public static ResAcc <string> TransformarXml(string sXml, string sXsl)
        {
            var Res = new ResAcc <string>(true);

            var                  oTexto   = new StringWriterMod(Encoding.UTF8);
            var                  oXml     = XmlWriter.Create(oTexto, FacturaXml.ConfigXml);
            XmlReader            oXsl     = XmlReader.Create(new StringReader(sXsl));
            XmlReader            oFactura = XmlReader.Create(new StringReader(sXml));
            XslCompiledTransform TransXsl = new XslCompiledTransform();
            string               sXmlRes  = "";

            try
            {
                TransXsl.Load(oXsl);
                TransXsl.Transform(oFactura, oXml);
                sXmlRes = oTexto.ToString();
            }
            catch (Exception e)
            {
                Res.Exito    = false;
                Res.Mensaje  = "Error en transformación de Xml\n\n";
                Res.Mensaje += (e.InnerException == null ? e.Message : e.InnerException.Message);
            }
            oXsl.Close();
            oFactura.Close();
            oXml.Close();
            oTexto.Close();

            Res.Respuesta = sXmlRes;
            return(Res);
        }
Example #6
0
        public static ResAcc <TimbreXml> LeerXmlTimbrado(string sXmlFactura)
        {
            var Res = new ResAcc <TimbreXml>();

            XmlDocument oXml = new XmlDocument();

            oXml.LoadXml(sXmlFactura);
            var oTfds = oXml.GetElementsByTagName("tfd:TimbreFiscalDigital");

            if (oTfds.Count <= 0)
            {
                Res.Mensaje = "El Xml no contiene la información de timbrado.";
                return(Res);
            }
            try
            {
                var oTimbreXml = new TimbreXml();
                var oTfd       = oTfds[0];
                oTimbreXml.FolioFiscal        = oTfd.Attributes["UUID"].Value;
                oTimbreXml.SelloSat           = oTfd.Attributes["selloSAT"].Value;
                oTimbreXml.CertificadoSat     = oTfd.Attributes["noCertificadoSAT"].Value;
                oTimbreXml.FechaCertificacion = Convert.ToDateTime(oTfd.Attributes["FechaTimbrado"].Value);

                // Se genera la cadena original del timbre fiscal
                var oFacturaComps = oXml.GetElementsByTagName("cfdi:Comprobante");
                if (oFacturaComps.Count <= 0)
                {
                    Res.Mensaje = "El Xml no contiene la información del comprobante o está mal formado.";
                    return(Res);
                }
                var    oFacturaComp  = oFacturaComps[0];
                string sVersion      = oFacturaComp.Attributes["version"].Value;
                string sSelloFactura = oFacturaComp.Attributes["sello"].Value;
                oTimbreXml.CadenaOriginal = FacturaXml.GenerarCadenaOriginalTimbre(sVersion, sSelloFactura, oTimbreXml);

                Res.Respuesta = oTimbreXml;
            }
            catch (Exception e)
            {
                Res.Mensaje = e.Message;
                return(Res);
            }

            Res.Exito = true;
            return(Res);
        }
Example #7
0
        public ResAcc <string> GenerarFactura()
        {
            // Se genera el Xml
            var ResXml = FacturaXml.GenerarXml(this);

            if (ResXml.Error)
            {
                return(new ResAcc <string>(false, ResXml.Mensaje));
            }
            // Se llenan algunos datos generados

            /* this.NumeroDeCertificado = ResXml.Respuesta.NumeroDeCertificado;
             * this.Certificado = ResXml.Respuesta.Certificado;
             * this.Sello = ResXml.Respuesta.Sello;
             * this.XmlFactura = ResXml.Respuesta.XmlFactura;
             */

            var Res = new ResAcc <string>(true);

            Res.Respuesta = this.XmlFactura;
            return(Res);
        }