Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Added Payment Gateway Stripe #106

Merged
merged 3 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('DJANGO_SECRET', 'default')
STRIPE_SECRET_KEY = os.environ.get('STRIPE_SECRET_KEY', 'default')
STRIPE_WEBHOOK_SECRET = os.environ.get('STRIPE_WEBHOOK_SECRET', 'default')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand Down
3 changes: 3 additions & 0 deletions backend/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
from django.conf.urls.static import static
from django.conf import settings

from .views import stripe_webhook

urlpatterns = [
path('admin/', admin.site.urls),
path('api/token/', include('auth.urls')),
path('api/accounts/', include('accounts.urls')),
path('api/events/', include('events.urls')),
path('api/tickets/', include('tickets.urls')),
path('api/stripe/webhook/', stripe_webhook, name='stripe-webhook'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
32 changes: 32 additions & 0 deletions backend/core/views.py
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no variable named STRIPE_WEBHOOK_SECRET in settings

)
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)
3 changes: 2 additions & 1 deletion backend/events/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from products.models import ProductImage

# Create your models here.

Expand Down Expand Up @@ -30,7 +31,7 @@ class Event (models.Model) :
)
event_type = models.CharField(max_length=100,
choices=event_type_choices)

cover_image = models.ImageField(upload_to='event_cover_images/', default='event_cover_images/default.jpg')
location = models.CharField(max_length=200)
ticket_price = models.DecimalField(max_digits=10, decimal_places=2)
created_at = models.DateTimeField(auto_now_add=True)
Expand Down
1 change: 1 addition & 0 deletions backend/events/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Meta:
'phone',
'date',
'time',
'cover_image',
'event_type',
'location',
'ticket_price',
Expand Down
1 change: 1 addition & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ sqlparse==0.4.4
tomli==2.0.1
typing_extensions==4.6.3
tzdata==2022.6
stripe
4 changes: 2 additions & 2 deletions backend/tickets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

status_enums = [
('pending','Pending'),
('approved','Approved'),
('rejected','Rejected'),
('purchased','Purchased'),
('failed','Purchase Failed'),
('cancelled','Cancelled'),
]

Expand Down
3 changes: 2 additions & 1 deletion backend/tickets/urls.py
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)),
]
35 changes: 33 additions & 2 deletions backend/tickets/views.py
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this?


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)
Loading