payment
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
from flask import render_template, request, redirect, url_for, send_from_directory
|
from flask import render_template, request, redirect, url_for, send_from_directory, flash
|
||||||
from flask_login import login_user, login_required, logout_user, current_user
|
from flask_login import login_user, login_required, logout_user, current_user
|
||||||
from app import app, db, bcrypt, login_manager
|
from app import app, db, bcrypt, login_manager
|
||||||
from models import User, Listing, Booking, Image, Document, Amenities
|
from models import User, Listing, Booking, Image, Document, Amenities
|
||||||
@@ -227,10 +227,11 @@ def booking(listing_id):
|
|||||||
user_document = Document.query.filter_by(user_id=current_user.id).first()
|
user_document = Document.query.filter_by(user_id=current_user.id).first()
|
||||||
if not user_document or not user_document.verified:
|
if not user_document or not user_document.verified:
|
||||||
return redirect(url_for('upload_document'))
|
return redirect(url_for('upload_document'))
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
check_in = datetime.strptime(request.form['check_in'], '%Y-%m-%d')
|
check_in = datetime.strptime(request.form['check_in'], '%Y-%m-%d').date()
|
||||||
check_out = datetime.strptime(request.form['check_out'], '%Y-%m-%d')
|
check_out = datetime.strptime(request.form['check_out'], '%Y-%m-%d').date()
|
||||||
|
guests = int(request.form['guests'])
|
||||||
|
|
||||||
for start_date, end_date in unavailable_dates:
|
for start_date, end_date in unavailable_dates:
|
||||||
if (check_in.strftime('%Y-%m-%d') >= start_date and check_in.strftime('%Y-%m-%d') <= end_date) or \
|
if (check_in.strftime('%Y-%m-%d') >= start_date and check_in.strftime('%Y-%m-%d') <= end_date) or \
|
||||||
@@ -239,21 +240,46 @@ def booking(listing_id):
|
|||||||
try:
|
try:
|
||||||
if check_out <= check_in:
|
if check_out <= check_in:
|
||||||
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates, error="Check-out date must be after check-in date.")
|
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates, error="Check-out date must be after check-in date.")
|
||||||
|
|
||||||
guests = int(request.form['guests'])
|
|
||||||
if guests > listing.guests:
|
if guests > listing.guests:
|
||||||
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates, error="Number of guests exceeds maximum allowed.")
|
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates, error="Number of guests exceeds maximum allowed.")
|
||||||
|
|
||||||
booking = Booking(user_id=current_user.id, listing_id=listing_id, check_in=check_in, check_out=check_out, guests=guests)
|
total_days = (check_out - check_in).days
|
||||||
db.session.add(booking)
|
total_price = total_days * listing.price
|
||||||
db.session.commit()
|
|
||||||
return redirect(url_for('home'))
|
return redirect(url_for('payment', listing_id=listing.id, check_in=check_in.isoformat(), check_out=check_out.isoformat(), guests=guests, total_price=total_price))
|
||||||
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates, error="Invalid date format.")
|
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates, error="Invalid date format.")
|
||||||
|
|
||||||
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates)
|
return render_template('booking.html', listing=listing, unavailable_dates=unavailable_dates)
|
||||||
|
|
||||||
|
@app.route('/payment', methods=['GET', 'POST'])
|
||||||
|
@login_required
|
||||||
|
def payment():
|
||||||
|
listing_id = request.args.get('listing_id', type=int)
|
||||||
|
check_in = request.args.get('check_in')
|
||||||
|
check_out = request.args.get('check_out')
|
||||||
|
guests = request.args.get('guests', type=int)
|
||||||
|
total_price = request.args.get('total_price', type=float)
|
||||||
|
|
||||||
|
if request.method == 'POST':
|
||||||
|
flash("Payment successful!", "success")
|
||||||
|
|
||||||
|
booking = Booking(
|
||||||
|
user_id=current_user.id,
|
||||||
|
listing_id=listing_id,
|
||||||
|
check_in=datetime.strptime(check_in, '%Y-%m-%d'),
|
||||||
|
check_out=datetime.strptime(check_out, '%Y-%m-%d'),
|
||||||
|
guests=guests
|
||||||
|
)
|
||||||
|
db.session.add(booking)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return redirect(url_for('home'))
|
||||||
|
|
||||||
|
return render_template('payment.html', total_price=total_price)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/view/<int:listing_id>', methods=['GET'])
|
@app.route('/view/<int:listing_id>', methods=['GET'])
|
||||||
def view_listing(listing_id):
|
def view_listing(listing_id):
|
||||||
listing = Listing.query.get_or_404(listing_id)
|
listing = Listing.query.get_or_404(listing_id)
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
{% extends 'base.html' %}
|
||||||
|
{% block title %}Payment{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div class="container mt-4">
|
||||||
|
<h1 class="text-center">Payment</h1>
|
||||||
|
<p class="text-center">Total Price: <strong>${{ total_price }}</strong></p>
|
||||||
|
|
||||||
|
<form method="POST">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="card_number" class="form-label">Card Number</label>
|
||||||
|
<input type="text" class="form-control" id="card_number" name="card_number" required placeholder="1234 5678 9012 3456">
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-6 mb-3">
|
||||||
|
<label for="expiry_date" class="form-label">Expiry Date</label>
|
||||||
|
<input type="text" class="form-control" id="expiry_date" name="expiry_date" required placeholder="MM/YY">
|
||||||
|
</div>
|
||||||
|
<div class="col-md-6 mb-3">
|
||||||
|
<label for="cvv" class="form-label">CVV</label>
|
||||||
|
<input type="text" class="form-control" id="cvv" name="cvv" required placeholder="123">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-success w-100">Pay Now</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user