Пример #1
0
        public async Task <IActionResult> Summary()
        {
            OrderDetailsCart = new OrderDetailsCartViewModel()
            {
                OrderHeader = new Models.OrderHeader(),
            };

            OrderDetailsCart.OrderHeader.OrderTotalDiscount = "0";

            var claimsIdentity = (ClaimsIdentity)User.Identity;
            var claim          = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

            if (claim == null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            ApplicationUser appUser = await _db.ApplicationUser.Where(c => c.Id.Equals(claim.Value)).FirstOrDefaultAsync();

            var cart = await _db.ShoppingCart.Where(c => c.ApplicationUserId.ToString().Equals(claim.Value)).ToListAsync();

            if (cart != null)
            {
                OrderDetailsCart.ListCart = cart.ToList();
            }

            foreach (var list in OrderDetailsCart.ListCart)
            {
                list.MenuItem = await _db.MenuItem.FirstOrDefaultAsync(m => m.Id.ToString().Equals(list.MenuItemId.ToString()));

                decimal orderTotal = decimal.Parse(OrderDetailsCart.OrderHeader.OrderTotalDiscount);
                orderTotal += (decimal)(list.MenuItem.Price * list.Count);
                OrderDetailsCart.OrderHeader.OrderTotalDiscount = orderTotal.ToString();
            }
            OrderDetailsCart.OrderHeader.OrderTotalOriginal = OrderDetailsCart.OrderHeader.OrderTotalDiscount;
            OrderDetailsCart.OrderHeader.PickupName         = appUser.FirstName + " " + appUser.LastName;
            OrderDetailsCart.OrderHeader.PhoneNumber        = appUser.PhoneNumber;
            OrderDetailsCart.OrderHeader.PickupTime         = DateTime.Now;


            var couponCodeFormSession = HttpContext.Session.GetString(SD.SessionCouponCodeCookie);

            if (couponCodeFormSession != null)
            {
                OrderDetailsCart.OrderHeader.CouponCode = couponCodeFormSession;
                var couponFromDb = await _db.Coupon.Where(c => c.Name.ToLower().Equals(couponCodeFormSession.ToLower())).FirstOrDefaultAsync();

                OrderDetailsCart.OrderHeader.OrderTotalDiscount = SD.DicountedPrice(couponFromDb, decimal.Parse(OrderDetailsCart.OrderHeader.OrderTotalOriginal)).ToString();
            }

            return(View(OrderDetailsCart));
        }
Пример #2
0
        public async Task <IActionResult> Index()
        {
            OrderDetailsCart = new OrderDetailsCartViewModel()
            {
                OrderHeader = new Models.OrderHeader(),
            };

            OrderDetailsCart.OrderHeader.OrderTotalDiscount = "0";

            var claimsIdentity = (ClaimsIdentity)User.Identity;
            var claim          = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

            if (claim == null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            var cart = await _db.ShoppingCart.Where(c => c.ApplicationUserId.ToString().Equals(claim.Value)).ToListAsync();

            if (cart != null)
            {
                OrderDetailsCart.ListCart = cart.ToList();
            }

            foreach (var list in OrderDetailsCart.ListCart)
            {
                list.MenuItem = await _db.MenuItem.FirstOrDefaultAsync(m => m.Id.ToString().Equals(list.MenuItemId.ToString()));

                decimal orderTotal = decimal.Parse(OrderDetailsCart.OrderHeader.OrderTotalDiscount);
                orderTotal += (decimal)(list.MenuItem.Price * list.Count);
                OrderDetailsCart.OrderHeader.OrderTotalDiscount = orderTotal.ToString();
                list.MenuItem.Description = Utility.SD.ConvertToRawHtml(list.MenuItem.Description);
                if (list.MenuItem.Description.Length > 100)
                {
                    list.MenuItem.Description = list.MenuItem.Description.Substring(0, 99) + "...";
                }
            }
            OrderDetailsCart.OrderHeader.OrderTotalOriginal = OrderDetailsCart.OrderHeader.OrderTotalDiscount;


            var couponCodeFormSession = HttpContext.Session.GetString(SD.SessionCouponCodeCookie);

            if (couponCodeFormSession != null)
            {
                OrderDetailsCart.OrderHeader.CouponCode = couponCodeFormSession;
                var couponFromDb = await _db.Coupon.Where(c => c.Name.ToLower().Equals(couponCodeFormSession.ToLower())).FirstOrDefaultAsync();

                OrderDetailsCart.OrderHeader.OrderTotalDiscount = SD.DicountedPrice(couponFromDb, decimal.Parse(OrderDetailsCart.OrderHeader.OrderTotalOriginal)).ToString();
            }

            return(View(OrderDetailsCart));
        }
        public async Task <IActionResult> Summary()
        {
            DetailCart = new OrderDetailsCart()
            {
                OrderHeader = new Models.OrderHeader()
            };

            DetailCart.OrderHeader.OrderTotal = 0;

            var             claimsIdentity  = (ClaimsIdentity)User.Identity;
            var             claim           = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
            ApplicationUser applicationUser = await _db.ApplicationUser.Where(c => c.Id == claim.Value).FirstOrDefaultAsync();

            var cart = _db.ShoppingCart.Where(c => c.ApplicationUserId == claim.Value);

            if (cart != null)
            {
                DetailCart.ListCart = cart.ToList();
            }

            foreach (var list in DetailCart.ListCart)
            {
                list.MenuItem = await _db.MenuItem.FirstOrDefaultAsync(m => m.Id == list.MenuItemId);

                DetailCart.OrderHeader.OrderTotal = DetailCart.OrderHeader.OrderTotal + (list.MenuItem.Price * list.Count);
            }
            DetailCart.OrderHeader.OrderTotalOriginal = DetailCart.OrderHeader.OrderTotal;
            DetailCart.OrderHeader.PickupName         = applicationUser.Name;
            DetailCart.OrderHeader.PhoneNumber        = applicationUser.PhoneNumber;
            DetailCart.OrderHeader.PickUpTime         = DateTime.Now;



            if (HttpContext.Session.GetString(SD.ssCouponCode) != null)
            {
                DetailCart.OrderHeader.CouponCode = HttpContext.Session.GetString(SD.ssCouponCode);
                var couponFromDB = await _db.Cupon.Where(c => c.Name.ToLower()
                                                         == DetailCart.OrderHeader.CouponCode.ToLower()).FirstOrDefaultAsync();

                DetailCart.OrderHeader.OrderTotal = SD.DicountedPrice(couponFromDB, DetailCart.OrderHeader.OrderTotalOriginal);
            }

            return(View(DetailCart));
        }
        public async Task <IActionResult> Index()
        {
            DetailCart = new OrderDetailsCart()
            {
                OrderHeader = new Models.OrderHeader()
            };

            DetailCart.OrderHeader.OrderTotal = 0;

            var claimsIdentity = (ClaimsIdentity)User.Identity;
            var claim          = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

            var cart = _db.ShoppingCart.Where(c => c.ApplicationUserId == claim.Value);

            if (cart != null)
            {
                DetailCart.ListCart = cart.ToList();
            }

            foreach (var list in DetailCart.ListCart)
            {
                list.MenuItem = await _db.MenuItem.FirstOrDefaultAsync(m => m.Id == list.MenuItemId);

                DetailCart.OrderHeader.OrderTotal = DetailCart.OrderHeader.OrderTotal + (list.MenuItem.Price * list.Count);
                list.MenuItem.Description         = SD.ConvertToRawHtml(list.MenuItem.Description);
                if (list.MenuItem.Description.Length > 100)
                {
                    list.MenuItem.Description = list.MenuItem.Description.Substring(0, 99) + "...";
                }
            }
            DetailCart.OrderHeader.OrderTotalOriginal = DetailCart.OrderHeader.OrderTotal;

            if (HttpContext.Session.GetString(SD.ssCouponCode) != null)
            {
                DetailCart.OrderHeader.CouponCode = HttpContext.Session.GetString(SD.ssCouponCode);
                var couponFromDB = await _db.Cupon.Where(c => c.Name.ToLower()
                                                         == DetailCart.OrderHeader.CouponCode.ToLower()).FirstOrDefaultAsync();

                DetailCart.OrderHeader.OrderTotal = SD.DicountedPrice(couponFromDB, DetailCart.OrderHeader.OrderTotalOriginal);
            }

            return(View(DetailCart));
        }
        public async Task <IActionResult> SummaryPost(string stripeEmail, string stripeToken)
        {
            var claimsIdentity = (ClaimsIdentity)User.Identity;
            var claim          = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);


            DetailCart.ListCart = await _db.ShoppingCart
                                  .Where(c => c.ApplicationUserId == claim.Value).ToListAsync();

            DetailCart.OrderHeader.PaymentStatus = SD.PaymentStatusPendeing;
            DetailCart.OrderHeader.OrderDate     = DateTime.Now;
            DetailCart.OrderHeader.UserId        = claim.Value;
            DetailCart.OrderHeader.Status        = SD.PaymentStatusPendeing;
            DetailCart.OrderHeader.PickUpTime    = Convert.ToDateTime(DetailCart.OrderHeader.PickUpTime.ToShortDateString() +
                                                                      " " + DetailCart.OrderHeader.PickUpTime.ToLongTimeString());

            List <OrderDetails> orderDetailsList = new List <OrderDetails>();

            _db.OrderHeader.Add(DetailCart.OrderHeader);
            await _db.SaveChangesAsync();

            DetailCart.OrderHeader.OrderTotalOriginal = 0;



            foreach (var item in DetailCart.ListCart)
            {
                item.MenuItem = await _db.MenuItem.FirstOrDefaultAsync(m => m.Id == item.MenuItemId);

                OrderDetails orderDetails = new OrderDetails
                {
                    MenuItemId  = item.MenuItemId,
                    OrderId     = DetailCart.OrderHeader.Id,
                    Description = item.MenuItem.Description,
                    Name        = item.MenuItem.Name,
                    Price       = item.MenuItem.Price,
                    Count       = item.Count
                };
                DetailCart.OrderHeader.OrderTotalOriginal += orderDetails.Count * orderDetails.Price;
                _db.OrderDetails.Add(orderDetails);
            }

            if (HttpContext.Session.GetString(SD.ssCouponCode) != null)
            {
                DetailCart.OrderHeader.CouponCode = HttpContext.Session.GetString(SD.ssCouponCode);
                var couponFromDB = await _db.Cupon.Where(c => c.Name.ToLower()
                                                         == DetailCart.OrderHeader.CouponCode.ToLower()).FirstOrDefaultAsync();

                DetailCart.OrderHeader.OrderTotal = SD.DicountedPrice(couponFromDB, DetailCart.OrderHeader.OrderTotalOriginal);
            }
            else
            {
                DetailCart.OrderHeader.OrderTotal = DetailCart.OrderHeader.OrderTotalOriginal;
            }
            DetailCart.OrderHeader.CouponCodeDiscount = DetailCart.OrderHeader.OrderTotalOriginal - DetailCart.OrderHeader.OrderTotal;

            _db.ShoppingCart.RemoveRange(DetailCart.ListCart);
            HttpContext.Session.SetInt32(SD.ssShoppingCartCount, 0);
            await _db.SaveChangesAsync();

            //STRIPE LOGIC
            if (stripeToken != null)
            {
                var customers = new CustomerService();
                var charges   = new ChargeService();

                var customer = customers.Create(new CustomerCreateOptions
                {
                    Email       = stripeEmail,
                    SourceToken = stripeToken
                });

                var charge = charges.Create(new ChargeCreateOptions
                {
                    Amount      = Convert.ToInt32(DetailCart.OrderHeader.OrderTotal * 100),
                    Description = "Order Id" + DetailCart.OrderHeader.Id,
                    Currency    = "usd",
                    CustomerId  = customer.Id
                });

                DetailCart.OrderHeader.TransactionId = charge.BalanceTransactionId;
                if (charge.Status.ToLower() == "succeeded")
                {
                    //email for successful order

                    DetailCart.OrderHeader.PaymentStatus = SD.PaymentStatusApproved;
                    DetailCart.OrderHeader.Status        = SD.StatusSubmitted;
                }
                else
                {
                    DetailCart.OrderHeader.PaymentStatus = SD.PaymentStatusRejected;
                }
            }
            else
            {
                DetailCart.OrderHeader.PaymentStatus = SD.PaymentStatusRejected;
            }

            await _db.SaveChangesAsync();

            //return RedirectToAction("Index", "Home");
            return(RedirectToAction("Confirm", "Order", new { id = DetailCart.OrderHeader.Id }));
        }
Пример #6
0
        public async Task <IActionResult> SummaryPost(string stripeToken)
        //Stripe's Script pass string token
        {
            var claimsIdentity = (ClaimsIdentity)User.Identity;
            var claim          = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

            if (claim == null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            OrderDetailsCart.OrderHeader.PaymentStatus = SD.Status.paymentPending;
            OrderDetailsCart.OrderHeader.OrderDate     = DateTime.Now;
            OrderDetailsCart.OrderHeader.UserId        = claim.Value;
            OrderDetailsCart.OrderHeader.Status        = SD.Status.paymentPending;
            OrderDetailsCart.OrderHeader.PickupTime    = Convert.ToDateTime(OrderDetailsCart.OrderHeader.PickupDate.ToShortDateString() + " " +
                                                                            OrderDetailsCart.OrderHeader.PickupTime.TimeOfDay.ToString());
            OrderDetailsCart.OrderHeader.OrderTotalOriginal = "0";
            OrderDetailsCart.OrderHeader.OrderTotalDiscount = "0";

            List <OrderDetails> orderDetailList = new List <OrderDetails>();
            //We have to add OrderHeader ecouse order details need orderHedear.Id
            await _db.OrderHeader.AddAsync(OrderDetailsCart.OrderHeader);

            await _db.SaveChangesAsync();

            OrderDetailsCart.ListCart = await _db.ShoppingCart.Where(c => c.ApplicationUserId.ToString().Equals(claim.Value)).ToListAsync();

            decimal orderTotalAcc = 0;

            foreach (var list in OrderDetailsCart.ListCart)
            {
                list.MenuItem = await _db.MenuItem.FirstOrDefaultAsync(m => m.Id.ToString().Equals(list.MenuItemId.ToString()));

                OrderDetails orderDetails = new OrderDetails
                {
                    MenuItemId  = list.MenuItemId,
                    OrderId     = OrderDetailsCart.OrderHeader.Id,
                    Description = list.MenuItem.Description,
                    Name        = list.MenuItem.Name,
                    Price       = list.MenuItem.Price.ToString(),
                    Count       = list.Count,
                };
                orderTotalAcc += (orderDetails.Count * decimal.Parse(orderDetails.Price));
                await _db.OrderDetails.AddAsync(orderDetails);

                //decimal orderTotal = decimal.Parse(OrderDetailsCart.OrderHeader.OrderTotalDiscount);
                //orderTotal+=(decimal)(list.MenuItem.Price*list.Count);
                //OrderDetailsCart.OrderHeader.OrderTotalDiscount = orderTotal.ToString();
            }
            OrderDetailsCart.OrderHeader.OrderTotalOriginal = orderTotalAcc.ToString();
            OrderDetailsCart.OrderHeader.OrderTotalDiscount = OrderDetailsCart.OrderHeader.OrderTotalOriginal;
            //OrderDetailsCart.OrderHeader.PickupName = appUser.FirstName + " " + appUser.LastName;
            //OrderDetailsCart.OrderHeader.PhoneNumber = appUser.PhoneNumber;
            //OrderDetailsCart.OrderHeader.PickupTime = DateTime.Now;


            var couponCodeFormSession = HttpContext.Session.GetString(SD.SessionCouponCodeCookie);

            if (couponCodeFormSession != null)
            {
                OrderDetailsCart.OrderHeader.CouponCode = couponCodeFormSession;
                var couponFromDb = await _db.Coupon.Where(c => c.Name.ToLower().Equals(couponCodeFormSession.ToLower())).FirstOrDefaultAsync();

                OrderDetailsCart.OrderHeader.OrderTotalDiscount = SD.DicountedPrice(couponFromDb, decimal.Parse(OrderDetailsCart.OrderHeader.OrderTotalOriginal)).ToString();
            }
            OrderDetailsCart.OrderHeader.CouponCodeDiscount = (decimal.Parse(OrderDetailsCart.OrderHeader.OrderTotalOriginal) - decimal.Parse(OrderDetailsCart.OrderHeader.OrderTotalDiscount)).ToString();
            await _db.SaveChangesAsync();

            _db.ShoppingCart.RemoveRange(OrderDetailsCart.ListCart);
            HttpContext.Session.SetInt32(SD.SessionCartCountCookie, 0);
            await _db.SaveChangesAsync();

            //stripe transaction part
            var options = new ChargeCreateOptions
            {
                Amount      = (long?)(decimal.Parse(OrderDetailsCart.OrderHeader.OrderTotalDiscount) * 100),
                Currency    = SD.TransactionCurrency,
                Description = "Order ID : " + OrderDetailsCart.OrderHeader.Id.ToString(),
                Source      = stripeToken
            };
            var service = new ChargeService();
            //start transaction
            Charge charge = await service.CreateAsync(options);

            if (charge.BalanceTransactionId == null)
            {
                OrderDetailsCart.OrderHeader.PaymentStatus = SD.Status.paymentRejected;
            }
            else
            {
                OrderDetailsCart.OrderHeader.TransactionId = charge.BalanceTransactionId;
            }

            if (charge.Status.ToLower() == SD.TransactionSucceeded)
            {
                OrderDetailsCart.OrderHeader.PaymentStatus = SD.Status.paymentApproved;
                OrderDetailsCart.OrderHeader.Status        = SD.Status.orderSubmitted;
                //email for succesful SD.CompanyInformations; for fancy email write here html

                var emailFromDb = (await _db.Users.Where(u => u.Id.Equals(claim.Value)).FirstOrDefaultAsync()).Email;

                await _emailSender.SendEmailAsync(
                    emailFromDb,
                    SD.CompanyInformations.emailSubject + OrderDetailsCart.OrderHeader.Id.ToString(),
                    SD.CompanyInformations.emailMessageOrderSubmitedSuccess
                    );
            }
            else
            {
                OrderDetailsCart.OrderHeader.PaymentStatus = SD.Status.paymentRejected;
            }


            await _db.SaveChangesAsync();

            //return RedirectToAction("Index","Home");
            return(RedirectToAction("Confirm", "Order", new { id = OrderDetailsCart.OrderHeader.Id }));
        }