Example #1
0
        public async Task <JsonResult> SubmitCheckoutCustomer(CheckoutModel model)
        {
            if (ModelState.IsValid)
            {
                var customer = await GlobalVariable.GetCustomer(HttpContext.User.Identity.Name, CustomerController.CustomerToken);

                if (customer == null)
                {
                    return(Json(new JsonStatus()
                    {
                        Status = false,
                        Message = "Can not get your info, try to log in again",
                        StatusCode = -1
                    }, JsonRequestBehavior.AllowGet));
                }
                var order = new ORDER
                {
                    CustomerID      = customer.CustomerID,
                    CustomerName    = customer.CustomerName,
                    CustomerAddress = model.CustomerAddress,
                    CustomerPhone   = model.CustomerPhone,
                    OrderStatusID   = 1,
                    Total           = await GetTotal(),
                    OrderDate       = DateTime.Now
                };
                var json = JsonConvert.SerializeObject(order);
                var data = new StringContent(json, Encoding.UTF8, "application/json");

                string url = GlobalVariable.url + "api/order/create";

                try
                {
                    var client = new HttpClient();
                    //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AdminController.AdminToken);
                    client.BaseAddress = new Uri(url);

                    var response = await client.PostAsync(url, data);

                    if (response.IsSuccessStatusCode)
                    {
                        var orderid = await response.Content.ReadAsStringAsync();

                        var result = AddOrderDetail(Convert.ToInt32(orderid)); Session["cart"] = null;
                        return(Json(new JsonStatus()
                        {
                            Status = true,
                            Message = "Create Success",
                            StatusCode = (int)response.StatusCode
                        }, JsonRequestBehavior.AllowGet));
                    }
                    else
                    {
                        return(Json(new JsonStatus()
                        {
                            Status = false,
                            Message = await response.Content.ReadAsStringAsync(),
                            StatusCode = (int)response.StatusCode
                        }, JsonRequestBehavior.AllowGet));
                    }
                }
                catch
                {
                    return(Json(new JsonStatus()
                    {
                        Status = false,
                        Message = "Error while creating your order",
                        StatusCode = 0
                    }, JsonRequestBehavior.AllowGet));
                }
            }
            else
            {
                return(Json(new JsonStatus()
                {
                    Status = false,
                    Message = "Invalid input"
                }, JsonRequestBehavior.AllowGet));
            }
        }