예제 #1
0
def checkout(request):

    new_booking = Bookings(
        bookingType=request.session['type'],
        seatsChosen=request.session['seatsChosen'],
        bookingPayment=request.session['amount'],
        user=request.user,
    )
    trip = Trip(destination=request.session['destination'],
                duration=request.session['duration'],
                price=request.session['price'],
                hotelName=request.session['hotelName'],
                totalCost=request.session['amount'])
    if request.method == "POST":
        token = request.POST.get("stripeToken")
    try:
        charge = stripe.Charge.create(amount=request.session['amount'],
                                      currency="gbp",
                                      source=token,
                                      description="booking successfully made")
        new_booking.charge_id = charge.id
    except stripe.error.CardError as ce:
        return False, ce
    else:
        new_booking.save() and trip.save()

        send_mail(
            'Walton Happy Travels Package Booking',
            'Thank you ' + request.user.email +
            ' for Booking with Walton Happy Travels. Please find below the ' +
            'details of your Booking' + "\n" + 'Details:' + "\n" +
            'destination: ' + str(request.session['destination']) + "\n" +
            'duration: ' + str(request.session['duration']) + ' Days' + "\n" +
            'Departure Date: ' + str(request.session['departureDate']) + "\n" +
            'Hotel Name: ' + str(request.session['hotelName']) + "\n" +
            'Number of People: ' + str(request.session['seatsChosen']) + "\n" +
            'Total Cost: £' + request.session['amount'][:-2],
            '*****@*****.**', ['*****@*****.**'],
            fail_silently=False)

        return redirect("home")
예제 #2
0
def checkout(
    request,
    show_id,
):
    show = get_object_or_404(Shows, pk=show_id)
    bookings = Bookings.objects.all()
    seatId = request.session.get('seatId')
    sId = (seatId['seatId'])
    seatName = (seatId['seatName'])
    #converting strings into array
    sIdx = sId.split(',')
    # removing last empty item in the array
    sIdx = sIdx[:-1]

    #converting array into string
    # sIdx = ''.join(sId)

    f_seats = Seats.objects.filter(id__in=sIdx)
    price = Seats.objects.filter(id__in=sIdx).aggregate(
        Sum('price'))['price__sum']
    tickets = len(sIdx)
    eventId = Shows.objects.values('event_id').filter(id=show_id)
    showId = Shows.objects.values('id').filter(id=show_id)

    context = {
        'f_seats': f_seats,
        'price': price,
        'show': show,
    }

    if request.method == 'POST':
        seats = Seats.objects.filter(id__in=sIdx)
        seats.update(is_booked=True)

        c_name = request.POST['name']
        c_email = request.POST['email']
        c_phone = request.POST['phoneNumber']
        eventId = eventId
        showId = showId
        seatsId = sId
        seatsName = seatName
        seatsPrice = price

        if bookings.filter(booked_seats=seatsId).exists():
            messages.error(request,
                           'Selected seat already booked, Please try again !')
            return redirect('home')
        else:
            #submitting form to the database
            bookings = Bookings(c_name=c_name,
                                c_email=c_email,
                                c_phone=c_phone,
                                event_id=eventId,
                                show_id=showId,
                                booked_seats=seatsId,
                                booked_seats_name=seatsName,
                                booked_price=seatsPrice,
                                booked_seats_count=tickets)

            b_seats = Bookings.objects.filter(booked_seats=sId)
            print(b_seats)

            context = {
                'f_seats': f_seats,
                'price': price,
                'tickets': tickets,
                'b_seats': b_seats,
                'show': show,
                'c_name': c_name,
                'c_email': c_email,
                'c_phone': c_phone,
                'booked_seats': seatsId,
            }

            bookings.save()
            return render(request, 'events/booking_confirmation.html', context)
    return render(request, 'events/checkout.html', context)