allow to specify the logger to user
This commit is contained in:
parent
fdf06922bc
commit
315297d29f
|
@ -62,9 +62,10 @@ class Payment(object):
|
|||
|
||||
'''
|
||||
|
||||
def __init__(self, kind, options):
|
||||
def __init__(self, kind, options, log_domain='eopayment'):
|
||||
self.logger = logging.getLogger(log_domain)
|
||||
self.kind = kind
|
||||
self.backend = get_backend(kind)(options)
|
||||
self.backend = get_backend(kind)(options, logger=self.logger)
|
||||
|
||||
def request(self, amount, email=None, next_url=None):
|
||||
'''Request a payment to the payment backend.
|
||||
|
@ -97,7 +98,7 @@ class Payment(object):
|
|||
# present the form in HTML to the user
|
||||
|
||||
'''
|
||||
return self.backend.request(amount, email=email, next_url=next_url)
|
||||
return self.backend.request(amount, email=email, next_url=next_url, logger=self.logger)
|
||||
|
||||
def response(self, query_string):
|
||||
'''
|
||||
|
@ -132,7 +133,7 @@ class Payment(object):
|
|||
your site as a web service.
|
||||
|
||||
'''
|
||||
return self.backend.response(query_string)
|
||||
return self.backend.response(query_string, logger=self.logger)
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
import urllib
|
||||
import string
|
||||
import logging
|
||||
|
||||
try:
|
||||
from cgi import parse_qs
|
||||
except:
|
||||
|
@ -11,6 +13,7 @@ __all__ = [ 'Payment' ]
|
|||
|
||||
SERVICE_URL = 'http://dummy-payment.demo.entrouvert.com/'
|
||||
ALPHANUM = string.letters + string.digits
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
class Payment(PaymentCommon):
|
||||
'''
|
||||
|
@ -68,7 +71,7 @@ class Payment(PaymentCommon):
|
|||
}
|
||||
}
|
||||
|
||||
def request(self, montant, email=None, next_url=None):
|
||||
def request(self, montant, email=None, next_url=None, logger=LOGGER):
|
||||
transaction_id = self.transaction_id(30, ALPHANUM, 'dummy', self.siret)
|
||||
if self.next_url:
|
||||
next_url = self.next_url
|
||||
|
@ -84,7 +87,7 @@ class Payment(PaymentCommon):
|
|||
url = '%s?%s' % (SERVICE_URL, urllib.urlencode(query))
|
||||
return transaction_id, URL, url
|
||||
|
||||
def response(self, query_string):
|
||||
def response(self, query_string, logger=LOGGER):
|
||||
form = parse_qs(query_string)
|
||||
transaction_id = form.get('transaction_id',[''])[0]
|
||||
form[self.BANK_ID] = transaction_id
|
||||
|
|
|
@ -7,6 +7,7 @@ import logging
|
|||
import os
|
||||
import os.path
|
||||
import uuid
|
||||
import logging
|
||||
|
||||
from common import PaymentCommon, HTML, PaymentResponse
|
||||
from cb import CB_RESPONSE_CODES
|
||||
|
@ -87,20 +88,20 @@ FINAREF_BANK_RESPONSE_CODE = {
|
|||
}
|
||||
|
||||
class Payment(PaymentCommon):
|
||||
def __init__(self, options):
|
||||
def __init__(self, options, logger=LOGGER):
|
||||
self.options = options
|
||||
LOGGER.debug('initializing sips payment class with %s' % options)
|
||||
logger.debug('initializing sips payment class with %s' % options)
|
||||
|
||||
def execute(self, executable, params):
|
||||
if PATHFILE in self.options:
|
||||
params[PATHFILE] = self.options[PATHFILE]
|
||||
executable = os.path.join(self.options[BINPATH], executable)
|
||||
args = [executable] + [ "%s=%s" % p for p in params.iteritems() ]
|
||||
LOGGER.debug('executing %s' % args)
|
||||
logger.debug('executing %s' % args)
|
||||
result, _ = subprocess.Popen(args, executable=executable,
|
||||
stdout=subprocess.PIPE, shell=True).communicate()
|
||||
result = result.split('!')
|
||||
LOGGER.debug('got response %s' % result)
|
||||
logger.debug('got response %s' % result)
|
||||
return result
|
||||
|
||||
def get_request_params(self):
|
||||
|
@ -108,7 +109,7 @@ class Payment(PaymentCommon):
|
|||
params.update(self.options.get(PARAMS, {}))
|
||||
return params
|
||||
|
||||
def request(self, amount, email=None, next_url=None):
|
||||
def request(self, amount, email=None, next_url=None, logger=LOGGER):
|
||||
params = self.get_request_params()
|
||||
transaction_id = self.transaction_id(6, string.digits, 'sips',
|
||||
params[MERCHANT_ID])
|
||||
|
@ -125,14 +126,14 @@ class Payment(PaymentCommon):
|
|||
else:
|
||||
raise RuntimeError('sips/request returned -1: %s' % error)
|
||||
|
||||
def response(self, query_string):
|
||||
def response(self, query_string, logger=LOGGER):
|
||||
form = urlparse.parse_qs(query_string)
|
||||
params = {'message': form[DATA]}
|
||||
result = self.execute('response', params)
|
||||
d = dict(zip(RESPONSE_PARAMS, result))
|
||||
# The reference identifier for the payment is the authorisation_id
|
||||
d[self.BANK_ID] = d.get(AUTHORISATION_ID)
|
||||
LOGGER.debug('response contains fields %s' % d)
|
||||
logger.debug('response contains fields %s' % d)
|
||||
response_result = d.get(RESPONSE_CODE) == '00'
|
||||
response_code_msg = CB_BANK_RESPONSE_CODES.get(d.get(RESPONSE_CODE))
|
||||
response = PaymentResponse(
|
||||
|
|
|
@ -70,7 +70,6 @@ def sign_url_paiement(ntkey, query):
|
|||
|
||||
ALPHANUM = string.letters + string.digits
|
||||
SERVICE_URL = "https://www.spplus.net/paiement/init.do"
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
class Payment(PaymentCommon):
|
||||
description = {
|
||||
|
@ -106,8 +105,8 @@ class Payment(PaymentCommon):
|
|||
}
|
||||
devise = '978'
|
||||
|
||||
def request(self, montant, email=None, next_url=None):
|
||||
LOGGER.debug('requesting spplus payment with montant %s email=%s and \
|
||||
def request(self, montant, email=None, next_url=None, logger=LOGGER):
|
||||
logger.debug('requesting spplus payment with montant %s email=%s and \
|
||||
next_url=%s' % (montant, email, next_url))
|
||||
reference = self.transaction_id(20, ALPHANUM, 'spplus', self.siret)
|
||||
validite = dt.date.today()+dt.timedelta(days=1)
|
||||
|
@ -130,19 +129,19 @@ next_url=%s' % (montant, email, next_url))
|
|||
or '?' in next_url:
|
||||
raise ValueError('next_url must be an absolute URL without parameters')
|
||||
fields['urlretour'] = next_url
|
||||
LOGGER.debug('sending fields %s' % fields)
|
||||
logger.debug('sending fields %s' % fields)
|
||||
query = urllib.urlencode(fields)
|
||||
url = '%s?%s&hmac=%s' % (SERVICE_URL, query, sign_url_paiement(self.cle,
|
||||
query))
|
||||
LOGGER.debug('full url %s' % url)
|
||||
logger.debug('full url %s' % url)
|
||||
return reference, URL, url
|
||||
|
||||
def response(self, query_string):
|
||||
def response(self, query_string, logger=LOGGER):
|
||||
form = urlparse.parse_qs(query_string)
|
||||
for key, value in form.iteritems():
|
||||
form[key] = value[0]
|
||||
LOGGER.debug('received query_string %s' % query_string)
|
||||
LOGGER.debug('parsed as %s' % form)
|
||||
logger.debug('received query_string %s' % query_string)
|
||||
logger.debug('parsed as %s' % form)
|
||||
reference = form.get(REFERENCE)
|
||||
bank_status = []
|
||||
signed_result = None
|
||||
|
@ -155,9 +154,9 @@ next_url=%s' % (montant, email, next_url))
|
|||
try:
|
||||
signed_data, signature = query_string.rsplit('&', 1)
|
||||
_, hmac = signature.split('=', 1)
|
||||
LOGGER.debug('got signature %s' % hmac)
|
||||
logger.debug('got signature %s' % hmac)
|
||||
computed_hmac = sign_ntkey_query(self.cle, signed_data)
|
||||
LOGGER.debug('computed signature %s' % hmac)
|
||||
logger.debug('computed signature %s' % hmac)
|
||||
if hmac == computed_hmac:
|
||||
signed_result = result
|
||||
else:
|
||||
|
|
|
@ -170,6 +170,8 @@ RESPONSE_SIGNATURE_FIELDS = ['version', 'site_id', 'ctx_mode', 'trans_id',
|
|||
|
||||
S2S_RESPONSE_SIGNATURE_FIELDS = RESPONSE_SIGNATURE_FIELDS + [ 'hash' ]
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
class Payment:
|
||||
'''
|
||||
ex.: Payment(secrets={'TEST': 'xxx', 'PRODUCTION': 'yyyy'}, site_id=123,
|
||||
|
@ -179,7 +181,7 @@ class Payment:
|
|||
def __init__(self, **kwargs):
|
||||
self.options = kwargs
|
||||
|
||||
def request(self, amount, **kwargs):
|
||||
def request(self, amount, **kwargs, logger=LOGGER):
|
||||
'''
|
||||
Create a dictionnary to send a payment request to systempay the
|
||||
Credit Card payment server of the NATIXIS group
|
||||
|
|
|
@ -166,7 +166,7 @@ class Payment(PaymentCommon):
|
|||
options = add_vads(options)
|
||||
self.options = options
|
||||
|
||||
def request(self, amount, email=None, next_url=None):
|
||||
def request(self, amount, email=None, next_url=None, logger=LOGGER):
|
||||
'''
|
||||
Create a dictionary to send a payment request to systempay the
|
||||
Credit Card payment server of the NATIXIS group
|
||||
|
@ -209,7 +209,7 @@ parameters received: %s' % (name, kwargs))
|
|||
transaction_id = '%s_%s' % (fields[VADS_TRANS_DATE], transaction_id)
|
||||
return transaction_id, URL, fields
|
||||
|
||||
def response(self, query_string):
|
||||
def response(self, query_string, logger=LOGGER):
|
||||
fields = urlparse.parse_qs(query_string)
|
||||
copy = fields.copy()
|
||||
bank_status = []
|
||||
|
@ -237,16 +237,16 @@ parameters received: %s' % (name, kwargs))
|
|||
copy[VADS_EXTRA_RESULT] = '%s: %s' % (v,
|
||||
EXTRA_RESULT_MAP.get(v, 'Code inconnu'))
|
||||
bank_status.append(copy[VADS_EXTRA_RESULT])
|
||||
LOGGER.debug('checking systempay response on:')
|
||||
logger.debug('checking systempay response on:')
|
||||
for key in sorted(fields.keys):
|
||||
LOGGER.debug(' %s: %s' % (key, copy[key]))
|
||||
signature = self.signature(fields)
|
||||
logger.debug(' %s: %s' % (key, copy[key]))
|
||||
signature = self.signature(fields, logger)
|
||||
signature_result = signature == fields[SIGNATURE]
|
||||
if not signature_result:
|
||||
bank_status.append('invalid signature')
|
||||
result = fields[VADS_AUTH_RESULT] == '00'
|
||||
signed_result = signature_result and result
|
||||
LOGGER.debug('signature check result: %s' % result)
|
||||
logger.debug('signature check result: %s' % result)
|
||||
transaction_id = '%s_%s' % (copy[VADS_TRANS_DATE], copy[VADS_TRANS_ID])
|
||||
# the VADS_AUTH_NUMBER is the number to match payment in bank logs
|
||||
copy[self.BANK_ID] = copy.get(VADS_AUTH_NUMBER, '')
|
||||
|
@ -259,16 +259,16 @@ parameters received: %s' % (name, kwargs))
|
|||
bank_status=' - '.join(bank_status))
|
||||
return response
|
||||
|
||||
def signature(self, fields):
|
||||
LOGGER.debug('got fields %s to sign' % fields )
|
||||
def signature(self, fields, logger):
|
||||
logger.debug('got fields %s to sign' % fields )
|
||||
ordered_keys = sorted([ key for key in fields.keys() if key.startswith('vads_') ])
|
||||
LOGGER.debug('ordered keys %s' % ordered_keys)
|
||||
logger.debug('ordered keys %s' % ordered_keys)
|
||||
ordered_fields = [ str(fields[key]) for key in ordered_keys ]
|
||||
secret = self.secrets[fields['vads_ctx_mode']]
|
||||
signed_data = '+'.join(ordered_fields)
|
||||
LOGGER.debug('generating signature on «%s»' % signed_data)
|
||||
logger.debug('generating signature on «%s»' % signed_data)
|
||||
sign = hashlib.sha1('%s+%s' % (signed_data, secret)).hexdigest()
|
||||
LOGGER.debug('signature «%s»' % sign)
|
||||
logger.debug('signature «%s»' % sign)
|
||||
return sign
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in New Issue