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 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: class BasePaymentRequest:
''' '''
Handles common information incident to payment processing Handles common information incident to payment processing
@ -71,6 +59,17 @@ class BasePaymentRequest:
def timestamp(self): def timestamp(self):
return str(datetime.datetime.now()) return str(datetime.datetime.now())
class Processor:
"""a function that returns for the given payment processor"""
requires_explicit_preapprovals=False
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 ): class Pay( BasePaymentRequest ):

View File

@ -4,6 +4,7 @@ from django.contrib.auth.models import User
from django.conf import settings from django.conf import settings
from regluit.payment.parameters import * from regluit.payment.parameters import *
from regluit.payment import baseprocessor
from regluit.payment.baseprocessor import BasePaymentRequest from regluit.payment.baseprocessor import BasePaymentRequest
@ -28,6 +29,7 @@ def credit_transaction(t,user,amount):
user.credit.add_to_pledged(pledge_amount) user.credit.add_to_pledged(pledge_amount)
t.set_credit_approved(pledge_amount) t.set_credit_approved(pledge_amount)
class Processor(baseprocessor.Processor):
class CancelPreapproval(BasePaymentRequest): class CancelPreapproval(BasePaymentRequest):
''' '''
Cancels an exisiting token. Cancels an exisiting token.

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

View File

@ -109,13 +109,13 @@ class Transaction(models.Model):
def get_payment_class(self): 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: if self.host == PAYMENT_HOST_NONE:
return None return None
else: else:
mod = __import__("regluit.payment." + self.host, fromlist=[str(self.host)]) mod = __import__("regluit.payment." + self.host, fromlist=[str(self.host)])
return mod return mod.Processor()
def set_credit_approved(self, amount): def set_credit_approved(self, amount):
self.amount=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""" """so far there is no need to have a separate class here"""
pass pass
def requires_explicit_preapprovals(): class Processor(baseprocessor.Processor):
"""a function that returns for the given payment processor"""
return False
def make_account(user, token): def make_account(self, user, token):
"""returns a payment.models.Account based on stripe token and user""" """returns a payment.models.Account based on stripe token and user"""
sc = StripeClient() sc = StripeClient()
@ -293,10 +291,10 @@ def make_account(user, token):
return account return account
class Pay(StripePaymentRequest, baseprocessor.Pay): class Pay(StripePaymentRequest, baseprocessor.Processor.Pay):
pass pass
class Preapproval(StripePaymentRequest, baseprocessor.Preapproval): class Preapproval(StripePaymentRequest, baseprocessor.Processor.Preapproval):
def __init__( self, transaction, amount, expiry=None, return_url=None, paymentReason=""): def __init__( self, transaction, amount, expiry=None, return_url=None, paymentReason=""):