|
|
@@ -8,12 +8,16 @@ from django.conf import settings
|
|
8
|
8
|
|
|
9
|
9
|
class Transaction(models.Model):
|
|
10
|
10
|
"""
|
|
11
|
|
- A transaction for payment sources which need a secondary 'transaction' to actually take the money
|
|
|
11
|
+ A transaction for payment sources which need a secondary 'transaction' to
|
|
|
12
|
+ actually take the money
|
|
12
|
13
|
|
|
13
|
|
- This applies mainly to credit card sources which can be a pre-auth for the money. A 'complete'
|
|
14
|
|
- needs to be run later to debit the money from the account.
|
|
|
14
|
+ This applies mainly to credit card sources which can be a pre-auth for the
|
|
|
15
|
+ money. A 'complete' needs to be run later to debit the money from the
|
|
|
16
|
+ account.
|
|
15
|
17
|
"""
|
|
16
|
|
- source = models.ForeignKey('payment.Source', related_name='transactions', verbose_name=_("Source"))
|
|
|
18
|
+ source = models.ForeignKey(
|
|
|
19
|
+ 'payment.Source', related_name='transactions',
|
|
|
20
|
+ verbose_name=_("Source"))
|
|
17
|
21
|
|
|
18
|
22
|
# We define some sample types
|
|
19
|
23
|
AUTHORISE, DEBIT, REFUND = 'Authorise', 'Debit', 'Refund'
|
|
|
@@ -24,7 +28,8 @@ class Transaction(models.Model):
|
|
24
|
28
|
date_created = models.DateTimeField(_("Date Created"), auto_now_add=True)
|
|
25
|
29
|
|
|
26
|
30
|
def __unicode__(self):
|
|
27
|
|
- return _("%(type)s of %(amount).2f") % {'type': self.txn_type, 'amount': self.amount}
|
|
|
31
|
+ return _("%(type)s of %(amount).2f") % {
|
|
|
32
|
+ 'type': self.txn_type, 'amount': self.amount}
|
|
28
|
33
|
|
|
29
|
34
|
class Meta:
|
|
30
|
35
|
verbose_name = _("Transaction")
|
|
|
@@ -40,18 +45,28 @@ class Source(models.Model):
|
|
40
|
45
|
multiple sources such as cheque, credit accounts, gift cards. Each payment
|
|
41
|
46
|
source will have its own entry.
|
|
42
|
47
|
"""
|
|
43
|
|
- order = models.ForeignKey('order.Order', related_name='sources', verbose_name=_("Order"))
|
|
44
|
|
- source_type = models.ForeignKey('payment.SourceType', verbose_name=_("Source Type"))
|
|
45
|
|
- currency = models.CharField(_("Currency"), max_length=12, default=settings.OSCAR_DEFAULT_CURRENCY)
|
|
|
48
|
+ order = models.ForeignKey('order.Order',
|
|
|
49
|
+ related_name='sources', verbose_name=_("Order"))
|
|
|
50
|
+ source_type = models.ForeignKey('payment.SourceType',
|
|
|
51
|
+ verbose_name=_("Source Type"))
|
|
|
52
|
+ currency = models.CharField(_("Currency"), max_length=12,
|
|
|
53
|
+ default=settings.OSCAR_DEFAULT_CURRENCY)
|
|
46
|
54
|
|
|
47
|
55
|
# Track the various amounts associated with this source
|
|
48
|
|
- amount_allocated = models.DecimalField(_("Amount Allocated"), decimal_places=2, max_digits=12, default=Decimal('0.00'))
|
|
49
|
|
- amount_debited = models.DecimalField(_("Amount Debited"), decimal_places=2, max_digits=12, default=Decimal('0.00'))
|
|
50
|
|
- amount_refunded = models.DecimalField(_("Amount Refunded"), decimal_places=2, max_digits=12, default=Decimal('0.00'))
|
|
51
|
|
-
|
|
52
|
|
- # Reference number for this payment source. This is often used to look up a
|
|
53
|
|
- # transaction model for a particular payment partner.
|
|
54
|
|
- reference = models.CharField(_("Reference"), max_length=128, blank=True, null=True)
|
|
|
56
|
+ amount_allocated = models.DecimalField(
|
|
|
57
|
+ _("Amount Allocated"), decimal_places=2, max_digits=12,
|
|
|
58
|
+ default=Decimal('0.00'))
|
|
|
59
|
+ amount_debited = models.DecimalField(
|
|
|
60
|
+ _("Amount Debited"), decimal_places=2, max_digits=12,
|
|
|
61
|
+ default=Decimal('0.00'))
|
|
|
62
|
+ amount_refunded = models.DecimalField(
|
|
|
63
|
+ _("Amount Refunded"), decimal_places=2, max_digits=12,
|
|
|
64
|
+ default=Decimal('0.00'))
|
|
|
65
|
+
|
|
|
66
|
+ # Reference number for this payment source. This is often used to look up
|
|
|
67
|
+ # a transaction model for a particular payment partner.
|
|
|
68
|
+ reference = models.CharField(_("Reference"), max_length=128,
|
|
|
69
|
+ blank=True, null=True)
|
|
55
|
70
|
|
|
56
|
71
|
# A customer-friendly label for the source, eg XXXX-XXXX-XXXX-1234
|
|
57
|
72
|
label = models.CharField(_("Label"), max_length=128, blank=True, null=True)
|
|
|
@@ -82,19 +97,22 @@ class Source(models.Model):
|
|
82
|
97
|
self._create_transaction(*txn)
|
|
83
|
98
|
|
|
84
|
99
|
def balance(self):
|
|
85
|
|
- return self.amount_allocated - self.amount_debited + self.amount_refunded
|
|
|
100
|
+ return (self.amount_allocated - self.amount_debited +
|
|
|
101
|
+ self.amount_refunded)
|
|
86
|
102
|
|
|
87
|
|
- def create_deferred_transaction(self, txn_type, amount, reference=None, status=None):
|
|
|
103
|
+ def create_deferred_transaction(self, txn_type, amount, reference=None,
|
|
|
104
|
+ status=None):
|
|
88
|
105
|
"""
|
|
89
|
106
|
Register the data for a transaction that can't be created yet due to FK
|
|
90
|
|
- constraints. This happens at checkout where create an payment source and a
|
|
91
|
|
- transaction but can't save them until the order model exists.
|
|
|
107
|
+ constraints. This happens at checkout where create an payment source
|
|
|
108
|
+ and a transaction but can't save them until the order model exists.
|
|
92
|
109
|
"""
|
|
93
|
110
|
if self.deferred_txns is None:
|
|
94
|
111
|
self.deferred_txns = []
|
|
95
|
112
|
self.deferred_txns.append((txn_type, amount, reference, status))
|
|
96
|
113
|
|
|
97
|
|
- def _create_transaction(self, txn_type, amount, reference=None, status=None):
|
|
|
114
|
+ def _create_transaction(self, txn_type, amount, reference=None,
|
|
|
115
|
+ status=None):
|
|
98
|
116
|
Transaction.objects.create(source=self,
|
|
99
|
117
|
txn_type=txn_type,
|
|
100
|
118
|
amount=amount,
|
|
|
@@ -107,7 +125,9 @@ class Source(models.Model):
|
|
107
|
125
|
"""
|
|
108
|
126
|
self.amount_allocated += amount
|
|
109
|
127
|
self.save()
|
|
110
|
|
- self._create_transaction(Transaction.AUTHORISE, amount, reference, status)
|
|
|
128
|
+ self._create_transaction(
|
|
|
129
|
+ Transaction.AUTHORISE, amount, reference, status)
|
|
|
130
|
+ allocate.alters_data = True
|
|
111
|
131
|
|
|
112
|
132
|
def debit(self, amount=None, reference=None, status=None):
|
|
113
|
133
|
"""
|
|
|
@@ -118,6 +138,7 @@ class Source(models.Model):
|
|
118
|
138
|
self.amount_debited += amount
|
|
119
|
139
|
self.save()
|
|
120
|
140
|
self._create_transaction(Transaction.DEBIT, amount, reference, status)
|
|
|
141
|
+ debit.alters_data = True
|
|
121
|
142
|
|
|
122
|
143
|
def refund(self, amount, reference=None, status=None):
|
|
123
|
144
|
"""
|
|
|
@@ -126,6 +147,7 @@ class Source(models.Model):
|
|
126
|
147
|
self.amount_refunded += amount
|
|
127
|
148
|
self.save()
|
|
128
|
149
|
self._create_transaction(Transaction.REFUND, amount, reference, status)
|
|
|
150
|
+ refund.alters_data = True
|
|
129
|
151
|
|
|
130
|
152
|
@property
|
|
131
|
153
|
def amount_available_for_refund(self):
|
|
|
@@ -143,8 +165,8 @@ class SourceType(models.Model):
|
|
143
|
165
|
or an internal source such as a managed account.i
|
|
144
|
166
|
"""
|
|
145
|
167
|
name = models.CharField(_("Name"), max_length=128)
|
|
146
|
|
- code = models.SlugField(_("Code"), max_length=128, help_text=_("""This is used within
|
|
147
|
|
- forms to identify this source type"""))
|
|
|
168
|
+ code = models.SlugField(_("Code"), max_length=128,
|
|
|
169
|
+ help_text=_("This is used within forms to identify this source type"))
|
|
148
|
170
|
|
|
149
|
171
|
class Meta:
|
|
150
|
172
|
verbose_name = _("Source Type")
|
|
|
@@ -160,14 +182,16 @@ class SourceType(models.Model):
|
|
160
|
182
|
|
|
161
|
183
|
|
|
162
|
184
|
class Bankcard(models.Model):
|
|
163
|
|
- user = models.ForeignKey('auth.User', related_name='bankcards', verbose_name=_("User"))
|
|
|
185
|
+ user = models.ForeignKey('auth.User', related_name='bankcards',
|
|
|
186
|
+ verbose_name=_("User"))
|
|
164
|
187
|
card_type = models.CharField(_("Card Type"), max_length=128)
|
|
165
|
188
|
name = models.CharField(_("Name"), max_length=255)
|
|
166
|
189
|
number = models.CharField(_("Number"), max_length=32)
|
|
167
|
190
|
expiry_date = models.DateField(_("Expiry Date"))
|
|
168
|
191
|
|
|
169
|
192
|
# For payment partners who are storing the full card details for us
|
|
170
|
|
- partner_reference = models.CharField(_("Partner Reference"), max_length=255, null=True, blank=True)
|
|
|
193
|
+ partner_reference = models.CharField(
|
|
|
194
|
+ _("Partner Reference"), max_length=255, null=True, blank=True)
|
|
171
|
195
|
|
|
172
|
196
|
class Meta:
|
|
173
|
197
|
verbose_name = _("Bankcard")
|