aboutsummaryrefslogtreecommitdiffstats
path: root/alphabetlearning/payments/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'alphabetlearning/payments/views.py')
-rw-r--r--alphabetlearning/payments/views.py28
1 files changed, 28 insertions, 0 deletions
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)