2014-07-28 17:28:00 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2014-07-23 16:24:56 +00:00
|
|
|
from django.db import models
|
2014-07-28 17:28:00 +00:00
|
|
|
from django.utils.functional import cached_property
|
2014-07-23 16:24:56 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from jsonfield import JSONField
|
|
|
|
|
2014-07-28 17:28:00 +00:00
|
|
|
from orchestra.core import accounts
|
|
|
|
|
2014-07-23 16:24:56 +00:00
|
|
|
from . import settings
|
|
|
|
from .methods import PaymentMethod
|
|
|
|
|
|
|
|
|
|
|
|
class PaymentSource(models.Model):
|
|
|
|
account = models.ForeignKey('accounts.Account', verbose_name=_("account"),
|
|
|
|
related_name='payment_sources')
|
|
|
|
method = models.CharField(_("method"), max_length=32,
|
|
|
|
choices=PaymentMethod.get_plugin_choices())
|
|
|
|
data = JSONField(_("data"))
|
2014-07-24 09:53:34 +00:00
|
|
|
is_active = models.BooleanField(_("is active"), default=True)
|
2014-07-28 17:28:00 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.label or str(self.account)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def label(self):
|
|
|
|
try:
|
|
|
|
plugin = PaymentMethod.get_plugin(self.method)()
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
return plugin.get_label(self.data)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def number(self):
|
|
|
|
try:
|
|
|
|
plugin = PaymentMethod.get_plugin(self.method)()
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
return plugin.get_number(self.data)
|
2014-07-23 16:24:56 +00:00
|
|
|
|
|
|
|
|
2014-07-30 12:55:33 +00:00
|
|
|
# TODO lock transaction in waiting confirmation
|
2014-07-23 16:24:56 +00:00
|
|
|
class Transaction(models.Model):
|
|
|
|
WAITTING_PROCESSING = 'WAITTING_PROCESSING'
|
|
|
|
WAITTING_CONFIRMATION = 'WAITTING_CONFIRMATION'
|
|
|
|
CONFIRMED = 'CONFIRMED'
|
|
|
|
REJECTED = 'REJECTED'
|
|
|
|
LOCKED = 'LOCKED'
|
|
|
|
DISCARTED = 'DISCARTED'
|
|
|
|
STATES = (
|
2014-07-24 09:53:34 +00:00
|
|
|
(WAITTING_PROCESSING, _("Waitting processing")),
|
|
|
|
(WAITTING_CONFIRMATION, _("Waitting confirmation")),
|
2014-07-23 16:24:56 +00:00
|
|
|
(CONFIRMED, _("Confirmed")),
|
|
|
|
(REJECTED, _("Rejected")),
|
|
|
|
(LOCKED, _("Locked")),
|
|
|
|
(DISCARTED, _("Discarted")),
|
|
|
|
)
|
|
|
|
|
2014-07-24 09:53:34 +00:00
|
|
|
# TODO account fk?
|
2014-07-23 16:24:56 +00:00
|
|
|
bill = models.ForeignKey('bills.bill', verbose_name=_("bill"),
|
|
|
|
related_name='transactions')
|
2014-07-30 12:55:33 +00:00
|
|
|
source = models.ForeignKey(PaymentSource, null=True, blank=True,
|
|
|
|
verbose_name=_("source"), related_name='transactions')
|
2014-07-23 16:24:56 +00:00
|
|
|
state = models.CharField(_("state"), max_length=32, choices=STATES,
|
|
|
|
default=WAITTING_PROCESSING)
|
|
|
|
amount = models.DecimalField(_("amount"), max_digits=12, decimal_places=2)
|
|
|
|
currency = models.CharField(max_length=10, default=settings.PAYMENT_CURRENCY)
|
|
|
|
created_on = models.DateTimeField(auto_now_add=True)
|
|
|
|
modified_on = models.DateTimeField(auto_now=True)
|
|
|
|
related = models.ForeignKey('self', null=True, blank=True)
|
2014-07-24 09:53:34 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return "Transaction {}".format(self.id)
|
2014-07-28 17:28:00 +00:00
|
|
|
|
|
|
|
|
2014-07-30 12:55:33 +00:00
|
|
|
# TODO rename to TransactionProcess
|
2014-07-29 20:10:37 +00:00
|
|
|
class PaymentProcess(models.Model):
|
|
|
|
"""
|
|
|
|
Stores arbitrary data generated by payment methods while processing transactions
|
|
|
|
"""
|
|
|
|
transactions = models.ManyToManyField(Transaction, related_name='processes',
|
|
|
|
verbose_name=_("transactions"))
|
|
|
|
data = JSONField(_("data"), blank=True)
|
|
|
|
file = models.FileField(_("file"), blank=True)
|
|
|
|
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
|
|
|
|
|
2014-07-30 12:55:33 +00:00
|
|
|
# TODO state: created, commited, secured (delayed persistence)
|
|
|
|
|
2014-07-29 20:10:37 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return str(self.id)
|
|
|
|
|
|
|
|
|
2014-07-28 17:28:00 +00:00
|
|
|
accounts.register(PaymentSource)
|
|
|
|
accounts.register(Transaction)
|