public ActionResult Cancel(string id, FormCollection formCollection)
        {
            Guid publicKey;
            if (!Guid.TryParse(id, out publicKey))
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);

            var comprobante = DBContext.Comprobantes.Where(e => e.PublicKey == publicKey).SingleOrDefault();

            if (comprobante == null)
                return HttpNotFound();

            var certificado = DBContext.Certificados.Where(e => e.NumSerie == comprobante.NoCertificado).SingleOrDefault();

            string[] UUIDs = new string[1];
            UUIDs[0] = ((TimbreFiscalDigital)comprobante.Complementos[0]).UUID;

            string user = ConfigurationManager.AppSettings["CfdiServiceUser"];
            string password = ConfigurationManager.AppSettings["CfdiServicePassword"];

            var model = new ComprobanteDetailViewModel(comprobante);
            string invoiceFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + comprobante.PublicKey.ToString("N") + "_cancelado";

            //string invoiceFileName = DateTime.Now.ToString("cancelado_yyyyMMddHHmmss_" + comprobante.PublicKey.ToString("N") + ".txt");

            try {

                SATManager manager = new SATManager();
                ICancelaResponse response = manager.CancelaCFDI(comprobante, user, password, comprobante.Emisor.RFC, UUIDs, certificado.PFXArchivo, certificado.PFXContrasena);
                // // response.Ack.ToString();
                if (response != null) {
                    Cancelacion cancelacion = new Cancelacion {
                        Ack = response.Ack,
                        Text = response.Text,
                        CancelacionXmlResponseUrl = response.XmlResponse
                    };
                    cancelacion.UUIDComprobantes = new List<CancelacionUUIDComprobante>();
                    cancelacion.UUIDComprobantes.Add(new CancelacionUUIDComprobante {
                        Comprobante = comprobante,
                        UUID = ((TimbreFiscalDigital)comprobante.Complementos[0]).UUID
                    });
                    comprobante.Status = "C";
                    DBContext.Cancelaciones.Add(cancelacion);
                    DBContext.SaveChanges();
                }
            }
            catch (Exception ex) {
                //TempData["msg"] = ex.Message.ToString();
                TempData["error"] = ex.Message.ToString();
                return View(model);
                //return View(model);
                //    return View();
            }

            return View(model);
        }
        public ActionResult Cancel(string id)
        {
            Guid publicKey;
            if (!Guid.TryParse(id, out publicKey))
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);

            var comprobante = DBContext.Comprobantes.Where(e => e.PublicKey == publicKey).SingleOrDefault();

            if (comprobante == null)
                return HttpNotFound();

            var certificado = DBContext.Certificados.Where(e => e.NumSerie == comprobante.NoCertificado).SingleOrDefault();

            var model = new ComprobanteDetailViewModel(comprobante);
            return View(model);
        }
        public ActionResult GetTimbre(string id, FormCollection formCollection) {
            //public ActionResult GetTimbre(string id, ComprobanteDetailViewModel model) {
            //public ActionResult GetTimbre(ComprobanteDetailViewModel model) {
            Guid publicKey;
            if (!Guid.TryParse(id, out publicKey))
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);

            var comprobante = DBContext.Comprobantes.Where(e => e.PublicKey == publicKey).SingleOrDefault();

            if (comprobante == null)
                return HttpNotFound();

            var certificado = DBContext.Certificados.Where(e => e.NumSerie == comprobante.NoCertificado).SingleOrDefault();

            string user = ConfigurationManager.AppSettings["CfdiServiceUser"];
            string password = ConfigurationManager.AppSettings["CfdiServicePassword"];

            var model = new ComprobanteDetailViewModel(comprobante);

            string invoiceFileName = DateTime.Now.ToString("yyyyMMddHmmss_" + comprobante.PublicKey.ToString("N"));
            //comprobante.WriteXml(invoicesPath + invoiceFileName + "_send.xml");



            //manager.GetCFDI(user, password, comprobante, certificado);


            //// Comprimir y enviar al servicio web
            //string pathFile = invoicesPath + invoiceFileName + "_send.xml";
            //Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile();
            //string saveToFilePath = invoicesPath + invoiceFileName + "_send.zip";
            //zip.AddFile(pathFile, "");
            //zip.Save(saveToFilePath);

            //string filePath = invoicesPath + invoiceFileName + "_send.zip";
            //string responsePath = invoicesPath + invoiceFileName + "_response.zip";



            try {

                SATManager manager = new SATManager();
                bool response = manager.GetCFDI(user, password, comprobante);
                if (response)
                    DBContext.SaveChanges();

                //byte[] response = Sistrategia.Server.SAT.SATManager.GetCFDI(user, password, file);

                //    byte[] response = Sistrategia.Server.SAT.SATManager.GetCFDI(user, password, filePath, responsePath);
                //    Ionic.Zip.ZipFile zipR = Ionic.Zip.ZipFile.Read(invoicesPath + invoiceFileName + "_response.zip");
                //    zipR.ExtractAll(invoicesPath, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently);
                //    zipR.Dispose();
                //    //return File(invoicesPath + "SIGN_" + invoiceFileName + "_send.xml", "text/xml");

                //    /* Insert Timbre */
                //    System.Xml.XmlDocument invoice = new System.Xml.XmlDocument();
                //    invoice.Load(invoicesPath + "SIGN_" + invoiceFileName + "_send.xml");
                //    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);

                //    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();

                //    string invoiceXml = string.Empty;
                //    StreamReader streamReader = new StreamReader(invoicesPath + "SIGN_" + invoiceFileName + "_send.xml");
                //    invoiceXml = streamReader.ReadToEnd();
                //    streamReader.Close();

                //    if (Sistrategia.Server.SAT.SATManager.InsertComprobanteTimbre(comprobante2)) {
                //        string QRCODE = "?re=" + comprobante.Emisor.RFC + "&rr=" + comprobante.Receptor.RFC + "&tt=" + comprobante.Total + "&id=" + comprobante2.Complemento.TimbreFiscalDigital.UUID;
                //        TempData["msg2"] = "¡Timbrado exitoso!";
                //    }
                //    /* Insert Timbre */

                //    return RedirectToAction("View", "Invoice", new { id = comprobante.ComprobanteId.ToString() });
            }
            catch (Exception ex) {
                TempData["msg"] = ex.Message.ToString();
                return View(model);
                //    return View();
            }




            return View(model);
        }