-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from Priyansh61/payment-gateway
[Feat] Added Payment Gateway Stripe
- Loading branch information
Showing
9 changed files
with
78 additions
and
6 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
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,32 @@ | ||
from django.views.decorators.csrf import csrf_exempt | ||
from django.http import HttpResponse | ||
import stripe | ||
|
||
from django.conf import settings | ||
from tickets.models import Ticket | ||
|
||
|
||
@csrf_exempt | ||
def stripe_webhook(request) : | ||
payload = request.body | ||
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: | ||
return HttpResponse(status=400) | ||
|
||
except stripe.error.SignatureVerificationError as e: | ||
return HttpResponse(status=400) | ||
|
||
if event['type'] == 'payment_intent.succeeded': | ||
payment_intent = event['data']['object'] | ||
ticket_id = payment_intent['metadata']['ticket_id'] | ||
ticket = Ticket.objects.get(id=ticket_id) | ||
ticket.update(status='purchased') | ||
ticket.save() | ||
|
||
return HttpResponse(status=200) |
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ class Meta: | |
'phone', | ||
'date', | ||
'time', | ||
'cover_image', | ||
'event_type', | ||
'location', | ||
'ticket_price', | ||
|
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 |
---|---|---|
|
@@ -24,3 +24,4 @@ sqlparse==0.4.4 | |
tomli==2.0.1 | ||
typing_extensions==4.6.3 | ||
tzdata==2022.6 | ||
stripe |
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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
from django.urls import path,include | ||
from rest_framework.routers import DefaultRouter | ||
from .views import TicketViewSet | ||
from .views import TicketViewSet, CreatePaymentIntent | ||
|
||
router = DefaultRouter() | ||
router.register('', TicketViewSet, basename='ticket') | ||
|
||
urlpatterns = [ | ||
path('create-payment-intent/', CreatePaymentIntent.as_view(), name='create-payment-intent'), | ||
path('', include(router.urls)), | ||
] |
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,9 +1,40 @@ | ||
from rest_framework import viewsets | ||
from django.conf import settings | ||
from rest_framework import viewsets | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
import stripe | ||
from .models import Ticket | ||
from .serializers import TicketSerializer | ||
from rest_framework import status | ||
|
||
stripe.api_key = settings.STRIPE_SECRET_KEY | ||
|
||
|
||
# Create your views here. | ||
|
||
class TicketViewSet(viewsets.ModelViewSet): | ||
queryset = Ticket.objects.all() | ||
serializer_class = TicketSerializer | ||
serializer_class = TicketSerializer | ||
|
||
class CreatePaymentIntent(APIView): | ||
|
||
def post(self, request, *args, **kwargs): | ||
amount = request.data.get('amount') | ||
print(request.data) | ||
|
||
try : | ||
intent = stripe.PaymentIntent.create( | ||
amount=amount, | ||
currency='inr', | ||
payment_method_types=['card','upi'], | ||
metadata={ | ||
'ticket_id': request.data.get('ticket_id') | ||
} | ||
) | ||
return Response({ | ||
'clientSecret': intent['client_secret'] | ||
}) | ||
except Exception as e: | ||
return Response({ | ||
'error': str(e) | ||
}, status = status.HTTP_400_BAD_REQUEST) |