Esempio n. 1
0
        public void Procesa(string xmlFactura)
        {
            if (!this.txtFolderSalida.Text.EndsWith("\\"))
            {
                this.txtFolderSalida.Text += "\\";
            }

            string directorio    = this.txtFolderSalida.Text;
            string nombreArchivo = this.txtConsecutivo.Text;

            XmlDocument xmlDocSF = new XmlDocument();

            xmlDocSF.LoadXml(xmlFactura);
            xmlDocSF.Save((directorio + (nombreArchivo + "_01_SF.xml")));
            XmlTextWriter xmlTextWriter = new XmlTextWriter((directorio + (nombreArchivo + "_01_SF.xml")), new System.Text.UTF8Encoding(false));

            xmlDocSF.WriteTo(xmlTextWriter);
            xmlTextWriter.Close();
            xmlDocSF = null;

            Firma _firma = new Firma();

            _firma.FirmaXML_Xades((directorio + nombreArchivo), this.txtPathCertificado.Text, this.txtCertificadoPIN.Text);

            XmlDocument xmlElectronica = new XmlDocument();

            xmlElectronica.Load((directorio + (nombreArchivo + "_02_Firmado.xml")));

            this.txtXMLFirmado.Text = xmlElectronica.OuterXml;

            Emisor myEmisor = new Emisor();

            myEmisor.numeroIdentificacion = this.txtEmisorNumero.Text;
            myEmisor.TipoIdentificacion   = this.txtEmisorTipo.Text;

            Receptor myReceptor = new Receptor();

            if ((this.txtReceptorNumero.Text.Trim().Length > 0))
            {
                myReceptor.sinReceptor          = false;
                myReceptor.numeroIdentificacion = this.txtReceptorNumero.Text;
                myReceptor.TipoIdentificacion   = this.txtReceptorTipo.Text;
            }
            else
            {
                myReceptor.sinReceptor = true;
            }

            Recepcion myRecepcion = new Recepcion();

            myRecepcion.emisor   = myEmisor;
            myRecepcion.receptor = myReceptor;

            myRecepcion.clave          = this.txtClave.Text;
            myRecepcion.fecha          = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz");
            myRecepcion.comprobanteXml = Funciones.EncodeStrToBase64(xmlElectronica.OuterXml);

            xmlElectronica = null;

            string Token = "";

            Token = getToken();
            this.txtTokenHacienda.Text = Token;

            Comunicacion enviaFactura = new Comunicacion();

            enviaFactura.EnvioDatos(Token, myRecepcion);
            string jsonEnvio = "";

            jsonEnvio = enviaFactura.jsonEnvio;
            string jsonRespuesta = "";

            jsonRespuesta         = enviaFactura.jsonRespuesta;
            txtJSONEnvio.Text     = jsonEnvio;
            txtJSONRespuesta.Text = jsonRespuesta;
            System.IO.StreamWriter outputFile = new System.IO.StreamWriter((directorio
                                                                            + (nombreArchivo + "_03_jsonEnvio.txt")));
            outputFile.Write(jsonEnvio);
            outputFile.Close();
            outputFile = new System.IO.StreamWriter((directorio
                                                     + (nombreArchivo + "_04_jsonRespuesta.txt")));
            outputFile.Write(jsonRespuesta);
            outputFile.Close();
            if (!(enviaFactura.xmlRespuesta == null))
            {
                this.txtRespuestaHacienda.Text = enviaFactura.xmlRespuesta.OuterXml;
                enviaFactura.xmlRespuesta.Save((directorio
                                                + (nombreArchivo + "_05_RESP.xml")));
            }
            else
            {
                outputFile = new System.IO.StreamWriter((directorio
                                                         + (nombreArchivo + "_05_RESP_SinRespuesta.txt")));
                outputFile.Write("");
                outputFile.Close();
                this.txtRespuestaHacienda.Text = "Consulte en unos minutos, factura se est� procesando.";
                this.txtRespuestaHacienda.Text = (this.txtRespuestaHacienda.Text + ("\r\n" + ("\r\n" + "Consulte por Clave")));
                this.txtRespuestaHacienda.Text = (this.txtRespuestaHacienda.Text + ("\r\n" + ("\r\n" + enviaFactura.mensajeRespuesta)));
            }
            MessageBox.Show(enviaFactura.mensajeRespuesta);
        }
Esempio n. 2
0
        public async void EnvioDatos(string TK, Recepcion objRecepcion)
        {
            try
            {
                string URL_RECEPCION = "https://api.comprobanteselectronicos.go.cr/recepcion-sandbox/v1/";

                HttpClient http = new HttpClient();



                Newtonsoft.Json.Linq.JObject JsonObject = new Newtonsoft.Json.Linq.JObject();
                JsonObject.Add(new JProperty("clave", objRecepcion.clave));
                JsonObject.Add(new JProperty("fecha", objRecepcion.fecha));
                JsonObject.Add(new JProperty("emisor",
                                             new JObject(new JProperty("tipoIdentificacion", objRecepcion.emisor.TipoIdentificacion),
                                                         new JProperty("numeroIdentificacion", objRecepcion.emisor.numeroIdentificacion))));

                if ((objRecepcion.receptor.sinReceptor) == false)
                {
                    JsonObject.Add(new JProperty("receptor",
                                                 new JObject(new JProperty("tipoIdentificacion", objRecepcion.receptor.TipoIdentificacion),
                                                             new JProperty("numeroIdentificacion", objRecepcion.receptor.numeroIdentificacion))));
                }

                JsonObject.Add(new JProperty("comprobanteXml", objRecepcion.comprobanteXml));

                jsonEnvio = JsonObject.ToString();

                StringContent oString = new StringContent(JsonObject.ToString());

                http.DefaultRequestHeaders.Add("authorization", ("Bearer " + TK));

                HttpResponseMessage response = http.PostAsync((URL_RECEPCION + "recepcion"), oString).Result;
                string res = await response.Content.ReadAsStringAsync();

                object Localizacion = response.StatusCode;
                // mensajeRespuesta = Localizacion

                http = new HttpClient();
                http.DefaultRequestHeaders.Add("authorization", ("Bearer " + TK));
                response = http.GetAsync((URL_RECEPCION + ("recepcion/" + objRecepcion.clave))).Result;
                res      = await response.Content.ReadAsStringAsync();

                jsonRespuesta = res.ToString();

                RespuestaHacienda RH = Newtonsoft.Json.JsonConvert.DeserializeObject <RespuestaHacienda>(res);

                if ((RH.respuesta_xml != ""))
                {
                    xmlRespuesta = Funciones.DecodeBase64ToXML(RH.respuesta_xml);
                }

                try { }
                catch { }

                estadoFactura = RH.ind_estado;
                statusCode    = response.StatusCode.ToString();

                mensajeRespuesta = ("Confirmación: " + (statusCode + "\r\n"));
                mensajeRespuesta = (mensajeRespuesta + ("Estado: " + estadoFactura));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());

                MessageBox.Show("En el momento de enviar el documento a Hacienda ocurrio un error.");
            }
        }