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:
Debanjum Singh Solanky
2023-11-07 10:15:21 -08:00
parent 156421d30a
commit 9aaf475c8a
5 changed files with 116 additions and 3 deletions

View File

@@ -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: