From 0a5ee69c517a0482d0e922093805e6d960e6db80 Mon Sep 17 00:00:00 2001 From: "Valeriya(vchu)" Date: Fri, 2 Jun 2023 14:38:26 +0000 Subject: [PATCH] [FIX] payment_adyen: set error when offline transaction failed When the offline transaction failed, the error was raised that rollback all the operations and the failed transaction was not traced anywhere. With this commit the error is caught and the failed transaction can be seen in chatter. X-original-commit: 21e84624841690d36bd38b768e35cc61b3c6a17b Part-of: odoo/odoo#124040 --- .../payment_adyen/models/payment_provider.py | 16 ++++++++++------ .../models/payment_transaction.py | 19 +++++++++++++------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/addons/payment_adyen/models/payment_provider.py b/addons/payment_adyen/models/payment_provider.py index e8a2d21ea75..9b7e3c84d52 100644 --- a/addons/payment_adyen/models/payment_provider.py +++ b/addons/payment_adyen/models/payment_provider.py @@ -117,15 +117,19 @@ class PaymentProvider(models.Model): headers = {'X-API-Key': self.adyen_api_key} try: response = requests.request(method, url, json=payload, headers=headers, timeout=60) - response.raise_for_status() + try: + response.raise_for_status() + except requests.exceptions.HTTPError: + _logger.exception( + "invalid API request at %s with data %s: %s", url, payload, response.text + ) + msg = response.json().get('message', '') + raise ValidationError( + "Adyen: " + _("The communication with the API failed. Details: %s", msg) + ) except requests.exceptions.ConnectionError: _logger.exception("unable to reach endpoint at %s", url) raise ValidationError("Adyen: " + _("Could not establish the connection to the API.")) - except requests.exceptions.HTTPError as error: - _logger.exception( - "invalid API request at %s with data %s: %s", url, payload, error.response.text - ) - raise ValidationError("Adyen: " + _("The communication with the API failed.")) return response.json() def _adyen_compute_shopper_reference(self, partner_id): diff --git a/addons/payment_adyen/models/payment_transaction.py b/addons/payment_adyen/models/payment_transaction.py index 42c447fca5f..1a32f53526d 100644 --- a/addons/payment_adyen/models/payment_transaction.py +++ b/addons/payment_adyen/models/payment_transaction.py @@ -94,12 +94,19 @@ class PaymentTransaction(models.Model): data.update(captureDelayHours=0) # Make the payment request to Adyen - response_content = self.provider_id._adyen_make_request( - url_field_name='adyen_checkout_api_url', - endpoint='/payments', - payload=data, - method='POST', - ) + try: + response_content = self.provider_id._adyen_make_request( + url_field_name='adyen_checkout_api_url', + endpoint='/payments', + payload=data, + method='POST', + ) + except ValidationError as e: + if self.operation == 'offline': + self._set_error(e) # Log the error message on linked documents' chatter. + return # There is nothing to process. + else: + raise e # Handle the payment request response _logger.info(