internal SkrillAuth(HttpResponse response) { Success = true; if (response.StatusCode != 200) SetError("Error connecting to Skrill payment endpoint.", string.Format("Additional information: {0}", response.ToString())); else { string sessionHeader = response.Headers .Where(x => x.Key.Contains("Set-Cookie") && x.Value.Contains("SESSION_ID")) .Select(x => x.Value) .FirstOrDefault(); string statusHeader = response.Headers .Where(x => x.Key == "X-skrill-status") .Select(x => x.Value) .FirstOrDefault(); if(statusHeader != null && statusHeader.Contains("error")) SetError(statusHeader); if (sessionHeader == null) SetError("SESSION_ID error.", "Set-Cookie header could not be found in response."); var match = Regex.Match(sessionHeader, @"(?<=SESSION_ID=).*(?=;\spath=)"); if (!match.Success) SetError("SESSION_ID not found.", "Request did not yield the SESSION_ID."); else { Token = match.Value; CheckoutUrl = string.Format("{0}?sid={1}", response.Host, Token); } } }
internal WePayAuth(HttpResponse response) { var jsonResponse = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(response.Response); var error = jsonResponse.Keys.Where(x => x.Contains("error")).FirstOrDefault(); if (error == null) { CheckoutUrl = jsonResponse["checkout_uri"].ToString(); Token = jsonResponse["checkout_id"].ToString(); Success = true; } else { Error = new WePayError(); Error.ShortMessage = jsonResponse["error"].ToString(); Error.Message = jsonResponse["error_description"].ToString(); Error.Code = Convert.ToInt32(jsonResponse["error_code"]); Success = false; } }
internal static HttpResponse Post(string url, NameValueCollection requestData, ContentType contentEncode = ContentType.FormData, NameValueCollection headers = null) { HttpResponse responseData = new HttpResponse(); HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url); httpRequest.Timeout = Timeout; httpRequest.Method = "POST"; httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; string postData = string.Empty; if (headers != null) httpRequest.Headers.Add(headers); switch(contentEncode) { case ContentType.Json: httpRequest.ContentType = "application/json"; var dictionary = requestData .AllKeys .ToDictionary(k => k, k => requestData[k]); postData = new JavaScriptSerializer().Serialize(dictionary); break; case ContentType.FormData: httpRequest.ContentType = "application/x-www-form-urlencoded"; postData = requestData.ToString(); break; default: postData = requestData.ToString(); break; } httpRequest.ContentLength = postData.Length; try { using (StreamWriter myWriter = new StreamWriter(httpRequest.GetRequestStream())) { myWriter.Write(postData); } } catch (Exception ex) { throw ex; } HttpWebResponse httpResponse; try { httpResponse = (HttpWebResponse)httpRequest.GetResponse(); } catch (WebException ex) { httpResponse = (HttpWebResponse)ex.Response; } using (StreamReader sr = new StreamReader(httpResponse.GetResponseStream())) { responseData.Response = sr.ReadToEnd(); } var keys = httpResponse.Headers.AllKeys; foreach(var key in keys) { var values = httpResponse.Headers.GetValues(key); if (values.Length > 1) { for (int i = 0; i < values.Length; i++) responseData.Headers.Add(string.Format("{0}-{1}", key, i), values[i]); continue; } responseData.Headers.Add(key, values.FirstOrDefault()); } responseData.StatusCode = (int)httpResponse.StatusCode; responseData.StatusDescription = httpResponse.StatusDescription; responseData.Host = url; return responseData; }