Skip to content

Subscriptions: attach stripe subscription to organizations #9751

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
Nov 22, 2022

Conversation

stsewd
Copy link
Member

@stsewd stsewd commented Nov 22, 2022

After deploying this code, we need to attach the proper stripe subscription object to organizations

from djstripe.enums import SubscriptionStatus
from readthedocs.organizations.models import Organization

organizations = (
    Organization.objects.filter(stripe_subscription=None)
    .exclude(stripe_customer=None)
    .select_related("stripe_customer")
)
for organization in organizations:
    stripe_subscription = organization.stripe_customer.subscriptions.filter(
        status=SubscriptionStatus.active
    ).first()
    if not stripe_subscription:
        stripe_subscription = organization.stripe_customer.subscriptions.latest()
    organization.stripe_subscription = stripe_subscription
    organization.save()

Closes #9652
Closes https://github.com/readthedocs/readthedocs-corporate/issues/1508
Ref #9313 (comment)

@stsewd stsewd requested a review from a team as a code owner November 22, 2022 00:05
@stsewd stsewd requested a review from humitos November 22, 2022 00:05
Copy link
Member

@humitos humitos left a comment

Choose a reason for hiding this comment

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

This is good to me. I'd like to see more comments in the code only, but 👍🏼

# TODO: remove this once we don't depend on our Subscription models.
from readthedocs.subscriptions.models import Subscription

subscription = Subscription.objects.get_or_create_default_subscription(self)
if not subscription:
# This only happens during development.
return None

active_subscription = self.stripe_customer.subscriptions.filter(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
active_subscription = self.stripe_customer.subscriptions.filter(
# Always return the active subscription first if the customer has multiple subscriptions
active_subscription = self.stripe_customer.subscriptions.filter(

Comment on lines 132 to 133
if not subscription:
# This only happens during development.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if not subscription:
# This only happens during development.
if not subscription and settings.DEBUG:
# This only happens during development.

Make sure that this only happens on development. Otherwise, we should log an error/warning here.

Comment on lines +161 to +163
if not stripe_subscription:
log.info("Stripe subscription not found.")
return
Copy link
Member

Choose a reason for hiding this comment

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

What is the case where this can happen? I'm not sure to follow it. If Stripe Checkout is the one that creates the Subscription, why we would not find it after that?

Copy link
Member Author

Choose a reason for hiding this comment

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

just being defensive, the creation of the subs object could have failed or could have been deleted.

@stsewd stsewd merged commit 52d4766 into main Nov 22, 2022
@stsewd stsewd deleted the attach-stripe-subs-to-org branch November 22, 2022 18:25
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.

Subscriptions: attach stripe_subscription to Organization model?
2 participants