diff --git a/app/bookings/routes.py b/app/bookings/routes.py index e5a2bf0011365df1d94e48f9c2993b4e539ca551..668c62637d988d55648109128856a6d73e3c8710 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 351f02a6a53b7e5225d43ebd2bd9993a42c360dd..eb1e650ce9de892e118ed55de7fcf23cf92b54a6 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)