// GET: Payments/Create public IActionResult Create(int?id) { OrderPaymentVM orderPayment = new OrderPaymentVM(); if (id != null) { orderPayment.order = SearchOrder(id); } //ViewData["CustomerID"] = new SelectList(_context.Customers, "id", "id"); return(View(orderPayment)); }
public async Task <IActionResult> Create([Bind("payment, order")] OrderPaymentVM orderPayment) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(_url); //HTTP GET // PizzaAPI.Controllers.CustomerController c = new PizzaAPI.Controllers.CustomerController(_context); int id = Convert.ToInt32(User.Claims.First().Value); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); Customer cust = SearchCustomerId(User.Claims.First().Value); //Payment payments = GetAllPayment().FirstOrDefault(x => x.CustomerId == cust.CustomerId); Order order = SearchOrder(Convert.ToInt32(this.RouteData.Values.Values.Last())); orderPayment.order = order; if (cust == null || GetAllPayment().FirstOrDefault(x => x.CustomerId == cust.CustomerId) != null) { ModelState.AddModelError(string.Empty, "Customer or payment information no exist! Please use the create one or update it."); } else { Payment payment = orderPayment.payment; var postTask = client.PostAsJsonAsync("Payments/" + cust.CustomerId, payment); postTask.Wait(); var result = postTask.Result; if (result.IsSuccessStatusCode) { return(RedirectToAction("Confirmation", new { id = order.OrderId, })); } ModelState.AddModelError(string.Empty, "Server Error. Please contact administrator."); } } //ViewData["CustomerID"] = new SelectList(_context.Customers, "id", "id", payment.CustomerID); return(View(orderPayment)); }