public async Task<int> CreateOrder(Order order) { decimal orderTotal = 0; var cartItems = await GetCartItems(); // Iterate over the items in the cart, adding the order details for each foreach (var item in cartItems) { var orderDetail = new OrderDetail { Product = item.Product, UnitPrice = item.Product.Price, Quantity = item.Count, }; // Set the order total of the shopping cart orderTotal += (item.Count * item.Product.Price); _dbContext.OrderDetails.Add(orderDetail); } // Set the order's total to the orderTotal count order.Total = orderTotal; // Empty the shopping cart EmptyCart(); // Return the OrderId as the confirmation number return order.Id; }
public async Task<IActionResult> AddressAndPayment(Order order) { var cart = ShoppingCart.GetCart(_dbContext, Context); var formCollection = await Context.Request.ReadFormAsync(); var charge = new StripeChargeCreateOptions { Amount = Convert.ToInt32(await cart.GetTotal() * 100), Currency = "cad", Card = new StripeCreditCardOptions { TokenId = formCollection.GetValues("stripeToken").FirstOrDefault() } }; // TODO: Read this from configuration var chargeService = new StripeChargeService("sk_test_VD5Xw4EehScXQdYV0fujg6Nr"); var stripeCharge = chargeService.Create(charge); order.UserId = Context.User.Identity.GetUserId(); order.OrderDate = DateTime.Now; // Add the Order await _dbContext.Orders.AddAsync(order, Context.RequestAborted); // Process the order await _dbContext.SaveChangesAsync(Context.RequestAborted); // Get the order's content foreach (var item in await cart.GetCartItems()) { // If the product is a ticket, add it to the user's account TicketType tt; switch (item.ProductId) { case "lanbyoc": tt = TicketType.BYOC; break; case "lanbyocvip": tt = TicketType.BYOCVIP; break; case "lanconsole": tt = TicketType.Console; break; case "lanvisitor": tt = TicketType.Visitor; break; default: continue; } var ticket = new Ticket { OrderId = order.Id, TicketType = tt, UserOwnerId = User.Identity.GetUserId() }; await _dbContext.Tickets.AddAsync(ticket); } // Save all changes await _dbContext.SaveChangesAsync(Context.RequestAborted); // Empty the cart cart.EmptyCart(); return RedirectToAction("Complete", new { id = order.Id }); }