From a761865724408efe92adf570d146ab44abac1917 Mon Sep 17 00:00:00 2001 From: sabaimran Date: Thu, 21 Nov 2024 20:31:03 -0800 Subject: [PATCH] Fix handling of customer.subscription.updated event to process new renewal end date --- src/khoj/routers/api_subscription.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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