예제 #1
0
    public static string TimbrarNotaCredito(string Id, string Token, Dictionary <string, object> NotaCredito, string RFC, string RefID, string NoCertificado, string Formato, string Correos, string RutaCFDI)
    {
        JObject      Respuesta   = new JObject();
        string       Xml         = "";
        CNotaCredito notaCredito = new CNotaCredito();;

        try { notaCredito = GenerarNotaCredito(NotaCredito); }
        catch (Exception ex)
        { }

        Xml = NotaCreditoXML.XML(notaCredito);

        // Save file XML in
        System.IO.Directory.CreateDirectory(@"" + RutaCFDI + @"\NotaCredito\in\" + RFC);
        System.IO.File.WriteAllText(@"" + RutaCFDI + @"\NotaCredito\in\" + RFC + @"\" + RefID + ".xml", Xml);
        //return Xml;
        string encode = Base64.Encode(Xml);

        JObject Request      = new JObject();
        JObject Credenciales = new JObject();
        JObject Issuer       = new JObject();
        JObject Receivers    = new JObject();
        JObject Document     = new JObject();

        Credenciales.Add("id", Id);
        Credenciales.Add("token", Token);

        Issuer.Add("rfc", RFC);

        Document.Add("ref-id", RefID);
        Document.Add("certificate-number", NoCertificado);
        Document.Add("section", "all");
        Document.Add("format", Formato);
        Document.Add("template", "letter");
        Document.Add("type", "application/vnd.diverza.cfdi_3.3+xml");
        Document.Add("content", encode);

        Request.Add("credentials", Credenciales);
        Request.Add("issuer", Issuer);
        Request.Add("receiver", Conector.ObtenerDestinatarios(Correos.Split(',').ToList()));
        Request.Add("document", Document);
        string response = Conector.NotaCredito(Request);
        //return response.ToString();
        Dictionary <string, object> Response = (Dictionary <string, object>)JSON.Parse(response);
        //return Response.ToString();
        string uuid    = "";
        string ref_id  = "";
        string content = "";
        string message = "Error en el timbrado";
        int    Error   = 0;

        if (!Response.ContainsKey("message"))
        {
            try
            {
                message = "";

                uuid    = (Response.ContainsKey("uuid")) ? (string)Response["uuid"] : "";
                ref_id  = (Response.ContainsKey("uuid")) ? (string)Response["ref_id"] : "";
                content = (Response.ContainsKey("content")) ? (string)Response["content"] : "";

                // Save file ZIP out
                GuardarContenido(RutaCFDI, content, notaCredito.Receptor.RFC, notaCredito.Serie, notaCredito.Folio);
            }
            catch (Exception ex)
            {
                message = ex.Message;
                Error   = 1;
            }
        }
        else
        {
            message = (string)Response["message"];
            Error   = 1;
        }

        Respuesta.Add("Error", Error);
        Respuesta.Add("message", message);
        Respuesta.Add("uuid", uuid);
        Respuesta.Add("ref_id", ref_id);
        Respuesta.Add("certificado", NoCertificado);
        Respuesta.Add("content", content);
        Respuesta.Add("request", Request);
        Respuesta.Add("response", response);

        return(Respuesta.ToString());
    }