Ejemplo n.º 1
0
        protected async Task <T> PostRequestAsync <T>(WePayRequest <T> Request,
                                                      string wePayEndPoint,
                                                      string accessToken,
                                                      bool?useStaging) where T : WePayResponse
        {
            StringContent httpContent = null;

            if (EnableValidation)
            {
                string message = GetExceptionMessage(Request, accessToken ?? AccessToken);

                if (message != null)
                {
                    throw new Exception(message);
                }
            }

            if (Request != null)
            {
                var json = await Task.Run(() => JsonConvert.SerializeObject(Request, Formatting, JsonSerializerSettings));

                httpContent = new StringContent(json, Encoding.UTF8, JsonMediaType);
            }

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
            ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return(true); };

            using (var httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken ?? AccessToken ?? "");
                httpClient.DefaultRequestHeaders.Add(UserAgentHeaderKey, UserAgentHeaderValue);

                HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(GenerateWePayEndPointUrl(wePayEndPoint, useStaging), httpContent);

                JObject response = (JObject)JsonConvert.DeserializeObject(await httpResponseMessage.Content.ReadAsStringAsync(), JsonSerializerSettings);

                if ((int?)response[ErrorCodeKey] != null)
                {
                    throw GetExceptionFromErrorResponse(response);
                }
                else
                {
                    return(response.ToObject <T>(JsonSerializer));
                }
            }
        }
Ejemplo n.º 2
0
        protected string GetExceptionMessage <T>(WePayRequest <T> Request,
                                                 string accessToken) where T : WePayResponse
        {
            string message = null;

            if (accessToken == null || accessToken == "")
            {
                message = Request.GetType().FullName + " requires an Access Token.";
            }

            if (Request != null)
            {
                string subMessage = Request.ValidateRequest();

                if (subMessage != null)
                {
                    message = (message == null ? "" : message + "\n") + subMessage;
                }
            }

            return(message);
        }