示例#1
0
        /// <summary>
        /// Constructor de la Clase ConexionSunat
        /// </summary>
        /// <param name="endPointServicio">Nombre del Endpoint</param>
        /// <param name="MS">Objeto MainSettings - Contiene ruc, user y pass de Sunat</param>
        public SunatConnection(string endPointServicio, MainSettings MS)
        {
            System.Net.ServicePointManager.UseNagleAlgorithm = true;
            System.Net.ServicePointManager.Expect100Continue = false;
            System.Net.ServicePointManager.CheckCertificateRevocationList = true;
            //System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(IgnoreCertificateErrorHandler);

            this.MS = MS;
            MS.GeneraLog.EnvioSunat01("URL: " + MS.UEnv);
            if (MS.Envi == "DEV")
            {
                EndPointServicio = "DEV" + endPointServicio;
                //string urldev = @"https://e-beta.sunat.gob.pe:443/ol-ti-itcpfegem-beta/billService";
                //string urldev = @"https://www.sunat.gob.pe:443/ol-ti-itcpfegem/billService";
                _proxyDEV = new SunatDEV.billServiceClient(EndPointServicio, MS.UEnv);
                // Agregamos el behavior configurado para soportar WS-Security.
                var behavior = new PasswordDigestBehavior(string.Concat(MS.RucNumber, MS.SunatUsr), MS.SunatPwd);
                _proxyDEV.Endpoint.EndpointBehaviors.Add(behavior);
            }
            if (MS.Envi == "HML")
            {
                EndPointServicio = "HML" + endPointServicio;
                _proxyPRD        = new SunatPRD.billServiceClient(EndPointServicio, MS.UEnv);
                // Agregamos el behavior configurado para soportar WS-Security.

                //var behavior = new PasswordDigestBehavior(string.Concat(MS.UserAltern.Length == 11 ? MS.UserAltern : MS.RucNumber, MS.SunatUsr), MS.SunatPwd);
                var behavior = new PasswordDigestBehavior(string.Concat(MS.RucNumber, MS.SunatUsr), MS.SunatPwd);
                _proxyPRD.Endpoint.EndpointBehaviors.Add(behavior);
            }
            if (MS.Envi == "PRD")
            {
                //if ("01|03|RC|RA".Contains(MS.DocumentType))
                //{
                EndPointServicio = "PRD" + endPointServicio;
                _proxyPRD        = new SunatPRD.billServiceClient(EndPointServicio, MS.UEnv);
                // Agregamos el behavior configurado para soportar WS-Security.
                //var behavior = new PasswordDigestBehavior(string.Concat(MS.RucNumber, MS.SunatUsr), MS.SunatPwd);
                var behavior = new PasswordDigestBehavior(string.Concat(MS.UserAltern.Length == 11 ? MS.UserAltern : MS.RucNumber, MS.SunatUsr), MS.SunatPwd);
                _proxyPRD.Endpoint.EndpointBehaviors.Add(behavior);
                //}

                //if ("20|40".Contains(MS.DocumentType))
                //{
                //    EndPointServicio = "PRD" + endPointServicio;
                //    _proxyPRDr = new SunatPRDr.billServiceClient(EndPointServicio);
                //    //_proxyPRDr.ClientCredentials.CreateSecurityTokenManager();
                //    //_proxyPRDr.ClientCredentials.UserName.UserName = string.Concat(MS.RucNumber, MS.SunatUsr);
                //    //_proxyPRDr.ClientCredentials.UserName.Password = MS.SunatPwd;

                //    // Agregamos el behavior configurado para soportar WS-Security.
                //    var behavior = new PasswordDigestBehavior(string.Concat(MS.RucNumber, MS.SunatUsr), MS.SunatPwd);
                //    _proxyPRDr.Endpoint.EndpointBehaviors.Add(behavior);
                //}
            }
        }
