PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/CmsWeb/Areas/OnlineReg/Models/OnlineReg/Transaction.cs

https://bitbucket.org/rbhakser/bvcms
C# | 318 lines | 297 code | 21 blank | 0 comment | 38 complexity | 0c3443627aa092a8343c2d80c78a32b4 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0, BSD-3-Clause, AGPL-3.0, MPL-2.0-no-copyleft-exception
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data.Linq;
  5. using System.Web;
  6. using CmsData;
  7. using System.Web.Mvc;
  8. using System.Text;
  9. using System.Configuration;
  10. using UtilityExtensions;
  11. using System.Data.Linq.SqlClient;
  12. using System.Net.Mail;
  13. using System.Text.RegularExpressions;
  14. using System.Collections;
  15. using System.Runtime.Serialization;
  16. using System.Net;
  17. using System.IO;
  18. namespace CmsWeb.Models
  19. {
  20. public partial class OnlineRegModel
  21. {
  22. public static string GetTransactionGateway()
  23. {
  24. return DbUtil.Db.Setting("TransactionGateway", "serviceu").ToLower();
  25. }
  26. public decimal PayAmount()
  27. {
  28. var amt = List.Sum(p => p.AmountToPay());
  29. var max = List.Max(p => p.org != null ? p.setting.MaximumFee ?? 0 : 0);
  30. if (max == 0)
  31. return amt;
  32. var totalother = List.Sum(p => p.TotalOther());
  33. if (List.Any(p => p.setting.ApplyMaxToOtherFees) && amt > max)
  34. amt = max;
  35. else if ((amt - totalother) > max)
  36. amt = max + totalother;
  37. return amt;
  38. }
  39. public decimal TotalAmount()
  40. {
  41. var amt = List.Sum(p => p.TotalAmount());
  42. var max = List.Max(p => p.org != null ? p.setting.MaximumFee ?? 0 : 0);
  43. if (max == 0)
  44. return amt;
  45. var totalother = List.Sum(p => p.TotalOther());
  46. if (List.Any(p => p.setting.ApplyMaxToOtherFees) && amt > max)
  47. amt = max;
  48. else if ((amt - totalother) > max)
  49. amt = max + totalother;
  50. return amt;
  51. }
  52. public class TransactionInfo
  53. {
  54. public string First { get; set; }
  55. public string Last { get; set; }
  56. public string Email { get; set; }
  57. public string Middle { get; set; }
  58. public string Suffix { get; set; }
  59. public string Address { get; set; }
  60. public string City { get; set; }
  61. public string State { get; set; }
  62. public string Zip { get; set; }
  63. public string Phone { get; set; }
  64. public PaymentInfo payinfo { get; set; }
  65. }
  66. public TransactionInfo GetTransactionInfo()
  67. {
  68. var p = List[0];
  69. var pp = p.person;
  70. var r = new TransactionInfo();
  71. if (user != null && GetTransactionGateway() == "sage" && pp != null)
  72. r.payinfo = pp.PaymentInfos.FirstOrDefault();
  73. if (r.payinfo == null)
  74. r.payinfo = new PaymentInfo { MaskedAccount = "", MaskedCard = "" };
  75. if (user != null)
  76. {
  77. pp = user;
  78. r.First = pp.FirstName;
  79. r.Last = pp.LastName;
  80. r.Middle = pp.MiddleName.Truncate(1);
  81. r.Email = pp.EmailAddress;
  82. r.Suffix = pp.SuffixCode;
  83. r.Phone = (pp.HomePhone ?? pp.CellPhone).FmtFone();
  84. }
  85. else
  86. {
  87. r.Email = p.email;
  88. r.Suffix = p.suffix;
  89. r.First = p.first;
  90. r.Middle = p.middle;
  91. r.Last = p.last;
  92. r.Suffix = p.suffix;
  93. r.Phone = p.homephone ?? p.phone;
  94. }
  95. if (p.org != null && p.setting.AskVisible("AskParents"))
  96. {
  97. p.suffix = "";
  98. var a = (p.fname ?? p.mname).Trim().Split(' ');
  99. if (a.Length > 1)
  100. {
  101. r.First = a[0];
  102. r.Last = a[1];
  103. }
  104. else
  105. r.Last = a[0];
  106. }
  107. if (UserPeopleId.HasValue || p.IsNew)
  108. {
  109. if (pp == null)
  110. {
  111. r.Address = p.address.Truncate(50);
  112. r.City = p.city;
  113. r.State = p.state;
  114. r.Zip = p.zip;
  115. r.Phone = p.phone.FmtFone();
  116. }
  117. else
  118. {
  119. r.Address = pp.PrimaryAddress.Truncate(50);
  120. r.City = pp.PrimaryCity;
  121. r.State = pp.PrimaryState;
  122. r.Zip = pp.PrimaryZip;
  123. r.Phone = Util.PickFirst(pp.HomePhone, pp.CellPhone).FmtFone();
  124. }
  125. }
  126. return r;
  127. }
  128. public static TransactionResponse PostTransaction(
  129. string card,
  130. string ccv,
  131. string expdate,
  132. decimal amt,
  133. int tranid,
  134. string description,
  135. int PeopleId,
  136. string email,
  137. string first,
  138. string last,
  139. string addr,
  140. string city,
  141. string state,
  142. string zip,
  143. bool testing)
  144. {
  145. string url = "https://secure.authorize.net/gateway/transact.dll";
  146. if (testing)
  147. url = "https://test.authorize.net/gateway/transact.dll";
  148. var p = new Dictionary<string, string>();
  149. p["x_delim_data"] = "TRUE";
  150. p["x_delim_char"] = "|";
  151. p["x_relay_response"] = "FALSE";
  152. p["x_type"] = "AUTH_CAPTURE";
  153. p["x_method"] = "CC";
  154. if (testing)
  155. {
  156. p["x_login"] = "9t8Pqzs4CW3S";
  157. p["x_tran_key"] = "9j33v58nuZB865WR";
  158. }
  159. else
  160. {
  161. p["x_login"] = DbUtil.Db.Setting("x_login", "");
  162. p["x_tran_key"] = DbUtil.Db.Setting("x_tran_key", "");
  163. }
  164. p["x_card_num"] = card;
  165. p["x_card_code"] = ccv;
  166. p["x_exp_date"] = expdate;
  167. p["x_amount"] = amt.ToString();
  168. p["x_description"] = description;
  169. p["x_invoice_num"] = tranid.ToString();
  170. p["x_cust_id"] = PeopleId.ToString();
  171. p["x_first_name"] = first;
  172. p["x_last_name"] = last;
  173. p["x_address"] = addr;
  174. p["x_city"] = city;
  175. p["x_state"] = state;
  176. p["x_zip"] = zip;
  177. p["x_email"] = email;
  178. var sb = new StringBuilder();
  179. foreach (var kv in p)
  180. sb.AppendFormat("{0}={1}&", kv.Key, HttpUtility.UrlEncode(kv.Value));
  181. sb.Length = sb.Length - 1;
  182. var wc = new WebClient();
  183. var req = WebRequest.Create(url);
  184. req.Method = "POST";
  185. req.ContentLength = sb.Length;
  186. req.ContentType = "application/x-www-form-urlencoded";
  187. var sw = new StreamWriter(req.GetRequestStream());
  188. sw.Write(sb.ToString());
  189. sw.Close();
  190. var r = req.GetResponse();
  191. using (var rs = new StreamReader(r.GetResponseStream()))
  192. {
  193. var resp = rs.ReadToEnd();
  194. rs.Close();
  195. var a = resp.Split('|');
  196. return new TransactionResponse
  197. {
  198. Approved = a[0] == "1",
  199. Message = a[3],
  200. AuthCode = a[4],
  201. TransactionId = a[6]
  202. };
  203. }
  204. }
  205. public static TransactionResponse PostECheck(
  206. string routing, string account,
  207. decimal amt, int tranid, string description,
  208. int PeopleId, string first, string last,
  209. string addr, string city, string state, string zip,
  210. bool testing)
  211. {
  212. string url = "https://secure.authorize.net/gateway/transact.dll";
  213. if (testing)
  214. url = "https://test.authorize.net/gateway/transact.dll";
  215. var p = new Dictionary<string, string>();
  216. p["x_delim_data"] = "TRUE";
  217. p["x_delim_char"] = "|";
  218. p["x_relay_response"] = "FALSE";
  219. p["x_method"] = "ECHECK";
  220. if (testing)
  221. {
  222. p["x_login"] = "9t8Pqzs4CW3S";
  223. p["x_tran_key"] = "9j33v58nuZB865WR";
  224. }
  225. else
  226. {
  227. p["x_login"] = DbUtil.Db.Setting("x_login", "");
  228. p["x_tran_key"] = DbUtil.Db.Setting("x_tran_key", "");
  229. }
  230. p["x_bank_aba_code"] = routing;
  231. p["x_bank_acct_num"] = account;
  232. p["x_bank_acct_type"] = "CHECKING";
  233. p["x_bank_acct_name"] = first + " " + last;
  234. p["x_echeck_type"] = "WEB";
  235. p["x_recurring_billing"] = "FALSE";
  236. p["x_amount"] = amt.ToString();
  237. p["x_description"] = description;
  238. p["x_invoice_num"] = tranid.ToString();
  239. p["x_cust_id"] = PeopleId.ToString();
  240. p["x_last_name"] = last;
  241. p["x_address"] = addr;
  242. p["x_city"] = city;
  243. p["x_state"] = state;
  244. p["x_zip"] = zip;
  245. var sb = new StringBuilder();
  246. foreach (var kv in p)
  247. sb.AppendFormat("{0}={1}&", kv.Key, HttpUtility.UrlEncode(kv.Value));
  248. sb.Length = sb.Length - 1;
  249. var wc = new WebClient();
  250. var req = WebRequest.Create(url);
  251. req.Method = "POST";
  252. req.ContentLength = sb.Length;
  253. req.ContentType = "application/x-www-form-urlencoded";
  254. var sw = new StreamWriter(req.GetRequestStream());
  255. sw.Write(sb.ToString());
  256. sw.Close();
  257. var r = req.GetResponse();
  258. using (var rs = new StreamReader(r.GetResponseStream()))
  259. {
  260. var resp = rs.ReadToEnd();
  261. rs.Close();
  262. var a = resp.Split('|');
  263. return new TransactionResponse
  264. {
  265. Approved = a[0] == "1",
  266. Message = a[3],
  267. AuthCode = a[4],
  268. TransactionId = a[6]
  269. };
  270. }
  271. }
  272. public static TransactionResponse PostTransactionSage(
  273. string card, string ccv, string expdate,
  274. decimal amt, int tranid, string description,
  275. int PeopleId, string email, string first, string middle, string last, string suffix,
  276. string addr, string city, string state, string zip, string phone,
  277. bool testing)
  278. {
  279. var t = new SagePayments(DbUtil.Db, testing);
  280. var resp = t.createTransactionRequest(PeopleId, amt, card, expdate, description, tranid, ccv,
  281. email, first, last, addr, city, state, zip, phone);
  282. return resp;
  283. }
  284. public static TransactionResponse PostVirtualCheckTransactionSage(
  285. string routing, string acct,
  286. decimal amt, int tranid, string description,
  287. int PeopleId, string email, string first, string middle, string last, string suffix,
  288. string addr, string city, string state, string zip, string phone,
  289. bool testing)
  290. {
  291. var t = new SagePayments(DbUtil.Db, testing);
  292. var resp = t.createCheckTransactionRequest(PeopleId, amt, routing, acct, description, tranid,
  293. email, first, middle, last, suffix, addr, city, state, zip, phone);
  294. return resp;
  295. }
  296. }
  297. }