To get credit module working again, had to move some methods into an object that inherits from a base class

So now all payment processor specific methods are on a Processor
object,
pull/1/head
eric 2012-10-02 23:49:19 -04:00
parent 7a1cf969ca
commit 73910ae8a0
5 changed files with 303 additions and 315 deletions

View File

@ -8,18 +8,6 @@ import datetime
import time
def requires_explicit_preapprovals():
"""a function that returns for the given payment processor"""
return False
def make_account(user, token):
"""template function for return a payment.Account corresponding to the payment system"""
return None
def ProcessIPN(request):
return HttpResponseForbidden()
class BasePaymentRequest:
'''
Handles common information incident to payment processing
@ -71,8 +59,19 @@ class BasePaymentRequest:
def timestamp(self):
return str(datetime.datetime.now())
class Processor:
"""a function that returns for the given payment processor"""
requires_explicit_preapprovals=False
class Pay( BasePaymentRequest ):
def make_account(self, user, token):
"""template function for return a payment.Account corresponding to the payment system"""
return None
def ProcessIPN(self, request):
return HttpResponseForbidden()
class Pay( BasePaymentRequest ):
'''
The pay function generates a redirect URL to approve the transaction
@ -96,7 +95,7 @@ class Pay( BasePaymentRequest ):
def next_url( self ):
return self.url
class Preapproval(Pay):
class Preapproval(Pay):
def __init__( self, transaction, amount, expiry=None, return_url=None, paymentReason=""):
@ -112,7 +111,7 @@ class Preapproval(Pay):
Pay.__init__(self, transaction, return_url=return_url, amount=amount, paymentReason=paymentReason)
class Execute(BasePaymentRequest):
class Execute(BasePaymentRequest):
'''
The Execute function sends an existing token(generated via the URL from the pay operation), and collects
@ -131,7 +130,7 @@ class Execute(BasePaymentRequest):
class Finish(BasePaymentRequest):
class Finish(BasePaymentRequest):
'''
The Finish function handles the secondary receiver in a chained payment.
'''
@ -140,7 +139,7 @@ class Finish(BasePaymentRequest):
print "Finish"
class PaymentDetails(BasePaymentRequest):
class PaymentDetails(BasePaymentRequest):
'''
Get details about executed PAY operation
@ -155,7 +154,7 @@ class PaymentDetails(BasePaymentRequest):
class CancelPreapproval(BasePaymentRequest):
class CancelPreapproval(BasePaymentRequest):
'''
Cancels an exisiting token.
'''
@ -164,7 +163,7 @@ class CancelPreapproval(BasePaymentRequest):
self.transaction = transaction
class RefundPayment(BasePaymentRequest):
class RefundPayment(BasePaymentRequest):
def __init__(self, transaction):
self.transaction = transaction
@ -173,7 +172,7 @@ class RefundPayment(BasePaymentRequest):
class PreapprovalDetails(BasePaymentRequest):
class PreapprovalDetails(BasePaymentRequest):
'''
Get details about an authorized token

View File

@ -4,6 +4,7 @@ from django.contrib.auth.models import User
from django.conf import settings
from regluit.payment.parameters import *
from regluit.payment import baseprocessor
from regluit.payment.baseprocessor import BasePaymentRequest
@ -28,7 +29,8 @@ def credit_transaction(t,user,amount):
user.credit.add_to_pledged(pledge_amount)
t.set_credit_approved(pledge_amount)
class CancelPreapproval(BasePaymentRequest):
class Processor(baseprocessor.Processor):
class CancelPreapproval(BasePaymentRequest):
'''
Cancels an exisiting token.
'''
@ -43,7 +45,7 @@ class CancelPreapproval(BasePaymentRequest):
self.errorMessage="couldn't cancel the transaction"
self.status = 'Credit Cancel Failure'
class PreapprovalDetails(BasePaymentRequest):
class PreapprovalDetails(BasePaymentRequest):
status = None
approved = None
currency = None

View File

@ -6,8 +6,6 @@ from regluit.payment.parameters import *
from regluit.payment.signals import transaction_charged, pledge_modified, pledge_created
from regluit.payment import credit
from regluit.payment.baseprocessor import Pay, Finish, Preapproval, ProcessIPN, CancelPreapproval, PaymentDetails, PreapprovalDetails, RefundPayment
import uuid
import traceback
from regluit.utils.localdatetime import now
@ -39,14 +37,13 @@ class PaymentManager( object ):
# Forward to our payment processor
mod = __import__("regluit.payment." + module, fromlist=[str(module)])
method = getattr(mod, "ProcessIPN")
method = getattr(mod.Processor, "ProcessIPN")
return method(request)
def update_preapproval(self, transaction):
"""Update a transaction to hold the data from a PreapprovalDetails on that transaction"""
t = transaction
method = getattr(transaction.get_payment_class(), "PreapprovalDetails")
p = method(t)
p = transaction.get_payment_class().PreapprovalDetails(t)
preapproval_status = {'id':t.id, 'key':t.preapproval_key}
@ -98,8 +95,7 @@ class PaymentManager( object ):
t = transaction
payment_status = {'id':t.id}
method = getattr(transaction.get_payment_class(), "PaymentDetails")
p = method(t)
p = transaction.get_payment_class().PaymentDetails(t)
if p.error() or not p.success():
logger.info("Error retrieving payment details for transaction %d" % t.id)
@ -413,8 +409,7 @@ class PaymentManager( object ):
transaction.date_executed = now()
transaction.save()
method = getattr(transaction.get_payment_class(), "Finish")
p = method(transaction)
p = transaction.get_payment_class().Finish(transaction)
# Create a response for this
envelope = p.envelope()
@ -468,8 +463,7 @@ class PaymentManager( object ):
transaction.date_payment = now()
transaction.save()
method = getattr(transaction.get_payment_class(), "Execute")
p = method(transaction)
p = transaction.get_payment_class().Execute(transaction)
# Create a response for this
envelope = p.envelope()
@ -509,12 +503,11 @@ class PaymentManager( object ):
'''
# does this transaction explicity require preapprovals?
requires_explicit_preapprovals = getattr(transaction.get_payment_class(), "requires_explicit_preapprovals")
requires_explicit_preapprovals = transaction.get_payment_class().requires_explicit_preapprovals
if requires_explicit_preapprovals():
if requires_explicit_preapprovals:
method = getattr(transaction.get_payment_class(), "CancelPreapproval")
p = method(transaction)
p = transaction.get_payment_class().CancelPreapproval(transaction)
# Create a response for this
envelope = p.envelope()
@ -575,8 +568,7 @@ class PaymentManager( object ):
urllib.urlencode({'tid':transaction.id}))
return_url = urlparse.urljoin(settings.BASE_URL, return_path)
method = getattr(transaction.get_payment_class(), "Preapproval")
p = method(transaction, transaction.amount, expiry, return_url=return_url, paymentReason=paymentReason)
p = transaction.get_payment_class().Preapproval(transaction, transaction.amount, expiry, return_url=return_url, paymentReason=paymentReason)
# Create a response for this
envelope = p.envelope()
@ -744,7 +736,7 @@ class PaymentManager( object ):
# does this transaction explicity require preapprovals?
requires_explicit_preapprovals = getattr(transaction.get_payment_class(), "requires_explicit_preapprovals")
requires_explicit_preapprovals = transaction.get_payment_class().requires_explicit_preapprovals
if transaction.type != PAYMENT_TYPE_AUTHORIZATION:
logger.info("Error, attempt to modify an invalid transaction type")
@ -785,7 +777,7 @@ class PaymentManager( object ):
credit.CancelPreapproval(transaction)
return t, reverse('fund_pledge', args=[t.id])
elif requires_explicit_preapprovals() and (amount > transaction.max_amount or expiry != transaction.date_expired):
elif requires_explicit_preapprovals and (amount > transaction.max_amount or expiry != transaction.date_expired):
# set the expiry date based on the campaign deadline
expiry = transaction.campaign.deadline + timedelta( days=settings.PREAPPROVAL_PERIOD_AFTER_CAMPAIGN )
@ -828,7 +820,7 @@ class PaymentManager( object ):
# corresponding notification to the user? that would go here.
return False, None
elif (requires_explicit_preapprovals() and amount <= transaction.max_amount) or (not requires_explicit_preapprovals()):
elif (requires_explicit_preapprovals and amount <= transaction.max_amount) or (not requires_explicit_preapprovals):
# Update transaction but leave the preapproval alone
transaction.amount = amount
transaction.set_pledge_extra(pledge_extra)
@ -861,8 +853,7 @@ class PaymentManager( object ):
logger.info("Refund Transaction failed, invalid transaction status")
return False
method = getattr(transaction.get_payment_class(), "RefundPayment")
p = method(transaction)
p = transaction.get_payment_class().RefundPayment(transaction)
# Create a response for this
envelope = p.envelope()
@ -920,8 +911,7 @@ class PaymentManager( object ):
t.date_payment=now()
t.execution=EXECUTE_TYPE_CHAINED_INSTANT
t.type=PAYMENT_TYPE_INSTANT
method = getattr(t.get_payment_class(), "Pay")
p = method(t,return_url=return_url, nevermind_url=nevermind_url)
p = t.get_payment_class().Pay(t,return_url=return_url, nevermind_url=nevermind_url)
# Create a response for this
envelope = p.envelope()
@ -954,8 +944,7 @@ class PaymentManager( object ):
"""delegate to a specific payment module the task of creating a payment account"""
mod = __import__("regluit.payment." + host, fromlist=[host])
method = getattr(mod, "make_account")
return method(user, token)
return mod.Processor().make_account(user, token)

