public string GetXml() { System.IO.MemoryStream ms = new System.IO.MemoryStream(); CFDIXmlTextWriter writer = new CFDIXmlTextWriter(this, ms, System.Text.Encoding.UTF8); writer.WriteXml(); ms.Position = 0; System.IO.StreamReader reader = new System.IO.StreamReader(ms); string xml = reader.ReadToEnd(); reader.Close(); writer.Close(); return(xml); }
public bool GetCFDI(string user, string password, CFDI.Comprobante comprobante) { //throw new NotImplementedException(); string invoiceFileName = DateTime.Now.ToString("yyyyMMddHHmmss_" + comprobante.PublicKey.ToString("N")); byte[] sendFileBytes; byte[] responseFileBytes; CloudStorageAccount account = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["AzureDefaultStorageConnectionString"]); CloudBlobClient client = account.CreateCloudBlobClient(); CloudBlobContainer container = client.GetContainerReference(ConfigurationManager.AppSettings["AzureDefaultStorage"]); try { using (MemoryStream ms = new MemoryStream()) { using (MemoryStream zipMs = new MemoryStream()) { CFDIXmlTextWriter writer = new CFDIXmlTextWriter(comprobante, ms, System.Text.Encoding.UTF8); writer.WriteXml(); ms.Position = 0; using (ZipArchive zip = new ZipArchive(zipMs, ZipArchiveMode.Create, true)) { var entry = zip.CreateEntry(invoiceFileName + "_send.xml"); using (Stream s = entry.Open()) { ms.CopyTo(s); } zipMs.Flush(); } // zip.Dispose() => Close(); // container.CreateIfNotExists(); CloudBlockBlob blob = container.GetBlockBlobReference(invoiceFileName + "_send.zip"); zipMs.Position = 0; blob.UploadFromStream(zipMs); blob.Properties.ContentType = "application/x-zip-compressed"; blob.SetMetadata(); blob.SetProperties(); zipMs.Position = 0; sendFileBytes = zipMs.ToArray(); } // zipMs.Dispose() => Close(); } // ms.Dispose() => Close(); //CFDI.EDICOM.TestCFDI.CFDiService webService = new CFDI.EDICOM.TestCFDI.CFDiService(); //responseFileBytes = webService.getCfdiTest(user, password, sendFileBytes); ICFDIService webService = CFDiServiceFactory.Create(); responseFileBytes = webService.GetCFDI(user, password, sendFileBytes); CloudBlockBlob blob2 = container.GetBlockBlobReference(invoiceFileName + "_response.zip"); //zipMs.Position = 0; blob2.UploadFromByteArray(responseFileBytes, 0, responseFileBytes.Length); // .UploadFromStream(zipMs); blob2.Properties.ContentType = "application/x-zip-compressed"; blob2.SetMetadata(); blob2.SetProperties(); using (var responseStream = new MemoryStream(responseFileBytes)) { using (var archive = new ZipArchive(responseStream, ZipArchiveMode.Read, true)) { var fileInArchive = archive.Entries[0]; // using (var entryStream = fileInArchive.Open()) { using (var reader = new StreamReader(entryStream)) { string output = reader.ReadToEnd(); System.Xml.XmlDocument invoice = new System.Xml.XmlDocument(); invoice.LoadXml(output); System.Xml.XmlNamespaceManager nsmgr = new System.Xml.XmlNamespaceManager(invoice.NameTable); nsmgr.AddNamespace("cfdi", "http://www.sat.gob.mx/cfd/3"); nsmgr.AddNamespace("tfd", "http://www.sat.gob.mx/TimbreFiscalDigital"); System.Xml.XmlNode timbre = invoice.SelectSingleNode("//tfd:TimbreFiscalDigital", nsmgr); TimbreFiscalDigital complemento = new TimbreFiscalDigital(); complemento.Version = timbre.Attributes.GetNamedItem("version").Value.ToString(); complemento.UUID = timbre.Attributes.GetNamedItem("UUID").Value.ToString(); complemento.FechaTimbrado = DateTime.Parse(timbre.Attributes.GetNamedItem("FechaTimbrado").Value); complemento.SelloCFD = timbre.Attributes.GetNamedItem("selloCFD").Value.ToString(); complemento.NoCertificadoSAT = timbre.Attributes.GetNamedItem("noCertificadoSAT").Value.ToString(); complemento.SelloSAT = timbre.Attributes.GetNamedItem("selloSAT").Value.ToString(); if (comprobante.Complementos == null) { comprobante.Complementos = new List <Complemento>(); } comprobante.Complementos.Add(complemento); //Complemento complemento = new Complemento(); //complemento. // // Sistrategia.Server.SAT.CFDI.Comprobante comprobante2 = Sistrategia.Server.SAT.SATManager.GetComprobante(Guid.Parse(post["comprobanteId"])); // // comprobante2.Complemento = new Sistrategia.Server.SAT.CFDI.ComprobanteComplemento(); // // comprobante2.Complemento.TimbreFiscalDigitalSpecified = true; // // comprobante2.Complemento.TimbreFiscalDigital = new Sistrategia.Server.SAT.CFDI.ComprobanteTimbre(); // // comprobante2.Complemento.TimbreFiscalDigital.SatTimbreId = Guid.NewGuid(); // // comprobante2.Complemento.TimbreFiscalDigital.Version = timbre.Attributes.GetNamedItem("version").Value.ToString(); // // comprobante2.Complemento.TimbreFiscalDigital.UUID = timbre.Attributes.GetNamedItem("UUID").Value.ToString(); // // comprobante2.Complemento.TimbreFiscalDigital.FechaTimbrado = DateTime.Parse(timbre.Attributes.GetNamedItem("FechaTimbrado").Value); // // comprobante2.Complemento.TimbreFiscalDigital.SelloCFD = timbre.Attributes.GetNamedItem("selloCFD").Value.ToString(); // // comprobante2.Complemento.TimbreFiscalDigital.NoCertificadoSAT = timbre.Attributes.GetNamedItem("noCertificadoSAT").Value.ToString(); // // comprobante2.Complemento.TimbreFiscalDigital.SelloSAT = timbre.Attributes.GetNamedItem("selloSAT").Value.ToString(); // entryStream.Position = 0; // entryStream.Position // SAVE final xml: Sistrategia.SAT.CFDiWebSite.CloudStorage.CloudStorageMananger manager = new Sistrategia.SAT.CFDiWebSite.CloudStorage.CloudStorageMananger(); //manager.UploadFromStream(ConfigurationManager.AppSettings["AzureAccountName"], manager.UploadFromString(ConfigurationManager.AppSettings["AzureAccountName"], ConfigurationManager.AppSettings["AzureAccountKey"], comprobante.Emisor.PublicKey.ToString("N"), comprobante.PublicKey.ToString("N") + ".xml", comprobante.Serie + comprobante.Folio + ".xml", //model.ComprobanteArchivo.FileName, "text/xml", //model.ComprobanteArchivo.ContentType, output); // model.ComprobanteArchivo.InputStream); comprobante.GeneratedCadenaOriginal = comprobante.GetCadenaOriginal(); comprobante.GeneratedXmlUrl = string.Format(@"https://sistrategiacfdi1.blob.core.windows.net/{0}/{1}.xml", comprobante.Emisor.PublicKey.ToString("N"), comprobante.PublicKey.ToString("N")); comprobante.Status = "A"; } } //using (var fileToCompressStream = new MemoryStream(fileBytes)) { // fileToCompressStream.CopyTo(entryStream); //} } } } catch (Exception ex) { CloudBlockBlob blob2 = container.GetBlockBlobReference(invoiceFileName + "_exception.txt"); //zipMs.Position = 0; blob2.UploadText(ex.ToString()); blob2.Properties.ContentType = "text/plain"; blob2.SetMetadata(); blob2.SetProperties(); throw; } return(true); }