public ActionResult Checkout() { Models.Checkout checkout = new Models.Checkout(); checkout.OrderList = new List <Models.OrderSummary>(); if (TempData["cartdata"] != null) { checkout = (Models.Checkout)TempData["cartdata"]; checkout.CVV = string.Empty; } try { if (Request.IsAuthenticated) { User user = _userRepository.Get(((BGBC.Core.CustomPrincipal)(User)).UserId); Profile profile = user.Profiles.FirstOrDefault(); checkout.FirstName = user.FirstName; checkout.LastName = user.LastName; checkout.Email = user.Email; checkout.BillingAddress = profile.BillingAddress; checkout.BillingAddress_2 = profile.BillingAddress_2; checkout.BillingCty = profile.BillingCty; checkout.BillingState = profile.BillingState; checkout.BillingZip = profile.BillingZip; checkout.Phone = profile.MobilePhone; UserCC ccinfo = _userCCRep.Get(((BGBC.Core.CustomPrincipal)(User)).UserId); if (ccinfo != null) { if (ccinfo.PaymentType == 1) { checkout.PaymentMethod = "Credit Card"; checkout.CardNo = Cryptography.Decrypt(ccinfo.CCNO); checkout.CardExpMon = Cryptography.Decrypt(ccinfo.ExpMon); checkout.CardExpYear = Cryptography.Decrypt(ccinfo.ExpYear); } else { checkout.PaymentMethod = "eCheck"; checkout.BankRoutingNumber = Cryptography.Decrypt(ccinfo.RoutingNo); checkout.BankAccountNumber = Cryptography.Decrypt(ccinfo.AccountNo); checkout.BankAccountType = ccinfo.AccountType; } checkout.SaveCard = true; } } Tuple <List <Models.OrderSummary>, decimal, string> ordersummary = getCartProducts(); checkout.OrderList = ordersummary.Item1; checkout.OrderTotal = ordersummary.Item2; checkout.ProductIds = ordersummary.Item3; } catch (Exception ex) { log.Error(ex.Message); } checkoutDropDown(); return(View(checkout)); }
public ActionResult Transaction() { Models.Checkout checkout = new Models.Checkout(); try { checkout = (Models.Checkout)TempData["cartdata"]; var lineItems = new AuthorizeNet.Api.Contracts.V1.lineItemType[checkout.OrderList.Count]; for (int i = 0; i < checkout.OrderList.Count; i++) { lineItems[i] = new AuthorizeNet.Api.Contracts.V1.lineItemType { itemId = i.ToString(), name = (checkout.OrderList[i].Item.Length < 10) ? checkout.OrderList[i].Item : checkout.OrderList[i].Item.Substring(0, 10), quantity = checkout.OrderList[i].Quantity, unitPrice = checkout.OrderList[i].Subtotal }; } int invoiceNumber = BGBCFunctions.GetInoiveNo(); AuthorizeNet.Api.Controllers.createTransactionController controller; var billAddress = new AuthorizeNet.Api.Contracts.V1.customerAddressType { firstName = checkout.FirstName, lastName = checkout.LastName, address = checkout.BillingAddress + ", " + (string.IsNullOrEmpty(checkout.BillingAddress_2) ? "" : checkout.BillingAddress_2), city = checkout.BillingCty, state = checkout.BillingState, zip = checkout.BillingZip, email = checkout.Email, phoneNumber = checkout.Phone, country = "USA" }; string address = string.Format("{0}<br/>{1}<br/>{2}, {3} {4}", checkout.BillingAddress, checkout.BillingAddress_2, checkout.BillingCty, checkout.BillingState, checkout.BillingZip); if (checkout.PaymentMethod == "eCheck") { var bankAccount = new AuthorizeNet.Api.Contracts.V1.bankAccountType { accountNumber = checkout.BankAccountNumber, routingNumber = checkout.BankRoutingNumber, echeckType = AuthorizeNet.Api.Contracts.V1.echeckTypeEnum.WEB, // change based on how you take the payment (web, telephone, etc) nameOnAccount = checkout.FirstName + " " + checkout.LastName }; controller = PaymentGateway.DebitBankAccount(bankAccount, lineItems, billAddress, checkout.OrderTotal, invoiceNumber); } else { var creditCard = new AuthorizeNet.Api.Contracts.V1.creditCardType { cardNumber = checkout.CardNo, expirationDate = checkout.CardExpMon + checkout.CardExpYear.ToString(), cardCode = checkout.CVV }; controller = PaymentGateway.ChargeCreditCard(creditCard, lineItems, billAddress, checkout.OrderTotal, invoiceNumber); } AuthorizeNet.Api.Contracts.V1.createTransactionResponse response = controller.GetApiResponse(); if (response != null) { if (response.messages.resultCode == AuthorizeNet.Api.Contracts.V1.messageTypeEnum.Ok) { if (response.transactionResponse != null) { if (response.transactionResponse.errors == null) { try { int userid = 0; if (!Request.IsAuthenticated) { User selUser = new User { FirstName = checkout.FirstName, LastName = checkout.LastName, Password = BGBC.Core.Security.Cryptography.Encrypt(checkout.ChoosePassword), UserType = 3, Email = checkout.Email }; selUser.Profiles.Add(new Profile { BillingAddress = checkout.BillingAddress, BillingAddress_2 = checkout.BillingAddress_2, BillingCty = checkout.BillingCty, BillingState = checkout.BillingState, MobilePhone = checkout.Phone }); selUser = _userRepository.Add(selUser); CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel(); serializeModel.UserId = selUser.UserID; serializeModel.FirstName = selUser.FirstName; serializeModel.LastName = selUser.LastName; serializeModel.roles = new string[] { "Customer" }; string userData = Newtonsoft.Json.JsonConvert.SerializeObject(serializeModel); System.Web.Security.FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, selUser.FirstName, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData); string encTicket = FormsAuthentication.Encrypt(authTicket); HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); Response.Cookies.Add(faCookie); userid = selUser.UserID; } else { userid = ((BGBC.Core.CustomPrincipal)(User)).UserId; } Order order = BGBCFunctions.ProductTrans(userid, checkout.Email, checkout.ChoosePassword, invoiceNumber, response.transactionResponse.accountNumber, response.transactionResponse.accountType, response.transactionResponse.transId, response.transactionResponse.messages[0].code, response.transactionResponse.messages[0].description, Request.UserHostAddress, address, checkout.Comments, checkout.ProductIds); IRepository <ProductOrder, int> productOrder = new ProductOrderRepository(); foreach (var item in checkout.OrderList.Where(x => x.ProductID == 0)) { productOrder.Add(new ProductOrder { OrderID = order.OrderID, Name = item.Item, Price = item.Price }); } //Save Payment details if (checkout.SaveCard) { UserCC ccinfo = _userCCRep.Get(userid); if (ccinfo == null) //There is no details in database { if (checkout.PaymentMethod == "eCheck") { _userCCRep.Add(new UserCC { UserID = userid, PaymentType = 2, AccountType = checkout.BankAccountType, RoutingNo = Cryptography.Encrypt(checkout.BankRoutingNumber), AccountNo = Cryptography.Encrypt(checkout.BankAccountNumber) }); } else { _userCCRep.Add(new UserCC { UserID = userid, PaymentType = 1, CCNO = Cryptography.Encrypt(checkout.CardNo), ExpMon = Cryptography.Encrypt(checkout.CardExpMon), ExpYear = Cryptography.Encrypt(checkout.CardExpYear) }); } } else { if (checkout.PaymentMethod == "eCheck") { ccinfo.CCNO = string.Empty; ccinfo.ExpMon = string.Empty; ccinfo.ExpYear = string.Empty; ccinfo.PaymentType = 2; ccinfo.AccountType = checkout.BankAccountType; ccinfo.RoutingNo = Cryptography.Encrypt(checkout.BankRoutingNumber); ccinfo.AccountNo = Cryptography.Encrypt(checkout.BankAccountNumber); } else { ccinfo.CCNO = Cryptography.Encrypt(checkout.CardNo); ccinfo.ExpMon = Cryptography.Encrypt(checkout.CardExpMon); ccinfo.ExpYear = Cryptography.Encrypt(checkout.CardExpYear); ccinfo.PaymentType = 1; ccinfo.AccountType = string.Empty; ccinfo.RoutingNo = string.Empty; ccinfo.AccountNo = string.Empty; } } } else { UserCC ccinfo = _userCCRep.Get(userid); if (ccinfo != null) { _userCCRep.Remove(ccinfo); } } HttpCookie authCookie = Request.Cookies[".BGBCProducts"]; authCookie.Value = string.Empty; Response.SetCookie(authCookie); TempData.Remove("cartdata"); return(RedirectToAction("OrderHistory", "Report")); } catch (Exception ex) { log.Error(ex.Message); ModelState.AddModelError("", "Transaction Error : " + ex.Message); } System.Diagnostics.Trace.TraceInformation("Success, Auth Code : " + response.transactionResponse.authCode); } else { ModelState.AddModelError("", "Transaction Error : " + response.transactionResponse.errors[0].errorCode + " " + response.transactionResponse.errors[0].errorText); } } } else { System.Diagnostics.Trace.TraceInformation("Error: " + response.messages.message[0].code + " " + response.messages.message[0].text); if (response.transactionResponse != null) { ModelState.AddModelError("", "Transaction Error : " + response.transactionResponse.errors[0].errorCode + " " + response.transactionResponse.errors[0].errorText); } TempData["cartdata"] = checkout; } } else { TempData["cartdata"] = checkout; ModelState.AddModelError("", "Transaction Error, unable to complete the transaction."); } } catch (Exception ex) { log.Error(ex.Message); ModelState.AddModelError("", "Transaction Error, unable to complete the transaction."); } Tuple <List <Models.OrderSummary>, decimal, string> ordersummary = getCartProducts(); checkout.OrderList = ordersummary.Item1; checkout.OrderTotal = ordersummary.Item2; checkout.ProductIds = ordersummary.Item3; checkoutDropDown(); return(View("Checkout", checkout)); }