From 1029162a62e2c257fb2e311da8a2c459cde8f6e9 Mon Sep 17 00:00:00 2001 From: Benjamin Bach Date: Tue, 30 Jul 2024 15:21:47 +0200 Subject: [PATCH] View for Stripe checkout --- .env.example | 1 + src/accounting/views.py | 59 ++++++++++++++++++++++++++++++++++------- src/project/settings.py | 2 ++ 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index 9da2eda..3b6b211 100644 --- a/.env.example +++ b/.env.example @@ -6,3 +6,4 @@ DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres # Use something along the the following if you are not using docker # DATABASE_URL=postgres://postgres:postgres@localhost:5432/datacoop_membersystem DEBUG=True +STRIPE_API_KEY=sk_test_51Pi1452K58NJXwrP3ayQi0LmGCjiVPUdVFnPOT4didTslRQpmGLSxiuYsmwoTJSLDny5I4uIPcpL2fwpG7GvlTbH00mewgemdz diff --git a/src/accounting/views.py b/src/accounting/views.py index c31c5d9..99ab1c7 100644 --- a/src/accounting/views.py +++ b/src/accounting/views.py @@ -1,21 +1,21 @@ """Views for the membership app.""" -from __future__ import annotations - -from typing import TYPE_CHECKING - +import stripe +from django.conf import settings +from django.contrib.sites.models import Site +from django.core.mail import send_mail +from django.http import HttpRequest +from django.http import HttpResponse +from django.shortcuts import redirect from django.shortcuts import render from django_view_decorator import namespaced_decorator_factory from . import models -if TYPE_CHECKING: - from django.http import HttpRequest - from django.http import HttpResponse - - order_view = namespaced_decorator_factory(namespace="order", base_path="order") +stripe.api_key = settings.STRIPE_API_KEY + @order_view( paths="/", @@ -36,3 +36,44 @@ def order_detail(request: HttpRequest, order_id: int) -> HttpResponse: template_name="accounting/order/detail.html", context=context, ) + + +@order_view( + paths="/pay/", + name="pay", + login_required=True, +) +def order_pay(request: HttpRequest, order_id: int) -> HttpResponse: + """Create a Stripe session and redirects to Stripe Checkout.""" + user = request.user # People just need to login to pay something, not necessarily be a member + order = models.Order.objects.get(pk=order_id, member=user) + current_site = Site.objects.get_current(request) + base_domain = f"https://{current_site.domain}" + + try: + line_items = [] + for item in order.items.all(): + line_items.append( # noqa: PERF401 + { + "price_data": { + "currency": item.total_with_vat.currency, + "unit_amount": (item.price, +item.vat).amount, + "product_data": { + "name": item.product.name, + }, + }, + "quantity": item.quantity, + } + ) + checkout_session = stripe.checkout.Session.create( + line_items=line_items, + mode="payment", + success_url=base_domain + "/success", + cancel_url=base_domain + "/cancel", + ) + except Exception as e: + send_mail("Error in checkout", str(e), settings.DEFAULT_FROM_EMAIL, settings.ADMINS) + raise + + # TODO: Redirect with status=303 + return redirect(checkout_session.url) diff --git a/src/project/settings.py b/src/project/settings.py index 21018c0..25e74bf 100644 --- a/src/project/settings.py +++ b/src/project/settings.py @@ -28,6 +28,8 @@ CSRF_TRUSTED_ORIGINS = env.list( ADMINS = [tuple(x.split(":")) for x in env.list("DJANGO_ADMINS", default=[])] +DEFAULT_FROM_EMAIL = "server@data.coop" + DEFAULT_AUTO_FIELD = "django.db.models.AutoField" # Application definition