diff --git a/src/khoj/routers/api_subscription.py b/src/khoj/routers/api_subscription.py index 4bb4f63e..b583a991 100644 --- a/src/khoj/routers/api_subscription.py +++ b/src/khoj/routers/api_subscription.py @@ -66,11 +66,18 @@ async def subscribe(request: Request): success = user is not None elif event_type in {"customer.subscription.updated"}: user_subscription = await sync_to_async(adapters.get_user_subscription)(customer_email) + + renewal_date = False + if subscription["current_period_end"]: + renewal_date = datetime.fromtimestamp(subscription["current_period_end"], tz=timezone.utc) + # Allow updating subscription status if paid user if user_subscription and user_subscription.renewal_date: # Mark user as unsubscribed or resubscribed is_recurring = not subscription["cancel_at_period_end"] - user, is_new = await adapters.set_user_subscription(customer_email, is_recurring=is_recurring) + user, is_new = await adapters.set_user_subscription( + customer_email, is_recurring=is_recurring, renewal_date=renewal_date + ) success = user is not None elif event_type in {"customer.subscription.deleted"}: # Reset the user to trial state