Skip to content

Commit

Permalink
Fixes an issue with duplicate gold subscriptions (#4597)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfischer authored and agjohnson committed Sep 5, 2018
1 parent c32d612 commit 181a964
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions readthedocs/gold/forms.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Gold subscription forms."""

from __future__ import absolute_import

from builtins import object
from django import forms

from stripe.error import InvalidRequestError
from readthedocs.payments.forms import StripeModelForm, StripeResourceMixin

from .models import LEVEL_CHOICES, GoldUser
Expand Down Expand Up @@ -57,21 +57,25 @@ def get_customer_kwargs(self):

def get_subscription(self):
customer = self.get_customer()
try:
# TODO get the first sub more intelligently
subscriptions = customer.subscriptions.all(limit=5)

# TODO get the first subscription more intelligently
subscriptions = customer.subscriptions.all(limit=5)
if subscriptions.data:
# Update an existing subscription - Stripe prorates by default
subscription = subscriptions.data[0]
subscription.plan = self.cleaned_data['level']
if 'stripe_token' in self.cleaned_data:
if 'stripe_token' in self.cleaned_data and self.cleaned_data['stripe_token']:
# Optionally update the card
subscription.source = self.cleaned_data['stripe_token']
subscription.save()
return subscription
except (InvalidRequestError, AttributeError, IndexError):
else:
# Add a new subscription
subscription = customer.subscriptions.create(
plan=self.cleaned_data['level'],
source=self.cleaned_data['stripe_token']
)
return subscription

return subscription


class GoldProjectForm(forms.Form):
Expand Down

0 comments on commit 181a964

Please sign in to comment.