private async void BtnFirmar_Click(object sender, EventArgs e) { try { //_documento.IdDocumento = "F001-00000001"; //Probar if (string.IsNullOrEmpty(_documento.IdDocumento)) { throw new InvalidOperationException("La Serie y el Correlativo no pueden estar vacíos"); } var tramaXmlSinFirma = Convert.ToBase64String(File.ReadAllBytes(RutaArchivo)); //Original //var tramaXmlSinFirma = Convert.ToBase64String(File.ReadAllBytes(@"D:\Valle\XML_SF\F001-00000001.xml")); var firmadoRequest = new FirmadoRequest { TramaXmlSinFirma = tramaXmlSinFirma, CertificadoDigital = Convert.ToBase64String(File.ReadAllBytes(@"E:\Certificado_Luna_Verde.pfx")), //@"E:\Valle\certificado\CertificadoPFX.pfx" PasswordCertificado = "pXnC3uUsdf7852Rz", UnSoloNodoExtension = false //rbRetenciones.Checked || rbResumen.Checked }; ICertificador certificador = new Certificador(); var respuestaFirmado = await new Firmar(certificador).Post(firmadoRequest); _documento.ResumenFirma = respuestaFirmado.ResumenFirma; //Firma para ser usada en el PDF si es necesario _documento.FirmaDigital = respuestaFirmado.ValorFirma; // No olvidar agregar las referencias iTextSharp para generar los PDF y las firmas if (!respuestaFirmado.Exito) { throw new ApplicationException(respuestaFirmado.MensajeError); } RutaArchivo = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{_documento.Emisor.NroDocumento+"-"+_documento.IdDocumento}.xml"); File.WriteAllBytes(RutaArchivo, Convert.FromBase64String(respuestaFirmado.TramaXmlFirmado)); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { Cursor = Cursors.Default; } }
private async void BtnFirmar_Click(object sender, EventArgs e) { try { //_documento.IdDocumento = "F001-00000001"; //Probar if (string.IsNullOrEmpty(_documento.IdDocumento)) { throw new InvalidOperationException("La Serie y el Correlativo no pueden estar vacíos"); } var tramaXmlSinFirma = Convert.ToBase64String(File.ReadAllBytes(RutaArchivo)); //Original //var tramaXmlSinFirma = Convert.ToBase64String(File.ReadAllBytes(@"D:\Valle\XML_SF\F001-00000001.xml")); var firmadoRequest = new FirmadoRequest { TramaXmlSinFirma = tramaXmlSinFirma, CertificadoDigital = Convert.ToBase64String(File.ReadAllBytes(@"D:\Valle\certificado\cervallespot.pfx")), PasswordCertificado = "#1DEFRDE32W", UnSoloNodoExtension = false //rbRetenciones.Checked || rbResumen.Checked }; ICertificador certificador = new Certificador(); var respuestaFirmado = await new Firmar(certificador).Post(firmadoRequest); if (!respuestaFirmado.Exito) { throw new ApplicationException(respuestaFirmado.MensajeError); } RutaArchivo = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"CF_{_documento.IdDocumento}.xml"); File.WriteAllBytes(RutaArchivo, Convert.FromBase64String(respuestaFirmado.TramaXmlFirmado)); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { Cursor = Cursors.Default; } }
private async void BtnGen_Click(object sender, EventArgs e) { try { Cursor = Cursors.WaitCursor; string codigoTipoDoc; switch (cboTipoDoc.SelectedIndex) { case 0: codigoTipoDoc = "01"; break; case 1: codigoTipoDoc = "03"; break; case 2: codigoTipoDoc = "07"; break; case 3: codigoTipoDoc = "08"; break; case 4: codigoTipoDoc = "20"; break; case 5: codigoTipoDoc = "40"; break; case 6: codigoTipoDoc = "RC"; break; case 7: codigoTipoDoc = "RA"; break; case 8: codigoTipoDoc = "09"; break; default: codigoTipoDoc = "01"; break; } if (string.IsNullOrEmpty(txtSerieCorrelativo.Text)) { throw new InvalidOperationException("La Serie y el Correlativo no pueden estar vacíos"); } var tramaXmlSinFirma = Convert.ToBase64String(File.ReadAllBytes(txtSource.Text)); var firmadoRequest = new FirmadoRequest { TramaXmlSinFirma = tramaXmlSinFirma, CertificadoDigital = Convert.ToBase64String(File.ReadAllBytes(txtRutaCertificado.Text)), PasswordCertificado = txtPassCertificado.Text, UnSoloNodoExtension = rbRetenciones.Checked || rbResumen.Checked }; ICertificador certificador = new Certificador(); var respuestaFirmado = await new Firmar(certificador).Post(firmadoRequest); if (!respuestaFirmado.Exito) { throw new ApplicationException(respuestaFirmado.MensajeError); } var enviarDocumentoRequest = new EnviarDocumentoRequest { Ruc = txtNroRuc.Text, UsuarioSol = txtUsuarioSol.Text, ClaveSol = txtClaveSol.Text, EndPointUrl = ValorSeleccionado(), IdDocumento = txtSerieCorrelativo.Text, TipoDocumento = codigoTipoDoc, TramaXmlFirmado = respuestaFirmado.TramaXmlFirmado }; ISerializador serializador = new Serializador(); IServicioSunatDocumentos servicioSunatDocumentos = new ServicioSunatDocumentos(); RespuestaComunConArchivo respuestaEnvio; if (rbResumen.Checked && codigoTipoDoc != "09") { respuestaEnvio = await new EnviarResumen(serializador, servicioSunatDocumentos).Post(enviarDocumentoRequest); } else { respuestaEnvio = await new EnviarDocumento(serializador, servicioSunatDocumentos).Post(enviarDocumentoRequest); } var rpta = (EnviarDocumentoResponse)respuestaEnvio; if (!rbResumen.Checked) { txtResult.Text = $@"{Resources.procesoCorrecto}{Environment.NewLine}{rpta.MensajeRespuesta} siendo las {DateTime.Now}"; try { if (rpta.Exito && !string.IsNullOrEmpty(rpta.TramaZipCdr)) { File.WriteAllBytes($"{Program.CarpetaXml}\\{respuestaEnvio.NombreArchivo}.xml", Convert.FromBase64String(respuestaFirmado.TramaXmlFirmado)); File.WriteAllBytes($"{Program.CarpetaCdr}\\R-{respuestaEnvio.NombreArchivo}.zip", Convert.FromBase64String(rpta.TramaZipCdr)); } } catch (Exception ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { var rptas = (EnviarResumenResponse)respuestaEnvio; txtResult.Text = $@"{Resources.procesoCorrecto}{Environment.NewLine}{rptas.NroTicket}"; } if (!respuestaEnvio.Exito) { throw new ApplicationException(respuestaEnvio.MensajeError); } if (chkVoz.Checked) { Hablar(); } } catch (Exception ex) { txtResult.Text = ex.Message; } finally { Cursor = Cursors.Default; } }
public Firmar() { Certificador certificador = new Certificador(); _certificador = (ICertificador)certificador; }
public async void enviar_documentos() { try { byte[] archivo_xml = null; byte[] archivo_cdr = null; int enviados = 0; byte[] archivo_pdf = null; foreach (DataGridViewRow row in dg_repositorio.Rows) { //if (!archivo_existe(row.Cells[rutaxml.Index].Value.ToString())) //{ descargar_xml(row.Index, 0); descargar_pdf(row.Index, 0); //} var tramaXmlSinFirma = (File.Exists(dg_repositorio.Rows[row.Index].Cells[rutaxml.Index].Value.ToString())) ? Convert.ToBase64String(File.ReadAllBytes(row.Cells[rutaxml.Index].Value.ToString())) : Convert.ToBase64String(File.ReadAllBytes(herramienta.GetResourcesPath5() + "\\" + dg_repositorio.Rows[row.Index].Cells[nombredocxml.Index].Value.ToString())); var firmadoRequest = new FirmadoRequest { TramaXmlSinFirma = tramaXmlSinFirma, CertificadoDigital = Convert.ToBase64String(File.ReadAllBytes(herramienta.GetResourcesPath() + "\\" + empresa.Rutacertificado)), PasswordCertificado = empresa.Clavecertificado, UnSoloNodoExtension = false }; ICertificador certificador = new Certificador(); var respuestaFirmado = await new Firmar(certificador).Post(firmadoRequest); if (!respuestaFirmado.Exito) { MessageBox.Show(respuestaFirmado.MensajeError); } var enviarDocumentoRequest = new EnviarDocumentoRequest { Ruc = empresa.Ruc, UsuarioSol = empresa.Usuariosol, ClaveSol = empresa.Clavesol, EndPointUrl = empresa.Urlenvio, //https://www.sunat.gob.pe/ol-ti-itcpgem-sqa/billService //RETENCION IdDocumento = row.Cells[comprobante.Index].Value.ToString(), TipoDocumento = row.Cells[tipocomprobante.Index].Value.ToString(), TramaXmlFirmado = respuestaFirmado.TramaXmlFirmado }; /*===================================*/ ISerializador serializador = new Serializador(); IServicioSunatDocumentos servicioSunatDocumentos = new ServicioSunatDocumentos(); RespuestaComunConArchivo respuestaEnvio; respuestaEnvio = await new EnviarDocumento(serializador, servicioSunatDocumentos).Post(enviarDocumentoRequest); var rpta = (EnviarDocumentoResponse)respuestaEnvio; if (!respuestaEnvio.Exito) { MessageBox.Show(respuestaEnvio.MensajeError); } /*==============================*/ if (rpta != null) { repositorio = new clsRepositorio(); repositorio.Repositorioid = Convert.ToInt32(row.Cells[repositorioid.Index].Value.ToString()); archivo_xml = Convert.FromBase64String(respuestaFirmado.TramaXmlFirmado); File.WriteAllBytes($"{Program.CarpetaXml}\\{rpta.NombreArchivo}.xml", archivo_xml); if (rpta.Exito) { if (rpta.MensajeRespuesta != null) { if (rpta.MensajeRespuesta.IndexOf("1033") > 0) { repositorio.Estadosunat = 0; repositorio.Mensajesunat = rpta.MensajeRespuesta; } else { if (rpta.MensajeRespuesta.IndexOf("ha sido aceptada") > 0) { repositorio.Estadosunat = 0; repositorio.Mensajesunat = rpta.MensajeRespuesta; } else { repositorio.Estadosunat = -1; repositorio.Mensajesunat = rpta.MensajeRespuesta; } } } else { repositorio.Estadosunat = -1; repositorio.Mensajesunat = "No Enviado"; } if (!string.IsNullOrEmpty(rpta.TramaZipCdr)) { archivo_cdr = Convert.FromBase64String(rpta.TramaZipCdr); File.WriteAllBytes($"{Program.CarpetaCdr}\\R-{rpta.NombreArchivo}.zip", archivo_cdr); } if (File.Exists($"{Program.CarpetaXml}\\{rpta.NombreArchivo}.xml") && File.Exists($"{Program.CarpetaCdr}\\R-{rpta.NombreArchivo}.zip")) { switch (row.Cells[tipocomprobante.Index].Value.ToString()) { case "03": archivo_pdf = (File.Exists(dg_repositorio.Rows[row.Index].Cells[rutapdf.Index].Value.ToString())) ? File.ReadAllBytes(row.Cells[rutapdf.Index].Value.ToString()) : File.ReadAllBytes(herramienta.GetResourcesPath6() + "\\" + row.Cells[nombredocpdf.Index].Value.ToString()); break; case "01": archivo_pdf = (File.Exists(dg_repositorio.Rows[row.Index].Cells[rutapdf.Index].Value.ToString())) ? File.ReadAllBytes(row.Cells[rutapdf.Index].Value.ToString()) : File.ReadAllBytes(herramienta.GetResourcesPath3() + "\\" + row.Cells[nombredocpdf.Index].Value.ToString()); break; case "07": archivo_pdf = (File.Exists(dg_repositorio.Rows[row.Index].Cells[rutapdf.Index].Value.ToString())) ? File.ReadAllBytes(row.Cells[rutapdf.Index].Value.ToString()) : File.ReadAllBytes(herramienta.GetResourcesPath7() + "\\" + row.Cells[nombredocpdf.Index].Value.ToString()); break; case "08": archivo_pdf = (File.Exists(dg_repositorio.Rows[row.Index].Cells[rutapdf.Index].Value.ToString())) ? File.ReadAllBytes(row.Cells[rutapdf.Index].Value.ToString()) : File.ReadAllBytes(herramienta.GetResourcesPath8() + "\\" + row.Cells[nombredocpdf.Index].Value.ToString()); break; } if (!string.IsNullOrEmpty(rpta.TramaZipCdr)) { archivo = new clsArchivo() { Xml = File.ReadAllBytes($"{Program.CarpetaXml}\\{rpta.NombreArchivo}.xml"), Zip = File.ReadAllBytes($"{Program.CarpetaCdr}\\R-{rpta.NombreArchivo}.zip"), Pdf = archivo_pdf }; repositorio.Archivo = archivo; repositorio.Pcorigen = SystemInformation.UserDomainName; repositorio.Usuariopc = SystemInformation.UserName; if (admrepo.actualizar_repositorio(repositorio) < 0) { MessageBox.Show("Problemas para actualizar información el proceso de detuvo...", "Información"); break; } } else { archivo = new clsArchivo() { Xml = File.ReadAllBytes($"{Program.CarpetaXml}\\{rpta.NombreArchivo}.xml"), Pdf = archivo_pdf }; repositorio.Archivo = archivo; repositorio.Pcorigen = SystemInformation.UserDomainName; repositorio.Usuariopc = SystemInformation.UserName; if (admrepo.actualizar_repositorio(repositorio) < 0) { MessageBox.Show("Problemas para actualizar información el proceso de detuvo...", "Información"); break; } } } else { switch (row.Cells[tipocomprobante.Index].Value.ToString()) { case "03": archivo_pdf = (File.Exists(dg_repositorio.Rows[row.Index].Cells[rutapdf.Index].Value.ToString())) ? File.ReadAllBytes(row.Cells[rutapdf.Index].Value.ToString()) : File.ReadAllBytes(herramienta.GetResourcesPath6() + "\\" + row.Cells[nombredocpdf.Index].Value.ToString()); break; case "01": archivo_pdf = (File.Exists(dg_repositorio.Rows[row.Index].Cells[rutapdf.Index].Value.ToString())) ? File.ReadAllBytes(row.Cells[rutapdf.Index].Value.ToString()) : File.ReadAllBytes(herramienta.GetResourcesPath3() + "\\" + row.Cells[nombredocpdf.Index].Value.ToString()); break; case "07": archivo_pdf = (File.Exists(dg_repositorio.Rows[row.Index].Cells[rutapdf.Index].Value.ToString())) ? File.ReadAllBytes(row.Cells[rutapdf.Index].Value.ToString()) : File.ReadAllBytes(herramienta.GetResourcesPath7() + "\\" + row.Cells[nombredocpdf.Index].Value.ToString()); break; case "08": archivo_pdf = (File.Exists(dg_repositorio.Rows[row.Index].Cells[rutapdf.Index].Value.ToString())) ? File.ReadAllBytes(row.Cells[rutapdf.Index].Value.ToString()) : File.ReadAllBytes(herramienta.GetResourcesPath8() + "\\" + row.Cells[nombredocpdf.Index].Value.ToString()); break; } archivo = new clsArchivo() { Xml = File.ReadAllBytes($"{Program.CarpetaXml}\\{rpta.NombreArchivo}.xml"), Pdf = archivo_pdf }; repositorio.Archivo = archivo; repositorio.Pcorigen = SystemInformation.UserDomainName; repositorio.Usuariopc = SystemInformation.UserName; if (admrepo.actualizar_repositorio(repositorio) < 0) { MessageBox.Show("Problemas para actualizar información el proceso de detuvo...", "Información"); break; } } } else { switch (row.Cells[tipocomprobante.Index].Value.ToString()) { case "03": archivo_pdf = (File.Exists(dg_repositorio.Rows[row.Index].Cells[rutapdf.Index].Value.ToString())) ? File.ReadAllBytes(row.Cells[rutapdf.Index].Value.ToString()) : File.ReadAllBytes(herramienta.GetResourcesPath6() + "\\" + row.Cells[nombredocpdf.Index].Value.ToString()); break; case "01": archivo_pdf = (File.Exists(dg_repositorio.Rows[row.Index].Cells[rutapdf.Index].Value.ToString())) ? File.ReadAllBytes(row.Cells[rutapdf.Index].Value.ToString()) : File.ReadAllBytes(herramienta.GetResourcesPath3() + "\\" + row.Cells[nombredocpdf.Index].Value.ToString()); break; case "07": archivo_pdf = (File.Exists(dg_repositorio.Rows[row.Index].Cells[rutapdf.Index].Value.ToString())) ? File.ReadAllBytes(row.Cells[rutapdf.Index].Value.ToString()) : File.ReadAllBytes(herramienta.GetResourcesPath7() + "\\" + row.Cells[nombredocpdf.Index].Value.ToString()); break; case "08": archivo_pdf = (File.Exists(dg_repositorio.Rows[row.Index].Cells[rutapdf.Index].Value.ToString())) ? File.ReadAllBytes(row.Cells[rutapdf.Index].Value.ToString()) : File.ReadAllBytes(herramienta.GetResourcesPath8() + "\\" + row.Cells[nombredocpdf.Index].Value.ToString()); break; } archivo = new clsArchivo() { Xml = File.ReadAllBytes($"{Program.CarpetaXml}\\{rpta.NombreArchivo}.xml"), Pdf = archivo_pdf }; if (rpta.MensajeRespuesta != null) { if (rpta.MensajeRespuesta.IndexOf("1033") > 0) { repositorio.Estadosunat = 0; repositorio.Mensajesunat = rpta.MensajeRespuesta; } else { enviados++; repositorio.Estadosunat = -1; repositorio.Mensajesunat = rpta.MensajeRespuesta; } } else { repositorio.Estadosunat = -1; repositorio.Mensajesunat = "No Enviado"; enviados++; } repositorio.Archivo = archivo; repositorio.Pcorigen = SystemInformation.UserDomainName; repositorio.Usuariopc = SystemInformation.UserName; if (admrepo.actualizar_repositorio(repositorio) < 0) { MessageBox.Show("Problemas para actualizar información el proceso de detuvo...", "Información"); break; } } } else { enviados++; } } if (enviados > 0) { MessageBox.Show("No todos los documentos se enviaron...", "Información"); btn_buscar.PerformClick(); } else { MessageBox.Show("Todos los documentos se enviaron...", "Información"); btn_buscar.PerformClick(); } } catch (Exception a) { MessageBox.Show(a.Message); } }