Adding unique tracking ID to paypal payments
parent
07248f16c8
commit
9a6a3d260a
|
@ -28,13 +28,14 @@ class PaymentManager( object ):
|
|||
|
||||
if ipn.success():
|
||||
logger.info("Valid IPN")
|
||||
|
||||
logger.info("IPN Transaction Type: %s" % ipn.transaction_type)
|
||||
|
||||
if ipn.transaction_type == IPN_TYPE_PAYMENT:
|
||||
# payment IPN
|
||||
# payment IPN. we use our unique reference for the transaction as the key
|
||||
# is only valid for 3 hours
|
||||
|
||||
key = ipn.key()
|
||||
t = Transaction.objects.get(reference=key)
|
||||
uniqueID = ipn.uniqueID()
|
||||
t = Transaction.objects.get(secret=uniqueID)
|
||||
|
||||
# The status is always one of the IPN_PAY_STATUS codes defined in paypal.py
|
||||
t.status = ipn.status
|
||||
|
@ -55,11 +56,17 @@ class PaymentManager( object ):
|
|||
|
||||
t.save()
|
||||
|
||||
logger.info("Final transaction status: %s" % t.status)
|
||||
|
||||
elif ipn.transaction_type == IPN_TYPE_ADJUSTMENT:
|
||||
# a chargeback, reversal or refund for an existng payment
|
||||
|
||||
key = ipn.key()
|
||||
t = Transaction.objects.get(reference=key)
|
||||
uniqueID = ipn.uniqueID()
|
||||
if uniqueID:
|
||||
t = Transaction.objects.get(secret=uniqueID)
|
||||
else:
|
||||
key = ipn.key()
|
||||
t = Transaction.objects.get(reference=key)
|
||||
|
||||
# The status is always one of the IPN_PAY_STATUS codes defined in paypal.py
|
||||
t.status = ipn.status
|
||||
|
@ -70,7 +77,7 @@ class PaymentManager( object ):
|
|||
|
||||
elif ipn.transaction_type == IPN_TYPE_PREAPPROVAL:
|
||||
|
||||
|
||||
# IPN for preapproval always uses the key to ref the transaction as this is always valid
|
||||
key = ipn.key()
|
||||
t = Transaction.objects.get(reference=key)
|
||||
|
||||
|
@ -285,7 +292,6 @@ class PaymentManager( object ):
|
|||
type=PAYMENT_TYPE_AUTHORIZATION,
|
||||
target=target,
|
||||
currency=currency,
|
||||
secret = str(uuid.uuid1()),
|
||||
status='NONE',
|
||||
campaign=campaign,
|
||||
list=list,
|
||||
|
@ -339,7 +345,6 @@ class PaymentManager( object ):
|
|||
type=PAYMENT_TYPE_INSTANT,
|
||||
target=target,
|
||||
currency=currency,
|
||||
secret = str(uuid.uuid1()),
|
||||
status='NONE',
|
||||
campaign=campaign,
|
||||
list=list,
|
||||
|
|
|
@ -3,6 +3,7 @@ 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):
|
||||
|
||||
|
@ -11,7 +12,7 @@ class Transaction(models.Model):
|
|||
status = models.CharField(max_length=32, default='NONE', null=False)
|
||||
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)
|
||||
secret = models.CharField(max_length=64, null=True)
|
||||
secret = models.CharField(max_length=64)
|
||||
reference = models.CharField(max_length=128, null=True)
|
||||
receipt = models.CharField(max_length=256, null=True)
|
||||
error = models.CharField(max_length=256, null=True)
|
||||
|
@ -25,6 +26,11 @@ class Transaction(models.Model):
|
|||
campaign = models.ForeignKey(Campaign, null=True)
|
||||
list = models.ForeignKey(Wishlist, null=True)
|
||||
|
||||
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)
|
||||
|
||||
|
|
|
@ -155,9 +155,12 @@ class Pay( object ):
|
|||
'cancelUrl': cancel_url,
|
||||
'requestEnvelope': { 'errorLanguage': 'en_US' },
|
||||
'ipnNotificationUrl': settings.BASE_URL + reverse('PayPalIPN'),
|
||||
'feesPayer': feesPayer
|
||||
'feesPayer': feesPayer,
|
||||
'trackingId': transaction.secret
|
||||
}
|
||||
|
||||
logging.info("paypal PAY data: %s" % data)
|
||||
|
||||
# a Pay operation can be for a payment that goes through immediately or for setting up a preapproval.
|
||||
# transaction.reference is not null if it represents a preapproved payment, which has a preapprovalKey.
|
||||
if transaction.reference:
|
||||
|
@ -411,6 +414,7 @@ class IPN( object ):
|
|||
self.preapproval_key = request.POST.get('preapproval_key', None)
|
||||
self.transaction_type = request.POST.get('transaction_type', None)
|
||||
self.reason_code = request.POST.get('reason_code', None)
|
||||
self.trackingId = request.POST.get('tracking_id', None)
|
||||
|
||||
self.process_transactions(request)
|
||||
|
||||
|
@ -418,6 +422,13 @@ class IPN( object ):
|
|||
self.error = "Error: ServerError"
|
||||
traceback.print_exc()
|
||||
|
||||
def uniqueID(self):
|
||||
|
||||
if self.trackingId:
|
||||
return self.trackingId
|
||||
else:
|
||||
return None
|
||||
|
||||
def key(self):
|
||||
# We only keep one reference, either a prapproval key, or a pay key, for the transaction. This avoids the
|
||||
# race condition that may result if the IPN for an executed pre-approval(with both a pay key and preapproval key) is received
|
||||
|
|
|
@ -78,7 +78,7 @@ PAYPAL_GLUEJAR_EMAIL = "glueja_1317336101_biz@gluejar.com"
|
|||
PAYPAL_TEST_RH_EMAIL = "rh1_1317336251_biz@gluejar.com"
|
||||
PAYPAL_TEST_NONPROFIT_PARTNER_EMAIL = "nppart_1318957063_per@gluejar.com"
|
||||
|
||||
BASE_URL = 'http://0.0.0.0/'
|
||||
BASE_URL = 'http://0.0.0.0'
|
||||
|
||||
# use database as queuing service in development
|
||||
BROKER_TRANSPORT = "djkombu.transport.DatabaseTransport"
|
||||
|
|
Loading…
Reference in New Issue