From 1ca40a63c9463516f05dfe5a131ecd6c8d21a3e7 Mon Sep 17 00:00:00 2001 From: Mahamadasif Ansari Date: Thu, 4 May 2023 05:33:36 +0000 Subject: [PATCH] [FIX] payment: prevent deletion of payment provider if it has external reference "ValueError: External ID not found in the system: payment.payment_provider_ stripe" is generated because the user deleted the Stripe payment provider record and its corresponding model tried to access the record of it. Steps to produce the error:(in >=16.0) 1. Install e-commerce 2. Install 'Payment Provider: Stripe' 3. delete the Stripe from payment Provider 4. Go to the website > Reporting > e-commerce 5. Click Activate Stripe on eCommerce Dashboard This commit solves the above issue by preventing the deletion of the payment provider if it has a external reference. sentry-4041178833 closes odoo/odoo#120534 X-original-commit: 7082ce2460494891e8e063c11678226b164f99e7 Signed-off-by: Antoine Vandevenne (anv) Signed-off-by: Ansari Mahamadasif (maan) --- addons/payment/i18n/payment.pot | 7 +++++++ addons/payment/models/payment_provider.py | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/addons/payment/i18n/payment.pot b/addons/payment/i18n/payment.pot index b11de450300..5eff41571c4 100644 --- a/addons/payment/i18n/payment.pot +++ b/addons/payment/i18n/payment.pot @@ -2008,6 +2008,13 @@ msgstr "" msgid "You can click here to be redirected to the confirmation page." msgstr "" +#. module: payment +#. odoo-python +#: code:addons/payment/models/payment_provider.py:0 +#, python-format +msgid "You cannot delete the payment provider %s; archive it instead." +msgstr "" + #. module: payment #. odoo-python #: code:addons/payment/models/payment_provider.py:0 diff --git a/addons/payment/models/payment_provider.py b/addons/payment/models/payment_provider.py index 692b54fe433..281c4e832c4 100644 --- a/addons/payment/models/payment_provider.py +++ b/addons/payment/models/payment_provider.py @@ -412,6 +412,17 @@ class PaymentProvider(models.Model): """ self.env['payment.token'].search([('provider_id', 'in', self.ids)]).write({'active': False}) + @api.ondelete(at_uninstall=False) + def _unlink_except_master_data(self): + """ Prevent the deletion of the payment provider if it has an xmlid. """ + external_ids = self.get_external_id() + for provider in self: + external_id = external_ids[provider.id] + if external_id and not external_id.startswith('__export__'): + raise UserError( + _("You cannot delete the payment provider %s; archive it instead.", provider.name) + ) + #=== ACTION METHODS ===# def button_immediate_install(self):