refactor
This commit is contained in:
@@ -132,27 +132,14 @@ class AccountContextMiddleware(MiddlewareMixin):
|
||||
def _validate_account_and_plan(self, request, user):
|
||||
"""
|
||||
Ensure the authenticated user has an account and an active plan.
|
||||
If not, logout the user (for session auth) and block the request.
|
||||
Uses shared validation helper for consistency.
|
||||
"""
|
||||
try:
|
||||
account = getattr(user, 'account', None)
|
||||
except Exception:
|
||||
account = None
|
||||
from .utils import validate_account_and_plan
|
||||
|
||||
if not account:
|
||||
return self._deny_request(
|
||||
request,
|
||||
error='Account not configured for this user. Please contact support.',
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
)
|
||||
is_valid, error_message, http_status = validate_account_and_plan(user)
|
||||
|
||||
plan = getattr(account, 'plan', None)
|
||||
if plan is None or getattr(plan, 'is_active', False) is False:
|
||||
return self._deny_request(
|
||||
request,
|
||||
error='Active subscription required. Visit igny8.com/pricing to subscribe.',
|
||||
status_code=status.HTTP_402_PAYMENT_REQUIRED,
|
||||
)
|
||||
if not is_valid:
|
||||
return self._deny_request(request, error_message, http_status)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
# Generated manually based on FINAL-IMPLEMENTATION-REQUIREMENTS.md
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('igny8_core_auth', '0006_soft_delete_and_retention'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
# Add payment_method to Account
|
||||
migrations.AddField(
|
||||
model_name='account',
|
||||
name='payment_method',
|
||||
field=models.CharField(
|
||||
max_length=30,
|
||||
choices=[
|
||||
('stripe', 'Stripe'),
|
||||
('paypal', 'PayPal'),
|
||||
('bank_transfer', 'Bank Transfer'),
|
||||
],
|
||||
default='stripe',
|
||||
help_text='Payment method used for this account'
|
||||
),
|
||||
),
|
||||
# Add payment_method to Subscription
|
||||
migrations.AddField(
|
||||
model_name='subscription',
|
||||
name='payment_method',
|
||||
field=models.CharField(
|
||||
max_length=30,
|
||||
choices=[
|
||||
('stripe', 'Stripe'),
|
||||
('paypal', 'PayPal'),
|
||||
('bank_transfer', 'Bank Transfer'),
|
||||
],
|
||||
default='stripe',
|
||||
help_text='Payment method for this subscription'
|
||||
),
|
||||
),
|
||||
# Add external_payment_id to Subscription
|
||||
migrations.AddField(
|
||||
model_name='subscription',
|
||||
name='external_payment_id',
|
||||
field=models.CharField(
|
||||
max_length=255,
|
||||
blank=True,
|
||||
null=True,
|
||||
help_text='External payment reference (bank transfer ref, PayPal transaction ID)'
|
||||
),
|
||||
),
|
||||
# Make stripe_subscription_id nullable
|
||||
migrations.AlterField(
|
||||
model_name='subscription',
|
||||
name='stripe_subscription_id',
|
||||
field=models.CharField(
|
||||
max_length=255,
|
||||
blank=True,
|
||||
null=True,
|
||||
db_index=True,
|
||||
help_text='Stripe subscription ID (when using Stripe)'
|
||||
),
|
||||
),
|
||||
# Add pending_payment status to Account
|
||||
migrations.AlterField(
|
||||
model_name='account',
|
||||
name='status',
|
||||
field=models.CharField(
|
||||
max_length=20,
|
||||
choices=[
|
||||
('active', 'Active'),
|
||||
('suspended', 'Suspended'),
|
||||
('trial', 'Trial'),
|
||||
('cancelled', 'Cancelled'),
|
||||
('pending_payment', 'Pending Payment'),
|
||||
],
|
||||
default='trial'
|
||||
),
|
||||
),
|
||||
# Add pending_payment status to Subscription
|
||||
migrations.AlterField(
|
||||
model_name='subscription',
|
||||
name='status',
|
||||
field=models.CharField(
|
||||
max_length=20,
|
||||
choices=[
|
||||
('active', 'Active'),
|
||||
('past_due', 'Past Due'),
|
||||
('canceled', 'Canceled'),
|
||||
('trialing', 'Trialing'),
|
||||
('pending_payment', 'Pending Payment'),
|
||||
]
|
||||
),
|
||||
),
|
||||
# Add index on payment_method
|
||||
migrations.AddIndex(
|
||||
model_name='account',
|
||||
index=models.Index(fields=['payment_method'], name='auth_acc_payment_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='subscription',
|
||||
index=models.Index(fields=['payment_method'], name='auth_sub_payment_idx'),
|
||||
),
|
||||
]
|
||||
@@ -62,6 +62,13 @@ class Account(SoftDeletableModel):
|
||||
('suspended', 'Suspended'),
|
||||
('trial', 'Trial'),
|
||||
('cancelled', 'Cancelled'),
|
||||
('pending_payment', 'Pending Payment'),
|
||||
]
|
||||
|
||||
PAYMENT_METHOD_CHOICES = [
|
||||
('stripe', 'Stripe'),
|
||||
('paypal', 'PayPal'),
|
||||
('bank_transfer', 'Bank Transfer'),
|
||||
]
|
||||
|
||||
name = models.CharField(max_length=255)
|
||||
@@ -77,6 +84,12 @@ class Account(SoftDeletableModel):
|
||||
plan = models.ForeignKey('igny8_core_auth.Plan', on_delete=models.PROTECT, related_name='accounts')
|
||||
credits = models.IntegerField(default=0, validators=[MinValueValidator(0)])
|
||||
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='trial')
|
||||
payment_method = models.CharField(
|
||||
max_length=30,
|
||||
choices=PAYMENT_METHOD_CHOICES,
|
||||
default='stripe',
|
||||
help_text='Payment method used for this account'
|
||||
)
|
||||
deletion_retention_days = models.PositiveIntegerField(
|
||||
default=14,
|
||||
validators=[MinValueValidator(1), MaxValueValidator(365)],
|
||||
@@ -191,17 +204,42 @@ class Plan(models.Model):
|
||||
|
||||
class Subscription(models.Model):
|
||||
"""
|
||||
Account subscription model linking to Stripe.
|
||||
Account subscription model supporting multiple payment methods.
|
||||
"""
|
||||
STATUS_CHOICES = [
|
||||
('active', 'Active'),
|
||||
('past_due', 'Past Due'),
|
||||
('canceled', 'Canceled'),
|
||||
('trialing', 'Trialing'),
|
||||
('pending_payment', 'Pending Payment'),
|
||||
]
|
||||
|
||||
PAYMENT_METHOD_CHOICES = [
|
||||
('stripe', 'Stripe'),
|
||||
('paypal', 'PayPal'),
|
||||
('bank_transfer', 'Bank Transfer'),
|
||||
]
|
||||
|
||||
account = models.OneToOneField('igny8_core_auth.Account', on_delete=models.CASCADE, related_name='subscription', db_column='tenant_id')
|
||||
stripe_subscription_id = models.CharField(max_length=255, unique=True)
|
||||
stripe_subscription_id = models.CharField(
|
||||
max_length=255,
|
||||
blank=True,
|
||||
null=True,
|
||||
db_index=True,
|
||||
help_text='Stripe subscription ID (when using Stripe)'
|
||||
)
|
||||
payment_method = models.CharField(
|
||||
max_length=30,
|
||||
choices=PAYMENT_METHOD_CHOICES,
|
||||
default='stripe',
|
||||
help_text='Payment method for this subscription'
|
||||
)
|
||||
external_payment_id = models.CharField(
|
||||
max_length=255,
|
||||
blank=True,
|
||||
null=True,
|
||||
help_text='External payment reference (bank transfer ref, PayPal transaction ID)'
|
||||
)
|
||||
status = models.CharField(max_length=20, choices=STATUS_CHOICES)
|
||||
current_period_start = models.DateTimeField()
|
||||
current_period_end = models.DateTimeField()
|
||||
|
||||
@@ -27,8 +27,8 @@ class SubscriptionSerializer(serializers.ModelSerializer):
|
||||
model = Subscription
|
||||
fields = [
|
||||
'id', 'account', 'account_name', 'account_slug',
|
||||
'stripe_subscription_id', 'status',
|
||||
'current_period_start', 'current_period_end',
|
||||
'stripe_subscription_id', 'payment_method', 'external_payment_id',
|
||||
'status', 'current_period_start', 'current_period_end',
|
||||
'cancel_at_period_end',
|
||||
'created_at', 'updated_at'
|
||||
]
|
||||
@@ -48,7 +48,11 @@ class AccountSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Account
|
||||
fields = ['id', 'name', 'slug', 'owner', 'plan', 'plan_id', 'credits', 'status', 'subscription', 'created_at']
|
||||
fields = [
|
||||
'id', 'name', 'slug', 'owner', 'plan', 'plan_id',
|
||||
'credits', 'status', 'payment_method',
|
||||
'subscription', 'created_at'
|
||||
]
|
||||
read_only_fields = ['owner', 'created_at']
|
||||
|
||||
|
||||
@@ -260,6 +264,12 @@ class RegisterSerializer(serializers.Serializer):
|
||||
allow_null=True,
|
||||
default=None
|
||||
)
|
||||
plan_slug = serializers.CharField(max_length=50, required=False)
|
||||
payment_method = serializers.ChoiceField(
|
||||
choices=['stripe', 'paypal', 'bank_transfer'],
|
||||
default='bank_transfer',
|
||||
required=False
|
||||
)
|
||||
|
||||
def validate(self, attrs):
|
||||
if attrs['password'] != attrs['password_confirm']:
|
||||
|
||||
@@ -128,3 +128,65 @@ def get_token_expiry(token_type='access'):
|
||||
return now + get_refresh_token_expiry()
|
||||
return now + get_access_token_expiry()
|
||||
|
||||
|
||||
|
||||
|
||||
def validate_account_and_plan(user_or_account):
|
||||
"""
|
||||
Validate account exists and has active plan.
|
||||
Allows trial, active, and pending_payment statuses.
|
||||
|
||||
Args:
|
||||
user_or_account: User or Account instance
|
||||
|
||||
Returns:
|
||||
tuple: (is_valid: bool, error_msg: str or None, http_status: int or None)
|
||||
"""
|
||||
from rest_framework import status
|
||||
from .models import User, Account
|
||||
|
||||
# Extract account from user or use directly
|
||||
if isinstance(user_or_account, User):
|
||||
try:
|
||||
account = getattr(user_or_account, 'account', None)
|
||||
except Exception:
|
||||
account = None
|
||||
elif isinstance(user_or_account, Account):
|
||||
account = user_or_account
|
||||
else:
|
||||
return (False, 'Invalid object type', status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# Check account exists
|
||||
if not account:
|
||||
return (
|
||||
False,
|
||||
'Account not configured for this user. Please contact support.',
|
||||
status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
# Check account status - allow trial, active, pending_payment
|
||||
# Block only suspended and cancelled
|
||||
if hasattr(account, 'status') and account.status in ['suspended', 'cancelled']:
|
||||
return (
|
||||
False,
|
||||
f'Account is {account.status}. Please contact support.',
|
||||
status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
# Check plan exists and is active
|
||||
plan = getattr(account, 'plan', None)
|
||||
if not plan:
|
||||
return (
|
||||
False,
|
||||
'No subscription plan assigned. Visit igny8.com/pricing to subscribe.',
|
||||
status.HTTP_402_PAYMENT_REQUIRED
|
||||
)
|
||||
|
||||
if hasattr(plan, 'is_active') and not plan.is_active:
|
||||
return (
|
||||
False,
|
||||
'Active subscription required. Visit igny8.com/pricing to subscribe.',
|
||||
status.HTTP_402_PAYMENT_REQUIRED
|
||||
)
|
||||
|
||||
return (True, None, None)
|
||||
|
||||
Reference in New Issue
Block a user