Skip to content

Commit 72c24d8

Browse files
authored
PW-781: Code optimizations (#53)
* PW-781: Code optimizations: raise an exception if response contains errorCode, removed redundant status_code 422, always return an error_code along with a message in AdyenExceptions closes #47, closes #48, closes #49 * pycodestyle fix * pycodestyle fix
1 parent 1dcbde4 commit 72c24d8

File tree

5 files changed

+62
-55
lines changed

5 files changed

+62
-55
lines changed

Adyen/client.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class AdyenResult(object):
2626
status_code (int, optional): Default 200. HTTP response code, ie 200,
2727
404, 500, etc.
2828
psp (str, optional): Psp reference returned by Adyen for a payment.
29-
raw_request (str, optionl): Raw request placed to Adyen.
29+
raw_request (str, optional): Raw request placed to Adyen.
3030
raw_response (str, optional): Raw response returned by Adyen.
3131
3232
"""
@@ -401,7 +401,20 @@ def _handle_response(self, url, raw_response, raw_request,
401401

402402
try:
403403
if response['errorCode']:
404-
return raw_response
404+
raise AdyenAPICommunicationError(
405+
"Unexpected error while communicating with Adyen."
406+
" Received the response data:'{}', HTTP Code:'{}'. "
407+
"Please reach out to support@adyen.com if the "
408+
"problem persists with the psp:{}"
409+
.format(raw_response, status_code,
410+
headers.get('pspReference')),
411+
status_code=status_code,
412+
raw_request=raw_request,
413+
raw_response=raw_response,
414+
url=url,
415+
psp=headers.get('pspReference'),
416+
headers=headers,
417+
error_code=response['errorCode'])
405418
except KeyError:
406419
erstr = 'KeyError: errorCode'
407420
raise AdyenAPICommunicationError(erstr)
@@ -454,7 +467,9 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
454467
erstr = "Received a 404 for url:'{}'. Please ensure that" \
455468
" the custom merchant specific url is correct" \
456469
.format(url)
457-
raise AdyenAPICommunicationError(erstr)
470+
raise AdyenAPICommunicationError(erstr,
471+
error_code=response_obj.get(
472+
"errorCode"))
458473
else:
459474
erstr = "Unexpected error while communicating with Adyen." \
460475
" Please reach out to support@adyen.com" \
@@ -464,8 +479,10 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
464479
raw_response=raw_response,
465480
url=url,
466481
psp=psp_ref,
467-
headers=headers)
468-
elif status_code in [400, 422]:
482+
headers=headers,
483+
error_code=response_obj.get(
484+
"errorCode"))
485+
elif status_code == 400:
469486
erstr = "Received validation error with errorCode: %s," \
470487
" message: %s, HTTP Code: %s. Please verify" \
471488
" the values provided. Please reach out" \
@@ -474,13 +491,16 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
474491
response_obj["errorCode"], response_obj["message"],
475492
status_code, psp_ref)
476493

477-
raise AdyenAPIValidationError(erstr)
494+
raise AdyenAPIValidationError(erstr, error_code=response_obj.get(
495+
"errorCode"))
478496
elif status_code == 401:
479497
erstr = "Unable to authenticate with Adyen's Servers." \
480498
" Please verify the credentials set with the Adyen base" \
481499
" class. Please reach out to your Adyen Admin" \
482500
" if the problem persists"
483-
raise AdyenAPIAuthenticationError(erstr)
501+
raise AdyenAPIAuthenticationError(erstr,
502+
error_code=response_obj.get(
503+
"errorCode"))
484504
elif status_code == 403:
485505

486506
if response_obj.get("message") == "Invalid Merchant Account":
@@ -490,26 +510,25 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
490510
"Reach out to support@adyen.com"
491511
" if the issue persists") \
492512
% raw_request['merchantAccount']
493-
raise AdyenAPIInvalidPermission(erstr)
513+
raise AdyenAPIInvalidPermission(erstr,
514+
error_code=response_obj.get(
515+
"errorCode"))
494516

495517
erstr = "Unable to perform the requested action. message: %s." \
496518
" If you think your webservice user: %s might not have" \
497519
" the necessary permissions to perform this request." \
498520
" Please reach out to support@adyen.com, providing" \
499521
" the PSP reference: %s" % (
500522
response_obj["message"], self.username, psp_ref)
501-
502-
raise AdyenAPIInvalidPermission(erstr, self.username, psp_ref,
503-
raw_request=raw_request,
504-
raw_response=raw_response, url=url,
505-
psp=psp_ref, headers=headers)
523+
raise AdyenAPIInvalidPermission(erstr, error_code=response_obj.get(
524+
"errorCode"))
506525
elif status_code == 422:
507526
if response_obj.get("message") == "Invalid amount specified":
508527
raise AdyenAPIInvalidAmount(
509528
"Invalid amount specified"
510529
"Amount may be improperly formatted, too small or too big."
511-
"If the issue persists, contact support@adyen.com"
512-
)
530+
"If the issue persists, contact support@adyen.com",
531+
error_code=response_obj.get("errorCode"))
513532

514533
elif status_code == 500:
515534
if response_obj.get("errorType") == "validation":
@@ -519,14 +538,17 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
519538
erstr = "Received validation error with errorCode: %s," \
520539
" message: %s, HTTP Code: %s. Please verify" \
521540
" the values provided." % err_args
522-
raise AdyenAPIValidationError(erstr)
541+
raise AdyenAPIValidationError(erstr,
542+
error_code=response_obj.get(
543+
"errorCode"))
523544

524545
if response_obj.get("message") == "Failed to serialize node " \
525546
"Failed to parse [123.34]" \
526547
" as a Long":
527548
raise AdyenAPIInvalidFormat(
528-
"The paymount amount must be set in cents,"
529-
" and can not contain commas or points."
549+
"The payment amount must be set in cents,"
550+
" and can not contain commas or points.",
551+
error_code=response_obj.get("errorCode")
530552
)
531553
else:
532554
raise AdyenAPICommunicationError(
@@ -539,7 +561,7 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
539561
raw_response=raw_response,
540562
url=url,
541563
psp=psp_ref,
542-
headers=headers)
564+
headers=headers, error_code=response_obj.get("errorCode"))
543565

544566
def _error_from_hpp(self, html):
545567
# Must be updated when Adyen response is changed:

Adyen/exceptions.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ def __init__(self,
99
url="",
1010
psp="",
1111
headers="",
12-
status_code=""):
12+
status_code="",
13+
error_code=""):
1314
self.message = message
1415
self.raw_request = raw_request
1516
self.raw_response = raw_response
1617
self.url = url
1718
self.psp = psp
1819
self.headers = headers
1920
self.status_code = status_code
21+
self.error_code = error_code
2022

2123
def __str__(self):
2224
return repr("{}:{}".format(self.__class__.__name__, self.message))
@@ -42,13 +44,9 @@ class AdyenInvalidRequestError(AdyenError):
4244
class AdyenAPIResponseError(AdyenError):
4345
def __init__(self,
4446
message,
45-
result="",
46-
error_code="",
4747
*args,
4848
**kwargs):
4949
super(AdyenAPIResponseError, self).__init__(message, *args, **kwargs)
50-
self.error_code = error_code
51-
self.result = result
5250

5351

5452
class AdyenAPIAuthenticationError(AdyenAPIResponseError):

test/ModificationTest.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,12 @@ def test_capture_error_167(self):
3636
'test/mocks/'
3737
'capture-error-167'
3838
'.json')
39-
self.assertRaisesRegexp(Adyen.AdyenAPIValidationError,
40-
"Received validation error with errorCode:"
41-
" 167, message: Original pspReference required"
42-
" for this operation, HTTP Code: 422." +
43-
" Please verify the values provided. Please "
44-
"reach out to support@adyen.com if the problem"
45-
" persists, providing the PSP reference.*",
46-
self.ady.payment.capture, request)
39+
self.assertRaisesRegexp(
40+
Adyen.AdyenAPICommunicationError,
41+
"Unexpected error",
42+
self.ady.payment.capture,
43+
request
44+
)
4745

4846
def test_cancel_or_refund_received(self):
4947
request = {}

test/RecurringTest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ def test_disable_803(self):
6060
'recurring/'
6161
'disable-error-803'
6262
'.json')
63-
self.assertRaisesRegexp(Adyen.AdyenAPIValidationError,
64-
"Received validation error with errorCode: "
65-
"803, message: PaymentDetail not found, "
66-
"HTTP Code: 422.*",
67-
self.ady.recurring.disable, request)
63+
self.assertRaisesRegexp(
64+
Adyen.AdyenAPICommunicationError,
65+
"Unexpected error",
66+
self.ady.recurring.disable,
67+
request
68+
)
6869

6970

7071
TestRecurring.client.http_force = "requests"

test/ThirdPartyPayoutTest.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,8 @@ def test_submit_invalid_recurring_reference(self):
132132
resp = 'test/mocks/payout/submit-invalid-reference.json'
133133
self.ady.client = self.test.create_client_from_file(422, request, resp)
134134
self.assertRaisesRegexp(
135-
Adyen.AdyenAPIValidationError,
136-
"Received validation error with errorCode: 800,"
137-
" message: Contract not found, HTTP Code: 422."
138-
" Please verify the values provided."
139-
" Please reach out to support@adyen.com"
140-
" if the problem persists, providing the PSP reference.*",
135+
Adyen.AdyenAPICommunicationError,
136+
"Unexpected error",
141137
self.ady.payout.submit,
142138
request
143139
)
@@ -183,12 +179,8 @@ def test_store_detail_and_submit_missing_payment(self):
183179
resp = 'test/mocks/payout/storeDetailAndSubmit-missing-payment.json'
184180
self.ady.client = self.test.create_client_from_file(422, request, resp)
185181
self.assertRaisesRegexp(
186-
Adyen.AdyenAPIValidationError,
187-
"Received validation error with errorCode: 000,"
188-
" message: Please supply paymentDetails, HTTP Code: 422."
189-
" Please verify the values provided."
190-
" Please reach out to support@adyen.com"
191-
" if the problem persists, providing the PSP reference:.*",
182+
Adyen.AdyenAPICommunicationError,
183+
"Unexpected error",
192184
self.ady.payout.store_detail_and_submit,
193185
request
194186
)
@@ -215,12 +207,8 @@ def test_store_detail_and_submit_invalid_iban(self):
215207
resp = 'test/mocks/payout/storeDetailAndSubmit-invalid-iban.json'
216208
self.ady.client = self.test.create_client_from_file(422, request, resp)
217209
self.assertRaisesRegexp(
218-
Adyen.AdyenAPIValidationError,
219-
"Received validation error with errorCode: 161,"
220-
" message: Invalid iban, HTTP Code: 422."
221-
" Please verify the values provided."
222-
" Please reach out to support@adyen.com"
223-
" if the problem persists, providing the PSP reference:.*",
210+
Adyen.AdyenAPICommunicationError,
211+
"Unexpected error",
224212
self.ady.payout.store_detail_and_submit,
225213
request
226214
)

0 commit comments

Comments
 (0)