Skip to content

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

Merged
merged 2 commits into from
Oct 10, 2022

Conversation

stsewd
Copy link
Member

@stsewd stsewd commented Oct 4, 2022

  • Have a setting with the stripe id of the price instead of the slug of the Plan object.
    We still need the plan object, but now this is separate from the djstripe subscription object.
  • Separate the creation of our subscription object with the creation of a stripe subscription.
  • Use the stripe customer object from the db instead of fetching it from stripe
    to decide if we should update the customer information
  • Allow updating the metadata, this is in case the org changes its slug,
    we save the ID too, but is nice to have the slug updated too.

📚 Documentation previews 📚

@stsewd stsewd requested a review from a team as a code owner October 4, 2022 23:26
@stsewd stsewd requested a review from humitos October 4, 2022 23:26
@@ -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'
Copy link
Member Author

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

Copy link
Member

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(
Copy link
Member Author

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.

Comment on lines -50 to -54
if stripe_subscriptions.count() > 1:
log.warning(
"Customer with more than one active subscription.",
stripe_customer=stripe_customer.id,
)
Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same on .org

Copy link
Member Author

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.

@@ -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'
Copy link
Member

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

Comment on lines -50 to -54
if stripe_subscriptions.count() > 1:
log.warning(
"Customer with more than one active subscription.",
stripe_customer=stripe_customer.id,
)
Copy link
Member

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.

Comment on lines +55 to +57
for key, value in org_metadata.items():
if current_metadata.get(key) != value:
current_metadata.update(org_metadata)
Copy link
Member

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.

Copy link
Member Author

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.

@stsewd stsewd merged commit 9e7787c into main Oct 10, 2022
@stsewd stsewd deleted the use-stripe-instead-of-plan branch October 10, 2022 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants