|
| 1 | +""" |
| 2 | +LiqPay Python SDK |
| 3 | +~~~~~~~~~~~~~~~~~ |
| 4 | +supports python 3 version |
| 5 | +requires requests module |
| 6 | +""" |
| 7 | + |
| 8 | +__title__ = 'LiqPay Python SDK' |
| 9 | +__version__ = '1.0' |
| 10 | + |
| 11 | +import base64 |
| 12 | +from copy import deepcopy |
| 13 | +import hashlib |
| 14 | +import json |
| 15 | +from urllib.parse import urljoin |
| 16 | + |
| 17 | +import requests |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +class ParamValidationError(Exception): |
| 23 | + pass |
| 24 | + |
| 25 | + |
| 26 | +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}"/>' |
| 33 | + |
| 34 | + SUPPORTED_PARAMS = [ |
| 35 | + 'public_key', 'amount', 'currency', 'description', 'order_id', |
| 36 | + 'result_url', 'server_url', 'type', 'signature', 'language', 'sandbox' |
| 37 | + ] |
| 38 | + |
| 39 | + def __init__(self, public_key, private_key, host='https://www.liqpay.com/api/'): |
| 40 | + self._public_key = public_key |
| 41 | + self._private_key = private_key |
| 42 | + self._host = host |
| 43 | + |
| 44 | + 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()) |
| 48 | + |
| 49 | + def _prepare_params(self, params): |
| 50 | + params = {} if params is None else deepcopy(params) |
| 51 | + params.update(public_key=self._public_key) |
| 52 | + return params |
| 53 | + |
| 54 | + def api(self, url, params=None): |
| 55 | + params = self._prepare_params(params) |
| 56 | + |
| 57 | + json_encoded_params = json.dumps(params) |
| 58 | + private_key = self._private_key |
| 59 | + signature = self._make_signature(private_key, json_encoded_params, private_key) |
| 60 | + |
| 61 | + request_url = urljoin(self._host, url) |
| 62 | + request_data = {'data': json_encoded_params, 'signature': signature} |
| 63 | + response = requests.post(request_url, data=request_data, verify=False) |
| 64 | + return json.loads(response.content.decode('utf-8')) |
| 65 | + |
| 66 | + def cnb_form(self, params): |
| 67 | + params = self._prepare_params(params) |
| 68 | + params_validator = ( |
| 69 | + ('amount', lambda x: x is not None and float(x) > 0), |
| 70 | + ('description', lambda x: x is not None) |
| 71 | + ) |
| 72 | + for key, validator in params_validator: |
| 73 | + if validator(params.get(key)): |
| 74 | + continue |
| 75 | + |
| 76 | + raise ParamValidationError('Invalid param: "%s"' % key) |
| 77 | + |
| 78 | + # spike to set correct values for language, currency and sandbox params |
| 79 | + language = params.get('language', 'ru') |
| 80 | + currency = params['currency'] |
| 81 | + params.update( |
| 82 | + language=language, |
| 83 | + currency=currency if currency != 'RUR' else 'RUB', |
| 84 | + sandbox=int(bool(params.get('sandbox'))) |
| 85 | + ) |
| 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()] |
| 91 | + return self.FORM_TEMPLATE.format( |
| 92 | + action=form_action_url, |
| 93 | + language=language, |
| 94 | + param_inputs=u'\n\t'.join(inputs) |
| 95 | + ) |
| 96 | + |
| 97 | + def cnb_signature(self, params): |
| 98 | + 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) |
| 101 | + |
| 102 | + def str_to_sign(self, str): |
| 103 | + return base64.b64encode(hashlib.sha1(str).digest()) |
0 commit comments