1616 AdyenAPIUnprocessableEntity ,
1717 AdyenEndpointInvalidFormat )
1818from . import settings
19- from re import match
19+ import re
2020
2121
2222class AdyenResult (object ):
@@ -84,7 +84,6 @@ def __init__(
8484 live_endpoint_prefix = None ,
8585 http_timeout = 30 ,
8686 api_bin_lookup_version = None ,
87- api_checkout_utility_version = None ,
8887 api_checkout_version = None ,
8988 api_management_version = None ,
9089 api_payment_version = None ,
@@ -96,6 +95,7 @@ def __init__(
9695 api_transfers_version = None ,
9796 api_stored_value_version = None ,
9897 api_balance_platform_version = None ,
98+
9999 ):
100100 self .username = username
101101 self .password = password
@@ -115,130 +115,41 @@ def __init__(
115115 self .http_force = http_force
116116 self .live_endpoint_prefix = live_endpoint_prefix
117117 self .http_timeout = http_timeout
118- self .api_bin_lookup_version = api_bin_lookup_version or settings .API_BIN_LOOKUP_VERSION
119- self .api_checkout_utility_version = api_checkout_utility_version or settings .API_CHECKOUT_UTILITY_VERSION
120- self .api_checkout_version = api_checkout_version or settings .API_CHECKOUT_VERSION
121- self .api_management_version = api_management_version or settings .API_MANAGEMENT_VERSION
122- self .api_payment_version = api_payment_version or settings .API_PAYMENT_VERSION
123- self .api_payout_version = api_payout_version or settings .API_PAYOUT_VERSION
124- self .api_recurring_version = api_recurring_version or settings .API_RECURRING_VERSION
125- self .api_terminal_version = api_terminal_version or settings .API_TERMINAL_VERSION
126- self .api_legal_entity_management_version = api_legal_entity_management_version or settings .API_LEGAL_ENTITY_MANAGEMENT_VERSION
127- self .api_data_protection_version = api_data_protection_version or settings .API_DATA_PROTECION_VERSION
128- self .api_transfers_version = api_transfers_version or settings .API_TRANSFERS_VERSION
129- self .api_stored_value_version = api_stored_value_version or settings .API_STORED_VALUE_VERSION
130- self .api_balance_platform_version = api_balance_platform_version or settings .API_BALANCE_PLATFORM_VERSION
131-
132- def _determine_base_url_and_version (self , platform , service ):
133-
134- live_pal_url = settings .PAL_LIVE_ENDPOINT_URL_TEMPLATE
135- live_checkout_url = settings .ENDPOINT_CHECKOUT_LIVE_SUFFIX
136-
137- if platform == 'live' and self .live_endpoint_prefix :
138- live_pal_url = live_pal_url .format (live_prefix = self .live_endpoint_prefix )
139- live_checkout_url = live_checkout_url .format (live_prefix = self .live_endpoint_prefix )
140-
141- versions_and_urls = {
142- 'recurring' : {
143- 'version' : self .api_recurring_version ,
144- 'base_url' : {
145- 'live' : live_pal_url + '/Recurring' ,
146- 'test' : settings .PAL_TEST_URL + '/Recurring' ,
147- }
148- },
149- 'payouts' : {
150- 'version' : self .api_payout_version ,
151- 'base_url' : {
152- 'live' : live_pal_url + '/Payout' ,
153- 'test' : settings .PAL_TEST_URL + '/Payout'
154- }
155- },
156- 'binlookup' : {
157- 'version' : self .api_bin_lookup_version ,
158- 'base_url' : {
159- 'live' : live_pal_url + '/BinLookup' ,
160- 'test' : settings .PAL_TEST_URL + '/BinLookup'
161- }
162- },
163- 'terminal' : {
164- 'version' : self .api_terminal_version ,
165- 'base_url' : {
166- 'live' : settings .BASE_TERMINAL_URL .format (platform ),
167- 'test' : settings .BASE_TERMINAL_URL .format (platform )
168- }
169- },
170- 'payments' : {
171- 'version' : self .api_payment_version ,
172- 'base_url' : {
173- 'live' : live_pal_url + '/Payment' ,
174- 'test' : settings .PAL_TEST_URL + '/Payment'
175- }
176- },
177- 'checkout' : {
178- 'version' : self .api_checkout_version ,
179- 'base_url' : {
180- 'live' : live_checkout_url ,
181- 'test' : settings .ENDPOINT_CHECKOUT_TEST
182- }
183- },
184- 'management' : {
185- 'version' : self .api_management_version ,
186- 'base_url' : {
187- 'live' : settings .BASE_MANAGEMENT_URL .format (platform ),
188- 'test' : settings .BASE_MANAGEMENT_URL .format (platform )
189- }
190- },
191- 'legalEntityManagement' : {
192- 'version' : self .api_legal_entity_management_version ,
193- 'base_url' : {
194- 'live' : settings .BASE_LEGAL_ENTITY_MANAGEMENT_URL .format (platform ),
195- 'test' : settings .BASE_LEGAL_ENTITY_MANAGEMENT_URL .format (platform )
196- },
197- },
198- 'balancePlatform' : {
199- 'version' : self .api_balance_platform_version ,
200- 'base_url' : {
201- 'live' : settings .BASE_CONFIGURATION_URL .format (platform ),
202- 'test' : settings .BASE_CONFIGURATION_URL .format (platform )
203- }
204- },
205- 'dataProtection' : {
206- 'version' : self .api_data_protection_version ,
207- 'base_url' : {
208- 'live' : settings .BASE_DATA_PROTECION_URL .format (platform ),
209- 'test' : settings .BASE_DATA_PROTECION_URL .format (platform )
210- }
211- },
212- 'transfers' : {
213- 'version' : self .api_transfers_version ,
214- 'base_url' : {
215- 'live' : settings .BASE_BTL_URL .format (platform ),
216- 'test' : settings .BASE_BTL_URL .format (platform )
217- }
218- },
219- 'storedValue' : {
220- 'version' : self .api_stored_value_version ,
221- 'base_url' : {
222- 'live' : settings .BASE_STORED_VALUE_URL .format (platform ),
223- 'test' : settings .BASE_STORED_VALUE_URL .format (platform )
224- }
225- },
226- }
227-
228- version = versions_and_urls [service ]['version' ]
229- base_url = versions_and_urls [service ]['base_url' ][platform ]
230- # Match urls that require a live prefix and do not have one
231-
232- if platform == 'live' and '{live_prefix}' in base_url :
233- errorstring = "Please set your live suffix. You can set it by running " \
234- "adyen.client.live_endpoint_prefix = 'Your live suffix'"
235- raise AdyenEndpointInvalidFormat (errorstring )
236-
237- return version , base_url
238-
239- def _determine_api_url (self , platform , service , endpoint ):
240- api_version , base_url = self ._determine_base_url_and_version (platform , service )
241- return base_url + '/' + api_version + endpoint
118+ self .api_bin_lookup_version = api_bin_lookup_version
119+ self .api_checkout_version = api_checkout_version
120+ self .api_management_version = api_management_version
121+ self .api_payment_version = api_payment_version
122+ self .api_payout_version = api_payout_version
123+ self .api_recurring_version = api_recurring_version
124+ self .api_terminal_version = api_terminal_version
125+ self .api_legal_entity_management_version = api_legal_entity_management_version
126+ self .api_data_protection_version = api_data_protection_version
127+ self .api_transfers_version = api_transfers_version
128+ self .api_stored_value_version = api_stored_value_version
129+ self .api_balance_platform_version = api_balance_platform_version
130+
131+ def _determine_api_url (self , platform , endpoint ):
132+ if platform == "test" :
133+ return endpoint
134+
135+ if "pal-" in endpoint :
136+ if self .live_endpoint_prefix is None :
137+ error_string = "Please set your live suffix. You can set it by running " \
138+ "adyen.client.live_endpoint_prefix = 'Your live suffix'"
139+ raise AdyenEndpointInvalidFormat (error_string )
140+ endpoint = endpoint .replace ("https://pal-test.adyen.com/pal/servlet/" ,
141+ "https://" + self .live_endpoint_prefix + "-pal-live.adyenpayments.com/pal/servlet/" )
142+ elif "checkout-" in endpoint :
143+ if self .live_endpoint_prefix is None :
144+ error_string = "Please set your live suffix. You can set it by running " \
145+ "adyen.client.live_endpoint_prefix = 'Your live suffix'"
146+ raise AdyenEndpointInvalidFormat (error_string )
147+ endpoint = endpoint .replace ("https://checkout-test.adyen.com/" ,
148+ "https://" + self .live_endpoint_prefix + "-checkout-live.adyenpayments.com/checkout/" )
149+
150+ endpoint = endpoint .replace ("-test" , "-live" )
151+
152+ return endpoint
242153
243154 def _review_payout_username (self , ** kwargs ):
244155 if 'username' in kwargs :
@@ -351,6 +262,24 @@ def _set_platform(self, **kwargs):
351262
352263 return platform
353264
265+ def _set_url_version (self , service , endpoint ):
266+ version_lookup = {"binlookup" : self .api_bin_lookup_version ,
267+ "checkout" : self .api_checkout_version ,
268+ "management" : self .api_management_version ,
269+ "payments" : self .api_payment_version ,
270+ "payouts" : self .api_payout_version ,
271+ "recurring" : self .api_recurring_version ,
272+ "terminal" : self .api_terminal_version ,
273+ "legalEntityManagement" : self .api_legal_entity_management_version ,
274+ "dataProtection" : self .api_data_protection_version ,
275+ "transfers" : self .api_transfers_version ,
276+ "storedValue" : self .api_stored_value_version ,
277+ "balancePlatform" : self .api_balance_platform_version }
278+
279+ new_version = f"v{ version_lookup [service ]} "
280+ endpoint = re .sub (r'\.com/v\d{1,2}' , f".com/{ new_version } " , endpoint )
281+ return endpoint
282+
354283 def call_adyen_api (
355284 self ,
356285 request_data ,
@@ -383,10 +312,25 @@ def call_adyen_api(
383312 self ._init_http_client ()
384313
385314 # Set credentials
386- xapikey , username , password , kwargs = self ._set_credentials (service , endpoint , ** kwargs )
315+ xapikey , username , password , kwargs = self ._set_credentials (service , endpoint , ** kwargs )
387316 # Set platform
388317 platform = self ._set_platform (** kwargs )
389318 message = request_data
319+ # Set version (if not default one)
320+ versions = [self .api_bin_lookup_version ,
321+ self .api_checkout_version ,
322+ self .api_management_version ,
323+ self .api_payment_version ,
324+ self .api_payout_version ,
325+ self .api_recurring_version ,
326+ self .api_terminal_version ,
327+ self .api_legal_entity_management_version ,
328+ self .api_data_protection_version ,
329+ self .api_transfers_version ,
330+ self .api_stored_value_version ,
331+ self .api_balance_platform_version ]
332+ if any (versions ):
333+ endpoint = self ._set_url_version (service , endpoint )
390334
391335 headers = {
392336 self .APPLICATION_INFO_HEADER_NAME : settings .LIB_NAME ,
@@ -398,7 +342,7 @@ def call_adyen_api(
398342 if idempotency_key :
399343 headers [self .IDEMPOTENCY_HEADER_NAME ] = idempotency_key
400344
401- url = self ._determine_api_url (platform , service , endpoint )
345+ url = self ._determine_api_url (platform , endpoint )
402346
403347 if 'query_parameters' in kwargs :
404348 url = url + util .get_query (kwargs ['query_parameters' ])
0 commit comments