2013-06-03 16:31:39 +00:00
|
|
|
"""
|
|
|
|
external library imports
|
|
|
|
"""
|
|
|
|
import datetime
|
|
|
|
import time
|
2012-10-02 17:28:15 +00:00
|
|
|
|
2012-08-31 07:12:15 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2013-06-03 16:31:39 +00:00
|
|
|
"""
|
|
|
|
django imports
|
|
|
|
"""
|
|
|
|
from django.http import HttpResponseForbidden
|
2018-04-19 16:24:34 +00:00
|
|
|
from django.utils.timezone import now
|
2013-06-03 16:31:39 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
regluit imports
|
|
|
|
"""
|
|
|
|
from regluit.payment.models import PaymentResponse
|
2012-08-31 07:12:15 +00:00
|
|
|
|
2012-10-06 16:51:43 +00:00
|
|
|
class ProcessorError(Exception):
|
|
|
|
"""An abstraction around payment processor exceptions"""
|
|
|
|
def __init__(self, message=None, original_exception=None):
|
|
|
|
super(ProcessorError, self).__init__(message)
|
|
|
|
self.original_exception = original_exception
|
|
|
|
|
|
|
|
|
2012-08-31 07:12:15 +00:00
|
|
|
|
|
|
|
class BasePaymentRequest:
|
|
|
|
'''
|
|
|
|
Handles common information incident to payment processing
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Global values for the class
|
|
|
|
response = None
|
|
|
|
raw_response = None
|
|
|
|
errorMessage = None
|
|
|
|
status = None
|
|
|
|
url = None
|
|
|
|
|
|
|
|
def ack( self ):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def success(self):
|
|
|
|
|
|
|
|
if self.errorMessage:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def error(self):
|
|
|
|
if self.errorMessage:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def error_data(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def error_id(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def error_string(self):
|
|
|
|
return self.errorMessage
|
|
|
|
|
|
|
|
def envelope(self):
|
|
|
|
# The envelope is used to store info about this request
|
|
|
|
if self.response:
|
|
|
|
return str(self.response)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def correlation_id(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def timestamp(self):
|
|
|
|
return str(datetime.datetime.now())
|
|
|
|
|
2012-10-03 03:49:19 +00:00
|
|
|
class Processor:
|
|
|
|
"""a function that returns for the given payment processor"""
|
|
|
|
requires_explicit_preapprovals=False
|
2012-08-31 07:12:15 +00:00
|
|
|
|
2014-02-20 20:56:10 +00:00
|
|
|
def make_account(self, user=None, token=None):
|
2012-10-03 03:49:19 +00:00
|
|
|
"""template function for return a payment.Account corresponding to the payment system"""
|
|
|
|
return None
|
2012-08-31 07:12:15 +00:00
|
|
|
|
2012-10-03 03:49:19 +00:00
|
|
|
def ProcessIPN(self, request):
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
2012-08-31 07:12:15 +00:00
|
|
|
|
2012-10-03 03:49:19 +00:00
|
|
|
class Pay( BasePaymentRequest ):
|
2012-08-31 07:12:15 +00:00
|
|
|
|
2012-10-03 03:49:19 +00:00
|
|
|
'''
|
|
|
|
The pay function generates a redirect URL to approve the transaction
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__( self, transaction, return_url=None, amount=None, paymentReason=""):
|
|
|
|
self.transaction=transaction
|
2013-06-25 22:34:20 +00:00
|
|
|
self.return_url = return_url
|
|
|
|
self.amount = amount
|
2012-08-31 07:12:15 +00:00
|
|
|
|
2012-10-03 03:49:19 +00:00
|
|
|
def api(self):
|
|
|
|
return "null api"
|
2012-08-31 07:12:15 +00:00
|
|
|
|
2012-10-03 03:49:19 +00:00
|
|
|
def exec_status( self ):
|
|
|
|
return None
|
2012-08-31 07:12:15 +00:00
|
|
|
|
2012-10-03 03:49:19 +00:00
|
|
|
def amount( self ):
|
2013-06-25 22:34:20 +00:00
|
|
|
return self.amount
|
2012-10-03 03:49:19 +00:00
|
|
|
|
|
|
|
def key( self ):
|
|
|
|
return None
|
2012-08-31 07:12:15 +00:00
|
|
|
|
2012-10-03 03:49:19 +00:00
|
|
|
def next_url( self ):
|
2013-06-25 22:34:20 +00:00
|
|
|
return self.return_url
|
2012-10-03 03:49:19 +00:00
|
|
|
|
|
|
|
class Preapproval(Pay):
|
|
|
|
|
|
|
|
def __init__( self, transaction, amount, expiry=None, return_url=None, paymentReason=""):
|
|
|
|
|
|
|
|
# set the expiration date for the preapproval if not passed in. This is what the paypal library does
|
|
|
|
now_val = now()
|
|
|
|
if expiry is None:
|
|
|
|
expiry = now_val + timedelta( days=settings.PREAPPROVAL_PERIOD )
|
|
|
|
transaction.date_authorized = now_val
|
|
|
|
transaction.date_expired = expiry
|
|
|
|
transaction.save()
|
|
|
|
|
|
|
|
# Call into our parent class
|
|
|
|
Pay.__init__(self, transaction, return_url=return_url, amount=amount, paymentReason=paymentReason)
|
|
|
|
|
|
|
|
|
|
|
|
class Execute(BasePaymentRequest):
|
|
|
|
|
|
|
|
'''
|
|
|
|
The Execute function sends an existing token(generated via the URL from the pay operation), and collects
|
|
|
|
the money.
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, transaction=None):
|
|
|
|
self.transaction = transaction
|
|
|
|
|
|
|
|
def api(self):
|
|
|
|
return "Base Pay"
|
|
|
|
|
|
|
|
def key(self):
|
|
|
|
# IN paypal land, our key is updated from a preapproval to a pay key here, just return the existing key
|
|
|
|
return self.transaction.pay_key
|
|
|
|
|
|
|
|
|
2012-08-31 07:12:15 +00:00
|
|
|
|
2012-10-03 03:49:19 +00:00
|
|
|
class Finish(BasePaymentRequest):
|
|
|
|
'''
|
|
|
|
The Finish function handles the secondary receiver in a chained payment.
|
|
|
|
'''
|
|
|
|
def __init__(self, transaction):
|
2012-08-31 07:12:15 +00:00
|
|
|
|
2020-02-12 15:43:41 +00:00
|
|
|
print("Finish")
|
2012-10-03 03:49:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PaymentDetails(BasePaymentRequest):
|
|
|
|
'''
|
|
|
|
Get details about executed PAY operation
|
|
|
|
|
|
|
|
This api must set the following class variables to work with the code in manager.py
|
|
|
|
|
|
|
|
status - one of the global transaction status codes
|
|
|
|
transactions -- Not supported for amazon, used by paypal
|
|
|
|
|
|
|
|
'''
|
|
|
|
def __init__(self, transaction=None):
|
|
|
|
self.transaction = transaction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CancelPreapproval(BasePaymentRequest):
|
|
|
|
'''
|
|
|
|
Cancels an exisiting token.
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, transaction):
|
|
|
|
self.transaction = transaction
|
|
|
|
|
|
|
|
|
|
|
|
class RefundPayment(BasePaymentRequest):
|
|
|
|
|
|
|
|
def __init__(self, transaction):
|
|
|
|
self.transaction = transaction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PreapprovalDetails(BasePaymentRequest):
|
|
|
|
'''
|
|
|
|
Get details about an authorized token
|
|
|
|
|
|
|
|
This api must set 4 different class variables to work with the code in manager.py
|
|
|
|
|
|
|
|
status - one of the global transaction status codes
|
|
|
|
approved - boolean value
|
|
|
|
currency - not used in this API, but we can get some more info via other APIs - TODO
|
|
|
|
amount - not used in this API, but we can get some more info via other APIs - TODO
|
|
|
|
|
|
|
|
'''
|
|
|
|
def __init__(self, transaction=None):
|
|
|
|
self.transaction = transaction
|
|
|
|
|