From 864ca5075f4b1054f984cd4a85307ff0c62091b0 Mon Sep 17 00:00:00 2001 From: Adrian Torres Date: Tue, 26 Dec 2017 14:49:21 +0100 Subject: [PATCH] [FIX] authorize: add error-checking Previous to this commit, if a user hadn't properly set up his Authorize.net credentials, when getting a response from Authorize.net's servers, we would expect some attributes to be in the response's body, but this is not the case if the credentials are wrong, thus we would have an unexpected error like "NoneType object has no attribute text" which is only a sympton of the real error (bad credentials and no error-checking). This is fixed by checking if the response body contains an error message, if it is the case we raise an UserError with the error code and the error message (which is only shown if debug mode is activated). OPW 788261 --- .../models/authorize_request.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/addons/payment_authorize/models/authorize_request.py b/addons/payment_authorize/models/authorize_request.py index eea5b6ae276..939abcaf049 100644 --- a/addons/payment_authorize/models/authorize_request.py +++ b/addons/payment_authorize/models/authorize_request.py @@ -150,6 +150,22 @@ class AuthorizeAPI(): etree.SubElement(creditCard, "cardCode").text = card_code etree.SubElement(root, "validationMode").text = 'liveMode' response = self._authorize_request(root) + + # If the user didn't set up authorize.net properly then the response + # won't contain stuff like customerProfileId and accessing text + # will raise a NoneType has no text attribute + msg = response.find('messages') + if msg is not None: + rc = msg.find('resultCode') + if rc is not None and rc.text == 'Error': + err = msg.find('message') + err_code = err.find('code').text + err_msg = err.find('text').text + raise UserError( + "Authorize.net Error:\nCode: %s\nMessage: %s" + % (err_code, err_msg) + ) + res = dict() res['profile_id'] = response.find('customerProfileId').text res['payment_profile_id'] = response.find('customerPaymentProfileIdList/numericString').text