From 299b008266ba2dbaa22c1365f4b4ce2d4e12185d Mon Sep 17 00:00:00 2001
From: Ethan-clay03 <ethanclay2017@gmail.com>
Date: Fri, 14 Feb 2025 14:38:57 +0000
Subject: [PATCH] Begin route for payment success screen

---
 app/bookings/routes.py | 20 ++++++++++++++++++--
 app/models/bookings.py |  6 +++++-
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/app/bookings/routes.py b/app/bookings/routes.py
index e5a2bf0..668c626 100644
--- a/app/bookings/routes.py
+++ b/app/bookings/routes.py
@@ -208,6 +208,21 @@ def listing(id):
         total_cost=total_cost
     )
 
+# This route should be used after show_listing if used internally as this clears the ajax parameters before redirecting the user
+@bp.route('/payment/successful/<int:id>', methods=['GET'])
+@permission_required(user_permission)
+def payment_complete(id):
+    
+    booking = Bookings.search_booking(id)
+
+    if booking.user_id != g.identity.id:
+        flash ("Unable to load payment, please check your booking ID", 'error')
+        return redirect(url_for('main.index'))
+
+    return render_template(
+        'bookings/payment_success.html',
+    )
+
 @bp.route('/checkout_post', methods=['POST'])
 @permission_required(user_permission)
 def checkout_post():
@@ -240,7 +255,8 @@ def checkout_post():
         return redirect(url_for('bookings.listing', id=listing_id))
 
     try:
-        if Bookings.create_booking(listing_id, user_id, total_cost, seat_type, num_seats):
+        booking = Bookings.create_booking(listing_id, user_id, total_cost, seat_type, num_seats)
+        if booking:
             # Update availability
             ListingAvailability.update_availability(listing_id, depart_date_obj, seat_type, num_seats)
             db.session.commit()
@@ -253,7 +269,7 @@ def checkout_post():
         error_logger.debug(f"Error processing booking: {e}")
         flash('Booking failed. Please try again.', 'error')
 
-    return redirect(url_for('bookings.listings'))
+    return redirect(url_for('bookings.payment_complete', id=booking.id))
 
 
 def validate_payment(card_number, card_expiry, card_cvc):
diff --git a/app/models/bookings.py b/app/models/bookings.py
index 351f02a..eb1e650 100644
--- a/app/models/bookings.py
+++ b/app/models/bookings.py
@@ -25,8 +25,12 @@ class Bookings(UserMixin, db.Model):
             )
             db.session.add(new_booking)
             db.session.commit()
-            return True
+            return new_booking
         except Exception as e:
             db.session.rollback()
             print(f"Error creating booking: {e}")
             return False
+
+    @classmethod
+    def search_booking(cls, id):
+        return cls.query.get(id)
-- 
GitLab