Skip to content

Commit 3b13c30

Browse files
authored
Merge pull request #6 from JFF-Bohdan/master
Fixes Python3 support and improves formatting
2 parents 04d1322 + 5659e91 commit 3b13c30

File tree

4 files changed

+160
-35
lines changed

4 files changed

+160
-35
lines changed

.flake8

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[flake8]
2+
exclude =
3+
.git,
4+
.idea,
5+
__pycache__,
6+
doc,
7+
logs,
8+
__init__.py,
9+
max-line-length = 150
10+
max-complexity = 10
11+
filename = *.py
12+
format = default
13+
inline-quotes = "
14+
ignore =
15+
# E221 multiple spaces before operator
16+
E221,
17+
# whitespace before ':'
18+
E203,
19+
# whitespace before ')'
20+
E202,
21+
# multiple spaces after ','
22+
E241,
23+
# unexpected spaces around keyword / parameter equals
24+
E251

.gitignore

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*,cover
46+
.hypothesis/
47+
48+
# Translations
49+
*.mo
50+
*.pot
51+
52+
# Django stuff:
53+
*.log
54+
local_settings.py
55+
56+
# Flask stuff:
57+
instance/
58+
.webassets-cache
59+
60+
# Scrapy stuff:
61+
.scrapy
62+
63+
# Sphinx documentation
64+
docs/_build/
65+
66+
# PyBuilder
67+
target/
68+
69+
# IPython Notebook
70+
.ipynb_checkpoints
71+
72+
# pyenv
73+
.python-version
74+
75+
# celery beat schedule file
76+
celerybeat-schedule
77+
78+
# dotenv
79+
.env
80+
81+
# virtualenv
82+
venv/
83+
ENV/
84+
85+
# Spyder project settings
86+
.spyderproject
87+
88+
# Rope project settings
89+
.ropeproject
90+
91+
#project files
92+
cached_uglify
93+
.idea
94+

liqpay/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
__author__ = 'user'
1+
import sys
2+
3+
if sys.version_info >= (3, 0):
4+
from .liqpay3 import *
5+
else:
6+
from .liqpay import *

