|
1 | 1 | """Gold subscription forms."""
|
2 | 2 |
|
3 | 3 | from __future__ import absolute_import
|
| 4 | + |
4 | 5 | from builtins import object
|
5 | 6 | from django import forms
|
6 | 7 |
|
7 |
| -from stripe.error import InvalidRequestError |
8 | 8 | from readthedocs.payments.forms import StripeModelForm, StripeResourceMixin
|
9 | 9 |
|
10 | 10 | from .models import LEVEL_CHOICES, GoldUser
|
@@ -57,21 +57,25 @@ def get_customer_kwargs(self):
|
57 | 57 |
|
58 | 58 | def get_subscription(self):
|
59 | 59 | 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 |
63 | 65 | subscription = subscriptions.data[0]
|
64 | 66 | 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 |
66 | 69 | subscription.source = self.cleaned_data['stripe_token']
|
67 | 70 | subscription.save()
|
68 |
| - return subscription |
69 |
| - except (InvalidRequestError, AttributeError, IndexError): |
| 71 | + else: |
| 72 | + # Add a new subscription |
70 | 73 | subscription = customer.subscriptions.create(
|
71 | 74 | plan=self.cleaned_data['level'],
|
72 | 75 | source=self.cleaned_data['stripe_token']
|
73 | 76 | )
|
74 |
| - return subscription |
| 77 | + |
| 78 | + return subscription |
75 | 79 |
|
76 | 80 |
|
77 | 81 | class GoldProjectForm(forms.Form):
|
|
0 commit comments