Guest User

Untitled

a guest
Jan 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. @property
  2. def stripe_customer(self):
  3. stripe.api_key = settings.STRIPE_SECRET
  4. if not self.stripe_customer_id:
  5. if not self.free_trial_ends_date:
  6. if datetime.datetime.now() > self.signup_date + datetime.timedelta(days=30):
  7. # They signed up pre-stripe
  8. self.free_trial_ends_date = datetime.datetime.now() + datetime.timedelta(minutes=1)
  9. else:
  10. self.free_trial_ends_date = self.signup_date + datetime.timedelta(days=30)
  11.  
  12. # Create a stripe customer
  13. c = stripe.Customer.create(
  14. description=self.name,
  15. email=self.primary_useraccount.email,
  16. plan=MONTHLY_PLAN_NAME,
  17. trial_end=self.free_trial_ends_date,
  18. coupon=self.feedback_coupon_code
  19. )
  20. self.stripe_customer_id = c.id
  21. self.save()
  22. return c
  23. else:
  24. return stripe.Customer.retrieve(self.stripe_customer_id)
  25.  
  26. @property
  27. def stripe_subscription(self):
  28. return self.stripe_customer.subscription
  29.  
  30. def update_account_status(self):
  31. sub = self.stripe_subscription
  32. # Possible statuses: canceled, past_due, unpaid, active, trialing
  33. if sub.status == "trialing":
  34. self.status = STATUS_FREE_TRIAL
  35. elif sub.status == "active":
  36. self.status = STATUS_ACTIVE
  37. elif sub.status == "past_due":
  38. self.status = STATUS_ACTIVE_BILLING_ISSUE
  39. elif sub.status == "unpaid":
  40. self.status = STATUS_DEACTIVATED
  41. elif sub.status == "cancelled":
  42. # should never happen
  43. self.status = STATUS_DEACTIVATED
  44. else:
  45. raise Exception, "Unknown subscription status"
  46.  
  47. if hasattr(sub,"current_period_end"):
  48. # For when Stripe upgrades their API
  49. if type(sub.current_period_end) == type(1):
  50. self.next_billing_date = datetime.datetime.fromtimestamp(sub.current_period_end)
  51. else:
  52. self.next_billing_date = sub.current_period_end
  53.  
  54. self.save()
  55. return self
  56.  
  57. @property
  58. def has_card_on_file(self):
  59. return self.last_four != None
Add Comment
Please, Sign In to add comment