diff --git a/core/migrations/0021_userpremiumplan_customer_and_more.py b/core/migrations/0021_userpremiumplan_customer_and_more.py new file mode 100644 index 00000000..08230421 --- /dev/null +++ b/core/migrations/0021_userpremiumplan_customer_and_more.py @@ -0,0 +1,35 @@ +# Generated by Django 4.2.5 on 2024-01-01 20:37 + +import auto_prefetch +from django.db import migrations +import django.db.models.deletion +import djmoney.models.fields + + +class Migration(migrations.Migration): + dependencies = [ + ("djstripe", "0012_2_8"), + ("core", "0020_alter_premiumplan_premium_templates"), + ] + + operations = [ + migrations.AddField( + model_name="userpremiumplan", + name="customer", + field=auto_prefetch.ForeignKey( + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to="djstripe.customer", + ), + ), + migrations.AlterField( + model_name="premiumplan", + name="price_currency", + field=djmoney.models.fields.CurrencyField( + choices=[("EUR", "Euro"), ("USD", "US Dollar")], + default="EUR", + editable=False, + max_length=3, + ), + ), + ] diff --git a/core/models/users.py b/core/models/users.py index 8ee92c44..e34d182e 100644 --- a/core/models/users.py +++ b/core/models/users.py @@ -8,6 +8,8 @@ from .plans import FreePlan from .plans import PremiumPlan +from djstripe.models import Customer + class User(AbstractUser): avatar_url = models.URLField(null=True, blank=True) @@ -36,6 +38,7 @@ class UserPremiumPlan(auto_prefetch.Model): User, related_name="user_premium_plans", on_delete=models.SET_NULL, null=True ) plan = auto_prefetch.ForeignKey(PremiumPlan, on_delete=models.SET_NULL, null=True) + customer = auto_prefetch.ForeignKey(Customer, on_delete=models.SET_NULL, null=True) created = models.DateField(auto_now_add=True) starts = models.DateField() expires = models.DateField() diff --git a/core/signals.py b/core/signals.py index 10af8219..902e2af1 100644 --- a/core/signals.py +++ b/core/signals.py @@ -4,11 +4,40 @@ from django.core.mail import EmailMessage from django.conf import settings -from .models.users import UserPremiumPlan +from .models.users import UserPremiumPlan, User +from .models.plans import PremiumPlan +from djstripe.models import Event, Customer +from djstripe import settings as djstripe_settings + + +customer_key = f"{djstripe_settings.djstripe_settings.SUBSCRIBER_CUSTOMER_KEY}" + + +@receiver(post_save, sender=Event) +def process_stripe_event(sender, instance, created, **kwargs): + proceed = ( + instance.type == "checkout.session.completed" + and "plan_id" + and "user_id" in instance.data["object"]["metadata"] + ) + + if not proceed: + return + + plan_id = int(instance.data["object"]["metadata"]["plan_id"]) + user_id = int(instance.data["object"]["metadata"]["user_id"]) + customer_id = int(instance.data["object"]["metadata"]["customer_key"]) + + try: + user = User.objects.get(id=user_id) + plan = PremiumPlan.objects.get(id=plan_id) + customer = Customer.objects.get(id=customer_id) + except (User.DoesNotExist, PremiumPlan.DoesNotExist, Customer.DoesNotExist): + pass + + userplan = UserPremiumPlan.objects.create(plan=plan, user=user, customer=customer) -@receiver(post_save, sender=UserPremiumPlan) -def send_welcome_email(sender, instance, created, **kwargs): subject = "Nice CV | " + _("Welcome") body = _( """Hi, @@ -21,11 +50,11 @@ def send_welcome_email(sender, instance, created, **kwargs): Rami (nicecv.online) """ ) - m = EmailMessage(subject, body, settings.DEFAULT_FROM_EMAIL, [instance.user.email]) + m = EmailMessage(subject, body, settings.DEFAULT_FROM_EMAIL, [userplan.user.email]) m.send(fail_silently=True) ## check if user has ordered a plan with manual profile creation - if instance.plan.profile_manual: + if userplan.plan.profile_manual: subject_m = "Nice CV | " + _("Send us your actual CV") body_m = _( """Hi again, @@ -38,6 +67,6 @@ def send_welcome_email(sender, instance, created, **kwargs): """ ) m_m = EmailMessage( - subject_m, body_m, settings.DEFAULT_FROM_EMAIL, [instance.user.email] + subject_m, body_m, settings.DEFAULT_FROM_EMAIL, [userplan.user.email] ) m_m.send(fail_silently=True) diff --git a/core/views/plans.py b/core/views/plans.py index 08b685b3..0e8f4d11 100644 --- a/core/views/plans.py +++ b/core/views/plans.py @@ -27,10 +27,8 @@ def plan_list(request): - context = { - "plans": PremiumPlan.objects.all(), - "faqs": FrequentAskedQuestion.objects.filter(active=True, category="pricing"), - } + faqs = FrequentAskedQuestion.objects.filter(active=True, category="pricing") + context = {"plans": PremiumPlan.objects.all(), "faqs": faqs} return render(request, "plans/plan_list.html", context)