public PayPalResponse CallApi(string method, string path, string accessToken, PayPalApiSettingsBase settings, string data) { var isJson = (data.HasValue() && data.StartsWith("{")); var encoding = (isJson ? Encoding.UTF8 : Encoding.ASCII); var result = new PayPalResponse(); HttpWebResponse webResponse = null; var url = GetApiUrl(settings.UseSandbox) + path.EnsureStartsWith("/"); if (method.IsCaseInsensitiveEqual("GET") && data.HasValue()) { url = url.EnsureEndsWith("?") + data; } if (settings.SecurityProtocol.HasValue) { ServicePointManager.SecurityProtocol = settings.SecurityProtocol.Value; } var request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; request.Accept = "application/json"; request.ContentType = (isJson ? "application/json" : "application/x-www-form-urlencoded"); try { if (HttpContext.Current != null && HttpContext.Current.Request != null) { request.UserAgent = HttpContext.Current.Request.UserAgent; } else { request.UserAgent = Plugin.SystemName; } } catch { } if (path.EmptyNull().EndsWith("/token")) { // see https://github.com/paypal/sdk-core-dotnet/blob/master/Source/SDK/OAuthTokenCredential.cs byte[] credentials = Encoding.UTF8.GetBytes("{0}:{1}".FormatInvariant(settings.ClientId, settings.Secret)); request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(credentials)); } else { request.Headers["Authorization"] = "Bearer " + accessToken.EmptyNull(); } if (data.HasValue() && (method.IsCaseInsensitiveEqual("POST") || method.IsCaseInsensitiveEqual("PUT") || method.IsCaseInsensitiveEqual("PATCH"))) { byte[] bytes = encoding.GetBytes(data); request.ContentLength = bytes.Length; using (var stream = request.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); } } else { request.ContentLength = 0; } try { webResponse = request.GetResponse() as HttpWebResponse; result.Success = ((int)webResponse.StatusCode < 400); } catch (WebException wexc) { result.Success = false; result.ErrorMessage = wexc.ToString(); webResponse = wexc.Response as HttpWebResponse; } catch (Exception exception) { result.Success = false; result.ErrorMessage = exception.ToString(); LogError(exception); } try { if (webResponse != null) { using (var reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { var rawResponse = reader.ReadToEnd(); if (rawResponse.HasValue()) { if (webResponse.ContentType.IsCaseInsensitiveEqual("application/json")) { if (rawResponse.StartsWith("[")) { result.Json = JArray.Parse(rawResponse); } else { result.Json = JObject.Parse(rawResponse); } if (result.Json != null) { if (!result.Success) { var name = (string)result.Json.name; var message = (string)result.Json.message; if (name.IsEmpty()) { name = (string)result.Json.error; } if (message.IsEmpty()) { message = (string)result.Json.error_description; } result.ErrorMessage = "{0} ({1}).".FormatInvariant(message.NaIfEmpty(), name.NaIfEmpty()); } } } else if (!result.Success) { result.ErrorMessage = rawResponse; } } } if (!result.Success) { if (result.ErrorMessage.IsEmpty()) { result.ErrorMessage = webResponse.StatusDescription; } LogError(null, result.ErrorMessage, result.Json == null ? null : result.Json.ToString(), false); } } } catch (Exception exception) { LogError(exception); } finally { if (webResponse != null) { webResponse.Close(); webResponse.Dispose(); } } return(result); }
public PayPalResponse CallApi(string method, string path, string accessToken, PayPalApiSettingsBase settings, string data) { var isJson = (data.HasValue() && data.StartsWith("{")); var encoding = (isJson ? Encoding.UTF8 : Encoding.ASCII); var result = new PayPalResponse(); HttpWebResponse webResponse = null; var url = GetApiUrl(settings.UseSandbox) + path.EnsureStartsWith("/"); if (method.IsCaseInsensitiveEqual("GET") && data.HasValue()) url = url.EnsureEndsWith("?") + data; if (settings.SecurityProtocol.HasValue) ServicePointManager.SecurityProtocol = settings.SecurityProtocol.Value; var request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; request.Accept = "application/json"; request.ContentType = (isJson ? "application/json" : "application/x-www-form-urlencoded"); try { if (HttpContext.Current != null && HttpContext.Current.Request != null) request.UserAgent = HttpContext.Current.Request.UserAgent; else request.UserAgent = Plugin.SystemName; } catch { } if (path.EmptyNull().EndsWith("/token")) { // see https://github.com/paypal/sdk-core-dotnet/blob/master/Source/SDK/OAuthTokenCredential.cs byte[] credentials = Encoding.UTF8.GetBytes("{0}:{1}".FormatInvariant(settings.ClientId, settings.Secret)); request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(credentials)); } else { request.Headers["Authorization"] = "Bearer " + accessToken.EmptyNull(); } if (data.HasValue() && (method.IsCaseInsensitiveEqual("POST") || method.IsCaseInsensitiveEqual("PUT") || method.IsCaseInsensitiveEqual("PATCH"))) { byte[] bytes = encoding.GetBytes(data); request.ContentLength = bytes.Length; using (var stream = request.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); } } else { request.ContentLength = 0; } try { webResponse = request.GetResponse() as HttpWebResponse; result.Success = ((int)webResponse.StatusCode < 400); } catch (WebException wexc) { result.Success = false; result.ErrorMessage = wexc.ToString(); webResponse = wexc.Response as HttpWebResponse; } catch (Exception exception) { result.Success = false; result.ErrorMessage = exception.ToString(); LogError(exception); } try { if (webResponse != null) { using (var reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { var rawResponse = reader.ReadToEnd(); if (rawResponse.HasValue()) { if (webResponse.ContentType.IsCaseInsensitiveEqual("application/json")) { if (rawResponse.StartsWith("[")) result.Json = JArray.Parse(rawResponse); else result.Json = JObject.Parse(rawResponse); if (result.Json != null) { if (!result.Success) { var name = (string)result.Json.name; var message = (string)result.Json.message; if (name.IsEmpty()) name = (string)result.Json.error; if (message.IsEmpty()) message = (string)result.Json.error_description; result.ErrorMessage = "{0} ({1}).".FormatInvariant(message.NaIfEmpty(), name.NaIfEmpty()); } } } else if (!result.Success) { result.ErrorMessage = rawResponse; } } } if (!result.Success) { if (result.ErrorMessage.IsEmpty()) result.ErrorMessage = webResponse.StatusDescription; LogError(null, result.ErrorMessage, result.Json == null ? null : result.Json.ToString(), false); } } } catch (Exception exception) { LogError(exception); } finally { if (webResponse != null) { webResponse.Close(); webResponse.Dispose(); } } return result; }