Store stripe customer id in Account.gold_subscr_id.

We need to do lookups of Account by stripe customer id and
gold_subscr_id has an index. Previously was using a new attribute
stripe_customer_id which didn't have an index and lookups were too
slow.
This commit is contained in:
Brian Simpson
2013-11-16 23:47:19 -05:00
parent 1784c60562
commit 210dd1b3fe
5 changed files with 26 additions and 17 deletions

View File

@@ -849,10 +849,10 @@ class StripeController(GoldPaymentController):
@classmethod
@handle_stripe_error
def set_creditcard(cls, form, user, token):
if not getattr(user, 'stripe_customer_id', None):
if not user.has_stripe_subscription:
return
customer = stripe.Customer.retrieve(user.stripe_customer_id)
customer = stripe.Customer.retrieve(user.gold_subscr_id)
customer.card = token
customer.save()
return customer
@@ -860,13 +860,13 @@ class StripeController(GoldPaymentController):
@classmethod
@handle_stripe_error
def cancel_subscription(cls, user):
if not getattr(user, 'stripe_customer_id', None):
if not user.has_stripe_subscription:
return
customer = stripe.Customer.retrieve(user.stripe_customer_id)
customer = stripe.Customer.retrieve(user.gold_subscr_id)
customer.delete()
user.stripe_customer_id = None
user.gold_subscr_id = None
user._commit()
subject = _('your gold subscription has been cancelled')
message = _('if you have any questions please email %(email)s')
@@ -915,7 +915,7 @@ class StripeController(GoldPaymentController):
return
if period:
c.user.stripe_customer_id = customer.id
c.user.gold_subscr_id = customer.id
c.user._commit()
status = _('subscription created')

View File

@@ -1703,10 +1703,10 @@ class ProfileBar(Templated):
self.gold_remaining = timeuntil(self.gold_expiration,
precision)
if hasattr(user, "gold_subscr_id"):
self.gold_subscr_id = user.gold_subscr_id
if hasattr(user, "stripe_customer_id"):
self.stripe_customer_id = user.stripe_customer_id
if user.has_paypal_subscription:
self.paypal_subscr_id = user.gold_subscr_id
if user.has_stripe_subscription:
self.stripe_customer_id = user.gold_subscr_id
if ((user._id == c.user._id or c.user_is_admin) and
user.gold_creddits > 0):
@@ -2257,7 +2257,7 @@ class GoldPayment(Templated):
class GoldSubscription(Templated):
def __init__(self, user):
if hasattr(user, 'stripe_customer_id'):
if user.has_stripe_subscription:
details = get_subscription_details(user)
else:
details = None

View File

@@ -614,8 +614,17 @@ class Account(Thing):
@property
def has_gold_subscription(self):
return bool(getattr(self, 'gold_subscr_id', None) or
getattr(self, 'stripe_customer_id', None))
return bool(getattr(self, 'gold_subscr_id', None))
@property
def has_paypal_subscription(self):
return (self.has_gold_subscription and
not self.gold_subscr_id.startswith('cus_'))
@property
def has_stripe_subscription(self):
return (self.has_gold_subscription and
self.gold_subscr_id.startswith('cus_'))
class FakeAccount(Account):

View File

@@ -312,7 +312,7 @@ def gold_goal_on(date):
def account_from_stripe_customer_id(stripe_customer_id):
q = Account._query(Account.c.stripe_customer_id == stripe_customer_id,
q = Account._query(Account.c.gold_subscr_id == stripe_customer_id,
Account.c._spam == (True, False), data=True)
return next(iter(q), None)
@@ -339,7 +339,7 @@ def _get_subscription_details(stripe_customer_id):
def get_subscription_details(user):
if not getattr(user, 'stripe_customer_id', None):
if not getattr(user, 'gold_subscr_id', None):
return
return _get_subscription_details(user.stripe_customer_id)
return _get_subscription_details(user.gold_subscr_id)

View File

@@ -124,7 +124,7 @@
<br>
${_("of reddit gold remaining")}
</div>
%if getattr(thing, "gold_subscr_id", None):
%if getattr(thing, "paypal_subscr_id", None):
<%
paypal_link = ("https://www.paypal.com/cgi-bin/webscr?cmd=_subscr-find&alias=%s" % g.goldthanks_email)
%>