11#!/bin/python
2+
3+ from __future__ import absolute_import , division , print_function , unicode_literals
4+
25import json as json_lib
36import re
47
1518
1619import datetime
1720from datetime import timedelta
18- import logging
19- from adyen_log import logname ,getlogger
20- logger = logging .getLogger (logname ())
21-
2221
2322HMAC_TEST_url = "https://ca-test.adyen.com/ca/ca/skin/checkhmac.shtml"
2423
@@ -77,7 +76,7 @@ class AdyenClient(object):
7776 def __init__ (self , username = None , password = None , review_payout_username = None ,
7877 review_payout_password = None , store_payout_username = None ,
7978 store_payout_password = None , platform = None ,
80- merchant_account = None , merchant_specific_url = None , skin_code = None , hmac = None ,app_name = None ,create_log = None ):
79+ merchant_account = None , merchant_specific_url = None , skin_code = None , hmac = None , app_name = None , http_force = None ):
8180 self .username = username
8281 self .password = password
8382 self .review_payout_username = review_payout_username
@@ -91,10 +90,10 @@ def __init__(self, username=None, password=None, review_payout_username=None,
9190 self .skin_code = skin_code
9291 self .psp_list = []
9392 self .app_name = app_name
94- self .create_log = create_log
95- self .LIB_VERSION = "1.0.0" ;
93+ self .LIB_VERSION = "1.1.0" ;
9694 self .USER_AGENT_SUFFIX = "adyen-python-api-library/" ;
9795 self .http_init = False
96+ self .http_force = http_force
9897
9998 def _determine_api_url (self , platform , service , action ):
10099 """This returns the Adyen API endpoint based on the provided platform,
@@ -123,7 +122,7 @@ def _determine_hpp_url(self, platform, action):
123122 return result
124123
125124 def _review_payout_username (self ,** kwargs ):
126- from Adyen import review_payout_username
125+ from . import review_payout_username
127126 if 'username' in kwargs :
128127 review_payout_username = kwargs ['username' ]
129128 elif self .review_payout_username :
@@ -132,13 +131,12 @@ def _review_payout_username(self,**kwargs):
132131 errorstring = """AdyenInvalidRequestError: Please set your review payout
133132 webservice username. You can do this by running
134133 'Adyen.review_payout_username = 'Your payout username' """
135- # logger.error(errorstring)
136134 raise AdyenInvalidRequestError (errorstring )
137135
138136 return review_payout_username
139137
140138 def _review_payout_pass (self ,** kwargs ):
141- from Adyen import review_payout_password
139+ from . import review_payout_password
142140 if 'password' in kwargs :
143141 review_payout_password = kwargs ["password" ]
144142 elif self .review_payout_password :
@@ -147,13 +145,12 @@ def _review_payout_pass(self,**kwargs):
147145 errorstring = """AdyenInvalidRequestError: Please set your review payout
148146 webservice password. You can do this by running
149147 'Adyen.review_payout_password = 'Your payout password'"""
150- # logger.error(errorstring)
151148 raise AdyenInvalidRequestError (errorstring )
152149
153150 return review_payout_password
154151
155152 def _store_payout_username (self ,** kwargs ):
156- from Adyen import store_payout_username
153+ from . import store_payout_username
157154 if 'username' in kwargs :
158155 store_payout_username = kwargs ['username' ]
159156 elif self .store_payout_username :
@@ -162,13 +159,12 @@ def _store_payout_username(self,**kwargs):
162159 errorstring = """AdyenInvalidRequestError: Please set your store payout
163160 webservice username. You can do this by running
164161 'Adyen.store_payout_username = 'Your payout username'"""
165- # logger.error(errorstring)
166162 raise AdyenInvalidRequestError (errorstring )
167163
168164 return store_payout_username
169165
170166 def _store_payout_pass (self ,** kwargs ):
171- from Adyen import store_payout_password
167+ from . import store_payout_password
172168 if 'password' in kwargs :
173169 store_payout_password = kwargs ["password" ]
174170 elif self .store_payout_password :
@@ -177,7 +173,6 @@ def _store_payout_pass(self,**kwargs):
177173 errorstring = """AdyenInvalidRequestError: Please set your store payout
178174 webservice password. You can do this by running
179175 'Adyen.store_payout_password = 'Your payout password'"""
180- # logger.error(errorstring)
181176 raise AdyenInvalidRequestError (errorstring )
182177
183178 return store_payout_password
@@ -202,10 +197,10 @@ def call_api(self, request_data, service, action, idempotency=False,
202197 AdyenResult: The AdyenResult is returned when a request was
203198 succesful.
204199 """
205- from Adyen import username , password , merchant_account , platform
206-
200+ from . import username , password , merchant_account , platform
201+ #
207202 if self .http_init == False :
208- self .http_client = HTTPClient (self .app_name ,self .LIB_VERSION ,self .USER_AGENT_SUFFIX )
203+ self .http_client = HTTPClient (self .app_name ,self .USER_AGENT_SUFFIX , self . LIB_VERSION ,self .http_force )
209204 self .http_init = True
210205
211206 #username at self object has highest priority. fallback to root module
@@ -222,7 +217,6 @@ def call_api(self, request_data, service, action, idempotency=False,
222217 if not username :
223218 errorstring = """AdyenInvalidRequestError: Please set your webservice username."
224219 You can do this by running 'Adyen.username = 'Your username'"""
225- # logger.error(errorstring)
226220 raise AdyenInvalidRequestError (errorstring )
227221 #Ensure that username has been removed so as not to be passed to adyen.
228222 if 'username' in kwargs :
@@ -242,7 +236,6 @@ def call_api(self, request_data, service, action, idempotency=False,
242236 if not password :
243237 errorstring = """AdyenInvalidRequestError: Please set your webservice password.
244238 You can do this by running 'Adyen.password = 'Your password'"""
245- # logger.error(errorstring)
246239 raise AdyenInvalidRequestError (errorstring )
247240 #Ensure that password has been removed so as not to be passed to adyen.
248241 if 'password' in kwargs :
@@ -306,10 +299,10 @@ def call_hpp(self, message, action, hmac_key="", **kwargs):
306299 AdyenResult: The AdyenResult is returned when a request was
307300 succesful.
308301 """
309- from Adyen import hmac , platform
310-
302+ from . import hmac , platform
303+ #
311304 if self .http_init == False :
312- self .http_client = HTTPClient (self .app_name ,self .LIB_VERSION ,self .USER_AGENT_SUFFIX )
305+ self .http_client = HTTPClient (self .app_name ,self .USER_AGENT_SUFFIX , self . LIB_VERSION ,self .http_force )
313306 self .http_init = True
314307
315308 #hmac provided in function has highest priority. fallback to self then
@@ -324,7 +317,6 @@ def call_hpp(self, message, action, hmac_key="", **kwargs):
324317 parameter in the function call ie.
325318 'Adyen.hpp.directory_lookup(hmac=\" !WR#F@...\" '. Please reach
326319 out to support@Adyen.com if the issue persists."""
327- # logger.error(errorstring)
328320 raise AdyenInvalidRequestError (errorstring )
329321
330322 #platform provided in self has highest priority, fallback to root module
@@ -333,11 +325,9 @@ def call_hpp(self, message, action, hmac_key="", **kwargs):
333325 platform = self .platform
334326 if platform .lower () not in ['live' ,'test' ]:
335327 errorstring = " 'platform' must be the value of 'live' or 'test' "
336- # logger.error(errorstring)
337328 raise ValueError (errorstring )
338329 elif not isinstance (platform , str ):
339330 errorstring = "'platform' must be type string"
340- # logger.error(errorstring)
341331 raise TypeError (errorstring )
342332
343333 if 'skinCode' not in message :
@@ -362,21 +352,19 @@ def call_hpp(self, message, action, hmac_key="", **kwargs):
362352
363353 def hpp_payment (self ,request_data , action , hmac_key = "" , ** kwargs ):
364354
365- from Adyen import hmac , platform
366-
355+ from . import hmac , platform
356+ #
367357 if self .http_init == False :
368- self .http_client = HTTPClient (self .app_name ,self .LIB_VERSION ,self .USER_AGENT_SUFFIX )
358+ self .http_client = HTTPClient (self .app_name ,self .USER_AGENT_SUFFIX , self . LIB_VERSION ,self .http_force )
369359 self .http_init = True
370360
371361 if self .platform :
372362 platform = self .platform
373363 if platform .lower () not in ['live' ,'test' ]:
374364 errorstring = " 'platform' must be the value of 'live' or 'test' "
375- # logger.error(errorstring)
376365 raise ValueError (errorstring )
377366 elif not isinstance (platform , str ):
378367 errorstring = "'platform' must be type string"
379- # logger.error(errorstring)
380368 raise TypeError (errorstring )
381369
382370 if 'skinCode' not in request_data :
@@ -421,25 +409,18 @@ def _handle_response(self, url, raw_response, raw_request, status_code, headers,
421409 response = {}
422410 # If the result can't be parsed into json, most likely is raw html.
423411 # Some response are neither json or raw html, handle them here:
424- try :
425- response = json_lib .loads (raw_result )
426-
427- self ._handle_http_error (url , response , status_code ,
428- headers .get ('pspReference' ), raw_request , raw_response , headers )
429- except :
430412
431- response = json_lib .loads (raw_response )
413+ response = json_lib .loads (raw_response )
432414
433- # Pass raised error to error handler.
434- self ._handle_http_error (url ,response ,status_code ,headers .get ('pspReference' ),raw_request ,raw_response ,headers ,request_dict )
415+ # Pass raised error to error handler.
416+ self ._handle_http_error (url ,response ,status_code ,headers .get ('pspReference' ),raw_request ,raw_response ,headers ,request_dict )
435417
436- try :
437- if response ['errorCode' ]:
438- return raw_response
439- except KeyError :
440- errstr = 'KeyError: errorCode'
441- # logger.error('Key Error: errorCode')
442- pass
418+ try :
419+ if response ['errorCode' ]:
420+ return raw_response
421+ except KeyError :
422+ errstr = 'KeyError: errorCode'
423+ pass
443424 else :
444425 try :
445426 response = json_lib .loads (raw_response )
@@ -465,8 +446,6 @@ def _handle_response(self, url, raw_response, raw_request, status_code, headers,
465446 raw_response = raw_response ,
466447 url = url
467448
468- # logger.error(errorstring)
469-
470449 raise AdyenInvalidRequestError (errorstring )
471450
472451 def _handle_http_error (self , url , response_obj , status_code , psp_ref ,
@@ -489,14 +468,12 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
489468 """
490469
491470 if status_code == 404 :
492- from Adyen import merchant_specific_url
471+ from . import merchant_specific_url
493472 if url == merchant_specific_url :
494473 erstr = "Received a 404 for url:'{}'. Please ensure that the custom merchant specific url is correct" .format (url )
495- # logger.error(erstr)
496474 raise AdyenAPICommunicationError (erstr )
497475 else :
498476 erstr = "Unexpected error while communicating with Adyen. Please reach out to support@adyen.com if the problem persists"
499- # logger.error(erstr)
500477 raise AdyenAPICommunicationError (erstr ,
501478 raw_request = raw_request ,
502479 raw_response = raw_response ,
@@ -506,26 +483,22 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
506483 elif status_code in [400 , 422 ]:
507484 erstr = "Received validation error with errorCode: %s, message: %s, HTTP Code: %s. Please verify the values provided. Please reach out to support@adyen.com if the problem persists, providing the PSP reference: %s" % (response_obj ["errorCode" ],response_obj ["message" ], status_code , psp_ref )
508485
509- # logger.error(erstr)
510-
511486 raise ValueError (erstr )
512487 elif status_code == 401 :
513488 erstr = "Unable to authenticate with Adyen's Servers. Please verify the credentials set with the Adyen base class. Please reach out to your Adyen Admin if the problem persists"
514- # logger.error(erstr)
515489 raise ValueError (erstr )
516490 elif status_code == 403 :
517- from Adyen import username
491+ from . import username
518492
519493 ma = raw_request ['merchantAccount' ]
520494
521495 if response_obj .get ("message" )== "Invalid Merchant Account" :
522496 erstr = ("You provided the merchant account:'%s' that doesn't exist or you don't have access to it.\n "
523497 "Please verify the merchant account provided. \n "
524498 "Reach out to support@adyen.com if the issue persists" ) % raw_request ['merchantAccount' ]
525- # logger.error(erstr)
526499 raise AdyenAPIInvalidPermission (erstr )
527500
528- erstr = "Unable to perform the requested action. message: %s. If you think your webservice user: %s might not have the necessary permissions to perform this request. Please reach out to support@adyen.com, providing the PSP reference: %s" % response_obj ["message" ],self .username ,psp_ref
501+ erstr = "Unable to perform the requested action. message: %s. If you think your webservice user: %s might not have the necessary permissions to perform this request. Please reach out to support@adyen.com, providing the PSP reference: %s" % ( response_obj ["message" ],self .username ,psp_ref )
529502
530503 raise AdyenAPIInvalidPermission (erstr ,username ,psp_ref ,raw_request = raw_request ,raw_response = raw_response ,url = url ,psp = psp_ref ,headers = headers )
531504 elif status_code == 422 :
0 commit comments