handling active subscription if exist

This commit is contained in:
rizwanisready
2024-05-24 15:59:39 +05:30
parent 150249cce3
commit 5b98584c57
4 changed files with 71 additions and 65 deletions

View File

@@ -304,6 +304,7 @@ SIMPLE_JWT = {
STRIPE_SECRET_KEY = env.str("STRIPE_SECRET_KEY")
STRIPE_PUBLISH_KEY = env.str("STRIPE_PUBLISH_KEY")
ONE_SIGNAL_APP_ID = env.str("ONE_SIGNAL_APP_ID")
ONE_SIGNAL_API_KEY = env.str("ONE_SIGNAL_API_KEY")

View File

@@ -9,56 +9,56 @@ DEBUG = False
ALLOWED_HOSTS = ["staging.goodtimesltd.co.uk", "77.68.8.229"]
# LOGGING_DIR = os.path.join(
# BASE_DIR, "logs"
# ) # Define the directory where log files will be stored
LOGGING_DIR = os.path.join(
BASE_DIR, "logs"
) # Define the directory where log files will be stored
# Ensure the directory exists; create it if it doesn't
# if not os.path.exists(LOGGING_DIR):
# os.makedirs(LOGGING_DIR)
if not os.path.exists(LOGGING_DIR):
os.makedirs(LOGGING_DIR)
# LOGGING_LEVEL = env.str(
# "LOG_LEVEL", "INFO"
# ) # Set your desired log level (e.g., DEBUG, INFO, WARNING, ERROR) in the env file
LOGGING_LEVEL = env.str(
"LOG_LEVEL", "INFO"
) # Set your desired log level (e.g., DEBUG, INFO, WARNING, ERROR)
# LOGGING = {
# "version": 1,
# "disable_existing_loggers": False,
# "formatters": {
# "verbose": {
# "()": colorlog.ColoredFormatter,
# "format": "%(cyan)s%(asctime)s%(reset)s | %(red)s[%(levelname)8s]%(reset)s | [ %(yellow)s%(name)s.%(module)s:%(white)s%(lineno)d%(reset)s - %(green)s%(funcName)10s()%(reset)s ] --> %(message)s",
# "datefmt": "%Y-%m-%d %H:%M:%S",
# "log_colors": {
# "DEBUG": "white",
# "INFO": "green",
# "WARNING": "yellow",
# "ERROR": "red",
# "CRITICAL": "bold_red",
# },
# "secondary_log_colors": {},
# "style": "%",
# }
# },
# "handlers": {
# "logfile": {
# "level": LOGGING_LEVEL,
# "class": "logging.handlers.RotatingFileHandler",
# "filename": os.path.join(LOGGING_DIR, "goodtimes_staging_error.log"),
# 'maxBytes': 5242880, # 5*1024*1024 bytes (5MB)
# "backupCount": 10, # Number of log files to keep (15 days' worth of logs)
# "formatter": "verbose",
# },
# },
# "loggers": {
# "django": {
# "handlers": ["logfile"],
# "level": LOGGING_LEVEL,
# "propagate": False,
# },
# },
# }
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"verbose": {
"()": colorlog.ColoredFormatter,
"format": "%(cyan)s%(asctime)s%(reset)s | %(red)s[%(levelname)8s]%(reset)s | [ %(yellow)s%(name)s.%(module)s:%(white)s%(lineno)d%(reset)s - %(green)s%(funcName)10s()%(reset)s ] --> %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S",
"log_colors": {
"DEBUG": "white",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "bold_red",
},
"secondary_log_colors": {},
"style": "%",
}
},
"handlers": {
"logfile": {
"level": LOGGING_LEVEL,
"class": "logging.handlers.RotatingFileHandler",
"filename": os.path.join(LOGGING_DIR, "godtimes_pro_error.log"),
"maxBytes": 5242880, # 5*1024*1024 bytes (5MB)
"backupCount": 10, # Number of log files to keep (15 days' worth of logs)
"formatter": "verbose",
},
},
"loggers": {
"django": {
"handlers": ["logfile"],
"level": LOGGING_LEVEL,
"propagate": False,
},
},
}
BASE_DOMAIN = "https://staging.goodtimesltd.co.uk"

View File

@@ -195,11 +195,11 @@ class StripeWebhookTest(APIView):
)
# Check if there is an active principal subscription
if self._has_active_principal_subscription(principal_id):
return ApiResponse.success(
status=status.HTTP_208_ALREADY_REPORTED,
message="Active principal subscription already exists",
)
# if self._has_active_principal_subscription(principal_id):
# return ApiResponse.success(
# status=status.HTTP_208_ALREADY_REPORTED,
# message="Active principal subscription already exists",
# )
# payment_service = services.PaymentProcessingService(webhook_data=event)
payment_service = PaymentProcessingService(webhook_data=event)

View File

@@ -405,6 +405,17 @@ def stripe_config(request):
return JsonResponse(stripe_config, safe=False)
def _has_active_principal_subscription(principal_id):
return PrincipalSubscription.objects.filter(
principal__id=principal_id,
active=True,
deleted=False,
cancelled=False,
is_paid=True,
end_date__gte=timezone.now().date(),
).exists()
@csrf_exempt
@require_POST
def create_checkout_session(request):
@@ -412,6 +423,13 @@ def create_checkout_session(request):
data = json.loads(request.body)
print("data: ", data)
subscription_id = data.get("subscriptionId", None)
principal_id = request.user.id
if _has_active_principal_subscription(principal_id):
return ApiResponse.success(
status=status.HTTP_208_ALREADY_REPORTED,
message="Active principal subscription already exists",
)
try:
subscription = Subscription.objects.get(id=subscription_id)
@@ -437,21 +455,6 @@ def create_checkout_session(request):
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,
@@ -485,7 +488,9 @@ def create_checkout_session(request):
"quantity": 1,
}
],
# allow_promotion_codes=True,
mode="payment",
# discounts=[{"coupon": "VLMAsicx"}],
success_url=request.build_absolute_uri("/subscriptions/success/"),
cancel_url=request.build_absolute_uri("/subscriptions/cancel/"),
metadata={