//METODO PARA CREAR DOCUMENTOS ELECTRONICOS DE PRUEBA (Factura, NotaDebito , NotaCredito,TiqueteElectronico ) - NO ES NESESARIO DENTRO DEL PROYECTO public bool CrearDocumentoElectronico(ComprobanteElectronicoCRI objComprobanteElectronicoCRI, string tipoDocumento) { bool resultado = false; try { EngineDocumentoXml MetodoCrear = new EngineDocumentoXml(); XmlDocument documentoXml = new XmlDocument(); documentoXml = MetodoCrear.CrearDocumentoXml(objComprobanteElectronicoCRI, tipoDocumento); documentoXml.Save(@"C:\Users\Public\Documents\" + MetodoCrear.NombreDocumentoXml(documentoXml)); resultado = true; } catch (Exception ex) { throw new Exception("Error en metodo CrearDocumentoElectronico ", ex); } return(resultado); }
//METODO UTILIZADO PARA CREAR , FIRMAR Y ENVIAR EL MENSAJE RECEPTOR public async Task <string> EnviarDocumentoPostReceptor(string accessToken, string [] valoresDocumento, string mensaje, string detalleMensaje) { urlValidacion = string.Empty; string jsonDocumento = string.Empty; EngineDocumentoXml EngineDataXml = new EngineDocumentoXml(); DataMensajeReceptor objDataMensajeReceptor = new DataMensajeReceptor(); objDataMensajeReceptor = EngineDataXml.SetearMensajeReceptor(valoresDocumento, mensaje, detalleMensaje); XmlDocument documentoXml = new XmlDocument(); documentoXml = EngineDataXml.CrearDocumentoXmlReceptor(objDataMensajeReceptor); FirmaXadesEpes MetodoFirmar = new FirmaXadesEpes(); documentoXml = MetodoFirmar.XadesEpesFirma(documentoXml, Valor.PathCertificadoNeo(), Valor.PinCertificadoNeo()); CreadorComprobante ComprobanteXml = new CreadorComprobante(); jsonDocumento = ComprobanteXml.CadenaComprobante(valoresDocumento, documentoXml); HttpClient client = new HttpClient(); System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(EngineData.application_json)); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(EngineData.bearer, accessToken); HttpResponseMessage response = await client.PostAsync(Valor.UrlEnvioDocumento(), new StringContent(jsonDocumento, Encoding.UTF8, EngineData.application_json)); if (response.IsSuccessStatusCode) { urlValidacion = response.Headers.Location.ToString(); } else { urlValidacion = EngineData.falso; } return(urlValidacion); }
//METODO PARA CREAR , FIRMAR Y ENVIAR DOCUMENTOS ELECTRONICOS EMITIDOS (Factura, NotaDebito , NotaCredito,TiqueteElectronico ) public async Task <string> EnviarDocumentoPost(string accessToken, ComprobanteElectronicoCRI objComprobanteElectronicoCRI, string tipoDocumento) { urlValidacion = string.Empty; string jsonDocumento = string.Empty; EngineDocumentoXml MetodoCrear = new EngineDocumentoXml(); XmlDocument documentoXml = new XmlDocument(); documentoXml = MetodoCrear.CrearDocumentoXml(objComprobanteElectronicoCRI, tipoDocumento); FirmaXadesEpes MetodoFirmar = new FirmaXadesEpes(); documentoXml = MetodoFirmar.XadesEpesFirma(documentoXml, Valor.PathCertificadoNeo(), Valor.PinCertificadoNeo()); // DATOS DEL CERTIFICADO DEL EMISOR DEL DOCUMENTO //documentoXml = MetodoFirmar.XadesEpesFirma(documentoXml, Valor.PathCertificadoByron(), Valor.PinCertificadoByron()); CreadorComprobante ComprobanteXml = new CreadorComprobante(); string tipoEmisorNumero = Valor.TipoEmisor().Substring(Valor.TipoEmisor().Length - 2, 2);//Quito la palabra Item al TipoEmisor string tipoReceptorNumero = objComprobanteElectronicoCRI.ComprobanteElectronicoCRIEntidadJuridicaReceptor.TipoIdentificacion.Substring(objComprobanteElectronicoCRI.ComprobanteElectronicoCRIEntidadJuridicaReceptor.TipoIdentificacion.Length - 2, 2); jsonDocumento = ComprobanteXml.CadenaComprobante(objComprobanteElectronicoCRI.Clave, objComprobanteElectronicoCRI.NumeroConsecutivo, tipoEmisorNumero, Valor.NumeroIdentificacionEmisor(), tipoEmisorNumero, objComprobanteElectronicoCRI.ComprobanteElectronicoCRIEntidadJuridicaReceptor.Identificacion, documentoXml); HttpClient client = new HttpClient(); System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(EngineData.application_json)); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(EngineData.bearer, accessToken); HttpResponseMessage response = await client.PostAsync(Valor.UrlEnvioDocumento(), new StringContent(jsonDocumento, Encoding.UTF8, EngineData.application_json)); if (response.IsSuccessStatusCode) { urlValidacion = response.Headers.Location.ToString(); } else { urlValidacion = EngineData.falso; } return(urlValidacion); }
private void button4_Click(object sender, EventArgs e) { richTextBox1.Text = string.Empty; if (chkFactura.Checked) { tipoDocumento = "FacturaElectronica"; nombreArchivoXml = "FacturaElectronica.xml"; } else if (chkNotaDebito.Checked) { tipoDocumento = "NotaDebitoElectronica"; nombreArchivoXml = "NotaDebitoElectronica.xml"; } else if (chkNotaCredito.Checked) { tipoDocumento = "NotaCreditoElectronica"; nombreArchivoXml = "NotaCreditoElectronica.xml"; } else if (chkTicket.Checked) { tipoDocumento = "TiqueteElectronico"; nombreArchivoXml = "TiqueteElectronico.xml"; } else { MessageBox.Show("Debe elegir un tipo de documento"); return; } ComprobanteElectronicoCRI objComprobanteElectronicoCRI = new ComprobanteElectronicoCRI(); TributarioCr.EngineDocumentoXml Metodo = new TributarioCr.EngineDocumentoXml(); objComprobanteElectronicoCRI = Metodo.ObjComprobanteElectronicoCRI(tipoDocumento); if (ConsumoApi.CrearDocumentoElectronico(objComprobanteElectronicoCRI, tipoDocumento)) { MessageBox.Show("Documento " + tipoDocumento + " creado satisfactoriamente"); } else { MessageBox.Show("El documento " + tipoDocumento + " no pudo ser creado"); } }
private async void MetodoConsumo() { try { switch (paso) { case 1: tokenAccess = await ConsumoApi.ObtenerAutentificacionPost(); if (tokenAccess != string.Empty && tokenAccess != "false") { Mostrar(tokenAccess); paso++; timer1.Start(); } else { MessageBox.Show("ERROR OBTENIENDO AUTENTIFICACION"); } break; case 2: ComprobanteElectronicoCRI objComprobanteElectronicoCRI = new ComprobanteElectronicoCRI(); TributarioCr.EngineDocumentoXml Metodo = new TributarioCr.EngineDocumentoXml(); objComprobanteElectronicoCRI = Metodo.ObjComprobanteElectronicoCRI(tipoDocumento); urlValidacion = await ConsumoApi.EnviarDocumentoPost(tokenAccess, objComprobanteElectronicoCRI, tipoDocumento); if (urlValidacion != string.Empty && urlValidacion != "false") { Mostrar(urlValidacion); paso++; timer1.Start(); } else { MessageBox.Show("ERROR ENVIANDO DOCUMENTO"); } break; case 3: ConsumidorApi.Validacion validacion = new ConsumidorApi.Validacion(); validacion = await ConsumoApi.ValidarDocumentoGet(urlValidacion, tokenAccess); Mostrar(validacion.IndEstado); Mostrar(validacion.RespuestaXml); Mostrar(Comprobante.DecodeBase64(validacion.RespuestaXml)); paso++; timer1.Start(); break; case 4: desconexion = await ConsumoApi.DeconexionGet(tokenAccess); Mostrar(desconexion); paso = 0; timer1.Stop(); break; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }