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 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. from decimal import Decimal
  2. from django.db import models
  3. from django.utils.translation import ugettext as _
  4. from django.conf import settings
  5. from oscar.core.utils import slugify
  6. class AbstractTransaction(models.Model):
  7. """
  8. A transaction with a payment gateway
  9. For example:
  10. * A 'pre-auth' with a bankcard gateway
  11. * A 'settle' with a credit provider (see django-oscar-accounts)
  12. """
  13. source = models.ForeignKey(
  14. 'payment.Source', related_name='transactions',
  15. verbose_name=_("Source"))
  16. # We define some sample types but don't constrain txn_type to be one of
  17. # these as there will be domain-specific ones that we can't anticipate
  18. # here.
  19. AUTHORISE, DEBIT, REFUND = 'Authorise', 'Debit', 'Refund'
  20. txn_type = models.CharField(_("Type"), max_length=128, blank=True)
  21. amount = models.DecimalField(_("Amount"), decimal_places=2, max_digits=12)
  22. reference = models.CharField(_("Reference"), max_length=128, blank=True)
  23. status = models.CharField(_("Status"), max_length=128, blank=True)
  24. date_created = models.DateTimeField(_("Date Created"), auto_now_add=True)
  25. def __unicode__(self):
  26. return _(u"%(type)s of %(amount).2f") % {
  27. 'type': self.txn_type,
  28. 'amount': self.amount}
  29. class Meta:
  30. abstract = True
  31. verbose_name = _("Transaction")
  32. verbose_name_plural = _("Transactions")
  33. class AbstractSource(models.Model):
  34. """
  35. A source of payment for an order.
  36. This is normally a credit card which has been pre-authed for the order
  37. amount, but some applications will allow orders to be paid for using
  38. multiple sources such as cheque, credit accounts, gift cards. Each payment
  39. source will have its own entry.
  40. This source object tracks how much money has been authorised, debited and
  41. refunded, which is useful when payment takes place in multiple stages.
  42. """
  43. order = models.ForeignKey(
  44. 'order.Order', related_name='sources', verbose_name=_("Order"))
  45. source_type = models.ForeignKey(
  46. 'payment.SourceType', verbose_name=_("Source Type"))
  47. currency = models.CharField(
  48. _("Currency"), max_length=12, default=settings.OSCAR_DEFAULT_CURRENCY)
  49. # Track the various amounts associated with this source
  50. amount_allocated = models.DecimalField(
  51. _("Amount Allocated"), decimal_places=2, max_digits=12,
  52. default=Decimal('0.00'))
  53. amount_debited = models.DecimalField(
  54. _("Amount Debited"), decimal_places=2, max_digits=12,
  55. default=Decimal('0.00'))
  56. amount_refunded = models.DecimalField(
  57. _("Amount Refunded"), decimal_places=2, max_digits=12,
  58. default=Decimal('0.00'))
  59. # Reference number for this payment source. This is often used to look up
  60. # a transaction model for a particular payment partner.
  61. reference = models.CharField(_("Reference"), max_length=128, blank=True)
  62. # A customer-friendly label for the source, eg XXXX-XXXX-XXXX-1234
  63. label = models.CharField(_("Label"), max_length=128, blank=True)
  64. # A dictionary of submission data that is stored as part of the
  65. # checkout process, where we need to pass an instance of this class around
  66. submission_data = None
  67. # We keep a list of deferred transactions that are only actually saved when
  68. # the source is saved for the first time
  69. deferred_txns = None
  70. class Meta:
  71. abstract = True
  72. verbose_name = _("Source")
  73. verbose_name_plural = _("Sources")
  74. def __unicode__(self):
  75. description = _("Allocation of %(amount).2f from type %(type)s") % {
  76. 'amount': self.amount_allocated, 'type': self.source_type}
  77. if self.reference:
  78. description += _(" (reference: %s)") % self.reference
  79. return description
  80. def save(self, *args, **kwargs):
  81. super(AbstractSource, self).save(*args, **kwargs)
  82. if self.deferred_txns:
  83. for txn in self.deferred_txns:
  84. self._create_transaction(*txn)
  85. def create_deferred_transaction(self, txn_type, amount, reference=None,
  86. status=None):
  87. """
  88. Register the data for a transaction that can't be created yet due to FK
  89. constraints. This happens at checkout where create an payment source
  90. and a transaction but can't save them until the order model exists.
  91. """
  92. if self.deferred_txns is None:
  93. self.deferred_txns = []
  94. self.deferred_txns.append((txn_type, amount, reference, status))
  95. def _create_transaction(self, txn_type, amount, reference=None,
  96. status=None):
  97. AbstractTransaction.objects.create(
  98. source=self, txn_type=txn_type, amount=amount,
  99. reference=reference, status=status)
  100. # =======
  101. # Actions
  102. # =======
  103. def allocate(self, amount, reference=None, status=None):
  104. """
  105. Convenience method for ring-fencing money against this source
  106. """
  107. self.amount_allocated += amount
  108. self.save()
  109. self._create_transaction(
  110. AbstractTransaction.AUTHORISE, amount, reference, status)
  111. allocate.alters_data = True
  112. def debit(self, amount=None, reference=None, status=None):
  113. """
  114. Convenience method for recording debits against this source
  115. """
  116. if amount is None:
  117. amount = self.balance()
  118. self.amount_debited += amount
  119. self.save()
  120. self._create_transaction(
  121. AbstractTransaction.DEBIT, amount, reference, status)
  122. debit.alters_data = True
  123. def refund(self, amount, reference=None, status=None):
  124. """
  125. Convenience method for recording refunds against this source
  126. """
  127. self.amount_refunded += amount
  128. self.save()
  129. self._create_transaction(
  130. AbstractTransaction.REFUND, amount, reference, status)
  131. refund.alters_data = True
  132. # ==========
  133. # Properties
  134. # ==========
  135. @property
  136. def balance(self):
  137. """
  138. Return the balance of this source
  139. """
  140. return (self.amount_allocated - self.amount_debited +
  141. self.amount_refunded)
  142. @property
  143. def amount_available_for_refund(self):
  144. """
  145. Return the amount available to be refunded
  146. """
  147. return self.amount_debited - self.amount_refunded
  148. class AbstractSourceType(models.Model):
  149. """
  150. A type of payment source.
  151. This could be an external partner like PayPal or DataCash,
  152. or an internal source such as a managed account.
  153. """
  154. name = models.CharField(_("Name"), max_length=128)
  155. code = models.SlugField(
  156. _("Code"), max_length=128,
  157. help_text=_("This is used within forms to identify this source type"))
  158. class Meta:
  159. abstract = True
  160. verbose_name = _("Source Type")
  161. verbose_name_plural = _("Source Types")
  162. def __unicode__(self):
  163. return self.name
  164. def save(self, *args, **kwargs):
  165. if not self.code:
  166. self.code = slugify(self.name)
  167. super(AbstractSourceType, self).save(*args, **kwargs)
  168. class AbstractBankcard(models.Model):
  169. user = models.ForeignKey('auth.User', related_name='bankcards',
  170. verbose_name=_("User"))
  171. card_type = models.CharField(_("Card Type"), max_length=128)
  172. name = models.CharField(_("Name"), max_length=255)
  173. number = models.CharField(_("Number"), max_length=32)
  174. expiry_date = models.DateField(_("Expiry Date"))
  175. # For payment partners who are storing the full card details for us
  176. partner_reference = models.CharField(
  177. _("Partner Reference"), max_length=255, blank=True)
  178. class Meta:
  179. abstract = True
  180. verbose_name = _("Bankcard")
  181. verbose_name_plural = _("Bankcards")
  182. def save(self, *args, **kwargs):
  183. self.number = self._get_obfuscated_number()
  184. super(AbstractBankcard, self).save(*args, **kwargs)
  185. def _get_obfuscated_number(self):
  186. return u"XXXX-XXXX-XXXX-%s" % self.number[-4:]