//validate cart in case of invalid return to check cart again or pay //in case of error make order cancel public async Task <IActionResult> CheckOut() { int initCount = cartServices.GetCartList(UserName).Count; int resCount = orderServices.ValidatingCartBeforAddingToCart(UserName); if (initCount != resCount) { return(RedirectToAction(nameof(Index), "Cart")); } var order = orderServices.GetOrder(UserName); int count = orderServices.AddProductToCart(UserName); var totalPrice = orderServices.GetOrder(UserName)?.AmountBuy; if (!totalPrice.HasValue) { return(View("Error")); } if (initCount != orderServices.GetOrder(UserName, null, true).OrderDetails.Count) { if (orderServices.CancelingOpenOrder(UserName)) { return(View("Error", new ErrorViewModel() { RequestId = "Something wrong" })); } return(View("Error")); } var payment = await new Payment(totalPrice.Value) .PaymentRequest("متن تست موقع خرید", Url.Action(nameof(CheckOutCallback), "Orders", new { amount = totalPrice }, Request.Scheme)); if (payment.Status == 100) { if (orderServices.AddAuthorityToOrder(UserName, payment.Authority)) { return((IActionResult)Redirect(payment.Link)); } orderServices.CancelingOpenOrder(UserName); return(BadRequest("خطا در پرداخت")); } else { orderServices.CancelingOpenOrder(UserName); return(BadRequest($"خطا در پرداخت. کد خطا:{payment.Status}")); } }
//return orderDetails to cart public void AddOrderDetailsToCart(string userName) { var order = orderServices.GetOrder(userName, null, true); if (order != null) { var orderDetails = order.OrderDetails.ToList(); foreach (OrderDetail od in orderDetails) { addToCart(userName, od.Product.Id, od.Quantity); } orderServices.CancelingOpenOrder(userName); } }