from django.db import models from django.utils import timezone import uuid from polymorphic.models import PolymorphicModel from .invoice import Invoice from ..signals import initiate_payment class InvoicePayment(PolymorphicModel): uuid = models.UUIDField(default=uuid.uuid4) invoice = models.ForeignKey(Invoice, models.PROTECT) amount = models.DecimalField(max_digits=9, decimal_places=2) gateway_id = models.CharField(max_length=256) timestamp = models.DateTimeField(default=timezone.now) @property def gateway(self): raise NotImplementedError("%s does not implement gateway" % type(self)) @property def status(self): raise NotImplementedError("%s does not implement status" % type(self)) @classmethod def initiate(cls, invoice): raise NotImplementedError("%s does not implement initiate()" % cls.__name__) def finalize(self, *args, **kwargs): return self.invoice.finalize(*args, **kwargs) @classmethod def from_invoice(cls, invoice, gateway): if not invoice.is_paid: responses = initiate_payment.send_robust(sender=cls, invoice=invoice, gateway=gateway) for handler, response in responses: try: return response["redirect"] except: continue return False