systempayv2: handle properly unicode params (#13562)
This commit is contained in:
parent
8320be27dd
commit
09f2ec7e3d
|
@ -35,6 +35,14 @@ def force_text(s, encoding='utf-8'):
|
|||
except UnicodeDecodeError:
|
||||
return unicode(s)
|
||||
|
||||
def force_byte(s, encoding='utf-8'):
|
||||
if isinstance(s, str):
|
||||
return s
|
||||
try:
|
||||
return s.encode(encoding)
|
||||
except UnicodeEncodeError:
|
||||
return s.encode()
|
||||
|
||||
|
||||
class ResponseError(Exception):
|
||||
pass
|
||||
|
|
|
@ -9,7 +9,7 @@ import warnings
|
|||
from gettext import gettext as _
|
||||
|
||||
from common import (PaymentCommon, PaymentResponse, PAID, ERROR, FORM, Form,
|
||||
ResponseError, force_text)
|
||||
ResponseError, force_text, force_byte)
|
||||
from cb import CB_RESPONSE_CODES
|
||||
|
||||
__all__ = ['Payment']
|
||||
|
@ -56,15 +56,15 @@ class Parameter:
|
|||
self.help_text = help_text
|
||||
|
||||
def check_value(self, value):
|
||||
if self.length and len(str(value)) != self.length:
|
||||
if self.length and len(value) != self.length:
|
||||
return False
|
||||
if self.max_length and len(str(value)) > self.max_length:
|
||||
if self.max_length and len(value) > self.max_length:
|
||||
return False
|
||||
if self.choices and str(value) not in self.choices:
|
||||
if self.choices and value not in self.choices:
|
||||
return False
|
||||
if value == '':
|
||||
return True
|
||||
value = str(value).replace('.', '')
|
||||
value = value.replace('.', '')
|
||||
if self.ptype == 'n':
|
||||
return value.isdigit()
|
||||
elif self.ptype == 'an':
|
||||
|
@ -415,11 +415,11 @@ class Payment(PaymentCommon):
|
|||
ordered_keys = sorted(
|
||||
[key for key in fields.keys() if key.startswith('vads_')])
|
||||
self.logger.debug('ordered keys %s' % ordered_keys)
|
||||
ordered_fields = [str(fields[key]) for key in ordered_keys]
|
||||
ordered_fields = [force_byte(fields[key]) for key in ordered_keys]
|
||||
secret = getattr(self, 'secret_%s' % fields['vads_ctx_mode'].lower())
|
||||
signed_data = '+'.join(ordered_fields)
|
||||
signed_data = '%s+%s' % (signed_data, secret)
|
||||
self.logger.debug(u'generating signature on «%s»' % signed_data)
|
||||
signed_data = '%s+%s' % (signed_data, force_byte(secret))
|
||||
self.logger.debug(u'generating signature on «%s»', signed_data)
|
||||
sign = hashlib.sha1(signed_data).hexdigest()
|
||||
self.logger.debug(u'signature «%s»' % sign)
|
||||
self.logger.debug(u'signature «%s»', sign)
|
||||
return sign
|
||||
|
|
|
@ -1,19 +1,45 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import urlparse
|
||||
|
||||
from eopayment.systempayv2 import Payment
|
||||
from eopayment.systempayv2 import Payment, VADS_CUST_FIRST_NAME, \
|
||||
VADS_CUST_LAST_NAME, PAID
|
||||
|
||||
PARAMS = {
|
||||
'secret_test': '1122334455667788'
|
||||
'secret_test': u'1122334455667788',
|
||||
'vads_site_id': u'12345678',
|
||||
'vads_ctx_mode': u'TEST',
|
||||
'vads_trans_date': u'20090501193530',
|
||||
}
|
||||
|
||||
|
||||
def test_systempayv2():
|
||||
p = Payment(PARAMS)
|
||||
data = {'amount': 15.24, 'orderid': '654321',
|
||||
'first_name': u'Sergheï',
|
||||
'last_name': u'Mihaï'
|
||||
}
|
||||
qs = 'vads_version=V2&vads_page_action=PAYMENT&vads_action_mode=INTERACTIV' \
|
||||
'E&vads_payment_config=SINGLE&vads_site_id=12345678&vads_ctx_mode=TES' \
|
||||
'T&vads_trans_id=654321&vads_trans_date=20090501193530&vads_amount=15' \
|
||||
'24&vads_currency=978'
|
||||
'24&vads_currency=978&vads_cust_first_name=Sergheï&vads_cust_last_name=Mihaï'
|
||||
qs = urlparse.parse_qs(qs)
|
||||
for key in qs.keys():
|
||||
qs[key] = qs[key][0]
|
||||
assert p.signature(qs) == '606b369759fac4f0864144c803c73676cbe470ff'
|
||||
assert p.signature(qs) == '4d2010d3b4566841ee0b0b2c74b6650bce65365e'
|
||||
transaction_id, f, form = p.request(**data)
|
||||
|
||||
# check that user first and last names are unicode
|
||||
for field in form.fields:
|
||||
if field['name'] in (VADS_CUST_FIRST_NAME, VADS_CUST_LAST_NAME):
|
||||
assert field['value'] in (u'Sergheï', u'Mihaï')
|
||||
|
||||
response_qs = 'vads_amount=1042&vads_auth_mode=FULL&vads_auth_number=3feadf' \
|
||||
'&vads_auth_result=00&vads_capture_delay=0&vads_card_brand=CB' \
|
||||
'&vads_card_number=497010XXXXXX0000' \
|
||||
'&vads_payment_certificate=582ba2b725057618706d7a06e9e59acdbf69ff53' \
|
||||
'&vads_ctx_mode=TEST&vads_currency=978&vads_effective_amount=1042' \
|
||||
'&vads_site_id=70168983&vads_trans_date=20161013101355' \
|
||||
'&vads_trans_id=226787&vads_trans_uuid=4b5053b3b1fe4b02a07753e7a' \
|
||||
'&signature=62a4fb6738ebadebf9cc720164bc70e47282d36e'
|
||||
response = p.response(response_qs)
|
||||
assert response.result == PAID
|
||||
|
|
Loading…
Reference in New Issue