mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-04 05:39:06 +00:00
Create API webhook, endpoints for subscription payments using Stripe
- Add fields to mark users as subscribed to a specific plan and subscription renewal date in DB - Add ability to unsubscribe a user using their email address - Expose webhook for stripe to callback confirming payment
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from typing import Type, TypeVar, List
|
||||
from datetime import date
|
||||
from datetime import date, datetime, timedelta
|
||||
import secrets
|
||||
from typing import Type, TypeVar, List
|
||||
from datetime import date
|
||||
@@ -103,6 +103,27 @@ async def create_google_user(token: dict) -> KhojUser:
|
||||
return user
|
||||
|
||||
|
||||
async def set_user_subscribed(email: str, type="standard") -> KhojUser:
|
||||
user = await KhojUser.objects.filter(email=email).afirst()
|
||||
if user:
|
||||
user.subscription_type = type
|
||||
start_date = user.subscription_renewal_date or datetime.now()
|
||||
user.subscription_renewal_date = start_date + timedelta(days=30)
|
||||
await user.asave()
|
||||
return user
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def is_user_subscribed(email: str, type="standard") -> bool:
|
||||
user = KhojUser.objects.filter(email=email, subscription_type=type).first()
|
||||
if user and user.subscription_renewal_date:
|
||||
is_subscribed = user.subscription_renewal_date > date.today()
|
||||
return is_subscribed
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
async def get_user_by_token(token: dict) -> KhojUser:
|
||||
google_user = await GoogleUser.objects.filter(sub=token.get("sub")).select_related("user").afirst()
|
||||
if not google_user:
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# Generated by Django 4.2.5 on 2023-11-07 18:19
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("database", "0012_entry_file_source"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="khojuser",
|
||||
name="subscription_renewal_date",
|
||||
field=models.DateTimeField(default=None, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="khojuser",
|
||||
name="subscription_type",
|
||||
field=models.CharField(
|
||||
choices=[("trial", "Trial"), ("standard", "Standard")], default="trial", max_length=20
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -14,7 +14,15 @@ class BaseModel(models.Model):
|
||||
|
||||
|
||||
class KhojUser(AbstractUser):
|
||||
class SubscriptionType(models.TextChoices):
|
||||
TRIAL = "trial"
|
||||
STANDARD = "standard"
|
||||
|
||||
uuid = models.UUIDField(models.UUIDField(default=uuid.uuid4, editable=False))
|
||||
subscription_type = models.CharField(
|
||||
max_length=20, choices=SubscriptionType.choices, default=SubscriptionType.TRIAL
|
||||
)
|
||||
subscription_renewal_date = models.DateTimeField(null=True, default=None)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.uuid:
|
||||
|
||||
Reference in New Issue
Block a user