You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.8 KiB

9 years ago
9 years ago
9 years ago
  1. import braintree
  2. from django.contrib.auth.models import User
  3. from django.db import models
  4. class Subscription(models.Model):
  5. user = models.OneToOneField(User, blank=True, null=True)
  6. customer_id = models.CharField(max_length=36, blank=True)
  7. payment_method_token = models.CharField(max_length=35, blank=True)
  8. subscription_id = models.CharField(max_length=10, blank=True)
  9. def _get_braintree_sub(self):
  10. if not hasattr(self, "_sub"):
  11. print("getting subscription over network")
  12. self._sub = braintree.Subscription.find(self.subscription_id)
  13. return self._sub
  14. def _get_braintree_payment_method(self):
  15. if not hasattr(self, "_pm"):
  16. print("getting payment method over network")
  17. self._pm = braintree.PaymentMethod.find(self.payment_method_token)
  18. return self._pm
  19. def is_active(self):
  20. if not self.subscription_id:
  21. return False
  22. o = self._get_braintree_sub()
  23. return o.status == "Active"
  24. def price(self):
  25. o = self._get_braintree_sub()
  26. return int(o.price)
  27. def next_billing_date(self):
  28. o = self._get_braintree_sub()
  29. return o.next_billing_date
  30. def pm_is_credit_card(self):
  31. return isinstance(self._get_braintree_payment_method(),
  32. braintree.credit_card.CreditCard)
  33. def pm_is_paypal(self):
  34. return isinstance(self._get_braintree_payment_method(),
  35. braintree.paypal_account.PayPalAccount)
  36. def card_type(self):
  37. o = self._get_braintree_payment_method()
  38. return o.card_type
  39. def last_4(self):
  40. o = self._get_braintree_payment_method()
  41. return o.last_4
  42. def paypal_email(self):
  43. o = self._get_braintree_payment_method()
  44. return o.email