532 lines
21 KiB
Python
532 lines
21 KiB
Python
from datetime import timedelta
|
|
from django.http import HttpResponseBadRequest, JsonResponse
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
import stripe
|
|
from accounts import resource_action
|
|
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
|
|
from manage_subscriptions.forms import (
|
|
PlanForm,
|
|
SubscriptionForm,
|
|
PrincipalSubscriptionForm,
|
|
)
|
|
from manage_wallets.models import (
|
|
PaymentMethod,
|
|
Transaction,
|
|
TransactionStatus,
|
|
TransactionType,
|
|
)
|
|
from .models import Plan, Subscription, PrincipalSubscription
|
|
from django.views import generic
|
|
from rest_framework import status
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.urls import reverse_lazy
|
|
from django.contrib import messages
|
|
from goodtimes import constants
|
|
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
|
|
|
|
# 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):
|
|
queryset = super().get_queryset().filter(deleted=False, active=True)
|
|
return queryset.order_by("-created_on")
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context["page_name"] = self.page_name
|
|
return context
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
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
|
|
|
|
|
|
# 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
|
|
|
|
# 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 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_PRINCIPAL_SUBSCRIPTIONS
|
|
resource = resource_action.RESOURCE_PRINCIPAL_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):
|
|
return super().get_queryset().all()
|
|
|
|
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)
|
|
|
|
return redirect(self.success_url)
|
|
|
|
|
|
class SubscriptionPageView(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)
|
|
|
|
|
|
@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
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
# Create a Transaction object with status INITIATE
|
|
transaction = Transaction.objects.create(
|
|
principal=request.user,
|
|
principal_subscription=None, # Since the subscription is not created yet
|
|
transaction_type=TransactionType.PAYMENT, # or PAYMENT, as applicable
|
|
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",
|
|
)
|
|
|
|
subscription_days = subscription.plan.days
|
|
today = timezone.now().date()
|
|
last_date = today + timedelta(days=int(subscription_days))
|
|
|
|
# To Avoid Duplicacy of Principal Subscription
|
|
principal_subscription = PrincipalSubscription.objects.create(
|
|
principal=request.user,
|
|
subscription=subscription,
|
|
is_paid=False,
|
|
order_id=order_id,
|
|
start_date=today,
|
|
end_date=last_date,
|
|
grace_period_end_date=last_date + timedelta(days=15),
|
|
)
|
|
|
|
try:
|
|
# customer = stripe.Customer.create(
|
|
# email=request.user.email,
|
|
# shipping={
|
|
# "name": request.user.first_name,
|
|
# "address": {
|
|
# "line1": request.user.city,
|
|
# "city": request.user.city,
|
|
# "postal_code": "SW1A 2AA",
|
|
# "country": request.user.address_line1, # 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": {
|
|
"name": subscription.title, # Adjust with your subscription/product name
|
|
},
|
|
"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/"),
|
|
cancel_url=request.build_absolute_uri("/subscriptions/cancel/"),
|
|
metadata={
|
|
"principal": str(request.user.id),
|
|
"order_id": str(order_id),
|
|
# "subscription_id": str(subscription.id),
|
|
"transaction_id": str(transaction.id),
|
|
"principal_subscription_id": str(principal_subscription.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):
|
|
template_name = "stripe_html/success.html"
|
|
|
|
|
|
class CancelView(TemplateView):
|
|
template_name = "stripe_html/cancel.html"
|
|
|
|
|
|
# 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)
|