// GET: PaymentFailure public ActionResult PaymentFailure(string token, string orderId) { if (string.IsNullOrEmpty(token)) { return(RedirectToAction("index", "home")); } if (string.IsNullOrEmpty(orderId)) { return(RedirectToAction("index", "home")); } var orderModel = OrderStrCrypto.Decrypt(orderId); if (orderModel == null) { return(RedirectToAction("index", "home")); } var order = _db.Orders.FirstOrDefault(x => x.Id == orderModel.OrderId && x.UserId == orderModel.UserId); if (order == null) { return(RedirectToAction("index", "home")); } var result = PayPalService.GetPayment(token); order.Status = OrderStatus.Canceled; order.OrderDate = DateTimeOffset.UtcNow; _db.SaveChanges(); return(RedirectToAction("index", "Cart")); }
// GET: PaymentGateway public ActionResult ExpressCheckoutSuccess(string token, string orderId) { if (string.IsNullOrEmpty(token)) { return(RedirectToAction("index", "home")); } if (string.IsNullOrEmpty(orderId)) { return(RedirectToAction("index", "home")); } var orderModel = OrderStrCrypto.Decrypt(orderId); if (orderModel == null) { return(RedirectToAction("index", "home")); } var order = _db.Orders.FirstOrDefault(x => x.Id == orderModel.OrderId && x.UserId == orderModel.UserId); if (order == null) { return(RedirectToAction("index", "home")); } var result = PayPalService.GetPayment(token); order.Status = result.Success ? OrderStatus.Success : OrderStatus.Fail; order.OrderDate = result.OrderDate; order.TotalCost = result.Success ? result.OrderTotal : (decimal?)null; _db.SaveChanges(); if (result.Success) { Session["cart"] = null; return(RedirectToAction("Thankyou", "Cart")); } else { Session["cart"] = null; return(RedirectToAction("Index", "Cart", new { message = result.ErrorMessage })); } }
public ActionResult Index(PaymentCartViewModel model) { var cart = (CartViewModel)Session["Cart"]; //if (cart == null) return RedirectToAction("Index"); model.Cart = cart; if (ModelState.IsValid) { var userId = User.Identity.GetUserId(); var user = UserManager.FindById(userId); var order = new Order { UserId = user.Id, Projects = model.Cart.Items.Select(x => x.Project).ToList() }; _db.Orders.Add(order); _db.SaveChanges(); var orderCrypto = new OrderStrCrypto { OrderId = order.Id, UserId = user.Id }; var orderId = orderCrypto.Encrypt(); var paymentFailureUrl = string.Format("{0}://{1}/PaymentGateway/PaymentFailure?orderId={2}", Request.Url.Scheme, Request.Url.Authority, orderId); var expressCheckoutSuccessUrl = string.Format("{0}://{1}/PaymentGateway/ExpressCheckoutSuccess?orderId={2}", Request.Url.Scheme, Request.Url.Authority, orderId); var result = PayPalService.Pay(user.Email, model.Cart, expressCheckoutSuccessUrl, paymentFailureUrl); if (result.Success) { return(Redirect(result.RedirectUrl)); } else { model.ErrorMessage = result.ErrorMessage; } } return(View("Cart", model)); }