示例#2
0
        /// <summary>
        /// Enviar documento ZIP al WS Sunat
        /// </summary>
        /// <param name="tramaArchivo">Cadena Base64 de la trama del archivo</param>
        /// <param name="nombreArchivo">Nombre del archivo (sin extension)</param>
        /// <returns>Devuelve una tupla con la cadena Base64 del ZIP de respuesta (CDR) y un booleano si SUNAT responde correctamente</returns>
        public Tuple <string, bool> EnviarDocumento(byte[] zipBytes, string nombreArchivo)
        {
            Tuple <string, bool> response = null;

            try
            {
                switch (MS.Envi)
                {
                case "DEV":
                    MS.GeneraLog.EnvioSunat01($"Se enviará el documento en ambiemte {MS.Envi} - Pruebas");
                    _proxyDEV.Open();
                    var resDEV = _proxyDEV.sendBill(nombreArchivo, zipBytes);
                    _proxyDEV.Close();
                    MS.GeneraLog.EnvioSunat01($"Documento enviado correctamente");
                    response = new Tuple <string, bool>(Convert.ToBase64String(resDEV), true);
                    break;

                case "HML":
                    MS.GeneraLog.EnvioSunat01($"Se enviará el documento en ambiemte {MS.Envi} - Homologación");
                    _proxyPRD.Open();
                    var resHML = _proxyPRD.sendBill(nombreArchivo, zipBytes);
                    _proxyPRD.Close();
                    MS.GeneraLog.EnvioSunat01($"Documento enviado correctamente");
                    response = new Tuple <string, bool>(Convert.ToBase64String(resHML), true);
                    break;

                case "PRD":
                    //if("01|03|RC|RA".Contains(MS.DocumentType))
                    //{
                    MS.GeneraLog.EnvioSunat01($"Se enviará el documento en ambiemte {MS.Envi} - Produccion");
                    _proxyPRD.Open();
                    var resPRD = _proxyPRD.sendBill(nombreArchivo, zipBytes);
                    _proxyPRD.Close();
                    MS.GeneraLog.EnvioSunat01($"Documento enviado correctamente");
                    response = new Tuple <string, bool>(Convert.ToBase64String(resPRD), true);
                    //}
                    //if("20|40".Contains(MS.DocumentType))
                    //{
                    //    MS.GeneraLog.EnvioSunat01($"Se enviará el documento en ambiemte {MS.Envi} - Produccion");
                    //    _proxyPRDr.Open();
                    //    var resPRDr = _proxyPRDr.sendBill(nombreArchivo, zipBytes);
                    //    _proxyPRDr.Close();
                    //    MS.GeneraLog.EnvioSunat01($"Documento enviado correctamente");
                    //    response = new Tuple<string, bool>(Convert.ToBase64String(resPRDr), true);
                    //}
                    break;

                default:
                    response = new Tuple <string, bool>("No se ha definido el ambiente de envío", false);
                    break;
                }
            }
            catch (FaultException ex)
            {
                response = new Tuple <string, bool>(EndPointServicio.Contains("Documento")
                    ? ex.Code.Name : ex.Message, false);

                if (response.Item1.Contains("1032") || response.Item1.Contains("1033"))
                {
                    MS.GeneraLog.EnvioSunat01($"El Documento ya ha sido enviado a Sunat anteriormente");
                    MS.GeneraLog.EnvioSunat01($"Obteniendo estado y CDR del documento");
                    _proxyCDR = new SunatCDR.billServiceClient("CDRConsulta", MS.UCdr);

                    var behavior = new PasswordDigestBehavior(string.Concat(MS.RucNumber, MS.SunatUsr), MS.SunatPwd);
                    _proxyCDR.Endpoint.EndpointBehaviors.Add(behavior);

                    _proxyCDR.Open();

                    string[] div = nombreArchivo.Replace(".zip", "").Split('-');

                    var resCDR = _proxyCDR.getStatusCdr(MS.RucNumber, div[1], div[2], int.Parse(div[3].Substring(0, 8)));

                    _proxyCDR.Close();
                    response = new Tuple <string, bool>(Convert.ToBase64String(resCDR.content), true);
                    //response = new Tuple<string, bool>(resCDR.statusCode, true);
                }
                else
                {
                    MS.GeneraLog.EnvioSunat01($"Ocurrió un error en el envío: {response.Item1}");
                }
            }
            catch (Exception ex)
            {
                var msg = string.Concat(ex.InnerException.Message ?? string.Empty, ex.Message);

                var faultCode = "<faultcode>";
                if (msg.Contains(faultCode))
                {
                    var posicion    = msg.IndexOf(faultCode, StringComparison.Ordinal);
                    var codigoError = msg.Substring(posicion + faultCode.Length, 4);
                    msg = string.Format("El Código de Error es {0}", codigoError);
                }
                MS.GeneraLog.EnvioSunat01(msg);
                response = new Tuple <string, bool>(msg, false);
            }
            return(response);
        }