regluit/payment/models.py

88 lines
3.6 KiB
Python

from django.db import models
from django.contrib.auth.models import User
from regluit.core.models import Campaign, Wishlist
from regluit.payment.parameters import *
from decimal import Decimal
import uuid
class Transaction(models.Model):
# type e.g., PAYMENT_TYPE_INSTANT or PAYMENT_TYPE_AUTHORIZATION -- defined in parameters.py
type = models.IntegerField(default=PAYMENT_TYPE_NONE, null=False)
# target: e.g, TARGET_TYPE_CAMPAIGN, TARGET_TYPE_LIST -- defined in parameters.py
target = models.IntegerField(default=TARGET_TYPE_NONE, null=False)
# status: constants defined in paypal.py (e.g., IPN_PAY_STATUS_ACTIVE, IPN_PAY_STATUS_CREATED)
status = models.CharField(max_length=32, default='NONE', null=False)
# amount & currency -- amount of money and its currency involved for transaction
amount = models.DecimalField(default=Decimal('0.00'), max_digits=14, decimal_places=2) # max 999,999,999,999.99
currency = models.CharField(max_length=10, default='USD', null=True)
# a unique ID that can be passed to PayPal to track a transaction
secret = models.CharField(max_length=64, null=True)
# a paykey that PayPal generates to identify this transaction
reference = models.CharField(max_length=128, null=True)
# (RY is not sure what receipt is for)
receipt = models.CharField(max_length=256, null=True)
# error message from a PayPal transaction
error = models.CharField(max_length=256, null=True)
# IPN.reason_code
reason = models.CharField(max_length=64, null=True)
# creation and last modified timestamps
date_created = models.DateTimeField(auto_now_add=True)
date_modified = models.DateTimeField(auto_now=True)
# date_payment: when an attempt is made to make the payment
date_payment = models.DateTimeField(null=True)
# datetime for creation of preapproval and for its expiration
date_authorized = models.DateTimeField(null=True)
date_expired = models.DateTimeField(null=True)
# associated User and Campaign for this Transaction
user = models.ForeignKey(User, null=True)
campaign = models.ForeignKey(Campaign, null=True)
# list: makes allowance for pledging against a Wishlist: not currently in use
list = models.ForeignKey(Wishlist, null=True)
# whether the user wants to be not listed publicly
anonymous = models.BooleanField(null=False)
def save(self, *args, **kwargs):
if not self.secret:
self.secret = str(uuid.uuid1())
super(Transaction, self).save(*args, **kwargs) # Call the "real" save() method.
def __unicode__(self):
return u"-- Transaction:\n \tstatus: %s\n \t amount: %s\n \treference: %s\n \terror: %s\n" % (self.status, str(self.amount), self.reference, self.error)
def create_receivers(self, receiver_list):
primary = True
for r in receiver_list:
receiver = Receiver.objects.create(email=r['email'], amount=r['amount'], currency=self.currency, status="None", primary=primary, transaction=self)
primary = False
class Receiver(models.Model):
email = models.CharField(max_length=64)
amount = models.DecimalField(default=Decimal('0.00'), max_digits=14, decimal_places=2) # max 999,999,999,999.99
currency = models.CharField(max_length=10)
status = models.CharField(max_length=64)
reason = models.CharField(max_length=64)
primary = models.BooleanField()
txn_id = models.CharField(max_length=64)
transaction = models.ForeignKey(Transaction)