Ejemplo n.º 1
0
        public async Task <JsonResult> SubmitCheckout()
        {
            try
            {
                string url      = GlobalVariable.url + "getcustomer/" + HttpContext.User.Identity.Name;
                string json     = await new GlobalVariable().GetApiAsync(url);
                var    customer = JsonConvert.DeserializeObject <CUSTOMER>(json);

                var order = new ORDER()
                {
                    CustomerID = customer.CustomerID,
                    Total      = await GetTotal()
                };

                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://localhost:44337/api/");

                    var postTask = client.PostAsJsonAsync <ORDER>("addorder", order);

                    postTask.Wait();

                    var result = postTask.Result;
                    if (result.IsSuccessStatusCode)
                    {
                        var orderjson = await result.Content.ReadAsStringAsync();

                        var orderdetail = new OrderDetailDao();
                        foreach (var item in (List <CartSession>)Session["cart"])
                        {
                            _ = await orderdetail.AddOrderDetailProc(int.Parse(orderjson), item.ProductID, item.SizeID, item.Quantity);
                        }

                        Session["cart"] = null;
                        return(Json(new { Success = true, ID = order }, JsonRequestBehavior.AllowGet));
                    }
                }
                return(Json(new { Success = false }, JsonRequestBehavior.AllowGet));
            }
            catch
            {
                return(Json(new { Success = false }, JsonRequestBehavior.AllowGet));
            }
        }