PageRenderTime 6ms CodeModel.GetById 14ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

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