dasdas
This commit is contained in:
@@ -286,6 +286,11 @@ class RegisterSerializer(serializers.Serializer):
|
||||
def create(self, validated_data):
|
||||
from django.db import transaction
|
||||
from igny8_core.business.billing.models import CreditTransaction
|
||||
from igny8_core.business.billing.models import Subscription
|
||||
from igny8_core.business.billing.models import AccountPaymentMethod
|
||||
from igny8_core.business.billing.services.invoice_service import InvoiceService
|
||||
from django.utils import timezone
|
||||
from datetime import timedelta
|
||||
|
||||
with transaction.atomic():
|
||||
plan_slug = validated_data.get('plan_slug')
|
||||
@@ -300,6 +305,9 @@ class RegisterSerializer(serializers.Serializer):
|
||||
})
|
||||
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)
|
||||
@@ -309,6 +317,8 @@ class RegisterSerializer(serializers.Serializer):
|
||||
})
|
||||
account_status = 'trial'
|
||||
initial_credits = plan.get_effective_credits_per_month()
|
||||
billing_period_start = None
|
||||
billing_period_end = None
|
||||
|
||||
# Generate account name if not provided
|
||||
account_name = validated_data.get('account_name')
|
||||
@@ -380,6 +390,35 @@ class RegisterSerializer(serializers.Serializer):
|
||||
# Update user to reference the new account
|
||||
user.account = account
|
||||
user.save()
|
||||
|
||||
# For paid plans, create subscription, invoice, and default bank transfer method
|
||||
if plan_slug and plan_slug in paid_plans:
|
||||
subscription = Subscription.objects.create(
|
||||
account=account,
|
||||
plan=plan,
|
||||
status='pending_payment',
|
||||
payment_method='bank_transfer',
|
||||
external_payment_id=None,
|
||||
current_period_start=billing_period_start,
|
||||
current_period_end=billing_period_end,
|
||||
cancel_at_period_end=False,
|
||||
)
|
||||
# Create pending invoice for the first period
|
||||
InvoiceService.create_subscription_invoice(
|
||||
subscription=subscription,
|
||||
billing_period_start=billing_period_start,
|
||||
billing_period_end=billing_period_end,
|
||||
)
|
||||
# Seed a default bank transfer payment method for the account
|
||||
AccountPaymentMethod.objects.create(
|
||||
account=account,
|
||||
type='bank_transfer',
|
||||
display_name='Bank Transfer (Manual)',
|
||||
is_default=True,
|
||||
is_enabled=True,
|
||||
is_verified=False,
|
||||
instructions='Please complete bank transfer and add your reference in Payments.',
|
||||
)
|
||||
|
||||
return user
|
||||
|
||||
|
||||
Reference in New Issue
Block a user