aboutsummaryrefslogtreecommitdiffstats
path: root/pyblackbird_cc/payments/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'pyblackbird_cc/payments/views.py')
-rw-r--r--pyblackbird_cc/payments/views.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/pyblackbird_cc/payments/views.py b/pyblackbird_cc/payments/views.py
new file mode 100644
index 0000000..2e53af8
--- /dev/null
+++ b/pyblackbird_cc/payments/views.py
@@ -0,0 +1,85 @@
+import stripe
+from django.conf import settings
+from django.contrib.auth.decorators import login_required
+from django.shortcuts import get_object_or_404
+from django.shortcuts import render, redirect
+from django.views import View
+from django.views.generic import TemplateView
+
+from pyblackbird_cc.resources.models import Resource
+from .models import ShoppingCart, CartItem, Price, Product
+
+stripe.api_key = settings.STRIPE_SECRET_KEY
+
+
+class CreateCheckoutSessionView(View):
+ def post(self, request, *args, **kwargs):
+ price = Price.objects.get(id=self.kwargs["pk"])
+ domain = "http://localhost:8000"
+ checkout_session = stripe.checkout.Session.create(
+ payment_method_types=["card"],
+ line_items=[
+ {
+ "price": price.stripe_price_id,
+ "quantity": 1,
+ },
+ ],
+ mode="payment",
+ success_url=domain + "payments/success/",
+ cancel_url=domain + "payments/cancel/",
+ )
+ return redirect(checkout_session.url, code=303)
+
+
+class SuccessView(TemplateView):
+ template_name = "payments/success.html"
+
+
+class CancelView(TemplateView):
+ template_name = "payments/cancel.html"
+
+
+class ProductLandingPageView(TemplateView):
+ template_name = "payments/landingpage.html"
+
+ def get_context_data(self, **kwargs):
+ product = Product.objects.get(name="Worksheet 1")
+ prices = Price.objects.filter(product=product)
+ context = super(ProductLandingPageView, self).get_context_data(**kwargs)
+ context.update({"product": product, "prices": prices})
+ return context
+
+
+@login_required
+def add_to_cart(request, resource_id):
+ resource = get_object_or_404(Resource, id=resource_id)
+ cart, created = ShoppingCart.objects.get_or_create(user=request.user)
+ cart_item, created = CartItem.objects.get_or_create(cart=cart, resource=resource)
+ cart_item.quantity += 1
+ cart_item.save()
+ return redirect("cart_detail")
+
+
+@login_required
+def cart_detail(request):
+ cart, created = ShoppingCart.objects.get_or_create(user=request.user)
+ return render(request, "payments/cart_detail.html", {"cart": cart})
+
+
+@login_required
+def checkout(request):
+ cart = ShoppingCart.objects.get(user=request.user)
+ total = sum(item.get_total_price() for item in cart.items.all())
+
+ if request.method == "POST":
+ # Create Stripe PaymentIntent
+ intent = stripe.PaymentIntent.create(
+ amount=int(total * 100), # Stripe amount is in cents
+ currency="usd",
+ automatic_payment_methods={"enabled": True},
+ )
+
+ # Redirect to Stripe checkout or handle payment confirmation
+ return render(request, "cart/checkout_success.html")
+
+ return render(request, "cart/checkout.html", {"cart": cart, "total": total})