public Main() { InitializeComponent(); MetodosGenerales.leerXML();//Inicializa config de BD y afip String a = ""; if (Testing == 1) { a = "TESTING"; } LogHelper.Log(LogTarget.File, DateTime.Now.ToString() + " - Inicio de programa. " + a); MetodosGenerales.crearCertificado(); connection = MetodosBD.ConnectRemote(ApplicationName, Host, Port, Username, Password, Database, Pooling); //Conexion con BD MetodosGenerales.leerLoginAnteriores(); //Leer si existe algun login anteiror if (EXPIRATION <= DateTime.Now) { cargarDatosAfip(); } //Linea en segundo plano para hacer facturacion y login si fuese necesario thread = new Thread(backgroundWork); thread.Start(); }
public static void hacer_login() { string cmsFirmadoBase64; string loginTicketResponse; XmlNode uniqueIdNode; XmlNode generationTimeNode; XmlNode ExpirationTimeNode; XmlNode ServiceNode; //String clave_o = Main.CLAVE; //String cert_path = Main.CERTIFICADO; String servicio_o = Main.SERVICIO; String url_testing = Main.URL_WSAA_test; String url_produccion = Main.URL_WSAA; try { //this._globalId += 1; // Preparo el XML Request XmlDocument XmlLoginTicketRequest = new XmlDocument(); XmlLoginTicketRequest.Load("LoginTemplate"); DateTime ahora = DateTime.Now; uniqueIdNode = XmlLoginTicketRequest.SelectSingleNode("//uniqueId"); generationTimeNode = XmlLoginTicketRequest.SelectSingleNode("//generationTime"); ExpirationTimeNode = XmlLoginTicketRequest.SelectSingleNode("//expirationTime"); ServiceNode = XmlLoginTicketRequest.SelectSingleNode("//service"); generationTimeNode.InnerText = ahora.AddMinutes(-10).ToString("s"); ExpirationTimeNode.InnerText = ahora.AddMinutes(+10).ToString("s"); uniqueIdNode.InnerText = System.Convert.ToString(1); ServiceNode.InnerText = servicio_o; DateTime fecha_expiracion = ahora.AddMinutes(+10); /* * SecureString clave = new SecureString(); * foreach (char character in clave_o) * clave.AppendChar(character); * clave.MakeReadOnly(); * * * // Obtenemos el Cert * //X509Certificate2 certificado = new X509Certificate2(); * if (clave.IsReadOnly()) * Main.certificado.Import(File.ReadAllBytes(cert_path), clave, X509KeyStorageFlags.PersistKeySet); * else * Main.certificado.Import(File.ReadAllBytes(cert_path)); */ byte[] msgBytes = Encoding.UTF8.GetBytes(XmlLoginTicketRequest.OuterXml); // Firmamos ContentInfo infoContenido = new ContentInfo(msgBytes); SignedCms cmsFirmado = new SignedCms(infoContenido); CmsSigner cmsFirmante = new CmsSigner(Main.certificado); cmsFirmante.IncludeOption = X509IncludeOption.EndCertOnly; cmsFirmado.ComputeSignature(cmsFirmante); cmsFirmadoBase64 = Convert.ToBase64String(cmsFirmado.Encode()); // Hago el login if (Main.Testing == 1) { AFIP.TEST.WSAA.LoginCMSService servicio = new AFIP.TEST.WSAA.LoginCMSService(); servicio.Url = url_testing; loginTicketResponse = servicio.loginCms(cmsFirmadoBase64); } else { AFIP.WSAA.LoginCMSService servicio = new AFIP.WSAA.LoginCMSService(); servicio.Url = url_produccion; loginTicketResponse = servicio.loginCms(cmsFirmadoBase64); } // Analizamos la respuesta XmlDocument XmlLoginTicketResponse = new XmlDocument(); XmlLoginTicketResponse.LoadXml(loginTicketResponse); String _Token = XmlLoginTicketResponse.SelectSingleNode("//token").InnerText; String _Sign = XmlLoginTicketResponse.SelectSingleNode("//sign").InnerText; //My.Settings.def_token = _Token; //My.Settings.def_sing = _Sign; var exStr = XmlLoginTicketResponse.SelectSingleNode("//expirationTime").InnerText; var genStr = XmlLoginTicketResponse.SelectSingleNode("//generationTime").InnerText; DateTime ExpirationTime = DateTime.Parse(exStr); DateTime GenerationTime = DateTime.Parse(genStr); // My.Settings.def_expiration = ExpirationTime; // My.Settings.Save(); MetodosBD.insertarLogin(_Token, _Sign, GenerationTime, fecha_expiracion /*ExpirationTime*/); //XDocRequest = XDocument.Parse(XmlLoginTicketRequest.OuterXml); //XDocResponse = XDocument.Parse(XmlLoginTicketResponse.OuterXml); //MessageBox.Show(ExpirationTime.ToString()); LogHelper.Log(LogTarget.File, DateTime.Now.ToString() + " Nuevo login. Exiration: " + ExpirationTime.ToString()); } // MsgBox("Exito" catch (Exception ex) { //Interaction.MsgBox(ex.Message); //MessageBox.Show(ex.Message); LogHelper.Log(LogTarget.File, DateTime.Now.ToString() + " - ERROR LOGIN AFIP: " + ex.Message); } }
public static void leerComprobantes() { int id = 0; int pv = 0; int codigo = 0; int estado; double importe = 0; double iva = 0; double total = 0; long doc = 0; int concepto = 0; string moneda = ""; int tipo_iva = 0; int tipo_doc = 0; DateTime fecha = new DateTime(); try { NpgsqlConnection connection = Main.connection; string sql = "SELECT * FROM afip_comprobante where comp_estado = 1 order by comp_fecha asc"; // data adapter making request from our connection NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, connection); NpgsqlCommand cmd = new NpgsqlCommand(sql, connection); using (var reader = cmd.ExecuteReader()) { if (reader.HasRows) { reader.Read(); pv = reader.GetInt32(reader.GetOrdinal("comp_pv")); codigo = reader.GetInt32(reader.GetOrdinal("comp_codigo")); estado = reader.GetInt32(reader.GetOrdinal("comp_estado")); id = reader.GetInt32(reader.GetOrdinal("id")); importe = reader.GetDouble(reader.GetOrdinal("comp_importe")); iva = reader.GetDouble(reader.GetOrdinal("comp_iva")); total = reader.GetDouble(reader.GetOrdinal("comp_total")); doc = reader.GetInt64(reader.GetOrdinal("comp_doc")); fecha = reader.GetDateTime(reader.GetOrdinal("comp_fecha")); moneda = reader.GetString(reader.GetOrdinal("comp_moneda")); tipo_doc = reader.GetInt32(reader.GetOrdinal("tipo_doc")); tipo_iva = reader.GetInt32(reader.GetOrdinal("tipo_iva")); concepto = reader.GetInt32(reader.GetOrdinal("concepto")); } //MessageBox.Show(pv + "-"+ codigo+": " + importe); } } catch (Exception msg) { // something went wrong, and you wanna know why LogHelper.Log(LogTarget.File, DateTime.Now.ToString() + " - ERROR: " + msg.Message + " - " + msg.StackTrace); //MessageBox.Show(msg.ToString()); throw; } if (id != 0) { //consultar Afip string respuesta = consultarAfip(pv, codigo, importe, iva, total, fecha, doc, concepto, tipo_doc, tipo_iva, moneda); /* * for (int i = 0; i <= 10000; i++) * { * for (int j = 0; j <= 1000; j++) * { * for (int k = 0; k <= 1000; k++) * { * k++; * } * } * }*/ //string respuesta = "respuesta"; if (respuesta != "ERROR") { //Escribir registro BD if (resultado != "R") { MetodosBD.modificarComprobante(id, 2, respuesta); } else { MetodosBD.modificarComprobante(id, 3, respuesta); } } } }