public bool EnvioSAT(TimbreWs33 topPComprobante) { try { GeneradorCadenasCfd gen = new GeneradorCadenasCfd(); string strLAcuseReciboSAT; byte[] result; int idErrorSAT; Encabezado encLMetadata; //SHA1 sha = new SHA1CryptoServiceProvider(); string strComprobante = null; if (string.IsNullOrEmpty(topPComprobante.Xml)) { var directorio = Path.Combine(ConfigurationManager.AppSettings["RutaTimbrado"], topPComprobante.RfcEmisor, topPComprobante.FechaFactura.ToString("yyyyMMdd")); if (!Directory.Exists(directorio)) { Directory.CreateDirectory(directorio); } var fileName = Path.Combine(directorio, "Comprobante_" + topPComprobante.Uuid + ".xml"); strComprobante = File.ReadAllText(fileName, Encoding.UTF8); } else { strComprobante = topPComprobante.Xml; } Comprobante comprobante = GeneradorCfdi.GetComprobanteFromString(strComprobante); //byte[] bytLCadenaOriginal = Encoding.UTF8.GetBytes(gen.CadenaOriginal(strComprobante)); //result = sha.ComputeHash(bytLCadenaOriginal); string strLRfcEmisor = comprobante.Emisor.Rfc; // string strLHash = topPComprobante.Hash; // string version = comprobante.Version; string version = "3.3"; //string strLCertificadoSAT = comprobante.Complemento.timbreFiscalDigital.NoCertificadoSAT; string strLCertificadoSAT = ConfigurationManager.AppSettings["NoCertificadoPac"]; string strLUUID = comprobante.Complemento.timbreFiscalDigital.UUID; DateTime datLFechaTimbrado = comprobante.Complemento.timbreFiscalDigital.FechaTimbrado; string strLPathArchivo = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["PathXMLTemporales"] + Guid.NewGuid() + ".xml"; //Quitar addenda si tiene XElement xe = XElement.Load(new StringReader(strComprobante)); XElement addenda = xe.Elements(Constantes.CFDVersionNamespace + "Addenda").FirstOrDefault(); if (addenda != null) { addenda.Remove(); } var tw = new SidetecStringWriter(Encoding.UTF8); xe.Save(tw, SaveOptions.DisableFormatting); string xml = tw.ToString(); //escribimos el archivo en una carpeta para que el proceso de envio SAT lo tome// File.WriteAllText(strLPathArchivo, xml, Encoding.UTF8); //armamos el encabezado// encLMetadata = new Encabezado(strLRfcEmisor, version, strLCertificadoSAT, strLUUID, datLFechaTimbrado, strLPathArchivo); Log.Info("Enviando CFDI al SAT."); Log.Info("Se enviará el comprobante con el identificador: " + topPComprobante.IdTimbre); Log.Info("Encabezado." + encLMetadata.version + "|" + encLMetadata.UUID + "|" + encLMetadata.NumeroCertificado + "|" + encLMetadata.RfcEmisor + "|" + encLMetadata.Xml); var acuseReciboSAT = EnviarCFDIalSAT(encLMetadata); if (acuseReciboSAT != null) { idErrorSAT = acuseReciboSAT.CodEstatus.Equals("Comprobante Rechazado", StringComparison.InvariantCultureIgnoreCase) ? Convert.ToInt32(acuseReciboSAT.Incidencia[0].CodigoError) : 0; if (idErrorSAT != 501 || idErrorSAT != 502) { } var acuseStream = new MemoryStream(); var xmlSerializer = new XmlSerializer(typeof(SAT.CFDI.Cliente.Procesamiento.ServicioRecepcionCFDI.Acuse)); xmlSerializer.Serialize(acuseStream, acuseReciboSAT); acuseStream.Seek(0, SeekOrigin.Begin); var acuseReader = new StreamReader(acuseStream); strLAcuseReciboSAT = acuseReader.ReadToEnd(); var directorio = Path.Combine(ConfigurationManager.AppSettings["RutaTimbrado"], topPComprobante.RfcEmisor, topPComprobante.FechaFactura.ToString("yyyyMMdd")); if (!Directory.Exists(directorio)) { Directory.CreateDirectory(directorio); } var fileName = Path.Combine(directorio, "Acuse_" + topPComprobante.Uuid + ".xml"); File.WriteAllText(fileName, strLAcuseReciboSAT); } else { idErrorSAT = 103; strLAcuseReciboSAT = ""; } //idErrorSAT = TraerEstatusSAT(strLAcuseReciboSAT); Log.Info("Código de retorno SAT: " + idErrorSAT); GuardarInformacion(topPComprobante, idErrorSAT, strLAcuseReciboSAT, acuseReciboSAT); File.Delete(strLPathArchivo); return(true); } catch (Exception ex) { Log.Error("Error al intentar enviar el CFD al SAT, Err:" + ex.ToString()); return(false); } }
public bool EnvioSatRet(TimbreWs33 topPComprobante) { try { GeneradorCadenasCfd gen = new GeneradorCadenasCfd(); string strLAcuseReciboSAT; byte[] result; int idErrorSAT; Encabezado encLMetadata; SHA1 sha = new SHA1CryptoServiceProvider(); string strComprobante = null; if (string.IsNullOrEmpty(topPComprobante.Xml)) { var directorio = Path.Combine(ConfigurationManager.AppSettings["RutaTimbrado"], topPComprobante.RfcEmisor, topPComprobante.FechaFactura.ToString("yyyyMMdd")); if (!Directory.Exists(directorio)) { Directory.CreateDirectory(directorio); } var fileName = Path.Combine(directorio, "Comprobante_" + topPComprobante.Uuid + ".xml"); strComprobante = File.ReadAllText(fileName, Encoding.UTF8); } else { strComprobante = topPComprobante.Xml; } Retenciones comprobante = TimbradoUtils.DesSerializarRetenciones(strComprobante); //byte[] bytLCadenaOriginal = Encoding.UTF8.GetBytes(gen.CadenaOriginal(strComprobante)); //result = sha.ComputeHash(bytLCadenaOriginal); string strLRfcEmisor = comprobante.Emisor.RFCEmisor; string strLHash = topPComprobante.Hash; //Quitar addenda si tiene XElement xe = XElement.Load(new StringReader(strComprobante)); XElement addenda = xe.Elements(Constantes.CFDVersionNamespace + "Addenda").FirstOrDefault(); if (addenda != null) { addenda.Remove(); } var tw = new SidetecStringWriter(Encoding.UTF8); xe.Save(tw, SaveOptions.DisableFormatting); Log.Info("Enviando CFDI al SAT."); Log.Info("Se enviará el comprobante con el identificador: " + topPComprobante.IdTimbre); var acuseReciboSAT = ret.EnviarRetencion(strComprobante, topPComprobante.Uuid); if (acuseReciboSAT != null) { idErrorSAT = acuseReciboSAT.codEstatus.Equals("Comprobante Rechazado", StringComparison.InvariantCultureIgnoreCase) ? Convert.ToInt32(acuseReciboSAT.listaIncidencia[0].CodigoError) : 0; var acuseStream = new MemoryStream(); var xmlSerializer = new XmlSerializer(typeof(CertificadorWs.Business.Retenciones.AcuseRecepcionRetencion)); xmlSerializer.Serialize(acuseStream, acuseReciboSAT); acuseStream.Seek(0, SeekOrigin.Begin); var acuseReader = new StreamReader(acuseStream); strLAcuseReciboSAT = acuseReader.ReadToEnd(); var directorio = Path.Combine(ConfigurationManager.AppSettings["RutaTimbrado"], topPComprobante.RfcEmisor, topPComprobante.FechaFactura.ToString("yyyyMMdd")); if (!Directory.Exists(directorio)) { Directory.CreateDirectory(directorio); } var fileName = Path.Combine(directorio, "Acuse_" + topPComprobante.Uuid + ".xml"); File.WriteAllText(fileName, strLAcuseReciboSAT); } else { idErrorSAT = 103; strLAcuseReciboSAT = ""; } //idErrorSAT = TraerEstatusSAT(strLAcuseReciboSAT); Log.Info("Código de retorno SAT: " + idErrorSAT); GuardarInformacion(topPComprobante, idErrorSAT, strLAcuseReciboSAT, acuseReciboSAT); return(true); } catch (Exception ex) { Log.Error("Error al intentar enviar el CFD al SAT, Err:" + ex.ToString()); return(false); } }