-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Subscriptions: use stripe price instead of relying on plan object #9640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
readthedocs/settings/base.py
Outdated
@@ -718,7 +718,8 @@ def DOCKER_LIMITS(self): | |||
|
|||
# Organization settings | |||
RTD_ALLOW_ORGANIZATIONS = False | |||
ORG_DEFAULT_SUBSCRIPTION_PLAN_SLUG = 'trial-v2-monthly' | |||
RTD_ORG_DEFAULT_STRIPE_PRICE = 'trial-v2-monthly' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the stripe id was the same as the slug :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, this was done on purpose when we created these objects on Stripe
@@ -43,25 +45,7 @@ def get_or_create_default_subscription(self, organization): | |||
) | |||
return None | |||
|
|||
stripe_customer = get_or_create_stripe_customer(organization) | |||
stripe_subscriptions = stripe_customer.subscriptions.exclude( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
our subscriptions should be correct now, we don't need to use this queryset anymore (excluding canceled subs), if the last subs was canceled we allow the user to buy a new one.
if stripe_subscriptions.count() > 1: | ||
log.warning( | ||
"Customer with more than one active subscription.", | ||
stripe_customer=stripe_customer.id, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could still leave this warning (on subscriptions/utils.py where this code was moved), but I think it makes more sense to query all of our subs searching for cases like this and decide what to do if any.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'm fine trusting that our data is consistent. However, if it's not, how we will detect this? This code at least gave us some hint were to look at when Stripe data was inconsistent.
We added it to detect these cases because we weren't sure which ones were those customers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have 0 cases of more than one active subs
In [11]: from djstripe import models as djstripe
...: from django.db import models
...:
...: subquery = models.Subquery(
...: djstripe.Subscription.objects
...: .filter(customer=models.OuterRef("id"), status="active")
...: .annotate(count=models.Count("id"))
...: .values("count")
...: )
...:
...: q = djstripe.Customer.objects.annotate(active_subs_count=subquery).filter(active_subs_count__gt=1)
In [12]: q.count()
Out[12]: 0
Should we still have this warning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same on .org
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm merging this, so I can create a new PR with some changes I have, with main as base instead of this branch as base. Let me know if you still think we should log this, I can just open a new PR for that.
readthedocs/settings/base.py
Outdated
@@ -718,7 +718,8 @@ def DOCKER_LIMITS(self): | |||
|
|||
# Organization settings | |||
RTD_ALLOW_ORGANIZATIONS = False | |||
ORG_DEFAULT_SUBSCRIPTION_PLAN_SLUG = 'trial-v2-monthly' | |||
RTD_ORG_DEFAULT_STRIPE_PRICE = 'trial-v2-monthly' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, this was done on purpose when we created these objects on Stripe
if stripe_subscriptions.count() > 1: | ||
log.warning( | ||
"Customer with more than one active subscription.", | ||
stripe_customer=stripe_customer.id, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'm fine trusting that our data is consistent. However, if it's not, how we will detect this? This code at least gave us some hint were to look at when Stripe data was inconsistent.
We added it to detect these cases because we weren't sure which ones were those customers.
for key, value in org_metadata.items(): | ||
if current_metadata.get(key) != value: | ||
current_metadata.update(org_metadata) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the complexity of this for is required. We could just do:
current_metadata.update(org_metadata)
fields_to_update["metadata"] = current_metadata
which will add new fields and update only the fields that have changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will save one request in case the data hasn't changed, otherwise we will hit the stripe API every time the organization is updated.
We still need the plan object, but now this is separate from the djstripe subscription object.
to decide if we should update the customer information
we save the ID too, but is nice to have the slug updated too.
📚 Documentation previews 📚
docs
): https://docs--9640.org.readthedocs.build/en/9640/dev
): https://dev--9640.org.readthedocs.build/en/9640/