Exemplo n.º 1
0
        public ActionResult AddCoupon(string code)
        {
            UserLogin user = (UserLogin)Session[Common.CommonConstants.USER_SESSION];

            //var cart = (List<CartItem>)Session[Common.CommonConstants.CART_SESSION];
            if (user == null)
            {
                SetAlert("Rất tiếc, bạn phải đăng nhập để thực hiện chức năng này.", "error");
                return(View());
            }
            var couponUserDao = new CouponUserDao();
            var coupon        = new CouponDao().GetByCode(code);

            if (coupon == null)
            {
                SetAlert("Mã giảm giá không chính xác. Xin nhập lại.", "error");
                return(View());
            }
            else
            {
                if (user != null)
                {
                    //check mã đã xài chưa
                    if (couponUserDao.CheckCouponUser(coupon.ID, user.UserID) == true)
                    {
                        SetAlert("Bạn đã dùng mã giảm giá này trong đơn đặt hàng trước.", "error");
                        return(View());
                    }
                }
                var couponList = Session[Common.CommonConstants.COUPON_SESSION];
                if (couponList != null)
                {
                    var list = (List <Coupon>)couponList;
                    if (list.Exists(x => x.Code == code))
                    {
                        //thông báo giỏ đã dùng mã này
                        SetAlert("Giỏ hàng của bạn đang sử dụng mã giảm giá này.", "error");
                    }
                    else
                    {
                        //Mã hợp lệ. Đưa coupon vào list
//                        list.Clear();   // chỉ có 1 coupon mỗi order // tùy chọn
                        list.Add(coupon);
                        SetAlert("Sử dụng mã giảm giá thành công.", "success");
                    }
                    //Gán list vào session
                    Session[Common.CommonConstants.COUPON_SESSION] = list;
                    return(View());
                }
                //giỏ hàng chưa có
                else
                {
                    //Mã hợp lệ. Đưa coupon vào list
                    //Tạo mới list
                    var list = new List <Coupon>();
                    //Tạo mới đối tượng cart item
                    //Đưa item vào list
                    list.Add(coupon);
                    SetAlert("Sử dụng mã giảm giá thành công.", "success");
                    //Gán list vào session
                    Session[Common.CommonConstants.COUPON_SESSION] = list;
                    //chưa xài dc
                    return(View());
                }
            }
            //return RedirectToAction("Index");
        }
