[ValidateAntiForgeryToken] /* [1] */ public async Task <ActionResult> BookJourney( [Bind("JourneyID,UserID,Passengers")] Booking booking, string SeatsReceived, string CouponCode, string ReturnJourneyInput, string Name, string CardNumber, string ExpirationYear, string ExpirationMonth, string CVV ) { /* [2] */ var user = await _userManager.GetUserAsync(HttpContext.User); /* [5] */ // the journey that is being booked Journey journey = _context.Journeys .Include(j => j.Train) .Include(j => j.Seats) .Single(j => j.JourneyID == booking.JourneyID); // Retriving a list of journeys that user may want to return from. ViewBag.Journeys = _context.Journeys .Where(j => j.Departure == journey.Destination && j.DepartureTime > journey.ArrivalTime); Booking[] bookings = _context.Bookings.ToArray(); // Making sure that the journey is reserved before the departure time if (journey.DepartureTime > DateTime.Now) { int TotalPassengers = 0; foreach (Booking b in bookings) { TotalPassengers = TotalPassengers + b.Passengers; } // initital booking cost; booking.Cost = booking.Passengers * journey.Price; // Check if the user is using a valid coupon and, if so, // apply it to the price. if (!String.IsNullOrEmpty(CouponCode)) { booking.Cost = coupon.ApplyCoupon(_context, booking.Cost, CouponCode); } int SeatsTaken = TotalPassengers + booking.Passengers; int TrainCapacity = journey.Train.Capacity; if (ModelState.IsValid && SeatsTaken <= TrainCapacity && journey.IsCanceled == false) /* [3] */ // check if user wants to book a return ticket { if (!String.IsNullOrEmpty(ReturnJourneyInput)) { // making sure that user has entered a number if (int.TryParse(ReturnJourneyInput, out int JourneyID)) { BookReturnJourney(booking, user, JourneyID); TempData["BookReturnTicket"] = "You have successfully booked the trip from " + journey.Destination + " to " + journey.Departure; } } _context.Add(booking); // check if seat(s) reservations are allowed, and if so, // check if a seats reservation request is provided by the user if (journey.AllowSeatReservation) { // Default state of `SeatsReceived` input in the client-side is 0 if (SeatsReceived == "0") { SeatsReceived = null; } if (!string.IsNullOrEmpty(SeatsReceived)) { // @param SeatsReceived: string of seats requested seperated by comma: Ex. "5,2,6" seat.reserveSeats(_context, user.Id, booking.BookingID, journey.JourneyID, SeatsReceived); // default price for a reserved seat // hard-coded, should be provided by the system administrator dynamically booking.Cost += 5; } } if (long.TryParse(CardNumber, out long cardNumber) && int.TryParse(ExpirationYear, out int expirationYear) && int.TryParse(ExpirationMonth, out int expirationMonth) && int.TryParse(CVV, out int cvv) ) { // if the journey is paid if (payment.Pay(Name, cardNumber, expirationYear, expirationMonth, cvv)) { await _context.SaveChangesAsync(); } else { TempData["PaymentError"] = Name.GetType() + " " + CardNumber.GetType() + " " + ExpirationYear.GetType() + " " + ExpirationMonth.GetType() + " " + CVV.GetType(); return(View("Views/Journeys/Details.cshtml", journey)); } } else { TempData["PaymentError"] = "Invalid credit card information."; return(View("Views/Journeys/Details.cshtml", journey)); } } else { if (journey == null) { return(NotFound()); } TempData["Error"] = "Something went wrong"; return(View("Views/Journeys/Details.cshtml", journey)); } } TempData["Success"] = "You have successfully booked the trip from " + journey.Departure + " to " + journey.Destination; return(RedirectToAction("Index")); }
public void ApplyCoupon(Coupon coupon) { coupon.ApplyCoupon(this); }