Ejemplo n.º 1
0
        public static string Request(string urlSuffix, string method, string type, Object args = null)
        {
            HttpWebRequest    Request;
            string            Url         = string.Concat(TwoCheckoutConfig.BaseUrl, urlSuffix);
            string            Params      = null;
            string            Result      = null;
            string            ContentType = null;
            HttpWebResponse   Response    = null;
            NetworkCredential Credential  = null;

            if (type == "admin")
            {
                ContentType = (method == "POST") ? "application/x-www-form-urlencoded" : "*/*";
                Params      = (args != null) ? TwoCheckoutUtil.SerializeObjectToQueryString(args) : null;
                Url         = (method == "GET") ? string.Concat(Url, "?", Params) : Url;
                Credential  = new NetworkCredential(TwoCheckoutConfig.ApiUsername, TwoCheckoutConfig.ApiPassword);
            }
            else
            {
                ContentType = "application/json";
                Params      = TwoCheckoutUtil.ConvertToJson(args);
            }

            try
            {
                Request = WebRequest.Create(Url.ToString()) as HttpWebRequest;
                ServicePointManager.ServerCertificateValidationCallback += delegate { return(true); };
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
                Request.Credentials = Credential;
                Request.Method      = method;
                Request.ContentType = ContentType;
                Request.Accept      = "application/json";
                Request.UserAgent   = "2Checkout .NET/" + TwoCheckoutConfig.Version;
                if (method == "POST" && Params != "")
                {
                    byte[] byteData = UTF8Encoding.UTF8.GetBytes(Params);
                    Request.ContentLength = byteData.Length;
                    using (Stream postStream = Request.GetRequestStream())
                    {
                        postStream.Write(byteData, 0, byteData.Length);
                    }
                }
                using (Response = Request.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(Response.GetResponseStream());
                    Result = reader.ReadToEnd();
                    return(Result);
                }
            }
            catch (WebException wex)
            {
                string ResultCode = null;
                if (wex.Response != null)
                {
                    using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)
                    {
                        StreamReader reader = new StreamReader(errorResponse.GetResponseStream());
                        Result = reader.ReadToEnd();
                        JObject RawError = JObject.Parse(Result);
                        if (RawError["errors"] != null)
                        {
                            Result     = RawError["errors"][0]["message"].ToString();
                            ResultCode = RawError["errors"][0]["code"].ToString();
                        }
                        else if (RawError["exception"] != null)
                        {
                            Result     = RawError["exception"]["errorMsg"].ToString();
                            ResultCode = RawError["exception"]["errorCode"].ToString();
                        }
                    }
                }
                else
                {
                    Result     = wex.Message;
                    ResultCode = "500";
                }
                throw new TwoCheckoutException(Result)
                      {
                          Code = ResultCode
                      };
            }
            finally
            {
                if (Response != null)
                {
                    Response.Close();
                }
            }
        }