In Django Paypal Webhook with Sandbox
Create url for webhook in django application or you can create from Paypal dashboard I'm creating from application because we are using sandbox
url('createWebhook', CreateWebhookAPIView.as_view(), name='create-webhook'),
then write a class API to Create Class for creation of webhook
class CreateWebhookAPIView(CreateAPIView):
"""
Subscribes your webhook listener to events.
"""
authentication_classes = ()
permission_classes = ()
serializer_class = BrandListSerializer
def __init__(self, **kwargs):
self.response_format = ResponseInfo().response
super(CreateWebhookAPIView, self).__init__(**kwargs)
def post(self, request, *args, **kwargs):
try:
webhook = URL+'v1/notifications/webhooks'
response = requests.post(webhook, headers=headers, json=request.data)
self.response_format = return_response(
response.json(), None, status.HTTP_200_OK, messages.SUCCESS)
return Response(self.response_format)
except Exception as e:
print(e, "error")
return Response(self.response_format)
and create another class where you want to perform the event specific operation make sure that you have created separate class to perform operation for each EVENT if you try to use same url for different event it will give error.I have created following class as if now just printing the event response you can do operation
class UpdateSubscriptionWebhookAPIView(CreateAPIView):
"""
Subscribes your webhook listener to events.
"""
authentication_classes = ()
permission_classes = ()
serializer_class = BrandListSerializer
def __init__(self, **kwargs):
self.response_format = ResponseInfo().response
super(UpdateSubscriptionWebhookAPIView, self).__init__(**kwargs)
def post(self, request, *args, **kwargs):
try:
event_json = json.loads(request.body)
print(event_json)
self.response_format = return_response(
event_json, None, status.HTTP_200_OK, messages.SUCCESS)
return Response(self.response_format)
except Exception as e:
print(e, "error")
return Response(self.response_format)
add url for the same
url('updateSubscriptionWebhook', UpdateSubscriptionWebhookAPIView.as_view(), name='update-webhook')
After that from post create on request and hit it with respective Event and you can see webhook created and once event occurred you will get respective output as followed

Output on console which print in Class
