Skip to content

Commit 181a964

Browse files
davidfischeragjohnson
authored andcommitted
Fixes an issue with duplicate gold subscriptions (#4597)
1 parent c32d612 commit 181a964

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

readthedocs/gold/forms.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""Gold subscription forms."""
22

33
from __future__ import absolute_import
4+
45
from builtins import object
56
from django import forms
67

7-
from stripe.error import InvalidRequestError
88
from readthedocs.payments.forms import StripeModelForm, StripeResourceMixin
99

1010
from .models import LEVEL_CHOICES, GoldUser
@@ -57,21 +57,25 @@ def get_customer_kwargs(self):
5757

5858
def get_subscription(self):
5959
customer = self.get_customer()
60-
try:
61-
# TODO get the first sub more intelligently
62-
subscriptions = customer.subscriptions.all(limit=5)
60+
61+
# TODO get the first subscription more intelligently
62+
subscriptions = customer.subscriptions.all(limit=5)
63+
if subscriptions.data:
64+
# Update an existing subscription - Stripe prorates by default
6365
subscription = subscriptions.data[0]
6466
subscription.plan = self.cleaned_data['level']
65-
if 'stripe_token' in self.cleaned_data:
67+
if 'stripe_token' in self.cleaned_data and self.cleaned_data['stripe_token']:
68+
# Optionally update the card
6669
subscription.source = self.cleaned_data['stripe_token']
6770
subscription.save()
68-
return subscription
69-
except (InvalidRequestError, AttributeError, IndexError):
71+
else:
72+
# Add a new subscription
7073
subscription = customer.subscriptions.create(
7174
plan=self.cleaned_data['level'],
7275
source=self.cleaned_data['stripe_token']
7376
)
74-
return subscription
77+
78+
return subscription
7579

7680

7781
class GoldProjectForm(forms.Form):

0 commit comments

Comments
 (0)