Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions Adyen/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AdyenResult(object):
status_code (int, optional): Default 200. HTTP response code, ie 200,
404, 500, etc.
psp (str, optional): Psp reference returned by Adyen for a payment.
raw_request (str, optionl): Raw request placed to Adyen.
raw_request (str, optional): Raw request placed to Adyen.
raw_response (str, optional): Raw response returned by Adyen.

"""
Expand Down Expand Up @@ -401,7 +401,20 @@ def _handle_response(self, url, raw_response, raw_request,

try:
if response['errorCode']:
return raw_response
raise AdyenAPICommunicationError(
"Unexpected error while communicating with Adyen."
" Received the response data:'{}', HTTP Code:'{}'. "
"Please reach out to support@adyen.com if the "
"problem persists with the psp:{}"
.format(raw_response, status_code,
headers.get('pspReference')),
status_code=status_code,
raw_request=raw_request,
raw_response=raw_response,
url=url,
psp=headers.get('pspReference'),
headers=headers,
error_code=response['errorCode'])
except KeyError:
erstr = 'KeyError: errorCode'
raise AdyenAPICommunicationError(erstr)
Expand Down Expand Up @@ -454,7 +467,9 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
erstr = "Received a 404 for url:'{}'. Please ensure that" \
" the custom merchant specific url is correct" \
.format(url)
raise AdyenAPICommunicationError(erstr)
raise AdyenAPICommunicationError(erstr,
error_code=response_obj.get(
"errorCode"))
else:
erstr = "Unexpected error while communicating with Adyen." \
" Please reach out to support@adyen.com" \
Expand All @@ -464,8 +479,10 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
raw_response=raw_response,
url=url,
psp=psp_ref,
headers=headers)
elif status_code in [400, 422]:
headers=headers,
error_code=response_obj.get(
"errorCode"))
elif status_code == 400:
erstr = "Received validation error with errorCode: %s," \
" message: %s, HTTP Code: %s. Please verify" \
" the values provided. Please reach out" \
Expand All @@ -474,13 +491,16 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
response_obj["errorCode"], response_obj["message"],
status_code, psp_ref)

raise AdyenAPIValidationError(erstr)
raise AdyenAPIValidationError(erstr, error_code=response_obj.get(
"errorCode"))
elif status_code == 401:
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"
raise AdyenAPIAuthenticationError(erstr)
raise AdyenAPIAuthenticationError(erstr,
error_code=response_obj.get(
"errorCode"))
elif status_code == 403:

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

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)

raise AdyenAPIInvalidPermission(erstr, self.username, psp_ref,
raw_request=raw_request,
raw_response=raw_response, url=url,
psp=psp_ref, headers=headers)
raise AdyenAPIInvalidPermission(erstr, error_code=response_obj.get(
"errorCode"))
elif status_code == 422:
if response_obj.get("message") == "Invalid amount specified":
raise AdyenAPIInvalidAmount(
"Invalid amount specified"
"Amount may be improperly formatted, too small or too big."
"If the issue persists, contact support@adyen.com"
)
"If the issue persists, contact support@adyen.com",
error_code=response_obj.get("errorCode"))

elif status_code == 500:
if response_obj.get("errorType") == "validation":
Expand All @@ -519,14 +538,17 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
erstr = "Received validation error with errorCode: %s," \
" message: %s, HTTP Code: %s. Please verify" \
" the values provided." % err_args
raise AdyenAPIValidationError(erstr)
raise AdyenAPIValidationError(erstr,
error_code=response_obj.get(
"errorCode"))

if response_obj.get("message") == "Failed to serialize node " \
"Failed to parse [123.34]" \
" as a Long":
raise AdyenAPIInvalidFormat(
"The paymount amount must be set in cents,"
" and can not contain commas or points."
"The payment amount must be set in cents,"
" and can not contain commas or points.",
error_code=response_obj.get("errorCode")
)
else:
raise AdyenAPICommunicationError(
Expand All @@ -539,7 +561,7 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref,
raw_response=raw_response,
url=url,
psp=psp_ref,
headers=headers)
headers=headers, error_code=response_obj.get("errorCode"))

def _error_from_hpp(self, html):
# Must be updated when Adyen response is changed:
Expand Down
8 changes: 3 additions & 5 deletions Adyen/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ def __init__(self,
url="",
psp="",
headers="",
status_code=""):
status_code="",
error_code=""):
self.message = message
self.raw_request = raw_request
self.raw_response = raw_response
self.url = url
self.psp = psp
self.headers = headers
self.status_code = status_code
self.error_code = error_code

def __str__(self):
return repr("{}:{}".format(self.__class__.__name__, self.message))
Expand All @@ -42,13 +44,9 @@ class AdyenInvalidRequestError(AdyenError):
class AdyenAPIResponseError(AdyenError):
def __init__(self,
message,
result="",
error_code="",
*args,
**kwargs):
super(AdyenAPIResponseError, self).__init__(message, *args, **kwargs)
self.error_code = error_code
self.result = result


class AdyenAPIAuthenticationError(AdyenAPIResponseError):
Expand Down
14 changes: 6 additions & 8 deletions test/ModificationTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ def test_capture_error_167(self):
'test/mocks/'
'capture-error-167'
'.json')
self.assertRaisesRegexp(Adyen.AdyenAPIValidationError,
"Received validation error with errorCode:"
" 167, message: Original pspReference required"
" for this operation, HTTP Code: 422." +
" Please verify the values provided. Please "
"reach out to support@adyen.com if the problem"
" persists, providing the PSP reference.*",
self.ady.payment.capture, request)
self.assertRaisesRegexp(
Adyen.AdyenAPICommunicationError,
"Unexpected error",
self.ady.payment.capture,
request
)

def test_cancel_or_refund_received(self):
request = {}
Expand Down
11 changes: 6 additions & 5 deletions test/RecurringTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ def test_disable_803(self):
'recurring/'
'disable-error-803'
'.json')
self.assertRaisesRegexp(Adyen.AdyenAPIValidationError,
"Received validation error with errorCode: "
"803, message: PaymentDetail not found, "
"HTTP Code: 422.*",
self.ady.recurring.disable, request)
self.assertRaisesRegexp(
Adyen.AdyenAPICommunicationError,
"Unexpected error",
self.ady.recurring.disable,
request
)


TestRecurring.client.http_force = "requests"
Expand Down
24 changes: 6 additions & 18 deletions test/ThirdPartyPayoutTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,8 @@ def test_submit_invalid_recurring_reference(self):
resp = 'test/mocks/payout/submit-invalid-reference.json'
self.ady.client = self.test.create_client_from_file(422, request, resp)
self.assertRaisesRegexp(
Adyen.AdyenAPIValidationError,
"Received validation error with errorCode: 800,"
" message: Contract not found, HTTP Code: 422."
" Please verify the values provided."
" Please reach out to support@adyen.com"
" if the problem persists, providing the PSP reference.*",
Adyen.AdyenAPICommunicationError,
"Unexpected error",
self.ady.payout.submit,
request
)
Expand Down Expand Up @@ -183,12 +179,8 @@ def test_store_detail_and_submit_missing_payment(self):
resp = 'test/mocks/payout/storeDetailAndSubmit-missing-payment.json'
self.ady.client = self.test.create_client_from_file(422, request, resp)
self.assertRaisesRegexp(
Adyen.AdyenAPIValidationError,
"Received validation error with errorCode: 000,"
" message: Please supply paymentDetails, HTTP Code: 422."
" Please verify the values provided."
" Please reach out to support@adyen.com"
" if the problem persists, providing the PSP reference:.*",
Adyen.AdyenAPICommunicationError,
"Unexpected error",
self.ady.payout.store_detail_and_submit,
request
)
Expand All @@ -215,12 +207,8 @@ def test_store_detail_and_submit_invalid_iban(self):
resp = 'test/mocks/payout/storeDetailAndSubmit-invalid-iban.json'
self.ady.client = self.test.create_client_from_file(422, request, resp)
self.assertRaisesRegexp(
Adyen.AdyenAPIValidationError,
"Received validation error with errorCode: 161,"
" message: Invalid iban, HTTP Code: 422."
" Please verify the values provided."
" Please reach out to support@adyen.com"
" if the problem persists, providing the PSP reference:.*",
Adyen.AdyenAPICommunicationError,
"Unexpected error",
self.ady.payout.store_detail_and_submit,
request
)
Expand Down