internal static OpenpayException GetFromJSON(HttpStatusCode code, string json) { OpenpayException result = JsonConvert.DeserializeObject <OpenpayException>(json); result.StatusCode = code; return(result); }
protected virtual string DoRequest(string path, HttpMethod method, string body) { string result = null; string endpoint = APIEndpoint + MerchantId + path; Console.WriteLine("Request to: " + endpoint); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; WebRequest req = SetupRequest(method.ToString(), endpoint); if (body != null) { byte[] bytes = encoding.GetBytes(body.ToString()); req.ContentLength = bytes.Length; using (Stream st = req.GetRequestStream()) { st.Write(bytes, 0, bytes.Length); } } try { using (WebResponse resp = (WebResponse)req.GetResponse()) { result = GetResponseAsString(resp); } } catch (WebException wexc) { if (wexc.Response != null) { string json_error = GetResponseAsString(wexc.Response); HttpStatusCode status_code = HttpStatusCode.BadRequest; HttpWebResponse resp = wexc.Response as HttpWebResponse; if (resp != null) { status_code = resp.StatusCode; } if ((int)status_code <= 500) { throw OpenpayException.GetFromJSON(status_code, json_error); } } throw; } return(result); }