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.

abstract_models.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from decimal import Decimal
  2. from django.db import models
  3. from django.template.defaultfilters import slugify
  4. from django.utils.translation import ugettext as _
  5. from django.conf import settings
  6. class AbstractSource(models.Model):
  7. """
  8. A source of payment for an order.
  9. This is normally a credit card which has been pre-authed for the order
  10. amount, but some applications will allow orders to be paid for using
  11. multiple sources such as cheque, credit accounts, gift cards. Each payment
  12. source will have its own entry.
  13. """
  14. order = models.ForeignKey('order.Order', related_name='sources')
  15. type = models.ForeignKey('payment.SourceType')
  16. currency = models.CharField(max_length=12, default=settings.OSCAR_DEFAULT_CURRENCY)
  17. amount_allocated = models.DecimalField(decimal_places=2, max_digits=12)
  18. amount_debited = models.DecimalField(decimal_places=2, max_digits=12, default=Decimal('0.00'))
  19. reference = models.CharField(max_length=128, blank=True, null=True)
  20. class Meta:
  21. abstract = True
  22. def __unicode__(self):
  23. description = "Allocation of %.2f from type %s" % (self.amount_allocated, self.type)
  24. if self.reference:
  25. description += " (reference: %s)" % self.reference
  26. return description
  27. def balance(self):
  28. return self.amount_allocated - self.amount_debited
  29. class AbstractSourceType(models.Model):
  30. """
  31. A type of payment source.
  32. This could be an external partner like PayPal or DataCash,
  33. or an internal source such as a managed account.i
  34. """
  35. name = models.CharField(max_length=128)
  36. code = models.SlugField(max_length=128, help_text=_("""This is used within
  37. forms to identify this source type"""))
  38. class Meta:
  39. abstract = True
  40. def __unicode__(self):
  41. return self.name
  42. def save(self, *args, **kwargs):
  43. if not self.code:
  44. self.code = slugify(self.name)
  45. super(AbstractSourceType, self).save(*args, **kwargs)
  46. class AbstractTransaction(models.Model):
  47. """
  48. A transaction for payment sources which need a secondary 'transaction' to actually take the money
  49. This applies mainly to credit card sources which can be a pre-auth for the money. A 'complete'
  50. needs to be run later to debit the money from the account.
  51. """
  52. source = models.ForeignKey('payment.Source', related_name='transactions')
  53. type = models.CharField(max_length=128, blank=True)
  54. delta_amount = models.FloatField()
  55. reference = models.CharField(max_length=128)
  56. date_created = models.DateField()
  57. class Meta:
  58. abstract = True
  59. def __unicode__(self):
  60. return "Transaction of %.2f" % self.delta_amount
  61. class AbstractBankcard(models.Model):
  62. user = models.ForeignKey('auth.User', related_name='bankcards')
  63. card_type = models.CharField(max_length=128)
  64. name = models.CharField(max_length=255)
  65. number = models.CharField(max_length=32)
  66. expiry_date = models.DateField()
  67. # For payment partners who are storing the full card details for us
  68. partner_reference = models.CharField(max_length=255, null=True, blank=True)
  69. class Meta:
  70. abstract = True
  71. def save(self, *args, **kwargs):
  72. self.number = self._get_obfuscated_number()
  73. super(AbstractBankcard, self).save(*args, **kwargs)
  74. def _get_obfuscated_number(self):
  75. return u"XXXX-XXXX-XXXX-%s" % self.number[-4:]