public ActionResult Checkout(NewPurchaseModel model, [ModelBinder(typeof(DataTablesRequestModelBinder))] DataTablesRequestModel dtRequestModel) { var user = Membership.GetUser().GetUserEntity(); if (Session["cart"] as List <Models.Purchase> == null) { Session["cart"] = new List <Models.Purchase>(); } model.Cart = ((List <Models.Purchase>)Session["cart"]); if (Request.HttpMethod == "POST") { if (ModelState.IsValid) { if (model.Cart.Count > 0) { var transaction = new Transaction(IsolationLevel.ReadCommitted, "purchase transfer"); try { // authorize and capture purchase CustomerGateway cg; var customer = EnsureProfile(out cg); var order = new Order(customer.ProfileID, model.CreditCard.AuthorizeId, "") { Amount = model.Cart.Sum(x => x.Price), Description = model.PurchaseNotes, InvoiceNumber = DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture) }; var response = (GatewayResponse)cg.AuthorizeAndCapture(order); if (!response.Approved) { throw new Exception(response.Message); } // set up all the transactions foreach (var purchase in model.Cart) { var toDevice = purchase.Device; var newPurchase = new PurchaseHistoryEntity { DeviceId = purchase.DeviceId, LocationId = toDevice.LocationId, UserId = user.UserId, PurchaseTime = DateTime.UtcNow, ScansPurchased = purchase.Quantity, AmountPaid = purchase.Price, PurchaseNotes = model.PurchaseNotes, TransactionId = response.TransactionID }; transaction.Add(newPurchase); newPurchase.Save(); toDevice.ScansAvailable += purchase.Quantity; transaction.Add(toDevice); toDevice.Save(); } transaction.Commit(); model.Cart.Clear(); OperationController.Update(); return(RedirectToAction("List")); } catch (Exception ex) { transaction.Rollback(); ModelState.AddModelError("", Purchase.CheckoutError); Log.Error(Purchase.CheckoutError, ex); } finally { transaction.Dispose(); } } } else { ModelState.AddModelError("", Purchase.NoItems); } Response.StatusCode = 417; Response.TrySkipIisCustomErrors = true; } model.Cards = user.UserCreditCards.AsQueryable(); var result = View(model); if (dtRequestModel == null) { return(result); } return(Query(result, dtRequestModel)); }
public ActionResult List(int?locationId, int?organizationId, PurchaseHistoryModel model, [ModelBinder(typeof(DataTablesRequestModelBinder))] DataTablesRequestModel dtRequestModel) { if (Request.HttpMethod == "POST" && ModelState.IsValid) { var user = Membership.GetUser().GetUserEntity(); var transaction = new Transaction(IsolationLevel.ReadCommitted, "purchase transfer"); try { var fromDevice = model.FromDevice; var toDevice = model.ToDevice; var from = new PurchaseHistoryEntity { DeviceId = model.FromDeviceId, LocationId = fromDevice.LocationId, UserId = user.UserId, PurchaseTime = DateTime.UtcNow, ScansPurchased = -model.Quantity, AmountPaid = 0, TransactionId = string.Empty, PurchaseNotes = String.Format(Purchase.TransferFrom, SharedRes.Formats.Device.FormatWith(fromDevice), SharedRes.Formats.Device.FormatWith(toDevice)) }; transaction.Add(from); from.Save(); var to = new PurchaseHistoryEntity { DeviceId = model.ToDeviceId, LocationId = toDevice.LocationId, UserId = user.UserId, PurchaseTime = DateTime.UtcNow, ScansPurchased = model.Quantity, AmountPaid = 0, TransactionId = string.Empty, PurchaseNotes = String.Format(Purchase.TransferFrom, SharedRes.Formats.Device.FormatWith(fromDevice), SharedRes.Formats.Device.FormatWith(toDevice)) }; transaction.Add(to); to.Save(); transaction.Add(fromDevice); fromDevice.ScansAvailable -= model.Quantity; fromDevice.Save(); transaction.Add(toDevice); toDevice.ScansAvailable += model.Quantity; toDevice.Save(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); ModelState.AddModelError("", Purchase.TransferFailed); Log.Error(Purchase.TransferFailed, ex); } finally { transaction.Dispose(); } } if (!organizationId.HasValue) { if (!locationId.HasValue) { model.Puchases = new LinqMetaData().PurchaseHistory.WithPermissions(); } else { var location = new LocationEntity(locationId.Value); if (location.IsNew) { throw new HttpException(404, SharedRes.Error.NotFound_Location); } if (!Permissions.UserHasPermission("View", location)) { throw new HttpException(401, SharedRes.Error.Unauthorized_Location); } model.Puchases = new LinqMetaData().PurchaseHistory.Where(x => x.LocationId == locationId.Value); } } else { var organization = new OrganizationEntity(organizationId.Value); if (organization.IsNew) { throw new HttpException(404, SharedRes.Error.NotFound_Organization); } if (!locationId.HasValue) { if (!Permissions.UserHasPermission("View", organization)) { throw new HttpException(401, SharedRes.Error.Unauthorized_Organization); } model.Puchases = new LinqMetaData().PurchaseHistory.Where(x => x.Location.OrganizationId == organizationId); } else { // do the same thing as above but check if the location is assigned to the organization var location = new LocationEntity(locationId.Value); if (location.IsNew && location.OrganizationId == organizationId) { throw new HttpException(404, SharedRes.Error.NotFound_Location); } if (!Permissions.UserHasPermission("View", location)) { throw new HttpException(401, SharedRes.Error.Unauthorized_Location); } model.Puchases = new LinqMetaData().PurchaseHistory.Where(x => x.LocationId == locationId.Value); } } var result = View(model); if (dtRequestModel == null) { return(result); } return(Query(result, dtRequestModel)); }