diff --git a/Adyen/client.py b/Adyen/client.py index 6b563e79..e9e928a0 100644 --- a/Adyen/client.py +++ b/Adyen/client.py @@ -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. """ @@ -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) @@ -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" \ @@ -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" \ @@ -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": @@ -490,7 +510,9 @@ 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" \ @@ -498,18 +520,15 @@ def _handle_http_error(self, url, response_obj, status_code, psp_ref, " 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": @@ -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( @@ -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: diff --git a/Adyen/exceptions.py b/Adyen/exceptions.py index 1b99efe7..aa8caa57 100644 --- a/Adyen/exceptions.py +++ b/Adyen/exceptions.py @@ -9,7 +9,8 @@ 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 @@ -17,6 +18,7 @@ def __init__(self, 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)) @@ -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): diff --git a/test/ModificationTest.py b/test/ModificationTest.py index 6b56a88f..77eb0c33 100644 --- a/test/ModificationTest.py +++ b/test/ModificationTest.py @@ -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 = {} diff --git a/test/RecurringTest.py b/test/RecurringTest.py index 4bb1072e..7cdfbd44 100644 --- a/test/RecurringTest.py +++ b/test/RecurringTest.py @@ -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" diff --git a/test/ThirdPartyPayoutTest.py b/test/ThirdPartyPayoutTest.py index 0d63acbf..517d48a0 100644 --- a/test/ThirdPartyPayoutTest.py +++ b/test/ThirdPartyPayoutTest.py @@ -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 ) @@ -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 ) @@ -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 )