View for Stripe checkout

This commit is contained in:
Benjamin Bach 2024-07-30 15:21:47 +02:00
parent 78b3264bb6
commit 1029162a62
No known key found for this signature in database
GPG key ID: 486F0D69C845416E
3 changed files with 53 additions and 9 deletions

View file

@ -6,3 +6,4 @@ DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres
# Use something along the the following if you are not using docker # Use something along the the following if you are not using docker
# DATABASE_URL=postgres://postgres:postgres@localhost:5432/datacoop_membersystem # DATABASE_URL=postgres://postgres:postgres@localhost:5432/datacoop_membersystem
DEBUG=True DEBUG=True
STRIPE_API_KEY=sk_test_51Pi1452K58NJXwrP3ayQi0LmGCjiVPUdVFnPOT4didTslRQpmGLSxiuYsmwoTJSLDny5I4uIPcpL2fwpG7GvlTbH00mewgemdz

View file

@ -1,21 +1,21 @@
"""Views for the membership app.""" """Views for the membership app."""
from __future__ import annotations import stripe
from django.conf import settings
from typing import TYPE_CHECKING 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.shortcuts import render
from django_view_decorator import namespaced_decorator_factory from django_view_decorator import namespaced_decorator_factory
from . import models 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") order_view = namespaced_decorator_factory(namespace="order", base_path="order")
stripe.api_key = settings.STRIPE_API_KEY
@order_view( @order_view(
paths="<int:order_id>/", paths="<int:order_id>/",
@ -36,3 +36,44 @@ def order_detail(request: HttpRequest, order_id: int) -> HttpResponse:
template_name="accounting/order/detail.html", template_name="accounting/order/detail.html",
context=context, context=context,
) )
@order_view(
paths="<int:order_id>/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)

View file

@ -28,6 +28,8 @@ CSRF_TRUSTED_ORIGINS = env.list(
ADMINS = [tuple(x.split(":")) for x in env.list("DJANGO_ADMINS", default=[])] 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" DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
# Application definition # Application definition