public ActionResult CreateDeliveyCookie(PlaceOrderCashOnDeliveryViewModel model) { var newOrder = new CreditCardInfoViewModel(); 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()); } } 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.TotalAmmount = newOrder.OrderItems.Sum(x => (x.ItemPrice * x.Quantity)); System.Web.HttpCookie DeliveryInfo = new System.Web.HttpCookie("DeliveryInfo"); DeliveryInfo["CustomerID"] = User.Identity.GetUserId(); DeliveryInfo["CustomerName"] = model.FullName; DeliveryInfo["CustomerEmail"] = model.Email; DeliveryInfo["CustomerPhone"] = model.PhoneNumber; DeliveryInfo["CustomerCountry"] = model.Country; DeliveryInfo["CustomerCity"] = model.City; DeliveryInfo["CustomerAddress"] = model.Address; DeliveryInfo["CustomerZipCode"] = model.ZipCode; if (model.PromoID > 0) { var promo = PromosService.Instance.GetPromoByID(model.PromoID); if (promo != null && promo.Value > 0) { if (promo.ValidTill >= DateTime.Now) { DeliveryInfo["PromoID"] = promo.ID.ToString(); if (promo.PromoType == (int)PromoTypes.Percentage) { DeliveryInfo["Discount"] = Math.Round((newOrder.TotalAmmount * promo.Value) / 100).ToString(); } else if (promo.PromoType == (int)PromoTypes.Amount) { DeliveryInfo["Discount"] = promo.Value.ToString(); } } } } Response.Cookies.Add(DeliveryInfo); return(Json(new { message = "Success" })); }
public ActionResult CreditCardCheckout() { JsonResult jsonResult = new JsonResult(); var newOrder = new CreditCardInfoViewModel(); var model = new PlaceOrderCashOnDeliveryViewModel(); var errorDetails = string.Empty; 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 deliveryInfo = Request.Cookies["DeliveryInfo"]; if (deliveryInfo == null) { return(View(newOrder)); } var user = SharedService.Instance.GetUserById(deliveryInfo["CustomerID"]); var name = user.FirstName + " " + user.MiddleName + " " + user.LastName; var payment = PaymentsService.Instance.GetUserPayment(user.Id); //var client = new RestClient("https://api.tap.company/v2/customers"); //client.Timeout = -1; //var request = new RestRequest(Method.GET); //request.AddHeader("Authorization", "Bearer sk_test_09I6oRDbXfC7hdKBmPiGL5Ex"); //request.AddParameter("text/plain", "", ParameterType.RequestBody); //IRestResponse response = client.Execute(request); //Console.WriteLine(response.Content); //string xmlString = response.Content; //Customer account = JsonConvert.DeserializeObject<Customer>(response.Content); if (payment == null) { var client = new RestClient("https://api.tap.company/v2/customers"); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Authorization", "Bearer sk_live_b1IR6r3CNYhKxWaPtLi8mH5u"); request.AddHeader("Content-Type", "application/json"); request.AddParameter("application/json", "{\r\n \"first_name\": \"" + user.FirstName + "\",\r\n \"middle_name\": \"" + user.MiddleName + "\",\r\n \"last_name\": \"" + user.LastName + "\",\r\n \"email\": \"" + user.Email + "\",\r\n \"phone\": {\r\n \"country_code\": \"" + user.CountryCode + "\",\r\n \"number\": \"" + user.PhoneNumber + "\"\r\n },\r\n \"description\": \"test\",\r\n \"metadata\": {\r\n \"udf1\": \"test\"\r\n },\r\n \"currency\": \"USD\"\r\n}", ParameterType.RequestBody); IRestResponse response = client.Execute(request); Console.WriteLine(response.Content); Customer account = JsonConvert.DeserializeObject <Customer>(response.Content); Payment newPayment = new Payment() { UserId = user.Id, TapUserId = account.id }; var paymentToCreate = PaymentsService.Instance.AddUserPayment(newPayment); newOrder.TapId = account.id; } else { newOrder.TapId = payment.TapUserId; } newOrder.CustomerID = user.Id; newOrder.CustomerName = name; newOrder.Firstname = user.FirstName; newOrder.Middlename = user.MiddleName; newOrder.Lastname = user.LastName; newOrder.CustomerEmail = deliveryInfo["Email"]; newOrder.CustomerPhone = user.PhoneNumber; newOrder.CountryCode = user.CountryCode; newOrder.CustomerCountry = deliveryInfo["Country"]; newOrder.CustomerCity = deliveryInfo["City"]; newOrder.CustomerAddress = deliveryInfo["Address"]; newOrder.CustomerZipCode = deliveryInfo["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 (deliveryInfo["PromoID"] != null) { if (Int32.Parse(deliveryInfo["PromoID"]) > 0) { var promo = PromosService.Instance.GetPromoByID(Int32.Parse(deliveryInfo["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.CashOnDelivery; newOrder.OrderHistory = new List <OrderHistory>() { new OrderHistory() { OrderStatus = (int)OrderStatus.Placed, ModifiedOn = DateTime.Now, Note = "Order Placed." } }; newOrder.PlacedOn = DateTime.Now; } else { jsonResult.Data = new { Success = false, Message = "Invalid products in cart." }; } newOrder.NewUrl = Url.Action("CreditCardCheckout", "Orders"); return(View(newOrder)); }