Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EliasOPrado/765cc959f1303322a748c3e489171ad2 to your computer and use it in GitHub Desktop.
Save EliasOPrado/765cc959f1303322a748c3e489171ad2 to your computer and use it in GitHub Desktop.
from django.shortcuts import render, get_object_or_404, reverse, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from .forms import MakePaymentForm, OrderForm
from .models import OrderLineItem
from django.conf import settings
from django.utils import timezone
from tour_store.models import Destinations
import stripe
# Create your views here.
stripe.api_key = settings.STRIPE_SECRET
#@login_required()
def checkout(request):
if request.method == "POST":
# call the two forms that will be used
order_form = OrderForm(request.POST)
payment_form = MakePaymentForm(request.POST)
print(payment_form.errors)
print(order_form.errors)
# Then will check if both forms are valid
if order_form.is_valid() and payment_form.is_valid():
order = order_form.save(commit=False)
order.date = timezone.now()
order.save()
cart = request.session.get('cart', {})
total = 0
for id, quantity in cart.items():
destination = get_object_or_404(Destinations, pk=id)
total += quantity * destination.price
order_line_item = OrderLineItem(
order=order,
destination=destination,
quantity=quantity
)
order_line_item.save()
try:
customer = stripe.Charge.create(
amount=int(total * 100),
currency="EUR",
description=request.user.email,
card=payment_form.cleaned_data['stripe_id']
)
except stripe.error.CardError:
messages.error(request, "Your card was declined!")
if customer.paid:
messages.error(request, "You have successfully paid")
request.session['cart'] = {}
return redirect(reverse('destination'))
else:
messages.error(request, "Unable to take payment")
else:
messages.error(request, "We were unable to take a payment with that card!")
else:
payment_form = MakePaymentForm()
order_form = OrderForm()
print(payment_form.errors)
print(order_form.errors)
return render(request, "checkout.html", {"order_form": order_form, "payment_form": payment_form, "publishable": settings.STRIPE_PUBLISHABLE})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment