public FirmadoResponse FirmadoResponse(FirmadoRequest request) { var response = new FirmadoResponse(); try { var serializador = new Serializador { RutaCertificadoDigital = request.CertificadoDigital, PasswordCertificado = request.PasswordCertificado, TipoDocumento = request.UnSoloNodoExtension ? 0 : 1 }; response.TramaXmlFirmado = serializador.FirmarXml(request.TramaXmlSinFirma); response.ResumenFirma = serializador.DigestValue; response.ValorFirma = serializador.ValorFirma; response.Exito = true; } catch (Exception ex) { response.MensajeError = ex.Message; response.Pila = ex.StackTrace; response.Exito = false; } return(response); }
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); } }
private void kryptonButton3_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; default: codigoTipoDoc = "01"; break; } if (cboTipoDoc.SelectedIndex == -1) { MessageBox.Show("Seleccione Tipo de documento"); throw new InvalidOperationException("Seleccione Tipo de documento"); } /* if (_documento.TipoDocumento != codigoTipoDoc) { * MessageBox.Show("Seleccione Tipo de Documento Igual al Documento Generado..!"); * throw new InvalidOperationException("Seleccione Tipo de Documento Igual al Documento Generado..!"); * }*/ 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 }; FirmarController enviar = new FirmarController(); var respuestaFirmado = enviar.FirmadoResponse(firmadoRequest); if (!respuestaFirmado.Exito) { throw new ApplicationException(respuestaFirmado.MensajeError); } var enviarDocumentoRequest = new EnviarDocumentoRequest { Ruc = txtNroRuc.Text, UsuarioSol = txtUsuarioSol.Text, ClaveSol = txtClaveSol.Text, EndPointUrl = ValorSeleccionado(), //https://www.sunat.gob.pe/ol-ti-itcpgem-sqa/billService //RETENCION IdDocumento = txtSerieCorrelativo.Text, TipoDocumento = codigoTipoDoc, TramaXmlFirmado = respuestaFirmado.TramaXmlFirmado }; // RespuestaComun respuestaEnvio; var respuestaEnvio = new EnviarDocumentoResponse(); var apiMetodo = rbResumen.Checked ? "EnviarResumen" : "EnviarDocumento"; if (!rbResumen.Checked) { if (apiMetodo == "EnviarDocumento") { EnviarDocumentoController enviarDoc = new EnviarDocumentoController(); respuestaEnvio = enviarDoc.EnviarDocumentoResponse(enviarDocumentoRequest); } // var rpta =new EnviarDocumentoResponse() ;//(EnviarDocumentoResponse)respuestaEnvio; var rpta = (EnviarDocumentoResponse)respuestaEnvio; txtResult.Text = $@"{Resources.procesoCorrecto}{Environment.NewLine}{rpta.MensajeRespuesta} siendo las {DateTime.Now}"; try { //ACTUALIZA CORRELATIVO SI EL DOCUMENTO FUE ACEPTADO if (_documento.Items != null && _documento.Receptor.NroDocumento != "") { clsNumeracion busnum = new clsNumeracion(); busnum = AdmNumeracion.BuscaNumeracion(_documento.TipoDocumento); Numeracion.TipoDocumento = Convert.ToString(_documento.TipoDocumento); Numeracion.Numeracion = busnum.Numeracion + 1; if (!AdmNumeracion.ActualizaNumeracion(Numeracion)) { MessageBox.Show("Ocurrió un Error al Actualizar la Numeración"); } } if (rpta.Exito && !string.IsNullOrEmpty(rpta.TramaZipCdr)) { File.WriteAllBytes($"{Program.CarpetaXml}\\{rpta.NombreArchivo}.xml", Convert.FromBase64String(respuestaFirmado.TramaXmlFirmado)); File.WriteAllBytes($"{Program.CarpetaCdr}\\R-{rpta.NombreArchivo}.zip", Convert.FromBase64String(rpta.TramaZipCdr)); _documento.FirmaDigital = respuestaFirmado.ValorFirma; } } catch (Exception ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { if (apiMetodo == "EnviarResumen") { EnviarResumenController enviaResumen = new EnviarResumenController(); respuestaEnvio = enviaResumen.EnviarResumenResponse(enviarDocumentoRequest); } var rpta = (EnviarDocumentoResponse)respuestaEnvio; txtResult.Text = $@"{Resources.procesoCorrecto}{Environment.NewLine}{rpta.NroTicket}"; if (rpta.Exito) { if (codigoTipoDoc == "RA") { clsNumeracion busnum = new clsNumeracion(); busnum = AdmNumeracion.BuscaNumeracion(codigoTipoDoc); Numeracion.TipoDocumento = Convert.ToString(codigoTipoDoc); Numeracion.Numeracion = busnum.Numeracion + 1; if (!AdmNumeracion.ActualizaNumeracion(Numeracion)) { MessageBox.Show("Ocurrió un Error al Actualizar la Numeración"); } } } } if (!respuestaEnvio.Exito) { throw new ApplicationException(respuestaEnvio.MensajeError); } } catch (Exception ex) { txtResult.Text = ex.Message; } finally { Cursor = Cursors.Default; } }