liqpay/liqpay3.py

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
requires requests module
66
"""
77

8-
__title__ = 'LiqPay Python SDK'
9-
__version__ = '1.0'
8+
__title__ = "LiqPay Python SDK"
9+
__version__ = "1.0"
1010

1111
import base64
1212
from copy import deepcopy
@@ -17,34 +17,32 @@
1717
import requests
1818

1919

20-
21-
2220
class ParamValidationError(Exception):
2321
pass
2422

2523

2624
class LiqPay(object):
27-
FORM_TEMPLATE = u'''\
28-
<form method="post" action="{action}" accept-charset="utf-8">
29-
\t{param_inputs}
30-
<input type="image" src="//static.liqpay.com/buttons/p1{language}.radius.png" name="btn_text" />
31-
</form>'''
32-
INPUT_TEMPLATE = u'<input type="hidden" name="{name}" value="{value}"/>'
25+
FORM_TEMPLATE = """\
26+
<form method="post" action="{action}" accept-charset="utf-8">
27+
\t{param_inputs}
28+
<input type="image" src="//static.liqpay.com/buttons/p1{language}.radius.png" name="btn_text" />
29+
</form>"""
30+
INPUT_TEMPLATE = "<input type='hidden' name='{name}' value='{value}'/>"
3331

3432
SUPPORTED_PARAMS = [
35-
'public_key', 'amount', 'currency', 'description', 'order_id',
36-
'result_url', 'server_url', 'type', 'signature', 'language', 'sandbox'
33+
"public_key", "amount", "currency", "description", "order_id",
34+
"result_url", "server_url", "type", "signature", "language", "sandbox"
3735
]
3836

39-
def __init__(self, public_key, private_key, host='https://www.liqpay.com/api/'):
37+
def __init__(self, public_key, private_key, host="https://www.liqpay.com/api/"):
4038
self._public_key = public_key
4139
self._private_key = private_key
4240
self._host = host
4341

4442
def _make_signature(self, *args):
45-
joined_fields = ''.join(x for x in args)
46-
joined_fields = joined_fields.encode('utf-8')
47-
return base64.b64encode(hashlib.sha1(joined_fields).digest())
43+
joined_fields = "".join(x for x in args)
44+
joined_fields = joined_fields.encode("utf-8")
45+
return base64.b64encode(hashlib.sha1(joined_fields).digest()).decode("ascii")
4846

4947
def _prepare_params(self, params):
5048
params = {} if params is None else deepcopy(params)
@@ -59,45 +57,49 @@ def api(self, url, params=None):
5957
signature = self._make_signature(private_key, json_encoded_params, private_key)
6058

6159
request_url = urljoin(self._host, url)
62-
request_data = {'data': json_encoded_params, 'signature': signature}
60+
request_data = {"data": json_encoded_params, "signature": signature}
6361
response = requests.post(request_url, data=request_data, verify=False)
64-
return json.loads(response.content.decode('utf-8'))
62+
return json.loads(response.content.decode("utf-8"))
6563

6664
def cnb_form(self, params):
6765
params = self._prepare_params(params)
6866
params_validator = (
69-
('amount', lambda x: x is not None and float(x) > 0),
70-
('description', lambda x: x is not None)
67+
("amount", lambda x: x is not None and float(x) > 0),
68+
("description", lambda x: x is not None)
7169
)
7270
for key, validator in params_validator:
7371
if validator(params.get(key)):
7472
continue
7573

76-
raise ParamValidationError('Invalid param: "%s"' % key)
74+
raise ParamValidationError("Invalid param: '{}'".format(key))
7775

7876
# spike to set correct values for language, currency and sandbox params
79-
language = params.get('language', 'ru')
80-
currency = params['currency']
77+
language = params.get("language", "ru")
78+
currency = params["currency"]
8179
params.update(
8280
language=language,
83-
currency=currency if currency != 'RUR' else 'RUB',
84-
sandbox=int(bool(params.get('sandbox')))
81+
currency=currency if currency != "RUR" else "RUB",
82+
sandbox=int(bool(params.get("sandbox")))
8583
)
86-
params_templ = {'data': base64.b64encode(json.dumps(params))}
87-
params_templ['signature'] = self._make_signature(self._private_key, params_templ['data'], self._private_key)
88-
form_action_url = urljoin(self._host, '3/checkout/')
89-
format_input = lambda k, v: self.INPUT_TEMPLATE.format(name=k, value=v)
90-
inputs = [format_input(k, v) for k, v in params_templ.iteritems()]
84+
85+
encoded_data = base64.b64encode(json.dumps(params).encode("utf-8")).decode("ascii")
86+
params_templ = {"data": encoded_data}
87+
88+
params_templ["signature"] = self._make_signature(self._private_key, params_templ["data"], self._private_key)
89+
form_action_url = urljoin(self._host, "3/checkout/")
90+
format_input = (lambda k, v: self.INPUT_TEMPLATE.format(name=k, value=v))
91+
inputs = [format_input(k, v) for k, v in params_templ.items()]
9192
return self.FORM_TEMPLATE.format(
9293
action=form_action_url,
9394
language=language,
94-
param_inputs=u'\n\t'.join(inputs)
95+
param_inputs="\n\t".join(inputs)
9596
)
9697

9798
def cnb_signature(self, params):
9899
params = self._prepare_params(params)
99-
print(base64.b64encode(json.dumps(params)))
100-
return self._make_signature(self._private_key, base64.b64encode(json.dumps(params)), self._private_key)
100+
101+
data_to_sign = base64.b64encode(json.dumps(params).encode("utf-8")).decode("ascii")
102+
return self._make_signature(self._private_key, data_to_sign, self._private_key)
101103

102104
def str_to_sign(self, str):
103-
return base64.b64encode(hashlib.sha1(str).digest())
105+
return base64.b64encode(hashlib.sha1(str.encode("utf-8")).digest()).decode("ascii")

0 commit comments

Comments
 (0)