From 956d0d26a3b79bac3ff33c7dca0b7e7839a1313b Mon Sep 17 00:00:00 2001 From: "Valentin Vallaeys (vava)" Date: Fri, 27 Jan 2023 15:52:09 +0000 Subject: [PATCH] [IMP] payment_stripe: prevent enabling Stripe if onboarding is ongoing For now, you can enable the Stripe payment provider while onboarding with Stripe Connect. This raises two issues: - This will raise errors in online payments because Stripe is not completely configured; - The button to continue the onboarding is not working anymore, preventing the user to finish the configuration of his account from Odoo. This commit prevents enabling Stripe if the onboarding has been initiated but is not finished. It also changes the way constants are read from const.py to allow patching one of them if needed. OPW-3149159 closes odoo/odoo#111534 X-original-commit: fa3bcac71d1699aa9b9a7bc1d6b6181a70171662 Signed-off-by: Antoine Vandevenne (anv) Signed-off-by: Vallaeys Valentin (vava) --- addons/payment_stripe/i18n/payment_stripe.pot | 14 +++++-- .../payment_stripe/models/payment_provider.py | 42 ++++++++++++++++--- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/addons/payment_stripe/i18n/payment_stripe.pot b/addons/payment_stripe/i18n/payment_stripe.pot index 9bcca50924e..ea5878a13e7 100644 --- a/addons/payment_stripe/i18n/payment_stripe.pot +++ b/addons/payment_stripe/i18n/payment_stripe.pot @@ -4,10 +4,10 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server saas~15.4\n" +"Project-Id-Version: Odoo Server 16.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2022-06-09 14:05+0000\n" -"PO-Revision-Date: 2022-06-09 14:05+0000\n" +"POT-Creation-Date: 2023-01-31 14:10+0000\n" +"PO-Revision-Date: 2023-01-31 14:10+0000\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -199,6 +199,14 @@ msgid "" "You cannot create a Stripe Webhook if your Stripe Secret Key is not set." msgstr "" +#. module: payment_stripe +#: code:addons/payment_stripe/models/payment_acquirer.py:0 +#, python-format +msgid "" +"You cannot set the provider state to Enabled until your onboarding to Stripe" +" is completed." +msgstr "" + #. module: payment_stripe #: code:addons/payment_stripe/models/payment_acquirer.py:0 #, python-format diff --git a/addons/payment_stripe/models/payment_provider.py b/addons/payment_stripe/models/payment_provider.py index a4bf4b80cc8..f054278edba 100644 --- a/addons/payment_stripe/models/payment_provider.py +++ b/addons/payment_stripe/models/payment_provider.py @@ -10,7 +10,7 @@ from odoo import _, api, fields, models from odoo.exceptions import UserError, ValidationError from odoo.addons.payment_stripe import utils as stripe_utils -from odoo.addons.payment_stripe.const import API_VERSION, PROXY_URL, HANDLED_WEBHOOK_EVENTS +from odoo.addons.payment_stripe import const from odoo.addons.payment_stripe.controllers.onboarding import OnboardingController from odoo.addons.payment_stripe.controllers.main import StripeController @@ -61,6 +61,7 @@ class PaymentProvider(models.Model): `state` together with writing on those custom fields, the constraint would be triggered. :return: None + :raise ValidationError: If the provider of a connected account is set in state 'test'. """ for provider in self: if provider.state == 'test' and provider._stripe_has_connected_account(): @@ -81,6 +82,37 @@ class PaymentProvider(models.Model): self.ensure_one() return False + @api.constrains('state') + def _check_onboarding_of_enabled_provider_is_completed(self): + """ Check that the provider cannot be set to 'enabled' if the onboarding is ongoing. + + This constraint is defined in the present module to allow the export of the translation + string of the `ValidationError` should it be raised by modules that would fully implement + Stripe Connect. + + :return: None + :raise ValidationError: If the provider of a connected account is set in state 'enabled' + while the onboarding is not finished. + """ + for provider in self: + if provider.state == 'enabled' and provider._stripe_onboarding_is_ongoing(): + raise ValidationError(_( + "You cannot set the provider state to Enabled until your onboarding to Stripe " + "is completed." + )) + + def _stripe_onboarding_is_ongoing(self): + """ Return whether the provider is linked to an ongoing onboarding to Stripe Connect. + + Note: This method serves as a hook for modules that would fully implement Stripe Connect. + Note: self.ensure_one() + + :return: Whether the provider is linked to an ongoing onboarding to Stripe Connect + :rtype: bool + """ + self.ensure_one() + return False + # === ACTION METHODS === # def action_stripe_connect_account(self, menu_id=None): @@ -152,8 +184,8 @@ class PaymentProvider(models.Model): webhook = self._stripe_make_request( 'webhook_endpoints', payload={ 'url': self._get_stripe_webhook_url(), - 'enabled_events[]': HANDLED_WEBHOOK_EVENTS, - 'api_version': API_VERSION, + 'enabled_events[]': const.HANDLED_WEBHOOK_EVENTS, + 'api_version': const.API_VERSION, } ) self.stripe_webhook_secret = webhook.get('secret') @@ -228,7 +260,7 @@ class PaymentProvider(models.Model): url = url_join('https://api.stripe.com/v1/', endpoint) headers = { 'AUTHORIZATION': f'Bearer {stripe_utils.get_secret_key(self)}', - 'Stripe-Version': API_VERSION, # SetupIntent requires a specific version. + 'Stripe-Version': const.API_VERSION, # SetupIntent requires a specific version. **self._get_stripe_extra_request_headers(), } if method == 'POST' and idempotency_key: @@ -365,7 +397,7 @@ class PaymentProvider(models.Model): 'proxy_data': self._stripe_prepare_proxy_data(stripe_payload=payload), }, } - url = url_join(PROXY_URL, f'{version}/{endpoint}') + url = url_join(const.PROXY_URL, f'{version}/{endpoint}') try: response = requests.post(url=url, json=proxy_payload, timeout=60) response.raise_for_status()