PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/App_Code/Account/MPD-Service.cs

https://github.com/mailekah/AgapeConnect1
C# | 274 lines | 203 code | 62 blank | 9 comment | 13 complexity | 1774a556b94d3d6875fde05217b9fa58 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Net;
  6. using System.IO;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. /// <summary>
  10. /// Summary description for MPD_Service
  11. /// </summary>
  12. public static class MPD_Service
  13. {
  14. //static string serviceURL = "https://staffweb.cru.org:443/ss/servlet/TntMPDServlet/";
  15. public struct Donation
  16. {
  17. public int account{get;set;}
  18. public int PeopleId{ get; set; }
  19. public string DonorName { get; set; }
  20. public DateTime DonationDate { get; set; }
  21. public string DonationId { get; set; }
  22. public string Payment_Method { get; set; }
  23. public double Amount { get; set; }
  24. public string FiscalPeriod { get; set; }
  25. public string MonthName { get; set; }
  26. }
  27. public static double? GetAccountBalance(string Username, string Password, string serviceURL, string Action)
  28. {
  29. try
  30. {
  31. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceURL);
  32. request.Method = "POST";
  33. request.ContentType = "application/x-www-form-urlencoded";
  34. StringBuilder data = new StringBuilder();
  35. data.Append("Username=" + HttpUtility.UrlEncode(Username));
  36. data.Append("&Password=" + HttpUtility.UrlEncode(Password));
  37. data.Append("&Action=" + HttpUtility.UrlEncode(Action));
  38. data.Append("&TextError=y");
  39. request.ContentLength = data.Length;
  40. StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
  41. requestWriter.Write(data);
  42. requestWriter.Close();
  43. WebResponse webResponse = request.GetResponse();
  44. if (!webResponse.ContentType.Contains("csv")) return null;
  45. Stream webStream = webResponse.GetResponseStream();
  46. StreamReader responseReader = new StreamReader(webStream);
  47. string response = responseReader.ReadToEnd();
  48. responseReader.Close();
  49. CSVHelper csv = new CSVHelper(response, ",");
  50. bool first = true;
  51. double rtn = 0.0;
  52. if (csv.Count <= 1) return null;
  53. foreach (string[] line in csv)
  54. {
  55. if (first) first = false;
  56. else
  57. {
  58. rtn += double.Parse(line[1]);
  59. }
  60. }
  61. return rtn;
  62. }
  63. catch (Exception)
  64. {
  65. return null;
  66. }
  67. }
  68. public static List<Donation> getDonations(string Username, string Password, string serviceURL, string Action, DateTime DateFrom, DateTime DateTo )
  69. {
  70. List<Donation> donations = new List<Donation>();
  71. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceURL);
  72. request.Method = "POST";
  73. request.ContentType = "application/x-www-form-urlencoded";
  74. StringBuilder data = new StringBuilder();
  75. data.Append("Username=" + HttpUtility.UrlEncode(Username));
  76. data.Append("&Password=" + HttpUtility.UrlEncode(Password));
  77. data.Append("&Action=" + HttpUtility.UrlEncode(Action));
  78. data.Append("&DateFrom=" + HttpUtility.UrlEncode(DateFrom.ToString("M/d/yyyy")));
  79. data.Append("&DateTo=" + HttpUtility.UrlEncode(DateTo.ToString("M/d/yyyy")));
  80. data.Append("&Order=Date");
  81. request.ContentLength = data.Length;
  82. StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
  83. requestWriter.Write(data);
  84. requestWriter.Close();
  85. try
  86. {
  87. WebResponse webResponse = request.GetResponse();
  88. if (!webResponse.ContentType.Contains("csv"))
  89. {
  90. Donation rtn = new Donation();
  91. rtn.DonorName = "Authentication/Connection Error. <a href=\"javascript: $('#divAddCountry').dialog('open');\">Click here</a> to reauthenticate.";
  92. rtn.PeopleId = -1;
  93. StaffBrokerFunctions.EventLog("MPD-ServiceError downloading Transaction for " + Username, rtn.DonorName, 1);
  94. donations.Add(rtn);
  95. return donations;
  96. }
  97. Stream webStream = webResponse.GetResponseStream();
  98. StreamReader responseReader = new StreamReader(webStream);
  99. string response = responseReader.ReadToEnd();
  100. //Console.Out.WriteLine(response);
  101. responseReader.Close();
  102. CSVHelper csv = new CSVHelper(response,",");
  103. bool first = true;
  104. foreach(string[] line in csv)
  105. {
  106. if (first) first = false;
  107. else
  108. {
  109. try
  110. {
  111. Donation don = new Donation();
  112. don.account = int.Parse(line[0]);
  113. don.PeopleId = int.Parse(line[1]);
  114. don.DonorName = line[2].Replace("\"", "");
  115. IFormatProvider culture = new System.Globalization.CultureInfo("en-US");
  116. don.DonationDate = DateTime.Parse(line[3], culture);
  117. don.DonationId = line[4];
  118. don.Payment_Method = line[6];
  119. try
  120. {
  121. don.Amount = double.Parse(line[10], new System.Globalization.CultureInfo(""));
  122. }
  123. catch (Exception)
  124. {
  125. don.Amount = double.Parse(line[9], new System.Globalization.CultureInfo(""));
  126. }
  127. don.FiscalPeriod = don.DonationDate.ToString("yyyyMM");
  128. don.MonthName = don.DonationDate.ToString("MMM yy");
  129. donations.Add(don);
  130. }
  131. catch (Exception e)
  132. {
  133. Donation rtn = new Donation();
  134. StaffBrokerFunctions.EventLog("MPD-Service Error downloading a donation for " + Username, e.Message, 1);
  135. rtn.DonorName = "There was an error downloading one or more of your donations: this list may not be complete.";
  136. rtn.PeopleId = -3;
  137. donations.Add(rtn);
  138. }
  139. }
  140. }
  141. return donations;
  142. }
  143. catch (Exception e)
  144. {
  145. Donation rtn = new Donation();
  146. StaffBrokerFunctions.EventLog("MPD-Service Error Downloading Donations for " + Username, e.Message, 1);
  147. rtn.DonorName = "There was an error downloading your transations from the remote server.";
  148. rtn.PeopleId = -2;
  149. donations.Add(rtn);
  150. return donations;
  151. }
  152. }
  153. }
  154. public class CSVHelper : List<string[]>
  155. {
  156. protected string csv = string.Empty;
  157. protected string separator = ",";
  158. public CSVHelper(string csv, string separator = "\",\"")
  159. {
  160. this.csv = csv;
  161. this.separator = separator;
  162. var objEventLog = new DotNetNuke.Services.Log.EventLog.EventLogController();
  163. var PS = (DotNetNuke.Entities.Portals.PortalSettings)HttpContext.Current.Items["PortalSettings"];
  164. foreach (string line in Regex.Split(csv, System.Environment.NewLine).ToList().Where(s => !string.IsNullOrEmpty(s)))
  165. {
  166. string[] values = SplitCSV(line).ToArray();
  167. //string str = "";
  168. for (int i = 0; i < values.Length; i++)
  169. {
  170. //Trim values
  171. values[i] = values[i].Trim(',').Trim('\"');
  172. // str += "Value[" + i + "]: " + values[i];
  173. }
  174. // objEventLog.AddLog("Account",values[9], PS, 0, DotNetNuke.Services.Log.EventLog.EventLogController.EventLogType.ADMIN_ALERT);
  175. this.Add(values);
  176. }
  177. }
  178. public static IEnumerable<string> SplitCSV(string csvString)
  179. {
  180. var sb = new StringBuilder();
  181. bool quoted = false;
  182. foreach (char c in csvString)
  183. {
  184. if (quoted)
  185. {
  186. if (c == '"')
  187. quoted = false;
  188. else
  189. sb.Append(c);
  190. }
  191. else
  192. {
  193. if (c == '"')
  194. {
  195. quoted = true;
  196. }
  197. else if (c == ',')
  198. {
  199. yield return sb.ToString();
  200. sb.Length = 0;
  201. }
  202. else
  203. {
  204. sb.Append(c);
  205. }
  206. }
  207. }
  208. if (quoted)
  209. throw new ArgumentException("csvString", "Unterminated quotation mark.");
  210. yield return sb.ToString();
  211. }
  212. }