1414 AdyenAPIValidationError ,
1515 AdyenInvalidRequestError ,
1616 AdyenAPIInvalidFormat ,
17- AdyenAPIInvalidAmount )
17+ AdyenAPIInvalidAmount ,
18+ )
1819from . import settings
1920
2021
@@ -68,7 +69,7 @@ class AdyenClient(object):
6869 def __init__ (self , username = None , password = None ,
6970 review_payout_username = None , review_payout_password = None ,
7071 store_payout_username = None , store_payout_password = None ,
71- platform = None , merchant_account = None ,
72+ platform = "test" , merchant_account = None ,
7273 merchant_specific_url = None , skin_code = None ,
7374 hmac = None , app_name = None ,
7475 http_force = None ):
@@ -79,8 +80,6 @@ def __init__(self, username=None, password=None,
7980 self .store_payout_username = store_payout_username
8081 self .store_payout_password = store_payout_password
8182 self .platform = platform
82- if not self .platform :
83- self .platform = 'test'
8483 self .merchant_specific_url = merchant_specific_url
8584 self .hmac = hmac
8685 self .merchant_account = merchant_account
@@ -124,55 +123,43 @@ def _determine_hpp_url(self, platform, action):
124123
125124 def _review_payout_username (self , ** kwargs ):
126125 if 'username' in kwargs :
127- review_payout_username = kwargs ['username' ]
126+ return kwargs ['username' ]
128127 elif self .review_payout_username :
129- review_payout_username = self .review_payout_username
130- else :
131- errorstring = """AdyenInvalidRequestError: Please set your review payout
132- webservice username. You can do this by running
133- 'Adyen.review_payout_username = 'Your payout username' """
134- raise AdyenInvalidRequestError (errorstring )
135-
136- return review_payout_username
128+ return self .review_payout_username
129+ errorstring = """Please set your review payout
130+ webservice username. You can do this by running
131+ 'Adyen.review_payout_username = 'Your payout username' """
132+ raise AdyenInvalidRequestError (errorstring )
137133
138134 def _review_payout_pass (self , ** kwargs ):
139135 if 'password' in kwargs :
140- review_payout_password = kwargs ["password" ]
136+ return kwargs ["password" ]
141137 elif self .review_payout_password :
142- review_payout_password = self .review_payout_password
143- else :
144- errorstring = """AdyenInvalidRequestError: Please set your review payout
145- webservice password. You can do this by running
146- 'Adyen.review_payout_password = 'Your payout password'"""
147- raise AdyenInvalidRequestError (errorstring )
148-
149- return review_payout_password
138+ return self .review_payout_password
139+ errorstring = """Please set your review payout
140+ webservice password. You can do this by running
141+ 'Adyen.review_payout_password = 'Your payout password'"""
142+ raise AdyenInvalidRequestError (errorstring )
150143
151144 def _store_payout_username (self , ** kwargs ):
152145 if 'username' in kwargs :
153- store_payout_username = kwargs ['username' ]
146+ return kwargs ['username' ]
154147 elif self .store_payout_username :
155- store_payout_username = self .store_payout_username
156- else :
157- errorstring = """AdyenInvalidRequestError: Please set your store payout
158- webservice username. You can do this by running
159- 'Adyen.store_payout_username = 'Your payout username'"""
160- raise AdyenInvalidRequestError (errorstring )
161-
162- return store_payout_username
148+ return self .store_payout_username
149+ errorstring = """Please set your store payout
150+ webservice username. You can do this by running
151+ 'Adyen.store_payout_username = 'Your payout username'"""
152+ raise AdyenInvalidRequestError (errorstring )
163153
164154 def _store_payout_pass (self , ** kwargs ):
165155 if 'password' in kwargs :
166- store_payout_password = kwargs ["password" ]
156+ return kwargs ["password" ]
167157 elif self .store_payout_password :
168- store_payout_password = self .store_payout_password
169- else :
170- errorstring = """AdyenInvalidRequestError: Please set your store payout
171- webservice password. You can do this by running
172- 'Adyen.store_payout_password = 'Your payout password'"""
173- raise AdyenInvalidRequestError (errorstring )
174-
175- return store_payout_password
158+ return self .store_payout_password
159+ errorstring = """Please set your store payout
160+ webservice password. You can do this by running
161+ 'Adyen.store_payout_password = 'Your payout password'"""
162+ raise AdyenInvalidRequestError (errorstring )
176163
177164 def call_api (self , request_data , service , action , idempotency = False ,
178165 ** kwargs ):
@@ -203,62 +190,53 @@ def call_api(self, request_data, service, action, idempotency=False,
203190
204191 # username at self object has highest priority. fallback to root module
205192 # and ensure that it is set.
206- if 'username' in kwargs :
207- username = kwargs ["username" ]
193+ if self .username :
194+ username = self .username
195+ elif 'username' in kwargs :
196+ username = kwargs .pop ("username" )
208197 elif service == "Payout" :
209198 if any (substring in action for substring in ["store" , "submit" ]):
210199 username = self ._store_payout_username (** kwargs )
211200 else :
212201 username = self ._review_payout_username (** kwargs )
213- elif self .username :
214- username = self .username
215202 if not username :
216- errorstring = """AdyenInvalidRequestError: Please set your webservice username."
203+ errorstring = """Please set your webservice username.
217204 You can do this by running 'Adyen.username = 'Your username'"""
218205 raise AdyenInvalidRequestError (errorstring )
219- # Ensure that username has been removed so as not to be passed to adyen
220- if 'username' in kwargs :
221- del kwargs ['username' ]
222206
223207 # password at self object has highest priority. fallback to root module
224208 # and ensure that it is set.
225- if 'password' in kwargs :
226- password = kwargs ["password" ]
209+ if self .password :
210+ password = self .password
211+ elif 'password' in kwargs :
212+ password = kwargs .pop ("password" )
227213 elif service == "Payout" :
228214 if any (substring in action for substring in ["store" , "submit" ]):
229215 password = self ._store_payout_pass (** kwargs )
230216 else :
231217 password = self ._review_payout_pass (** kwargs )
232- elif self .password :
233- password = self .password
234218 if not password :
235- errorstring = """AdyenInvalidRequestError: Please set your webservice password.
219+ errorstring = """Please set your webservice password.
236220 You can do this by running 'Adyen.password = 'Your password'"""
237221 raise AdyenInvalidRequestError (errorstring )
238- # Ensure that password has been removed so as not to be passed to adyen
239- if 'password' in kwargs :
240- del kwargs ["password" ]
241222
242223 # platform at self object has highest priority. fallback to root module
243224 # and ensure that it is set to either 'live' or 'test'.
244- if 'platform' in kwargs :
245- platform = kwargs ['platform' ]
246- del kwargs ['platform' ]
247- elif self .platform :
225+ if self .platform :
248226 platform = self .platform
227+ elif 'platform' in kwargs :
228+ platform = kwargs .pop ('platform' )
249229
250- if platform .lower () not in ['live' , 'test' ]:
251- errorstring = "'platform' must be the value of 'live' or 'test'"
252- raise ValueError (errorstring )
253- elif not isinstance (platform , str ):
230+ if not isinstance (platform , str ):
254231 errorstring = "'platform' value must be type of string"
255232 raise TypeError (errorstring )
233+ elif platform .lower () not in ['live' , 'test' ]:
234+ errorstring = "'platform' must be the value of 'live' or 'test'"
235+ raise ValueError (errorstring )
256236
257237 message = request_data
258238
259- if 'merchantAccount' not in message :
260- message ['merchantAccount' ] = self .merchant_account
261- if message ['merchantAccount' ] == "" :
239+ if not message .get ('merchantAccount' ):
262240 message ['merchantAccount' ] = self .merchant_account
263241
264242 # Adyen requires this header to be set and uses the combination of
@@ -314,7 +292,8 @@ def call_hpp(self, message, action, hmac_key="", **kwargs):
314292 if self .hmac :
315293 hmac = self .hmac
316294 elif not hmac :
317- errorstring = """Please set an hmac with your Adyen.Adyen class instance.
295+ errorstring = """Please set an hmac with your Adyen.Adyen
296+ class instance.
318297 'Adyen.hmac = \" !WR#F@...\" ' or as an additional
319298 parameter in the function call ie.
320299 'Adyen.hpp.directory_lookup(hmac=\" !WR#F@...\" '. Please reach
@@ -323,14 +302,13 @@ def call_hpp(self, message, action, hmac_key="", **kwargs):
323302
324303 # platform provided in self has highest priority,
325304 # fallback to root module and ensure that it is set.
326- if self .platform :
327- platform = self .platform
328- if platform .lower () not in ['live' , 'test' ]:
329- errorstring = " 'platform' must be the value of 'live' or 'test' "
330- raise ValueError (errorstring )
331- elif not isinstance (platform , str ):
305+ platform = self .platform
306+ if not isinstance (platform , str ):
332307 errorstring = "'platform' must be type string"
333308 raise TypeError (errorstring )
309+ elif platform .lower () not in ['live' , 'test' ]:
310+ errorstring = " 'platform' must be the value of 'live' or 'test' "
311+ raise ValueError (errorstring )
334312
335313 if 'skinCode' not in message :
336314 message ['skinCode' ] = self .skin_code
@@ -363,12 +341,12 @@ def hpp_payment(self, request_data, action, hmac_key="", **kwargs):
363341 self .http_init = True
364342
365343 platform = self .platform
366- if platform .lower () not in ['live' , 'test' ]:
367- errorstring = " 'platform' must be the value of 'live' or 'test' "
368- raise ValueError (errorstring )
369- elif not isinstance (platform , str ):
344+ if not isinstance (platform , str ):
370345 errorstring = "'platform' must be type string"
371346 raise TypeError (errorstring )
347+ elif platform .lower () not in ['live' , 'test' ]:
348+ errorstring = " 'platform' must be the value of 'live' or 'test' "
349+ raise ValueError (errorstring )
372350
373351 if 'skinCode' not in request_data :
374352 request_data ['skinCode' ] = self .skin_code
@@ -444,14 +422,11 @@ def _handle_response(self, url, raw_response, raw_request,
444422 reference = message .get ("reference" ,
445423 message .get ("merchantReference" ))
446424
447- errorstring = """AdyenInvalidRequestError: Unable to retrieve payment "
425+ errorstring = """Unable to retrieve payment "
448426 list. Received the error: {}. Please verify your request "
449427 and try again. If the issue persists, please reach out to "
450428 support@adyen.com including the "
451429 merchantReference: {}""" .format (error , reference ),
452- raw_request = message ,
453- raw_response = raw_response ,
454- url = url
455430
456431 raise AdyenInvalidRequestError (errorstring )
457432
@@ -568,6 +543,6 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
568543
569544 def _error_from_hpp (self , html ):
570545 # Must be updated when Adyen response is changed:
571- match_obj = re .search ('>Error:\s*(.*?)<br' , html )
546+ match_obj = re .search (r '>Error:\s*(.*?)<br' , html )
572547 if match_obj :
573548 return match_obj .group (1 )
0 commit comments