JourneyJoker/payment/views.py

54 lines
1.4 KiB
Python
Raw Normal View History

2019-12-24 13:41:49 +00:00
from django.shortcuts import render
2020-01-26 10:53:25 +00:00
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
2019-12-24 13:41:49 +00:00
# Create your views here.
2020-01-26 10:53:25 +00:00
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)