generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class methods to webhook handler and webhook view
- Loading branch information
1 parent
4c84078
commit 8ec5bfc
Showing
4 changed files
with
78 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from django.urls import path | ||
from checkout.views import Checkout, CheckoutSuccess | ||
from .webhooks import webhook | ||
|
||
urlpatterns = [ | ||
path('', Checkout.as_view(), name='checkout'), | ||
path('checkout_success/<order_number>', CheckoutSuccess.as_view(), name='checkout_success'), | ||
path('wh/>', webhook, name='webhook'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from django.conf import settings | ||
from django.http import HttpResponse | ||
from django.views.decorators.http import require_POST | ||
from django.views.decorators.csrf import csrf_exempt | ||
|
||
from checkout.webhook_handler import StripeWH_Handler | ||
|
||
import stripe | ||
|
||
@require_POST | ||
@csrf_exempt | ||
def webhook(request): | ||
"""Listen for webhooks from Stripe""" | ||
# Setup | ||
wh_secret = settings.STRIPE_WH_SECRET | ||
stripe.api_key = settings.STRIPE_SECRET_KEY | ||
|
||
# Get the webhook data and verify its signature | ||
payload = request.body | ||
sig_header = request.META['HTTP_STRIPE_SIGNATURE'] | ||
event = None | ||
|
||
try: | ||
event = stripe.Webhook.construct_event( | ||
payload, sig_header, wh_secret | ||
) | ||
except ValueError as e: | ||
# Invalid payload | ||
return HttpResponse(status=400) | ||
except stripe.error.SignatureVerificationError as e: | ||
# Invalid signature | ||
return HttpResponse(status=400) | ||
except Exception as e: | ||
return HttpResponse(content=e, status=400) | ||
|
||
# Set up a webhook handler | ||
handler = StripeWH_Handler(request) | ||
|
||
# Map webhook events to relevant handler functions | ||
event_map = { | ||
'payment_intent.succeeded': handler.handle_payment_intent_succeeded, | ||
'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, | ||
} | ||
|
||
# Get the webhook type from Stripe | ||
event_type = event['type'] | ||
|
||
# If there's a handler for it, get it from the event map | ||
# Use the generic one by default | ||
event_handler = event_map.get(event_type, handler.handle_event) | ||
|
||
# Call the event handler with the event | ||
response = event_handler(event) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters