Django Stripe Webhook Integration

Hi,
I have an application that I want to integrate with stripe. I have created a webhook that on Stripe CLI when one pays a permission is turned on from the django side and an email is sent to the client. My problem is taking the webhook to production. I get a 500 error. Anyone who has done the integration successfully to help me?
Regards

My View

class AdvancedHalfHourPricingView(TemplateView):

template_name = "courses/landing.html"

def get_context_data(self, **kwargs):

    product = ModuleVariations.objects.get(variation_name__name="Advanced Swahili", lesson_duration="30 Minutes")

    # product = Module.objects.get(name="Advanced Swahili")

    context = super(AdvancedHalfHourPricingView, self).get_context_data(**kwargs)

    context.update({

        "product": product,

        "user": self.request.user.username,

        "STRIPE_PUBLIC_KEY": settings.STRIPE_PUBLIC_KEY

    })

    print(context)

    return context

class CreateCheckoutSessionView(View):

# @csrf_exempt

def post(self, request, *args, **kwargs):

    product_id = self.kwargs["pk"]

    user = self.request.user.username

    user_email = self.request.user.email

    product = ModuleVariations.objects.get(id=product_id)

    print(product)

    domain = Site.objects.get_current().domain

    checkout_session = stripe.checkout.Session.create(

        payment_method_types=['card'],

        line_items=[

            {

                'price_data': {

                    'currency': 'usd',

                    'unit_amount': product.get_display_price(),

                    'product_data': {

                        'name': product.variation_name.name,

                    },

                },

                'quantity': 1,

            },

        ],

        metadata={

            "product_id": product.id,

            "username": user,

            "user_email": user_email

        },

        mode='payment',

        success_url=f'https://{domain}/dashboard/',

        cancel_url=f'https://{domain}/cancel/',

        #

        # success_url='http://localhost:8000/success/',

        # cancel_url='https://localhost:8000/cancel/',

    )

    print(checkout_session)

    return JsonResponse({'id': checkout_session.id})

@csrf_exempt

def stripe_webhook(request):

payload = request.body.decode('utf-8')

sig_header = request.META['HTTP_STRIPE_SIGNATURE']

event = None

try:

    event = stripe.Webhook.construct_event(

        payload, sig_header, settings.STRIPE_WEBHOOK_SECRET

    )

except ValueError as e:

    # Invalid payload

    return HttpResponse(status=400)

except stripe.error.SignatureVerificationError as e:

    # Invalid signature

    return HttpResponse(status=400)

# Handle the checkout.session.completed event

if event['type'] == 'checkout.session.completed':

    session = event['data']['object']

    customer_email = session["customer_details"]["email"]

    user_email = session["metadata"]["user_email"]

    product_id = session["metadata"]["product_id"]

    user_name = session["metadata"]["username"]

    product = ModuleVariations.objects.get(id=product_id)

    # send email to access the dashboard

    send_mail(

        subject="Here is your product",

        message=f"Thanks for your purchase. {user_name} Here is the product you ordered. The URL is {product.variation_name.name}",

        recipient_list=[customer_email, user_email],

        from_email="admissions@africadiasporaci.com"

    )

    # change a user permission to access the Lessons

    permission = Permission.objects.get(name="Can view lesson")

    user = CustomUser.objects.get(username=user_name)

    user.user_permissions.add(permission)

    user.save()

return HttpResponse(status=200)

Hi there,

you need to add your endpoint to stripe dash,
1 goto https://dashboard.stripe.com/
2 then webhooks
3 then click add endpoint button from the right up corner.
4 allow which rules you need and then copy the endpoint_secret code to your project.
all done.
I hope this helps.