public override string ToString() { PayfabricCustomerResponseModel model = new PayfabricCustomerResponseModel(); if (CustomerPaymentDetails != null && CustomerPaymentDetails.Count > 0) { model = CustomerPaymentDetails.FirstOrDefault(); } return(string.Format("Message:{0},Result:{1},Key:{2},Token:{3},CustomerPaymentDetails:{4},", model.ToString())); }
//verifies the details entered and stores the details in the database public async Task <ActionResult> MakePayment(PaymentViewModel model) { if (ModelState.IsValid) { //checks that the data passed in is not empty if (model.CardNumber != null && model.NameOnCard != null && model.BookingId != null && model.Cost != null) { //creates a payment detail object to store the users payment details var userId = User.Identity.GetUserId(); CustomerPaymentDetails paymentDetails = new CustomerPaymentDetails(); paymentDetails.CardNumber = model.CardNumber; paymentDetails.NameOnCard = model.NameOnCard; paymentDetails.UserId = userId; //retrieves the booking being paid for from the database and updates it Booking booking = new Booking(); booking = db.Booking.Where(b => b.id == model.BookingId).First(); booking.Status = "Paid"; booking.Lessons = await db.Lesson.Where(l => l.BookingId == booking.id).ToListAsync(); //checks if the payment details are already in the database if not adds them to the database if ((db.CustomerPaymentDetails.Find(paymentDetails.CardNumber) == null)) { db.CustomerPaymentDetails.Add(paymentDetails); db.Entry(paymentDetails).State = EntityState.Added; } //if the payment details exist in the database ensures that the users name matchs the name stored in the database else { var name = db.CustomerPaymentDetails.Find(paymentDetails.CardNumber).NameOnCard; //returns an appropriate error message if the names do not match if ((name != paymentDetails.NameOnCard)) { this.AddNotification("Payment Failed Name on card did not match our records", NotificationType.ERROR); return(RedirectToAction("Index", "Home")); } var id = db.CustomerPaymentDetails.Find(paymentDetails.CardNumber).UserId; if (id != paymentDetails.UserId) { this.AddNotification("Sorry but this card belongs to another user", NotificationType.ERROR); return(RedirectToAction("Index", "Home")); } } //saves changes to the database, returns the user to the home page and displays a success message db.Entry(booking).State = EntityState.Modified; db.SaveChanges(); this.AddNotification("Payment for booking " + model.BookingId + " processed succesfully", NotificationType.SUCCESS); return(RedirectToAction("Index", "Home")); } } //returns the user to the home page with an error message if the model is not valid this.AddNotification("Payment for booking " + model.BookingId + " was unable to be processed succesfully, please try again later. You may continute your payment through view your bookings", NotificationType.ERROR); return(RedirectToAction("Index", "Home")); }