-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Generated by Django 3.2.16 on 2022-11-21 22:52 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("djstripe", "0010_alter_customer_balance"), | ||
("organizations", "0010_add_stripe_customer"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="historicalorganization", | ||
name="stripe_subscription", | ||
field=models.ForeignKey( | ||
blank=True, | ||
db_constraint=False, | ||
null=True, | ||
on_delete=django.db.models.deletion.DO_NOTHING, | ||
related_name="+", | ||
to="djstripe.subscription", | ||
verbose_name="Stripe subscription", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="organization", | ||
name="stripe_subscription", | ||
field=models.OneToOneField( | ||
blank=True, | ||
null=True, | ||
on_delete=django.db.models.deletion.SET_NULL, | ||
related_name="rtd_organization", | ||
to="djstripe.subscription", | ||
verbose_name="Stripe subscription", | ||
), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ | |||||||
from django.urls import reverse | ||||||||
from django.utils.crypto import salted_hmac | ||||||||
from django.utils.translation import gettext_lazy as _ | ||||||||
from djstripe.enums import SubscriptionStatus | ||||||||
|
||||||||
from readthedocs.core.history import ExtraHistoricalRecords | ||||||||
from readthedocs.core.permissions import AdminPermission | ||||||||
|
@@ -101,6 +102,14 @@ class Organization(models.Model): | |||||||
null=True, | ||||||||
blank=True, | ||||||||
) | ||||||||
stripe_subscription = models.OneToOneField( | ||||||||
"djstripe.Subscription", | ||||||||
verbose_name=_("Stripe subscription"), | ||||||||
on_delete=models.SET_NULL, | ||||||||
related_name="rtd_organization", | ||||||||
null=True, | ||||||||
blank=True, | ||||||||
) | ||||||||
|
||||||||
# Managers | ||||||||
objects = OrganizationQuerySet.as_manager() | ||||||||
|
@@ -115,15 +124,20 @@ class Meta: | |||||||
def __str__(self): | ||||||||
return self.name | ||||||||
|
||||||||
@property | ||||||||
def stripe_subscription(self): | ||||||||
def get_or_create_stripe_subscription(self): | ||||||||
# 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( | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
status=SubscriptionStatus.active | ||||||||
).first() | ||||||||
if active_subscription: | ||||||||
return active_subscription | ||||||||
return self.stripe_customer.subscriptions.latest() | ||||||||
|
||||||||
def get_absolute_url(self): | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,6 +155,15 @@ def checkout_completed(event): | |
return | ||
|
||
stripe_subscription_id = event.data["object"]["subscription"] | ||
stripe_subscription = djstripe.Subscription.objects.filter( | ||
id=stripe_subscription_id | ||
).first() | ||
if not stripe_subscription: | ||
log.info("Stripe subscription not found.") | ||
return | ||
Comment on lines
+161
to
+163
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
organization.stripe_subscription = stripe_subscription | ||
organization.save() | ||
|
||
_update_subscription_from_stripe( | ||
rtd_subscription=organization.subscription, | ||
stripe_subscription_id=stripe_subscription_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.
Make sure that this only happens on development. Otherwise, we should log an error/warning here.