Fix handling of customer.subscription.updated event to process new renewal end date

This commit is contained in:
sabaimran 2024-11-21 20:31:03 -08:00
parent 6a054d884b
commit a761865724

View file

@ -66,11 +66,18 @@ async def subscribe(request: Request):
success = user is not None success = user is not None
elif event_type in {"customer.subscription.updated"}: elif event_type in {"customer.subscription.updated"}:
user_subscription = await sync_to_async(adapters.get_user_subscription)(customer_email) 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 # Allow updating subscription status if paid user
if user_subscription and user_subscription.renewal_date: if user_subscription and user_subscription.renewal_date:
# Mark user as unsubscribed or resubscribed # Mark user as unsubscribed or resubscribed
is_recurring = not subscription["cancel_at_period_end"] 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 success = user is not None
elif event_type in {"customer.subscription.deleted"}: elif event_type in {"customer.subscription.deleted"}:
# Reset the user to trial state # Reset the user to trial state