free and trial plans fixes and styling of sigini and signup forms
This commit is contained in:
@@ -320,10 +320,18 @@ class RegisterSerializer(serializers.Serializer):
|
||||
if 'plan_id' in attrs and attrs.get('plan_id') == '':
|
||||
attrs['plan_id'] = None
|
||||
|
||||
# Validate billing fields for paid plans
|
||||
# Validate billing fields for paid plans (check by price, not hardcoded slugs)
|
||||
plan_slug = attrs.get('plan_slug')
|
||||
paid_plans = ['starter', 'growth', 'scale']
|
||||
if plan_slug and plan_slug in paid_plans:
|
||||
if plan_slug:
|
||||
try:
|
||||
plan = Plan.objects.get(slug=plan_slug, is_active=True)
|
||||
is_paid_plan = plan.price > 0
|
||||
except Plan.DoesNotExist:
|
||||
raise serializers.ValidationError({"plan": f"Plan '{plan_slug}' not found."})
|
||||
else:
|
||||
is_paid_plan = False
|
||||
|
||||
if is_paid_plan:
|
||||
# Require billing_country for paid plans
|
||||
if not attrs.get('billing_country'):
|
||||
raise serializers.ValidationError({
|
||||
@@ -348,27 +356,36 @@ class RegisterSerializer(serializers.Serializer):
|
||||
|
||||
with transaction.atomic():
|
||||
plan_slug = validated_data.get('plan_slug')
|
||||
paid_plans = ['starter', 'growth', 'scale']
|
||||
|
||||
if plan_slug and plan_slug in paid_plans:
|
||||
# Fetch plan and determine paid/free by price, not hardcoded slugs
|
||||
if plan_slug:
|
||||
try:
|
||||
plan = Plan.objects.get(slug=plan_slug, is_active=True)
|
||||
except Plan.DoesNotExist:
|
||||
raise serializers.ValidationError({
|
||||
"plan": f"Plan '{plan_slug}' not available. Please contact support."
|
||||
})
|
||||
else:
|
||||
# No plan_slug provided, get first free plan (price=0)
|
||||
try:
|
||||
plan = Plan.objects.filter(is_active=True, price=0).first()
|
||||
if not plan:
|
||||
raise Plan.DoesNotExist
|
||||
except Plan.DoesNotExist:
|
||||
raise serializers.ValidationError({
|
||||
"plan": "No free plan available. Please contact support."
|
||||
})
|
||||
|
||||
# Determine account status based on plan price
|
||||
is_paid_plan = plan.price > 0
|
||||
|
||||
if is_paid_plan:
|
||||
account_status = 'pending_payment'
|
||||
initial_credits = 0
|
||||
billing_period_start = timezone.now()
|
||||
# simple monthly cycle; if annual needed, extend here
|
||||
billing_period_end = billing_period_start + timedelta(days=30)
|
||||
else:
|
||||
try:
|
||||
plan = Plan.objects.get(slug='free', is_active=True)
|
||||
except Plan.DoesNotExist:
|
||||
raise serializers.ValidationError({
|
||||
"plan": "Free plan not configured. Please contact support."
|
||||
})
|
||||
account_status = 'trial'
|
||||
initial_credits = plan.get_effective_credits_per_month()
|
||||
billing_period_start = None
|
||||
|
||||
Reference in New Issue
Block a user