Files
goodtimes/manage_subscriptions/views.py

520 lines
20 KiB
Python
Raw Normal View History

2024-03-07 18:06:57 +05:30
from django.http import HttpResponseBadRequest, JsonResponse
2024-02-29 13:25:50 +05:30
from django.shortcuts import get_object_or_404, redirect, render
2024-03-07 18:06:57 +05:30
import stripe
2024-02-29 13:25:50 +05:30
from accounts import resource_action
2024-03-07 18:06:57 +05:30
from accounts.models import IAmPrincipal
from goodtimes.utils import ApiResponse
from django.contrib.auth import authenticate, login
import jwt
from rest_framework_simplejwt.tokens import AccessToken
from django.utils import timezone
from django.contrib.auth import get_user_model
2024-02-29 13:25:50 +05:30
from manage_subscriptions.forms import (
PlanForm,
SubscriptionForm,
PrincipalSubscriptionForm,
)
2024-03-07 18:06:57 +05:30
from manage_wallets.models import (
PaymentMethod,
Transaction,
TransactionStatus,
TransactionType,
)
2024-02-29 13:25:50 +05:30
from .models import Plan, Subscription, PrincipalSubscription
from django.views import generic
2024-03-07 18:06:57 +05:30
from rest_framework import status
2024-02-29 13:25:50 +05:30
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.contrib import messages
from goodtimes import constants
2024-03-07 18:06:57 +05:30
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
from rest_framework.permissions import IsAuthenticated
from django.views.generic.base import TemplateView
2024-02-29 13:25:50 +05:30
# Create your views here.
class SubscriptionCreateOrUpdateView(LoginRequiredMixin, generic.View):
# Set the page_name and resource
page_name = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
resource = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
# Initialize the action as ACTION_CREATE (can change based on logic)
action = resource_action.ACTION_CREATE # Default action
template_name = "manage_subscriptions/subscription_add.html"
model = Subscription
form_class = SubscriptionForm
success_url = reverse_lazy("manage_subscriptions:subscription_list")
error_message = "An error occurred while saving the data."
# Determine the success message dynamically based on whether it's an update or create
def get_success_message(self):
self.success_message = (
constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED
)
return self.success_message
# Get the object (if exists) based on URL parameter 'pk'
def get_object(self):
pk = self.kwargs.get("pk")
return get_object_or_404(self.model, pk=pk) if pk else None
# Add page_name and operation to the context
def get_context_data(self, **kwargs):
context = {
"page_name": self.page_name,
"operation": "Add" if not self.object else "Edit",
}
context.update(kwargs) # Include any additional context data passed to the view
return context
def get(self, request, *args, **kwargs):
self.object = self.get_object()
# If an object is found, change action to ACTION_UPDATE
if self.object is not None:
self.action = resource_action.ACTION_UPDATE
form = self.form_class(instance=self.object)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
# If an object is found, change action to ACTION_UPDATE
if self.object is not None:
self.action = resource_action.ACTION_UPDATE
form = self.form_class(request.POST, instance=self.object)
if not form.is_valid():
print(form.errors)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
form.save()
messages.success(self.request, self.get_success_message())
return redirect(self.success_url)
class SubscriptionView(LoginRequiredMixin, generic.ListView):
page_name = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
resource = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
action = resource_action.ACTION_READ
model = Subscription
template_name = "manage_subscriptions/subscription_list.html"
context_object_name = "subscription_obj"
def get_queryset(self):
2024-03-08 18:52:59 +05:30
return super().get_queryset().filter(deleted=False, active=True)
2024-02-29 13:25:50 +05:30
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_name"] = self.page_name
return context
2024-03-15 15:57:09 +05:30
# class SubscriptionDeleteView(LoginRequiredMixin, generic.View):
# page_name = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
# resource = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
# action = resource_action.ACTION_DELETE
# model = Subscription
# success_url = reverse_lazy("manage_subscriptions:subscription_list")
# success_message = constants.RECORD_DELETED
# error_message = constants.RECORD_NOT_FOUND
# def get(self, request, pk):
# try:
# type_obj = self.model.objects.get(id=pk)
# type_obj.deleted = True
# type_obj.active = False
# type_obj.save()
# messages.success(request, self.success_message)
# except self.model.DoesNotExist:
# messages.success(request, self.error_message)
# return redirect(self.success_url)
# class PlanCreateOrUpdateView(LoginRequiredMixin, generic.View):
# # Set the page_name and resource
# page_name = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
# resource = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
# # Initialize the action as ACTION_CREATE (can change based on logic)
# action = resource_action.ACTION_CREATE # Default action
# template_name = "manage_subscriptions/plan_add.html"
# model = Plan
# form_class = PlanForm
# success_url = reverse_lazy("manage_subscriptions:plan_list")
# error_message = "An error occurred while saving the data."
# # Determine the success message dynamically based on whether it's an update or create
# def get_success_message(self):
# self.success_message = (
# constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED
# )
# return self.success_message
# # Get the object (if exists) based on URL parameter 'pk'
# def get_object(self):
# pk = self.kwargs.get("pk")
# return get_object_or_404(self.model, pk=pk) if pk else None
# # Add page_name and operation to the context
# def get_context_data(self, **kwargs):
# context = {
# "page_name": self.page_name,
# "operation": "Add" if not self.object else "Edit",
# }
# context.update(kwargs) # Include any additional context data passed to the view
# return context
# def get(self, request, *args, **kwargs):
# self.object = self.get_object()
# # If an object is found, change action to ACTION_UPDATE
# if self.object is not None:
# self.action = resource_action.ACTION_UPDATE
# form = self.form_class(instance=self.object)
# context = self.get_context_data(form=form)
# return render(request, self.template_name, context=context)
# def post(self, request, *args, **kwargs):
# self.object = self.get_object()
# # If an object is found, change action to ACTION_UPDATE
# if self.object is not None:
# self.action = resource_action.ACTION_UPDATE
# form = self.form_class(request.POST, instance=self.object)
# if not form.is_valid():
# print(form.errors)
# context = self.get_context_data(form=form)
# return render(request, self.template_name, context=context)
# form.save()
# messages.success(self.request, self.get_success_message())
# return redirect(self.success_url)
2024-02-29 13:25:50 +05:30
class PlanView(LoginRequiredMixin, generic.ListView):
page_name = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
resource = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
action = resource_action.ACTION_READ
model = Plan
template_name = "manage_subscriptions/plan_list.html"
context_object_name = "plan_obj"
def get_queryset(self):
return super().get_queryset().filter(deleted=False)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_name"] = self.page_name
return context
2024-03-15 15:57:09 +05:30
# class PlanDeleteView(LoginRequiredMixin, generic.View):
# page_name = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
# resource = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
# action = resource_action.ACTION_DELETE
# model = Plan
# success_url = reverse_lazy("manage_subscriptions:plan_list")
# success_message = constants.RECORD_DELETED
# error_message = constants.RECORD_NOT_FOUND
2024-02-29 13:25:50 +05:30
2024-03-15 15:57:09 +05:30
# def get(self, request, pk):
# try:
# type_obj = self.model.objects.get(id=pk)
# type_obj.deleted = True
# type_obj.active = False
# type_obj.save()
# messages.success(request, self.success_message)
# except self.model.DoesNotExist:
# messages.success(request, self.error_message)
2024-02-29 13:25:50 +05:30
2024-03-15 15:57:09 +05:30
# return redirect(self.success_url)
2024-02-29 13:25:50 +05:30
class PrincipalSubscriptionCreateOrUpdateView(LoginRequiredMixin, generic.View):
# Set the page_name and resource
page_name = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
resource = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
# Initialize the action as ACTION_CREATE (can change based on logic)
action = resource_action.ACTION_CREATE # Default action
template_name = "manage_subscriptions/principal_subscription_add.html"
model = PrincipalSubscription
form_class = PrincipalSubscriptionForm
success_url = reverse_lazy("manage_subscriptions:principal_subscriptions_list")
error_message = "An error occurred while saving the data."
# Determine the success message dynamically based on whether it's an update or create
def get_success_message(self):
self.success_message = (
constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED
)
return self.success_message
# Get the object (if exists) based on URL parameter 'pk'
def get_object(self):
pk = self.kwargs.get("pk")
return get_object_or_404(self.model, pk=pk) if pk else None
# Add page_name and operation to the context
def get_context_data(self, **kwargs):
context = {
"page_name": self.page_name,
"operation": "Add" if not self.object else "Edit",
}
context.update(kwargs) # Include any additional context data passed to the view
return context
def get(self, request, *args, **kwargs):
self.object = self.get_object()
# If an object is found, change action to ACTION_UPDATE
if self.object is not None:
self.action = resource_action.ACTION_UPDATE
form = self.form_class(instance=self.object)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
# If an object is found, change action to ACTION_UPDATE
if self.object is not None:
self.action = resource_action.ACTION_UPDATE
form = self.form_class(request.POST, instance=self.object)
if not form.is_valid():
print(form.errors)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
form.save()
messages.success(self.request, self.get_success_message())
return redirect(self.success_url)
class PrincipalSubscriptionView(LoginRequiredMixin, generic.ListView):
page_name = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
resource = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
action = resource_action.ACTION_READ
model = PrincipalSubscription
template_name = "manage_subscriptions/principal_subscriptions_list.html"
context_object_name = "principal_subscription_obj"
def get_queryset(self):
2024-03-15 17:12:06 +05:30
return super().get_queryset().all()
2024-02-29 13:25:50 +05:30
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_name"] = self.page_name
return context
class PrincipalSubscriptionDeleteView(LoginRequiredMixin, generic.View):
page_name = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
resource = resource_action.RESOURCE_MANAGE_SUBSCRIPTIONS
action = resource_action.ACTION_DELETE
model = PrincipalSubscription
success_url = reverse_lazy("manage_subscriptions:principal_subscriptions_list")
success_message = constants.RECORD_DELETED
error_message = constants.RECORD_NOT_FOUND
def get(self, request, pk):
try:
type_obj = self.model.objects.get(id=pk)
type_obj.deleted = True
type_obj.active = False
type_obj.save()
messages.success(request, self.success_message)
except self.model.DoesNotExist:
messages.success(request, self.error_message)
2024-03-07 18:06:57 +05:30
return redirect(self.success_url)
class SubscriptionPageView(TemplateView):
template_name = "stripe_html/index.html"
2024-03-07 18:06:57 +05:30
def get(self, request, *args, **kwargs):
# Example of extracting the token from a query parameter or cookie
token = request.GET.get("token")
# token = request.GET.get("token") or request.COOKIES.get("jwt")
print("token: ", token)
if token:
try:
# Decode and validate token
payload = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
print("payload: ", payload)
try:
UserModel = get_user_model()
user = UserModel.objects.get(id=payload["user_id"])
# Manually specify the authentication backend
user.backend = "django.contrib.auth.backends.ModelBackend"
# Log the user in
login(request, user)
print("Logged in user: ", user)
except IAmPrincipal.DoesNotExist:
# Handle expired token
return HttpResponseBadRequest("No Principal Found")
except jwt.ExpiredSignatureError:
# Handle expired token
return HttpResponseBadRequest("Expired Signature Error")
except jwt.InvalidTokenError:
return HttpResponseBadRequest("Invalid Token Error")
return super().get(request, *args, **kwargs)
@csrf_exempt
def stripe_config(request):
if request.method == "GET":
stripe_config = {"publicKey": settings.STRIPE_PUBLISH_KEY}
return JsonResponse(stripe_config, safe=False)
@csrf_exempt
def create_checkout_session(request):
if request.method == "GET":
stripe.api_key = settings.STRIPE_SECRET_KEY
2024-03-08 15:40:51 +05:30
subscription_id = None # Assuming 3 is a default or fallback subscription ID
if request.user.is_authenticated:
print("request.user: ", request.user)
if request.user.principal_type.name == "event_user":
subscription_id = 1
elif request.user.principal_type.name == "event_manager":
subscription_id = 2
2024-03-07 18:06:57 +05:30
try:
subscription = Subscription.objects.get(id=subscription_id)
except Subscription.DoesNotExist:
return ApiResponse.error(
status=status.HTTP_404_NOT_FOUND, message="Subscription not found."
)
order_id = (
"order_" + str(timezone.localtime().timestamp()) + str(request.user.email)
)
print("order_id: ", order_id)
2024-03-08 15:40:51 +05:30
try:
# Create a Transaction object with status INITIATE
transaction = Transaction.objects.create(
principal=request.user,
principal_subscription=None, # Since the subscription is not created yet
2024-03-14 13:48:41 +05:30
transaction_type=TransactionType.PAYMENT, # or PAYMENT, as applicable
2024-03-08 15:40:51 +05:30
payment_method=PaymentMethod.CARD, # Assuming CARD for this example
transaction_status=TransactionStatus.INITIATE,
amount=subscription.amount, # Fetching amount from the Subscription object
order_id=order_id,
comment="Principal Subscription Initiated",
)
except Exception as e:
return ApiResponse.error(
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
message="Failed to create transaction",
errors=str(e)
)
2024-03-07 18:06:57 +05:30
try:
customer = stripe.Customer.create(
email=request.user.email,
shipping={
"name": request.user.first_name,
"address": {
"line1": "Test Address",
"city": "Test City",
"postal_code": "SW1A 2AA",
"country": "GB", # Adjust accordingly
},
},
)
# Create a checkout session
checkout_session = stripe.checkout.Session.create(
payment_method_types=["card"],
customer=customer.id, # Optional: Link the session to the Stripe customer created above
line_items=[
{
"price_data": {
"currency": "gbp",
"product_data": {
2024-03-08 15:40:51 +05:30
"name": subscription.title, # Adjust with your subscription/product name
2024-03-07 18:06:57 +05:30
},
"unit_amount": int(
subscription.amount * 100
), # Unit amount in cents/pence
"tax_behavior": "inclusive", # or 'exclusive', based on your tax settings
},
"quantity": 1,
}
],
mode="payment",
success_url=request.build_absolute_uri("/subscriptions/success/"),
2024-03-07 19:19:26 +05:30
cancel_url=request.build_absolute_uri("/subscriptions/cancel/"),
2024-03-07 18:06:57 +05:30
metadata={
"principal": str(request.user.id),
"order_id": str(order_id),
"subscription_id": str(subscription.id),
"transaction_id": str(transaction.id),
},
# Optionally, set shipping options, allow promotion codes, etc.
)
return JsonResponse({"sessionId": checkout_session["id"]})
except Exception as e:
return JsonResponse({"error": str(e)})
class SuccessView(TemplateView):
2024-03-07 19:19:26 +05:30
template_name = "stripe_html/success.html"
2024-03-07 18:06:57 +05:30
class CancelView(TemplateView):
2024-03-07 19:19:26 +05:30
template_name = "stripe_html/cancel.html"
2024-03-15 15:57:09 +05:30
# class IndexView(TemplateView):
# template_name = "stripe_html/index.html"
# def get(self, request, *args, **kwargs):
# # Example of extracting the token from a query parameter or cookie
# token = request.GET.get("token")
# # token = request.GET.get("token") or request.COOKIES.get("jwt")
# print("token: ", token)
# if token:
# try:
# # Decode and validate token
# payload = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
# print("payload: ", payload)
# try:
# UserModel = get_user_model()
# user = UserModel.objects.get(id=payload["user_id"])
# # Manually specify the authentication backend
# user.backend = "django.contrib.auth.backends.ModelBackend"
# # Log the user in
# login(request, user)
# print("Logged in user: ", user)
# except IAmPrincipal.DoesNotExist:
# # Handle expired token
# return HttpResponseBadRequest("No Principal Found")
# except jwt.ExpiredSignatureError:
# # Handle expired token
# return HttpResponseBadRequest("Expired Signature Error")
# except jwt.InvalidTokenError:
# return HttpResponseBadRequest("Invalid Token Error")
# return super().get(request, *args, **kwargs)