diff options
author | Matthew Lemon <y@yulqen.org> | 2024-10-17 20:30:00 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-10-17 20:30:00 +0100 |
commit | 71d65aa438ccec15102f1e3729e70f3b25dc9a9f (patch) | |
tree | f6b65f09261f034655f0e3a06e4a3f0121d966e2 | |
parent | 1a5359eaa9bb2d2d8aeaf10d97a6b4c7aa0d47d2 (diff) |
skeleton webhook for stripe working
-rw-r--r-- | alphabetlearning/payments/urls.py | 2 | ||||
-rw-r--r-- | alphabetlearning/payments/views.py | 28 | ||||
-rw-r--r-- | config/settings/base.py | 1 |
3 files changed, 30 insertions, 1 deletions
diff --git a/alphabetlearning/payments/urls.py b/alphabetlearning/payments/urls.py index 52e2451..b782bdc 100644 --- a/alphabetlearning/payments/urls.py +++ b/alphabetlearning/payments/urls.py @@ -18,5 +18,5 @@ urlpatterns = [ ), path("add-to-card/<int:resource_id>", views.add_to_cart, name="add_to_cart"), path("landing/", views.ProductLandingPageView.as_view(), name="landing"), - # path("webhook/", views.webhook, name="webhook"), + path("webhooks/stripe/", views.stripe_webhook, name="stripe-webhook"), ] diff --git a/alphabetlearning/payments/views.py b/alphabetlearning/payments/views.py index 80d6aa2..dfbf8f1 100644 --- a/alphabetlearning/payments/views.py +++ b/alphabetlearning/payments/views.py @@ -1,4 +1,5 @@ import stripe +from django.http import HttpResponse from django.conf import settings from django.contrib.auth.decorators import login_required from django.shortcuts import get_object_or_404 @@ -6,6 +7,7 @@ from django.shortcuts import redirect from django.shortcuts import render from django.views import View from django.views.generic import TemplateView +from django.views.decorators.csrf import csrf_exempt from alphabetlearning.resources.models import Resource from alphabetlearning.users.models import User @@ -18,6 +20,7 @@ from .models import ShoppingCart # TODO get the cart integrated with Stripe # Steps to convert our Cart into something that can be used with Stripe: # +# - Sort out the webhook # - Associate each of our resources with a Product item # - this should be done in the create resource page # - or we can do it manually for the time being @@ -107,3 +110,28 @@ def checkout(request): return render(request, "cart/checkout_success.html") return render(request, "cart/checkout.html", {"cart": cart, "total": total}) + + +@csrf_exempt +def stripe_webhook(request): + payload = request.body + sig_header = request.META["HTTP_STRIPE_SIGNATURE"] + event = None + + # verify that the request came from STRIPE + try: + event = stripe.Webhook.construct_event(payload, sig_header, settings.STRIPE_WEBHOOK_SECRET) + except ValueError as e: + return HttpResponse(status=400) + except stripe.error.SignatureVerificationError as e: + return HttpResponse(status=400) + + if event["type"] == "checkout.session.completed": + session = event["data"]["object"] + customer_email = session["customer_details"]["email"] + payment_intent = session["payment_intent"] + + # TODO send an email to the customer + print("Here we send an email to the customer") + + return HttpResponse(status=200) diff --git a/config/settings/base.py b/config/settings/base.py index 82d11ac..81e0f82 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -81,6 +81,7 @@ THIRD_PARTY_APPS = [ STRIPE_SECRET_KEY = env("STRIPE_SECRET_KEY") STRIPE_PUBLIC_KEY = env("STRIPE_PUBLIC_KEY") +STRIPE_WEBHOOK_SECRET = env("STRIPE_WEBHOOK_SECRET") STRIPE_LIVE_MODE = False LOCAL_APPS = [ |