選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

abstract_models.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. source_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. amount_refunded = models.DecimalField(decimal_places=2, max_digits=12, default=Decimal('0.00'))
  20. reference = models.CharField(max_length=128, blank=True, null=True)
  21. # A dictionary of submission data that is stored as part of the
  22. # checkout process.
  23. submission_data = {}
  24. class Meta:
  25. abstract = True
  26. def __unicode__(self):
  27. description = "Allocation of %.2f from type %s" % (self.amount_allocated, self.type)
  28. if self.reference:
  29. description += " (reference: %s)" % self.reference
  30. return description
  31. def balance(self):
  32. return self.amount_allocated - self.amount_debited + self.amount_refunded
  33. class AbstractSourceType(models.Model):
  34. """
  35. A type of payment source.
  36. This could be an external partner like PayPal or DataCash,
  37. or an internal source such as a managed account.i
  38. """
  39. name = models.CharField(max_length=128)
  40. code = models.SlugField(max_length=128, help_text=_("""This is used within
  41. forms to identify this source type"""))
  42. class Meta:
  43. abstract = True
  44. def __unicode__(self):
  45. return self.name
  46. def save(self, *args, **kwargs):
  47. if not self.code:
  48. self.code = slugify(self.name)
  49. super(AbstractSourceType, self).save(*args, **kwargs)
  50. class AbstractTransaction(models.Model):
  51. """
  52. A transaction for payment sources which need a secondary 'transaction' to actually take the money
  53. This applies mainly to credit card sources which can be a pre-auth for the money. A 'complete'
  54. needs to be run later to debit the money from the account.
  55. """
  56. source = models.ForeignKey('payment.Source', related_name='transactions')
  57. type = models.CharField(max_length=128, blank=True)
  58. delta_amount = models.FloatField()
  59. reference = models.CharField(max_length=128)
  60. status = models.CharField(max_length=128, null=True)
  61. date_created = models.DateField()
  62. class Meta:
  63. abstract = True
  64. def __unicode__(self):
  65. return "Transaction of %.2f" % self.delta_amount
  66. class AbstractBankcard(models.Model):
  67. user = models.ForeignKey('auth.User', related_name='bankcards')
  68. card_type = models.CharField(max_length=128)
  69. name = models.CharField(max_length=255)
  70. number = models.CharField(max_length=32)
  71. expiry_date = models.DateField()
  72. # For payment partners who are storing the full card details for us
  73. partner_reference = models.CharField(max_length=255, null=True, blank=True)
  74. class Meta:
  75. abstract = True
  76. def save(self, *args, **kwargs):
  77. self.number = self._get_obfuscated_number()
  78. super(AbstractBankcard, self).save(*args, **kwargs)
  79. def _get_obfuscated_number(self):
  80. return u"XXXX-XXXX-XXXX-%s" % self.number[-4:]