dynamicweb/webhook/views.py

36 lines
1 KiB
Python
Raw Normal View History

import logging
import stripe
# Create your views here.
from django.conf import settings
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
logger = logging.getLogger(__name__)
@require_POST
@csrf_exempt
def handle_invoice_webhook(request):
payload = request.body
sig_header = request.META['HTTP_STRIPE_SIGNATURE']
event = None
try:
event = stripe.Webhook.construct_event(
payload, sig_header, settings.INVOICE_WEBHOOK_SECRET
)
except ValueError as e:
logger.error("Invalid payload details = " + str(e))
# Invalid payload
return HttpResponse(status=400)
except stripe.error.SignatureVerificationError as e:
logger.error("SignatureVerificationError details = " + str(e))
# Invalid signature
return HttpResponse(status=400)
# Do something with event
logger.debug("Passed invoice signature verification")
return HttpResponse(status=200)