Exemple #1
0
        public JsonResult PlaceOrder(PlaceOrderCrediCardViewModel model)
        {
            JsonResult jsonResult = new JsonResult();

            var errorDetails = string.Empty;

            if (model != null && ModelState.IsValid)
            {
                var cartItemsCookie = Request.Cookies["cartItems"];
                if (cartItemsCookie != null && !string.IsNullOrEmpty(cartItemsCookie.Value))
                {
                    model.ProductIDs = cartItemsCookie.Value.Split('-').Select(x => int.Parse(x)).ToList();

                    if (model.ProductIDs.Count > 0)
                    {
                        model.Products = ProductsService.Instance.GetProductsByIDs(model.ProductIDs.Distinct().ToList());
                    }
                }

                if (model.ProductIDs != null && model.ProductIDs.Count > 0 && model.Products != null && model.Products.Count > 0)
                {
                    var newOrder = new Order();

                    newOrder.CustomerID      = User.Identity.GetUserId();
                    newOrder.CustomerName    = model.FullName;
                    newOrder.CustomerEmail   = model.Email;
                    newOrder.CustomerPhone   = model.PhoneNumber;
                    newOrder.CustomerCountry = model.Country;
                    newOrder.CustomerCity    = model.City;
                    newOrder.CustomerAddress = model.Address;
                    newOrder.CustomerZipCode = model.ZipCode;

                    newOrder.OrderItems = new List <OrderItem>();
                    foreach (var product in model.Products)
                    {
                        var orderItem = new OrderItem();
                        orderItem.ProductID   = product.ID;
                        orderItem.ProductName = product.Name;
                        orderItem.ItemPrice   = product.Price;
                        orderItem.Quantity    = model.ProductIDs.Where(x => x == product.ID).Count();

                        newOrder.OrderItems.Add(orderItem);
                    }

                    newOrder.OrderCode       = Guid.NewGuid().ToString();
                    newOrder.TotalAmmount    = newOrder.OrderItems.Sum(x => (x.ItemPrice * x.Quantity));
                    newOrder.DeliveryCharges = ConfigurationsHelper.FlatDeliveryCharges;

                    //Applying Promo/voucher
                    if (model.PromoID > 0)
                    {
                        var promo = PromosService.Instance.GetPromoByID(model.PromoID);
                        if (promo != null && promo.Value > 0)
                        {
                            if (promo.ValidTill >= DateTime.Now)
                            {
                                newOrder.PromoID = promo.ID;

                                if (promo.PromoType == (int)PromoTypes.Percentage)
                                {
                                    newOrder.Discount = Math.Round((newOrder.TotalAmmount * promo.Value) / 100);
                                }
                                else if (promo.PromoType == (int)PromoTypes.Amount)
                                {
                                    newOrder.Discount = promo.Value;
                                }
                            }
                        }
                    }

                    newOrder.FinalAmmount  = newOrder.TotalAmmount + newOrder.DeliveryCharges - newOrder.Discount;
                    newOrder.PaymentMethod = (int)PaymentMethods.CreditCard;

                    newOrder.OrderHistory = new List <OrderHistory>()
                    {
                        new OrderHistory()
                        {
                            OrderStatus = (int)OrderStatus.Placed,
                            ModifiedOn  = DateTime.Now,
                            Note        = "Order Placed."
                        }
                    };

                    newOrder.PlacedOn = DateTime.Now;

                    #region ExecuteAuthorizeNetPayment Execution

                    var creditCardInfo = new creditCardType
                    {
                        cardNumber     = model.CCCardNumber,
                        expirationDate = string.Format("{0}{1}", model.CCExpiryMonth, model.CCExpiryYear),
                        cardCode       = model.CCCVC
                    };

                    var authorizeNetResponse = AuthorizeNetHelper.ExecutePayment(newOrder, creditCardInfo);

                    #endregion

                    if (authorizeNetResponse.Success)
                    {
                        newOrder.TransactionID = authorizeNetResponse.Response.transactionResponse.transId;

                        if (OrdersService.Instance.SaveOrder(newOrder))
                        {
                            jsonResult.Data = new
                            {
                                Success = true,
                                Order   = newOrder
                            };
                        }
                        else
                        {
                            jsonResult.Data = new
                            {
                                Success = false,
                                Message = "System can not take any order."
                            };
                        }
                    }
                    else
                    {
                        jsonResult.Data = new
                        {
                            Success = authorizeNetResponse.Success,
                            Message = authorizeNetResponse.Message
                        };
                    }
                }
                else
                {
                    jsonResult.Data = new
                    {
                        Success = false,
                        Message = "Invalid products in cart."
                    };
                }
            }
            else
            {
                jsonResult.Data = new
                {
                    Success = false,
                    Message = string.Join("\n", ModelState.Values
                                          .SelectMany(x => x.Errors)
                                          .Select(x => x.ErrorMessage))
                };
            }

            return(jsonResult);
        }
        public JsonResult PlaceOrder(PlaceOrderCrediCardViewModel model)
        {
            JsonResult jsonResult = new JsonResult();

            var errorDetails = string.Empty;

            if (model != null && ModelState.IsValid)
            {
                if (model.ProductIDs == null)
                {
                    jsonResult.Data = new
                    {
                        Success = false,
                        Message = "There is no items in model"
                    };

                    return(jsonResult);
                }

                model.Products = ProductsService.Instance.GetProductsByIDs(model.ProductIDs.Distinct().ToList());
                if (model.ProductIDs != null && model.ProductIDs.Count > 0 && model.Products != null && model.Products.Count > 0)
                {
                    var newOrder = new Order();

                    newOrder.CustomerID      = model.UserId;
                    newOrder.CustomerName    = model.FullName;
                    newOrder.CustomerEmail   = model.Email;
                    newOrder.CustomerPhone   = model.PhoneNumber;
                    newOrder.CustomerCountry = model.Country;
                    newOrder.CustomerCity    = model.City;
                    newOrder.CustomerAddress = model.Address;
                    newOrder.CustomerZipCode = model.ZipCode;

                    newOrder.OrderItems = new List <OrderItem>();
                    foreach (var product in model.Products)
                    {
                        var orderItem = new OrderItem();
                        orderItem.ProductID   = product.ID;
                        orderItem.ProductName = product.Name;
                        orderItem.ItemPrice   = product.Price;
                        orderItem.Quantity    = model.ProductIDs.Where(x => x == product.ID).Count();

                        newOrder.OrderItems.Add(orderItem);
                    }

                    newOrder.OrderCode       = Guid.NewGuid().ToString();
                    newOrder.TotalAmmount    = newOrder.OrderItems.Sum(x => (x.ItemPrice * x.Quantity));
                    newOrder.DeliveryCharges = ConfigurationsHelper.FlatDeliveryCharges;

                    //Applying Promo/voucher
                    if (model.PromoID > 0)
                    {
                        var promo = PromosService.Instance.GetPromoByID(model.PromoID);
                        if (promo != null && promo.Value > 0)
                        {
                            if (promo.ValidTill >= DateTime.Now)
                            {
                                newOrder.PromoID = promo.ID;

                                if (promo.PromoType == (int)PromoTypes.Percentage)
                                {
                                    newOrder.Discount = Math.Round((newOrder.TotalAmmount * promo.Value) / 100);
                                }
                                else if (promo.PromoType == (int)PromoTypes.Amount)
                                {
                                    newOrder.Discount = promo.Value;
                                }
                            }
                        }
                    }

                    newOrder.FinalAmmount  = newOrder.TotalAmmount + newOrder.DeliveryCharges - newOrder.Discount;
                    newOrder.PaymentMethod = model.PaymentMethod;

                    newOrder.OrderHistory = new List <OrderHistory>()
                    {
                        new OrderHistory()
                        {
                            OrderStatus = (int)OrderStatus.Placed,
                            ModifiedOn  = DateTime.Now,
                            Note        = "Order Placed."
                        }
                    };

                    newOrder.PlacedOn = DateTime.Now;

                    if (OrdersService.Instance.SaveOrder(newOrder))
                    {
                        jsonResult.Data = new
                        {
                            Success = true,
                            Order   = newOrder
                        };
                    }
                    else
                    {
                        jsonResult.Data = new
                        {
                            Success = false,
                            Message = "System can not take any order."
                        };
                    }
                }
                else
                {
                    jsonResult.Data = new
                    {
                        Success = false,
                        Message = "Invalid products in cart."
                    };
                }
            }
            else
            {
                jsonResult.Data = new
                {
                    Success = false,
                    Message = string.Join("\n", ModelState.Values
                                          .SelectMany(x => x.Errors)
                                          .Select(x => x.ErrorMessage))
                };
            }

            return(jsonResult);
        }