public RedirectToRouteResult AddToCart(Cart cart, int ID, string returnUrl) { Product product = repository.Products.FirstOrDefault(p => p.ID == ID); if (product != null) { cart.AddItem(product, 1); } return RedirectToAction("Index", new { returnUrl }); }
public ActionResult Checkout(Cart cart) { var cartItems = new List<Dictionary<string, object>>(); foreach (Cart.CartLine item in GetCart().Lines) { cartItems.Add(new Dictionary<string, object> { { "reference", item.Product.ArticleNumber.ToString() }, { "name", item.Product.Name }, { "quantity", item.Quantity }, { "unit_price", (int)item.Product.Price * 100}, { "tax_rate", 2500 } }); } var cart_info = new Dictionary<string, object> { { "items", cartItems } }; var data = new Dictionary<string, object> { { "cart", cart_info } }; var merchant = new Dictionary<string, object> { { "id", "5214" }, { "back_to_store_uri", "https://localhost:44300/" }, { "terms_uri", "https://localhost:44300/Cart/Terms" }, { "checkout_uri", "https://localhost:44300/Cart/Checkout" }, { "confirmation_uri", "https://localhost:44300/Cart/ThankYou" + "?klarna_order_id={checkout.order.id}" }, { "push_uri", "https://localhost:44300/Cart/push" + "?klarna_order_id={checkout.order.id}" } }; data.Add("purchase_country", "SE"); data.Add("purchase_currency", "SEK"); data.Add("locale", "sv-se"); data.Add("merchant", merchant); var connector = Connector.Create("Y7mNXnxIdYxXU6c", Connector.TestBaseUri); Order order = new Order(connector); order.Create(data); order.Fetch(); // Store order id of checkout in session object. // session["klarna_order_id"] = order.GetValue("id") as string; // Display checkout var gui = order.GetValue("gui") as JObject; var snippet = gui["snippet"]; return View(snippet); }
public void Calculate_Cart_Total() { // Arrange - create some test products Product p1 = new Product { ID = 1, Name = "P1", Price = 100M }; Product p2 = new Product { ID = 2, Name = "P2", Price = 40M }; // Arrange - create a new cart Cart target = new Cart(); // Act target.AddItem(p1, 1); target.AddItem(p2, 1); target.AddItem(p1, 3); decimal result = target.ComputeTotalValue(); // Assert Assert.AreEqual(result, 440M); }
public void Can_Clear_Contents() { // Arrange - create some test products Product p1 = new Product { ID = 1, Name = "P1", Price = 100M }; Product p2 = new Product { ID = 2, Name = "P2", Price = 50M }; // Arrange - create a new cart Cart target = new Cart(); // Arrange - add some items target.AddItem(p1, 1); target.AddItem(p2, 1); // Act - reset the cart target.Clear(); // Assert Assert.AreEqual(target.Lines.Count(), 0); }
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { // get the Cart from the session Cart cart = null; if (controllerContext.HttpContext.Session != null) { cart = (Cart)controllerContext.HttpContext.Session[sessionKey]; } // create the Cart if there wasn't one in the session data if (cart == null) { cart = new Cart(); if (controllerContext.HttpContext.Session != null) { controllerContext.HttpContext.Session[sessionKey] = cart; } } // return the cart return cart; }
public void Can_Add_Quantity_For_Existing_Lines() { // Arrange - create some test products Product p1 = new Product { ID = 1, Name = "P1" }; Product p2 = new Product { ID = 2, Name = "P2" }; // Arrange - create a new cart Cart target = new Cart(); // Act target.AddItem(p1, 1); target.AddItem(p2, 1); target.AddItem(p1, 10); ProductsMVC.Models.Cart.CartLine[] results = target.Lines.OrderBy(c => c.Product.ID).ToArray(); // Assert Assert.AreEqual(results.Length, 2); Assert.AreEqual(results[0].Quantity, 11); Assert.AreEqual(results[1].Quantity, 1); }
public void Can_Remove_Line() { // Arrange - create some test products Product p1 = new Product { ID = 1, Name = "P1" }; Product p2 = new Product { ID = 2, Name = "P2" }; Product p3 = new Product { ID = 3, Name = "P3" }; // Arrange - create a new cart Cart target = new Cart(); // Act target.AddItem(p1, 1); target.AddItem(p2, 3); target.AddItem(p3, 5); target.AddItem(p2, 1); target.RemoveLine(p2); // Assert Assert.AreEqual(target.Lines.Where(c => c.Product == p2).Count(), 0); Assert.AreEqual(target.Lines.Count(), 2); }
public void ProductAddedToCartPass() { // Arrange - create some test products Product p1 = new Product { ID = 1, Name = "P1" }; Product p2 = new Product { ID = 2, Name = "P2" }; // Arrange - create a new cart Cart target = new Cart(); // Act target.AddItem(p1, 1); target.AddItem(p2, 1); ProductsMVC.Models.Cart.CartLine[] results = target.Lines.ToArray(); // Assert Assert.AreEqual(results.Length, 2); Assert.AreEqual(results[0].Product, p1); Assert.AreEqual(results[1].Product, p2); }
public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl) { Product product = db.Products .FirstOrDefault(p => p.ID == productId); if (product != null) { cart.RemoveLine(product); } return RedirectToAction("Index", new { returnUrl }); }
public ViewResult Index(Cart cart, string returnUrl) { return View(new CartIndexViewModel { ReturnUrl = returnUrl, Cart = cart }); }
private Cart GetCart() { Cart cart = (Cart)Session["Cart"]; if (cart == null) { cart = new Cart(); Session["Cart"] = cart; } return cart; }
public PartialViewResult Summary(Cart cart) { return PartialView(cart); }
public ViewResult Checkout(Cart cart) { var cartItems = new List<Dictionary<string, object>>(); foreach(CartLine item in cart.Lines) { cartItems.Add(new Dictionary<string, object> { { "reference", item.Product.ArticleNumber.ToString() }, { "name", item.Product.Name }, { "quantity", item.Quantity }, { "unit_price", (int)item.Product.Price * 100 }, { "tax_rate", 2500 } }); } cartItems.Add( new Dictionary<string, object> { { "type", "shipping_fee" }, { "reference", "SHIPPING" }, { "name", "Shipping Fee" }, { "quantity", 1 }, { "unit_price", 4900 }, { "tax_rate", 2500 } }); var cart_info = new Dictionary<string, object> { { "items", cartItems } }; var data = new Dictionary<string, object> { { "cart", cart_info } }; var merchant = new Dictionary<string, object> { { "id", "5160" }, { "back_to_store_uri", "http://127.0.0.1:53284/" }, { "terms_uri", "http://127.0.0.1:53284/Cart/Terms" }, { "checkout_uri", "http://127.0.0.1:53284/sv/Cart/Checkout" }, { "confirmation_uri", "http://127.0.0.1:53284/sv/Cart/OrderConfirmed" + "?klarna_order_id={checkout.order.id}" }, { "push_uri", "https://snowroller.azurewebsites.net/Home/Callback" + "?secret="+Shared_Secret+"&klarna_order_id={checkout.order.id}" } }; data.Add("purchase_country", "SE"); data.Add("purchase_currency", "SEK"); data.Add("locale", "sv-se"); data.Add("merchant", merchant); var connector = Connector.Create(Shared_Secret, Connector.TestBaseUri); Order order = new Order(connector); order.Create(data); order.Fetch(); // Store order id of checkout in session object. Session.Add("klarna_order_id", order.GetValue("id")); // Display checkout var gui = order.GetValue("gui") as JObject; var snippet = gui["snippet"]; return View(snippet); }