Exemplo n.º 1
0
        public ActionResult Changed(int quantity, int?productVarientID = null, int?productID = null)
        {
            var jsonSuccessResult = new JsonSuccessResult();

            Cart cart;

            try
            {
                cart = CartController.GetCart(HttpContext);

                CartItem cartItem = CartItems.GetByProductVarientID(cart.ID, productVarientID, productID);

                if (cartItem != null)
                {
                    cartItem.Quantity = quantity;

                    CartItems.Update(cartItem);
                }

                var cartItems = CartItems.GetByCartID(cart.ID, UserID);

                int total         = 0,
                    totalDiscount = 0;

                foreach (var item in cartItems)
                {
                    totalDiscount += item.Quantity * (item.DiscountPercent > 0 ? item.DiscountPrice : item.Price);
                    total         += item.Quantity * item.Price;
                }

                jsonSuccessResult.Data = new
                {
                    CartItems     = cartItems,
                    Total         = total,
                    TotalDiscount = totalDiscount,
                };
                jsonSuccessResult.Success = true;
            }
            catch (DbException ex)
            {
                jsonSuccessResult.Errors  = ex.Errors.ToArray();
                jsonSuccessResult.Success = false;
            }
            catch (Exception ex)
            {
                jsonSuccessResult.Errors  = new string[] { ex.Message };
                jsonSuccessResult.Success = false;
            }

            return(new JsonResult()
            {
                Data = jsonSuccessResult
            });
        }
Exemplo n.º 2
0
        public JsonResult Payment(OSUser osUser, SendMethodType sendMethodType, PaymentMethodType paymentMethodType, string userDescription)
        {
            var jsonSuccessResult = new JsonSuccessResult();

            try
            {
                var payDate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0');
                var payTime = DateTime.Now.Hour.ToString().PadLeft(2, '0') + DateTime.Now.Minute.ToString().PadLeft(2, '0') + DateTime.Now.Second.ToString().PadLeft(2, '0');
                var orderID = payDate + payTime;

                string userID;
                int?   cityID;

                if (User.Identity.IsAuthenticated)
                {
                    userID = UserID;
                    cityID = OSUsers.GetByID(UserID).CityID;
                }
                else
                {
                    cityID = osUser.CityID;

                    IdentityResult result = registerUser(ref osUser);

                    if (result.Succeeded)
                    {
                        UserNotifications.Send(StaticValues.AdminID, String.Format("ثبت نام کاربر - در فرم سبد خرید '{0}'", osUser.UserName), "/Admin/OSUsers/Edit/" + osUser.Id, NotificationType.Success);
                        userID = osUser.Id;
                    }
                    else
                    {
                        throw new Exception(result.Errors.Aggregate((a, b) => a + ", " + b));
                    }
                }

                int total         = 0,
                    totalDiscount = 0,
                    delivaryPrice = -1;

                var cart      = GetCart(HttpContext);
                var cartItems = CartItems.GetByCartID(cart.ID, (User.Identity.IsAuthenticated ? userID : null));
                var listGifts = new List <CartItemGift>();

                foreach (var item in cartItems)
                {
                    var price = (item.DiscountPercent > 0 ? item.DiscountPrice : item.Price);

                    #region Update CartItem

                    var cartItem = CartItems.GetByID(item.ID);
                    cartItem.Price          = price;
                    cartItem.IsFreeDelivery = item.IsFreeDelivery;
                    CartItems.Update(cartItem);

                    #endregion Update CartItem

                    totalDiscount += item.Quantity * price;
                    total         += item.Quantity * item.Price;

                    foreach (var gift in item.Gifts)
                    {
                        listGifts.Add(new CartItemGift
                        {
                            CartItemID = item.ID,
                            GiftID     = gift.GiftID,
                            Price      = gift.Price,
                            LastUpdate = DateTime.Now
                        });
                    }
                }

                if (
                    (sendMethodType == SendMethodType.Free && cityID == 468) ||      // مشهد
                    (StaticValues.MaxPriceFreeDelivery && totalDiscount >= 10000) || // طرح های بالای ارسال
                    (cartItems.Any(a => a.IsFreeDelivery))                           // محصولات دارای ارسال رایگان
                    )
                {
                    delivaryPrice = 50000;
                }
                else
                {
                    delivaryPrice = StaticValues.DeliveryPrice;
                }

                // ثبت هدایا
                if (listGifts.Count > 0)
                {
                    CartItemGifts.Insert(listGifts);
                }

                var toPay = (totalDiscount + delivaryPrice);

                string refID = String.Empty;

                if (paymentMethodType == PaymentMethodType.Online)
                {
                    Logs.Alert(Utilities.GetIP(), "PaymentMethodType.Online", String.Format("payDate: {0}, payTime: {1}, orderID: {2}, toPay: {3}", payDate, payTime, orderID, toPay));
                    refID = connectToMellat(payDate, payTime, orderID, toPay);
                }

                #region Update Cart

                cart.UserID            = userID;
                cart.SendMethodType    = sendMethodType;
                cart.PaymentMethodType = paymentMethodType;
                cart.Tax             = Int32.Parse(StaticValues.Tax);
                cart.IP              = Utilities.GetIP();
                cart.UserDescription = userDescription;

                cart.Total         = total;
                cart.DelivaryPrice = delivaryPrice;
                cart.TotalDiscount = totalDiscount;
                cart.ToPay         = toPay;

                cart.DateTime = cart.LastUpdate = DateTime.Now;
                cart.CartGuid = null;

                if (paymentMethodType == PaymentMethodType.Online)
                {
                    cart.CartStatus = CartStatus.DuringPay;
                }
                else if (paymentMethodType == PaymentMethodType.Card || paymentMethodType == PaymentMethodType.Home)
                {
                    cart.CartStatus = CartStatus.FuturePay;
                }

                cart.SendStatus = SendStatus.NotChecked;
                cart.OrderID    = orderID;

                Carts.Update(cart);

                #endregion Update Cart

                logPaymentData(orderID, toPay, cart.ID);

                jsonSuccessResult.Success = true;

                if (paymentMethodType == PaymentMethodType.Online)
                {
                    jsonSuccessResult.Data = new
                    {
                        PgwSite = StaticValues.PgwSite,
                        RefID   = refID
                    }
                }
                ;
                else
                {
                    jsonSuccessResult.Data = new
                    {
                        ToPayPrice = toPay
                    };

                    OSUser user;
                    // اطلاع رسانی به مدیر سایت
                    if (User.Identity.IsAuthenticated)
                    {
                        user = OSUsers.GetByID(UserID);
                    }
                    else
                    {
                        user = osUser;
                    }

                    NotifyNewOrder(user, cart, "-1");
                }
            }
            catch (DbException ex)
            {
                jsonSuccessResult.Errors  = ex.Errors.ToArray();
                jsonSuccessResult.Success = false;

                Logs.Alert(Utilities.GetIP(), "Payment Error", ex.Errors.Aggregate((a, b) => a + ", " + b) + "_" + ex.StackTrace, LogType.Error);
            }
            catch (Exception ex)
            {
                jsonSuccessResult.Errors  = new string[] { ex.Message };
                jsonSuccessResult.Success = false;

                Logs.Alert(Utilities.GetIP(), "Payment Error", ex.Message + "_" + ex.StackTrace, LogType.Error);
            }

            return(new JsonResult()
            {
                Data = jsonSuccessResult,
            });
        }