Skip to content

Commit

Permalink
stripe webhook by djstripe
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiboutas committed Jan 1, 2024
1 parent d8166f9 commit 141b0e4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 10 deletions.
35 changes: 35 additions & 0 deletions core/migrations/0021_userpremiumplan_customer_and_more.py
Original file line number Diff line number Diff line change
@@ -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,
),
),
]
3 changes: 3 additions & 0 deletions core/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
41 changes: 35 additions & 6 deletions core/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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)
6 changes: 2 additions & 4 deletions core/views/plans.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit 141b0e4

Please sign in to comment.