55requires requests module
66"""
77
8- __title__ = ' LiqPay Python SDK'
9- __version__ = ' 1.0'
8+ __title__ = " LiqPay Python SDK"
9+ __version__ = " 1.0"
1010
1111import base64
1212from copy import deepcopy
1717import requests
1818
1919
20-
21-
2220class ParamValidationError (Exception ):
2321 pass
2422
2523
2624class 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