from django.shortcuts import render from django.views.generic import DetailView, TemplateView from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt from payment.models import Payment from dbsettings.models import Setting import stripe # Create your views here. class StatusView(DetailView): model = Payment template_name = "payment/status.html" class StripeRedirectView(TemplateView): template_name = "payment/redirect.html" class StripeRedirectJSView(DetailView): model = Payment template_name = "payment/redirect_stripe.js" @csrf_exempt def stripe_webhook(request): endpoint_secret = Setting.objects.get(key="stripe.webhook.secret").value # pylint: disable=no-member payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event( payload, sig_header, endpoint_secret ) except ValueError: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError: # Invalid signature return HttpResponse(status=400) # Handle the checkout.session.completed event if event['type'] == 'checkout.session.completed': session = event['data']['object'] payment = Payment.objects.get(session=session["id"]) payment.session_status = event["type"] return HttpResponse(status=200)