Skip to content

Instantly share code, notes, and snippets.

@skoczen
Created September 17, 2011 22:02
Show Gist options
  • Save skoczen/1224417 to your computer and use it in GitHub Desktop.
Save skoczen/1224417 to your computer and use it in GitHub Desktop.
a few stripe-y methods
@property
def stripe_customer(self):
stripe.api_key = settings.STRIPE_SECRET
if not self.stripe_customer_id:
if not self.free_trial_ends_date:
if datetime.datetime.now() > self.signup_date + datetime.timedelta(days=30):
# They signed up pre-stripe
self.free_trial_ends_date = datetime.datetime.now() + datetime.timedelta(minutes=1)
else:
self.free_trial_ends_date = self.signup_date + datetime.timedelta(days=30)
# Create a stripe customer
c = stripe.Customer.create(
description=self.name,
email=self.primary_useraccount.email,
plan=MONTHLY_PLAN_NAME,
trial_end=self.free_trial_ends_date,
coupon=self.feedback_coupon_code
)
self.stripe_customer_id = c.id
self.save()
return c
else:
return stripe.Customer.retrieve(self.stripe_customer_id)
@property
def stripe_subscription(self):
return self.stripe_customer.subscription
def update_account_status(self):
sub = self.stripe_subscription
# Possible statuses: canceled, past_due, unpaid, active, trialing
if sub.status == "trialing":
self.status = STATUS_FREE_TRIAL
elif sub.status == "active":
self.status = STATUS_ACTIVE
elif sub.status == "past_due":
self.status = STATUS_ACTIVE_BILLING_ISSUE
elif sub.status == "unpaid":
self.status = STATUS_DEACTIVATED
elif sub.status == "cancelled":
# should never happen
self.status = STATUS_DEACTIVATED
else:
raise Exception, "Unknown subscription status"
if hasattr(sub,"current_period_end"):
# For when Stripe upgrades their API
if type(sub.current_period_end) == type(1):
self.next_billing_date = datetime.datetime.fromtimestamp(sub.current_period_end)
else:
self.next_billing_date = sub.current_period_end
self.save()
return self
@property
def has_card_on_file(self):
return self.last_four != None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment