-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ivan
committed
Apr 5, 2023
1 parent
b2b53f9
commit 6be4531
Showing
8 changed files
with
106 additions
and
42 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
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,22 @@ | ||
from django.conf import settings | ||
from rest_framework.authentication import BaseAuthentication | ||
|
||
from api.exceptions import AuthenticationFailedException | ||
from payments.core import stripe | ||
|
||
|
||
class StripeAuthentication(BaseAuthentication): | ||
def authenticate(self, request): | ||
try: | ||
signature = request.headers['Stripe-Signature'] | ||
except KeyError: | ||
raise AuthenticationFailedException('SSO header is missing') | ||
try: | ||
event: stripe.Event = stripe.Webhook.construct_event(request.body, signature, | ||
settings.STRIPE_ENDPOINT_SECRET) | ||
except ValueError as e: | ||
raise AuthenticationFailedException() | ||
except stripe.error.SignatureVerificationError: | ||
raise AuthenticationFailedException() | ||
|
||
return None, event |
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,13 @@ | ||
from datetime import datetime | ||
|
||
from rest_framework.views import exception_handler | ||
|
||
|
||
def api_exception_handler(exc, context): | ||
response = exception_handler(exc, context) | ||
if response is not None: | ||
response.data['message'] = response.data['detail'] | ||
response.data['time'] = datetime.now() | ||
del response.data['detail'] | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from rest_framework import status | ||
from rest_framework.exceptions import APIException | ||
|
||
|
||
class BaseCustomException(APIException): | ||
detail = None | ||
status_code = None | ||
|
||
def __init__(self, detail=None, code=None): | ||
super().__init__(detail=detail, code=code) | ||
self.detail = detail | ||
self.status_code = code | ||
|
||
|
||
class AuthenticationFailedException(BaseCustomException): | ||
def __init__(self, detail=None): | ||
if detail is None: | ||
detail = 'Not authenticated' | ||
super().__init__(detail=detail, code=status.HTTP_401_UNAUTHORIZED) |
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,26 @@ | ||
from payments.core import stripe | ||
from payments.models import get_payment_instance, Subscription | ||
|
||
|
||
class StripeWebhookService: | ||
def __init__(self, event: stripe.Event): | ||
self.event = event | ||
|
||
def procces_post_request(self): | ||
if self.event.type == 'checkout.session.completed': | ||
self.checkout_session_completed() | ||
elif self.event.type == 'invoice.payment_succeeded': | ||
self.invoice_payment_succeeded() | ||
elif self.event.type == 'customer.subscription.updated': | ||
self.customer_subscription_updated() | ||
|
||
def customer_subscription_updated(self): | ||
payment_instance = Subscription.objects.get(psp_id=self.event.data.object.id) | ||
payment_instance.update_from_event(self.event) | ||
|
||
def checkout_session_completed(self): | ||
payment_instance = get_payment_instance(self.event) | ||
payment_instance.from_event(self.event, save=True) | ||
|
||
def invoice_payment_succeeded(self): | ||
pass |
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,50 +1,19 @@ | ||
from http import HTTPStatus | ||
|
||
from django.conf import settings | ||
from django.http import HttpResponse | ||
from django.utils.decorators import method_decorator | ||
from django.views import View | ||
from django.views.decorators.csrf import csrf_exempt | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from payments.core import stripe | ||
from payments.models import get_payment_instance, Subscription | ||
from api.authentications import StripeAuthentication | ||
from api.v1.services import StripeWebhookService | ||
|
||
|
||
@method_decorator(csrf_exempt, name='dispatch') | ||
class StripeWebhook(View): | ||
def post(self, request, *args, **kwargs): | ||
try: | ||
signature = request.headers['Stripe-Signature'] | ||
except KeyError: | ||
return HttpResponse(status=HTTPStatus.FORBIDDEN) | ||
|
||
try: | ||
event = stripe.Webhook.construct_event(request.body, signature, settings.STRIPE_ENDPOINT_SECRET) | ||
except ValueError as e: | ||
return HttpResponse(status=HTTPStatus.FORBIDDEN) | ||
except stripe.error.SignatureVerificationError: | ||
return HttpResponse(status=HTTPStatus.FORBIDDEN) | ||
|
||
print(event.data.object.object) | ||
if event.type == 'checkout.session.completed': | ||
self.checkout_session_completed(event) | ||
elif event.type == 'invoice.payment_succeeded': | ||
self.invoice_payment_succeeded(event) | ||
elif event.type == 'customer.subscription.updated': | ||
self.customer_subscription_updated(event) | ||
class StripeWebhook(APIView): | ||
authentication_classes = (StripeAuthentication,) | ||
|
||
return HttpResponse(status=HTTPStatus.OK) | ||
|
||
@staticmethod | ||
def customer_subscription_updated(event: stripe.Event): | ||
payment_instance = Subscription.objects.get(psp_id=event.data.object.id) | ||
payment_instance.update_from_event(event) | ||
|
||
@staticmethod | ||
def checkout_session_completed(event: stripe.Event): | ||
payment_instance = get_payment_instance(event) | ||
payment_instance.from_event(event, save=True) | ||
def post(self, request, *args, **kwargs): | ||
service = StripeWebhookService(request.auth) | ||
service.procces_post_request() | ||
|
||
@staticmethod | ||
def invoice_payment_succeeded(event: stripe.Event): | ||
pass | ||
return Response(status=status.HTTP_200_OK) |
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