@@ -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:
0 commit comments