Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

record transaction log

  • Loading branch information...
commit c9a7df355d256440a285ca2d9a3ea82c715093c6 1 parent afb2d39
Celso Pinto cpinto authored
86 billing/gateways/paylane_gateway.py
View
@@ -9,7 +9,7 @@
from django.utils.translation import ugettext_lazy as _
from billing import Gateway
-from billing.models import PaylaneResponse
+from billing.models import PaylaneTransaction,PaylaneAuthorization
from billing.utils.credit_card import CreditCard,InvalidCard,Visa,MasterCard
from billing.utils.paylane import PaylaneError
from billing.signals import transaction_was_successful,transaction_was_unsuccessful
@@ -76,14 +76,32 @@ def purchase(self,money,credit_card,options=None):
res = self.client.service.multiSale(params)
+ transaction = PaylaneTransaction()
+ transaction.amount = money
+ transaction.customer_name = customer.name
+ transaction.customer_email = customer.email
+ transaction.product = product.description
+
if hasattr(res,'OK'):
- return {'status':'SUCCESS','response':PaylaneResponse(sale_authorization_id=res.OK.id_sale)}
+ transaction.success = True
+ transaction.save()
+
+ return {'status':'SUCCESS','response':{'transaction':transaction}}
else:
+ transaction.success = False
+ error_code = int(getattr(res.ERROR,'error_number'))
+ error_description = getattr(res.ERROR,'error_description')
+ acquirer_error = getattr(res.ERROR,'processor_error_number','')
+ acquirer_description = getattr(res.ERROR,'processor_error_description','')
+ transaction.save()
+
return {'status':'FAILURE',
- 'response':PaylaneError(getattr(res.ERROR,'error_number'),
+ 'response':{'error':PaylaneError(getattr(res.ERROR,'error_number'),
getattr(res.ERROR,'error_description'),
getattr(res.ERROR,'processor_error_number',''),
- getattr(res.ERROR,'processor_error_description',''))
+ getattr(res.ERROR,'processor_error_description','')),
+ 'transaction':transaction
+ }
}
def recurring(self,money,credit_card,options=None):
@@ -120,33 +138,73 @@ def recurring(self,money,credit_card,options=None):
res = self.client.service.multiSale(params)
+ transaction = PaylaneTransaction()
+ transaction.amount = money
+ transaction.customer_name = customer.name
+ transaction.customer_email = customer.email
+ transaction.product = product.description
+
if hasattr(res,'OK'):
- return {'status':'SUCCESS','response':PaylaneResponse(sale_authorization_id=res.OK.id_sale_authorization)}
+ transaction.success = True
+ transaction.save()
+
+ authz = PaylaneAuthorization.objects.create(sale_authorization_id=res.OK.id_sale_authorization,transaction=transaction)
+ return {'status':'SUCCESS','response':{'transaction':transaction,'authorization':authz}}
else:
+ transaction.success = False
+ error_code = int(getattr(res.ERROR,'error_number'))
+ error_description = getattr(res.ERROR,'error_description')
+ acquirer_error = getattr(res.ERROR,'processor_error_number','')
+ acquirer_description = getattr(res.ERROR,'processor_error_description','')
+ transaction.save()
+
return {'status':'FAILURE',
- 'response':PaylaneError(getattr(res.ERROR,'error_number'),
+ 'response':{'error':PaylaneError(getattr(res.ERROR,'error_number'),
getattr(res.ERROR,'error_description'),
getattr(res.ERROR,'processor_error_number',''),
- getattr(res.ERROR,'processor_error_description',''))
+ getattr(res.ERROR,'processor_error_description','')),
+ 'transaction':transaction
+ }
}
- def bill_recurring(self,amount,paylane_response,description):
+ def bill_recurring(self,amount,paylane_recurring,description):
""" Debit a recurring transaction payment, eg. monthly subscription.
- Use the result of recurring() as the paylane_response parameter.
+ Use the result of recurring() as the paylane_recurring parameter.
If this transaction is successful, use it's response as input for the
next bill_recurring() call.
"""
processing_date = datetime.datetime.today().strftime("%Y-%m-%d")
- res = self.client.service.resale(id_sale=paylane_response.sale_authorization_id,amount=amount,currency=self.default_currency,
+ res = self.client.service.resale(id_sale=paylane_recurring.sale_authorization_id,amount=amount,currency=self.default_currency,
description=description,processing_date=processing_date)
+ previous_transaction = paylane_recurring.transaction
+
+ transaction = PaylaneTransaction()
+ transaction.amount = previous_transaction.amount
+ transaction.customer_name = previous_transaction.name
+ transaction.customer_email = previous_transaction.email
+ transaction.product = previous_transaction.description
+
if hasattr(res,'OK'):
- return {'status':'SUCCESS','response':PaylaneResponse(sale_authorization_id=res.OK.id_sale)}
+ transaction.success = True
+ transaction.save()
+
+ authz = PaylaneAuthorization.objects.create(sale_authorization_id=res.OK.id_sale_authorization,transaction=transaction)
+ return {'status':'SUCCESS','response':{'transaction':transaction,'authorization':authz}}
else:
- return {'status':'FAILURE',
- 'response':PaylaneError(getattr(res.ERROR,'error_number'),
+ transaction.success = False
+ error_code = int(getattr(res.ERROR,'error_number'))
+ error_description = getattr(res.ERROR,'error_description')
+ acquirer_error = getattr(res.ERROR,'processor_error_number','')
+ acquirer_description = getattr(res.ERROR,'processor_error_description','')
+ transaction.save()
+
+ return {'status':'FAILURE',
+ 'response':{'error':PaylaneError(getattr(res.ERROR,'error_number'),
getattr(res.ERROR,'error_description'),
getattr(res.ERROR,'processor_error_number',''),
- getattr(res.ERROR,'processor_error_description',''))
+ getattr(res.ERROR,'processor_error_description','')),
+ 'transaction':transaction
+ }
}
2  billing/models/__init__.py
View
@@ -3,4 +3,4 @@
from world_pay_models import WorldPayResponse
from eway_models import EwayResponse
from amazon_fps_models import AmazonFPSResponse
-from paylane_models import PaylaneResponse
+from paylane_models import PaylaneTransaction,PaylaneAuthorization
27 billing/models/paylane_models.py
View
@@ -3,12 +3,31 @@
from django.db import models
-class PaylaneResponse(models.Model):
- #this enables recurring payments
- sale_authorization_id = models.BigIntegerField()
+class PaylaneTransaction(models.Model):
+ transaction_date = models.DateTimeField(auto_now_add=True)
+ amount = models.FloatField()
+ customer_name = models.CharField(max_length=200)
+ customer_email = models.CharField(max_length=200)
+ product = models.CharField(max_length=200)
+ success = models.BooleanField(default=False)
+ error_code = models.IntegerField(default=0)
+ error_description = models.CharField(max_length=300,blank=True)
+ acquirer_error = models.CharField(max_length=40,blank=True)
+ acquirer_description = models.CharField(max_length=300,blank=True)
def __unicode__(self):
- return 'Authorization: %s' % (self.sale_authorization_id)
+ return u'Transaction for %s (%s)' % (self.customer_name,self.customer_email)
class Meta:
app_label = __name__.split(".")[0]
+
+class PaylaneAuthorization(models.Model):
+ sale_authorization_id = models.BigIntegerField(db_index=True)
+ transaction = models.OneToOneField(PaylaneTransaction)
+
+ def __unicode__(self):
+ return u'Authorization: %s' % (self.sale_authorization_id)
+
+ class Meta:
+ app_label = __name__.split(".")[0]
+
35 billing/tests/paylane_tests.py
View
@@ -10,7 +10,7 @@
from billing.signals import *
from billing.utils.paylane import *
-from billing.models.paylane_models import PaylaneResponse
+from billing.models import PaylaneTransaction,PaylaneAuthorization
VALID_TEST_VISA = ''
VALID_TEST_MASTERCARD = '5500000000000004'
@@ -37,8 +37,8 @@ def testOneShotPurchaseOK(self):
options['product'] = self.product
res = self.merchant.purchase(1.0,credit_card,options=options)
self.assertEqual(res['status'],'SUCCESS',unicode(res['response']))
- self.assertTrue(isinstance(res['response'],PaylaneResponse))
- self.assertTrue(res['response'].sale_authorization_id > 0)
+ self.assertTrue('transaction' in res['response'])
+ self.assertFalse('authorization' in res['response'])
def testRecurringSetupOK(self):
credit_card = Visa(first_name='Celso',last_name='Pinto',month=10,year=2012,number='4242424242424242',verification_value=435)
@@ -47,8 +47,9 @@ def testRecurringSetupOK(self):
options['product'] = self.product
res = self.merchant.recurring(1.0,credit_card,options=options)
self.assertEqual(res['status'],'SUCCESS',unicode(res['response']))
- self.assertTrue(isinstance(res['response'],PaylaneResponse))
- self.assertTrue(res['response'].sale_authorization_id > 0)
+ self.assertTrue('transaction' in res['response'])
+ self.assertTrue('authorization' in res['response'])
+ self.assertTrue(res['response']['authorization'].sale_authorization_id > 0)
def testRecurringBillingOK(self):
time.sleep(60)
@@ -56,17 +57,22 @@ def testRecurringBillingOK(self):
options = {}
options['customer'] = self.customer
options['product'] = self.product
- res = self.merchant.purchase(1.0,credit_card,options=options)
+ res = self.merchant.recurring(1.0,credit_card,options=options)
self.assertEqual(res['status'],'SUCCESS',unicode(res['response']))
+ self.assertTrue('transaction' in res['response'])
+ self.assertTrue('authorization' in res['response'])
time.sleep(60)
- pr = res['response']
- res = self.merchant.bill_recurring(12.0,pr,'OK recurring')
+ res = self.merchant.bill_recurring(12.0,res['response']['authorization'],'OK recurring')
self.assertEqual(res['status'],'SUCCESS',unicode(res['response']))
+ self.assertTrue('transaction' in res['response'])
+ self.assertTrue('authorization' in res['response'])
time.sleep(60)
- res = self.merchant.bill_recurring(12.0,pr,'OK recurring')
+ res = self.merchant.bill_recurring(12.0,res['response']['authorization'],'OK recurring')
self.assertEqual(res['status'],'SUCCESS',unicode(res['response']))
+ self.assertTrue('transaction' in res['response'])
+ self.assertTrue('authorization' in res['response'])
def testRecurringBillingFailWithChargeback(self):
time.sleep(60)
@@ -74,11 +80,14 @@ def testRecurringBillingFailWithChargeback(self):
options = {}
options['customer'] = self.customer
options['product'] = self.product
- res = self.merchant.purchase(1.0,credit_card,options=options)
+ res = self.merchant.recurring(1.0,credit_card,options=options)
self.assertEqual(res['status'],'SUCCESS',unicode(res['response']))
+ self.assertTrue('transaction' in res['response'])
+ self.assertTrue('authorization' in res['response'])
time.sleep(60)
- pr = res['response']
- res = self.merchant.bill_recurring(float(PaylaneError.ERR_RESALE_WITH_CHARGEBACK),pr,'OK recurring')
+ res = self.merchant.bill_recurring(float(PaylaneError.ERR_RESALE_WITH_CHARGEBACK),res['response']['authorization'],'Fail recurring')
self.assertEqual(res['status'],'FAILURE',unicode(res['response']))
- self.assertEqual(res['response'].error_code,PaylaneError.ERR_RESALE_WITH_CHARGEBACK)
+ self.assertTrue('transaction' in res['response'])
+ self.assertTrue('error' in res['response'])
+ self.assertEqual(res['response']['error'].error_code,PaylaneError.ERR_RESALE_WITH_CHARGEBACK)
Please sign in to comment.
Something went wrong with that request. Please try again.