Exemplo n.º 2
0
        public ActionResult SendOrder(string shipName, string mobile, string address, string email)
        {
            try
            {
                UserLogin user     = (UserLogin)Session[Common.CommonConstants.USER_SESSION];
                var       cart     = (List <CartItem>)Session[Common.CommonConstants.CART_SESSION];
                var       itemList = new List <CartItem>();
                if (cart != null)
                {
                    itemList = (List <CartItem>)cart;
                }
                var coupon     = (List <Coupon>)Session[Common.CommonConstants.COUPON_SESSION];
                var couponList = new List <Coupon>();
                if (coupon != null)
                {
                    couponList = (List <Coupon>)coupon;
                }
                var     productDao  = new ProductDao();
                decimal originPrice = 0;
                foreach (var item in itemList)
                {
                    //check quantity+status của product
                    if (productDao.CheckProduct(item.Product.ID, item.Quantity) == false)
                    {
                        var alertMessage = "Số lượng sản phẩm [" + item.Product.Name + "] đã hết. Xin vui lòng chọn sản phẩm khác.";
                        SetAlert(alertMessage, "error");
                        return(Redirect("/het-san-pham"));
                    }
                    if (item.Product.PromotionPrice != null)
                    {
                        originPrice += (item.Product.PromotionPrice.GetValueOrDefault(0) * item.Quantity);
                    }
                    else
                    {
                        originPrice += (item.Product.Price.GetValueOrDefault(0) * item.Quantity);
                    }
                }
                decimal newPrice = originPrice;
                foreach (var item in couponList)
                {
                    if (item.ByPercentage == true)
                    {
                        newPrice = (newPrice * (1 - item.DiscountBy / 100));
                    }
                    else
                    {
                        newPrice = newPrice - item.DiscountBy;
                    }
                }
                if (newPrice < 0)
                {
                    newPrice = 0;
                }
                //Gửi Email
                //đọc file neworder.html //email mẫu
                string content = System.IO.File.ReadAllText(Server.MapPath("/assets/client/template/neworder.html"));
                //replace các key {{}}
                content = content.Replace("{{CustomerName}}", shipName);
                //tùy chọn: cần 2 phone+email hoặc ít nhất 1
                int infoCount = (mobile != "" ? 1 : 0) + (email != "" ? 1 : 0);
                if (infoCount > 0)
                {
                    content = content.Replace("{{Phone}}", mobile != "" ? mobile : "trống");
                    content = content.Replace("{{Email}}", email != "" ? email : "trống");
                }
                else
                {
                    return(Redirect("/loi-dat-hang"));
                }
                content = content.Replace("{{Address}}", address);
                decimal ship = 100000;       // ship 100k
                content = content.Replace("{{Ship}}", ship.ToString("N0") + "đ");
                if (newPrice == originPrice) //ko có coupon, ko có newPrice
                {
                    content = content.Replace("{{OriginPrice}}", "Giá: " + originPrice.ToString("N0") + "đ");
                    content = content.Replace("{{NewPrice}}", "");
                }
                else
                {
                    content = content.Replace("{{OriginPrice}}", "Giá gốc: " + originPrice.ToString("N0") + "đ");
                    content = content.Replace("{{NewPrice}}", "Giá mới: " + newPrice.ToString("N0") + "đ");
                }
                content = content.Replace("{{Total}}", (newPrice + ship).ToString("N0") + "đ");
                //đọc tham số từ key trong Web.config của project OnlineShop
                var toEmail = ConfigurationManager.AppSettings["ToEmailAddress"].ToString();
                //gửi đi 2 nơi
                new MailHelper().SendMail(toEmail, "Đơn hàng mới từ OnlineShop đến người cung cấp sản phẩm", content);
                new MailHelper().SendMail(email, "Đơn hàng mới từ OnlineShop đến người đặt đơn hàng", content);

                //Gửi thành công // bắt đầu xử lý database
                //ghi order vào databasse
                var orderDao = new OrderDao();
                var order    = new Model.EF.Order();
                order.CreatedDate = DateTime.Now;
                if (user != null)
                {
                    order.CustomerID = user.UserID;
                }
                order.ShipName    = shipName;
                order.ShipMobile  = mobile;
                order.ShipAddress = address;
                order.ShipEmail   = email;
                order.Status      = -1; //đang chờ shipper nhận
                var orderId = orderDao.Insert(order);
                //xử lý OrderDetail + Product
                var detailDao = new OrderDetailDao();

                foreach (var item in itemList)
                {
                    //Xử lý OrderDetail
                    var orderDetail = new OrderDetail();
                    orderDetail.ProductID = item.Product.ID;
                    orderDetail.OrderID   = orderId;
                    orderDetail.Price     = item.Product.Price;
                    orderDetail.Quantity  = item.Quantity;
                    detailDao.Insert(orderDetail);
                    //Xử lý sản phẩm // cập nhật số lượng sản phẩm
                    productDao.UpdateQuantity(item.Product.ID, item.Quantity);
                }
                //xử lý coupon + CouponUser
                var couponDao     = new CouponDao();
                var couponUserDao = new CouponUserDao();
                foreach (var item in couponList)
                {
                    //xử lý coupon
                    couponDao.UseCouponDiscountCode(item.Code);
                    //Xử lý CouponUser
                    var couponUser = new CouponUser();
                    couponUser.CouponID = item.ID;
                    couponUser.UserID   = user.UserID;
                    couponUserDao.Insert(couponUser);
                }
                //gán cart+coupon null
                Session[Common.CommonConstants.CART_SESSION]   = null;
                Session[Common.CommonConstants.COUPON_SESSION] = null;
            }
            catch (Exception ex)
            {
                //ghi log
                Logger.Log("Error" + ex.Message);
                return(Redirect("/loi-dat-hang"));
            }
            return(Redirect("/hoan-thanh"));
        }