View File

@ -109,13 +109,13 @@ class Transaction(models.Model):
def get_payment_class(self):
'''
Returns the specific payment module that implements this transaction
Returns the specific payment processor that implements this transaction
'''
if self.host == PAYMENT_HOST_NONE:
return None
else:
mod = __import__("regluit.payment." + self.host, fromlist=[str(self.host)])
return mod
return mod.Processor()
def set_credit_approved(self, amount):
self.amount=amount

View File

@ -265,11 +265,9 @@ class StripePaymentRequest(baseprocessor.BasePaymentRequest):
"""so far there is no need to have a separate class here"""
pass
def requires_explicit_preapprovals():
"""a function that returns for the given payment processor"""
return False
class Processor(baseprocessor.Processor):
def make_account(user, token):
def make_account(self, user, token):
"""returns a payment.models.Account based on stripe token and user"""
sc = StripeClient()
@ -293,10 +291,10 @@ def make_account(user, token):
return account
class Pay(StripePaymentRequest, baseprocessor.Pay):
class Pay(StripePaymentRequest, baseprocessor.Processor.Pay):
pass
class Preapproval(StripePaymentRequest, baseprocessor.Preapproval):
class Preapproval(StripePaymentRequest, baseprocessor.Processor.Preapproval):
def __init__( self, transaction, amount, expiry=None, return_url=None, paymentReason=""):
@ -351,7 +349,7 @@ class Preapproval(StripePaymentRequest, baseprocessor.Preapproval):
return None
class Execute(StripePaymentRequest):
class Execute(StripePaymentRequest):
'''
The Execute function sends an existing token(generated via the URL from the pay operation), and collects
@ -390,7 +388,7 @@ class Execute(StripePaymentRequest):
return self.transaction.pay_key
class PreapprovalDetails(StripePaymentRequest):
class PreapprovalDetails(StripePaymentRequest):
'''
Get details about an authorized token