From c54a398513666508161796a498e8f8c3c7401b9f Mon Sep 17 00:00:00 2001
From: Mahamadasif Ansari <maan@odoo.com>
Date: Tue, 2 May 2023 05:46:07 +0000
Subject: [PATCH] [FIX] payment: prevent deletion of payment acquirer if it has
 external reference

"ValueError: External ID not found in the system: payment.payment_acquirer_
stripe" is generated because the user deleted the Stripe payment acquirer
record and its corresponding model tried to access the record of it.

Steps to produce the error:
1. Install e-commerce
2. Install install 'Stripe Payment Acquirer ' module
2. delete the Stripe payment from payment acquirer
3. Go to the e-commerce dashoboard
4. Click Set Payment
5. select Credit card (via Stripe)
6. enter any secret and Publishable key
7. click apply

This commit solves the above issue by preventing the deletion
of the payment acquirer if it has a external reference.

sentry-4041178833

closes odoo/odoo#119140

Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
---
 addons/payment/i18n/payment.pot           |  6 ++++++
 addons/payment/models/payment_acquirer.py | 17 ++++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/addons/payment/i18n/payment.pot b/addons/payment/i18n/payment.pot
index 0ca3abe4a019..d003b7c47a6d 100644
--- a/addons/payment/i18n/payment.pot
+++ b/addons/payment/i18n/payment.pot
@@ -1987,6 +1987,12 @@ msgstr ""
 msgid "You can click here to be redirected to the confirmation page."
 msgstr ""
 
+#. module: payment
+#: code:addons/payment/models/payment_acquirer.py:0
+#, python-format
+msgid "You cannot delete the payment acquirer %s; archive it instead."
+msgstr ""
+
 #. module: payment
 #: code:addons/payment/wizards/payment_acquirer_onboarding_wizard.py:0
 #, python-format
diff --git a/addons/payment/models/payment_acquirer.py b/addons/payment/models/payment_acquirer.py
index 70a7a03f2aa1..f85ecbb8470c 100644
--- a/addons/payment/models/payment_acquirer.py
+++ b/addons/payment/models/payment_acquirer.py
@@ -10,12 +10,14 @@ import psycopg2
 
 from odoo import api, exceptions, fields, models, _, SUPERUSER_ID
 from odoo.tools import consteq, float_round, image_process, ustr
-from odoo.exceptions import ValidationError
+from odoo.exceptions import UserError, ValidationError
 from odoo.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT
 from odoo.tools.misc import formatLang
 from odoo.http import request
 from odoo.osv import expression
 
+from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG
+
 _logger = logging.getLogger(__name__)
 
 
@@ -336,6 +338,19 @@ class PaymentAcquirer(models.Model):
         self._check_required_if_provider()
         return result
 
+    def unlink(self):
+        """ Prevent the deletion of the payment acquirer if it has an xmlid. """
+        external_ids = self.get_external_id()
+        for acquirer in self:
+            external_id = external_ids[acquirer.id]
+            if external_id \
+               and not external_id.startswith('__export__') \
+               and not self._context.get(MODULE_UNINSTALL_FLAG):
+                raise UserError(
+                    _("You cannot delete the payment acquirer %s; archive it instead.", acquirer.name)
+                )
+        return super().unlink()
+
     def get_acquirer_extra_fees(self, amount, currency_id, country_id):
         extra_fees = {
             'currency_id': currency_id
-- 
GitLab