Exemplo n.º 3
0
        //Pay=paypal
        public ActionResult PaymentWithPaypal(string Cancel = null)
        {
            UserLogin user     = (UserLogin)Session[Common.CommonConstants.USER_SESSION];
            var       cart     = (List <CartItem>)Session[Common.CommonConstants.CART_SESSION];
            var       itemList = new List <CartItem>();

            if (cart != null)
            {
                itemList = (List <CartItem>)cart;
            }
            var coupon     = (List <Coupon>)Session[Common.CommonConstants.COUPON_SESSION];
            var couponList = new List <Coupon>();

            if (cart != null)
            {
                couponList = (List <Coupon>)coupon;
            }
            var productDao = new ProductDao();

            //check quantity+status của product
            foreach (var item in cart)
            {
                if (productDao.CheckProduct(item.Product.ID, item.Quantity) == false)
                {
                    var alertMessage = "Số lượng sản phẩm [" + item.Product.Name + "] đã hết. Xin vui lòng chọn sản phẩm khác.";
                    SetAlert(alertMessage, "error");
                    return(Redirect("/het-san-pham"));
                }
            }
            //getting the apiContext
            APIContext apiContext = helper.PaypalConfiguration.GetAPIContext();

            try
            {
                //A resource representing a Payer that funds a payment Payment Method as paypal
                //Payer Id will be returned when payment proceeds or click to pay
                string payerId = Request.Params["PayerID"];
                if (string.IsNullOrEmpty(payerId))
                {
                    //this section will be executed first because PayerID doesn't exist
                    //it is returned by the create function call of the payment class
                    // Creating a payment
                    // baseURL is the url on which paypal sendsback the data.
                    // So we have provided URL of this controller only
                    string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority +
                                     "/Cart/PaymentWithPaypal?";
                    //here we are generating guid for storing the paymentID received in session
                    //which will be used in the payment execution.
                    //guid we are generating for storing the paymentID received in session
                    //after calling the create function and it is used in the payment execution
                    var guid = Convert.ToString((new Random()).Next(100000));
                    //CreatePayment function gives us the payment approval url
                    //on which payer is redirected for paypal account payment
                    var createdPayment = this.CreatePayment(apiContext, baseURI + "guid=" + guid);
                    //get links returned from paypal in response to Create function call
                    var    links             = createdPayment.links.GetEnumerator();
                    string paypalRedirectUrl = null;
                    while (links.MoveNext())
                    {
                        Links lnk = links.Current;
                        if (lnk.rel.ToLower().Trim().Equals("approval_url"))
                        {
                            //saving the payapalredirect URL to which user will be redirected for payment
                            paypalRedirectUrl = lnk.href;
                        }
                    }
                    // saving the paymentID in the key guid
                    Session.Add(guid, createdPayment.id);
                    return(Redirect(paypalRedirectUrl));
                }
                else
                {
                    // This section is executed when we have received all the payments parameters
                    // from the previous call to the function Create
                    // Executing a payment
                    var guid            = Request.Params["guid"];
                    var executedPayment = ExecutePayment(apiContext, payerId, Session[guid] as string);
                    //If executed payment failed then we will show payment failure message to user
                    if (executedPayment.state.ToLower() != "approved")
                    {
                        Session.Remove(guid);
                        return(View("FailureView"));
                    }

                    //Tới đây coi như thành công
                    //ghi order vào databasse
                    var orderDetailDao  = new Model.Dao.OrderDetailDao();
                    var orderCounponDao = new Model.Dao.OrderCouponDao();
                    var order           = new Model.EF.Order();
                    order.CreatedDate = DateTime.Parse(executedPayment.create_time);
                    if (user != null)
                    {
                        order.CustomerID = user.UserID;
                    }
                    order.ShipName    = executedPayment.payer.payer_info.shipping_address.recipient_name;
                    order.ShipMobile  = executedPayment.payer.payer_info.shipping_address.phone;
                    order.ShipAddress = executedPayment.payer.payer_info.shipping_address.line1 + ", "
                                        + executedPayment.payer.payer_info.shipping_address.city + ", "
                                        + executedPayment.payer.payer_info.shipping_address.state + ", "
                                        + executedPayment.payer.payer_info.shipping_address.country_code + ".";
                    order.ShipEmail = executedPayment.payer.payer_info.email;
                    order.Status    = -1; //đang chờ shipper nhận
                    var     orderID    = new OrderDao().Insert(order);
                    decimal sumProduct = 0;
                    foreach (var item in itemList)
                    {
                        //Xử lý orderDetail
                        var orderDetail = new OrderDetail();
                        orderDetail.ProductID = item.Product.ID;
                        orderDetail.OrderID   = orderID;
                        decimal?tempPrice = item.Product.PromotionPrice != null ? item.Product.PromotionPrice : item.Product.Price;
                        orderDetail.Price    = tempPrice;
                        orderDetail.Quantity = item.Quantity;
                        orderDetailDao.Insert(orderDetail);
                        //Xử lý sản phẩm // cập nhật số lượng sản phẩm
                        productDao.UpdateQuantity(item.Product.ID, item.Quantity);
                        //
                        sumProduct += orderDetail.Price.GetValueOrDefault(0) * orderDetail.Quantity.GetValueOrDefault(0);
                    }
                    //xử lý coupon + CouponUser
                    var couponDao     = new CouponDao();
                    var couponUserDao = new CouponUserDao();
                    if (couponList != null)
                    {
                        foreach (var item in couponList)
                        {
                            //xử lý coupon // giảm quantity của coupon đi 1
                            couponDao.UseCouponDiscountCode(item.Code);
                            //Xử lý CouponUser
                            var couponUser = new CouponUser();
                            couponUser.CouponID = item.ID;
                            couponUser.UserID   = user.UserID;
                            couponUserDao.Insert(couponUser);
                            //Xử lý orderCoupon
                            var orderCoupon = new OrderCoupon();
                            orderCoupon.OrderID  = orderID;
                            orderCoupon.CouponID = item.ID;
                            decimal tempAmount = 0;
                            if (item.ByPercentage == true)
                            {
                                tempAmount = sumProduct * item.DiscountBy / 100; //discount theo %
                            }
                            else
                            {
                                tempAmount = item.DiscountBy; //discount cố định
                            }
                            orderCoupon.DiscountAmount = tempAmount;
                            orderCounponDao.Insert(orderCoupon);
                        }
                    }
                    //gán cart+coupon null
                    Session[Common.CommonConstants.CART_SESSION]   = null;
                    Session[Common.CommonConstants.COUPON_SESSION] = null;
                }
            }
            catch (Exception ex)
            {
                Logger.Log("Error" + ex.Message);
                return(View("FailureView"));
            }
            //on successful payment, show success page to user.
            return(View("SuccessView"));
        }