Add webhook app and create_webhook management command
This commit is contained in:
parent
68538ac981
commit
9b32290964
5 changed files with 91 additions and 0 deletions
|
@ -153,6 +153,7 @@ INSTALLED_APPS = (
|
|||
'rest_framework',
|
||||
'opennebula_api',
|
||||
'django_celery_results',
|
||||
'webhook',
|
||||
)
|
||||
|
||||
MIDDLEWARE_CLASSES = (
|
||||
|
@ -719,6 +720,8 @@ X_FRAME_OPTIONS = ('SAMEORIGIN' if X_FRAME_OPTIONS_ALLOW_FROM_URI is None else
|
|||
X_FRAME_OPTIONS_ALLOW_FROM_URI.strip()
|
||||
))
|
||||
|
||||
INVOICE_WEBHOOK_SECRET = env('INVOICE_WEBHOOK_SECRET')
|
||||
|
||||
DEBUG = bool_env('DEBUG')
|
||||
|
||||
if DEBUG:
|
||||
|
|
0
webhook/__init__.py
Normal file
0
webhook/__init__.py
Normal file
49
webhook/management/commands/create_webhook.py
Normal file
49
webhook/management/commands/create_webhook.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
import logging
|
||||
import stripe
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = '''creates webhook with the supplied arguments and returns the
|
||||
webhook secret
|
||||
'''
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
'--webhook_endpoint',
|
||||
help="The url of the webhook endpoint that accepts the events "
|
||||
"from stripe",
|
||||
dest="webhook_endpoint"
|
||||
)
|
||||
parser.add_argument('--events_csv', dest="events_csv")
|
||||
|
||||
def handle(self, *args, **options):
|
||||
wep_exists = False
|
||||
try:
|
||||
we_list = stripe.WebhookEndpoint.list(limit=100)
|
||||
for wep in we_list.data:
|
||||
if set(wep.enabled_events) == set(options['events_csv'].split(",")):
|
||||
if wep.url == options['webhook_endpoint']:
|
||||
logger.debug("We have this webhook already")
|
||||
wep_exists = True
|
||||
break
|
||||
if wep_exists is False:
|
||||
logger.debug(
|
||||
"No webhook exists for {} at {}. Creatting a new endpoint "
|
||||
"now".format(
|
||||
options['webhook_endpoint'], options['events_csv']
|
||||
)
|
||||
)
|
||||
wep = stripe.WebhookEndpoint.create(
|
||||
url=options['webhook_endpoint'],
|
||||
enabled_events=options['events_csv'].split(",")
|
||||
)
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS('Creation successful. '
|
||||
'webhook_secret = %s' % wep.secret)
|
||||
)
|
||||
except Exception as e:
|
||||
print(" *** Error occurred. Details {}".format(str(e)))
|
3
webhook/models.py
Normal file
3
webhook/models.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
36
webhook/views.py
Normal file
36
webhook/views.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
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)
|
Loading…
Add table
Reference in a new issue