From dff6a0fe55608d100212fabae51d248b9f7a67d3 Mon Sep 17 00:00:00 2001 From: "Horacio Tellez (hote)" Date: Wed, 7 Jun 2023 13:47:08 +0000 Subject: [PATCH] [FIX] payment_mercado_pago: forbid float amounts for currencies w/o minor unit The Chilean Peso and Colombian Peso don't have a minor unit. MercadoPago requires amounts be expressed as integers. Failing to send such amounts as integers results in an error. opw-3292798 closes odoo/odoo#138859 X-original-commit: 75fa1e55207050e57b8b912b5e0607c8a3e31515 Signed-off-by: Antoine Vandevenne (anv) --- .../i18n/payment_mercado_pago.pot | 109 ++++++++++++++++++ .../models/payment_transaction.py | 17 ++- 2 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 addons/payment_mercado_pago/i18n/payment_mercado_pago.pot diff --git a/addons/payment_mercado_pago/i18n/payment_mercado_pago.pot b/addons/payment_mercado_pago/i18n/payment_mercado_pago.pot new file mode 100644 index 00000000000..eea54b354c4 --- /dev/null +++ b/addons/payment_mercado_pago/i18n/payment_mercado_pago.pot @@ -0,0 +1,109 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * payment_mercado_pago +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server saas~16.4\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2023-07-25 11:23+0000\n" +"PO-Revision-Date: 2023-07-25 11:23+0000\n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: payment_mercado_pago +#: model_terms:ir.ui.view,arch_db:payment_mercado_pago.payment_provider_form +msgid "Access Token" +msgstr "" + +#. module: payment_mercado_pago +#: model:ir.model.fields,field_description:payment_mercado_pago.field_payment_provider__code +msgid "Code" +msgstr "" + +#. module: payment_mercado_pago +#. odoo-python +#: code:addons/payment_mercado_pago/models/payment_provider.py:0 +#, python-format +msgid "Could not establish the connection to the API." +msgstr "" + +#. module: payment_mercado_pago +#: model:ir.model.fields.selection,name:payment_mercado_pago.selection__payment_provider__code__mercado_pago +msgid "Mercado Pago" +msgstr "" + +#. module: payment_mercado_pago +#: model:ir.model.fields,field_description:payment_mercado_pago.field_payment_provider__mercado_pago_access_token +msgid "Mercado Pago Access Token" +msgstr "" + +#. module: payment_mercado_pago +#. odoo-python +#: code:addons/payment_mercado_pago/models/payment_transaction.py:0 +#, python-format +msgid "No transaction found matching reference %s." +msgstr "" + +#. module: payment_mercado_pago +#: model:ir.model,name:payment_mercado_pago.model_payment_provider +msgid "Payment Provider" +msgstr "" + +#. module: payment_mercado_pago +#: model:ir.model,name:payment_mercado_pago.model_payment_transaction +msgid "Payment Transaction" +msgstr "" + +#. module: payment_mercado_pago +#. odoo-python +#: code:addons/payment_mercado_pago/models/payment_transaction.py:0 +#, python-format +msgid "Prices in the currency %s must be expressed in integer values." +msgstr "" + +#. module: payment_mercado_pago +#. odoo-python +#: code:addons/payment_mercado_pago/models/payment_transaction.py:0 +#, python-format +msgid "Received data with invalid status: %s" +msgstr "" + +#. module: payment_mercado_pago +#. odoo-python +#: code:addons/payment_mercado_pago/models/payment_transaction.py:0 +#, python-format +msgid "Received data with missing payment id." +msgstr "" + +#. module: payment_mercado_pago +#. odoo-python +#: code:addons/payment_mercado_pago/models/payment_transaction.py:0 +#, python-format +msgid "Received data with missing reference." +msgstr "" + +#. module: payment_mercado_pago +#. odoo-python +#: code:addons/payment_mercado_pago/models/payment_transaction.py:0 +#, python-format +msgid "Received data with missing status." +msgstr "" + +#. module: payment_mercado_pago +#. odoo-python +#: code:addons/payment_mercado_pago/models/payment_provider.py:0 +#, python-format +msgid "" +"The communication with the API failed. Mercado Pago gave us the following " +"information: '%s' (code %s)" +msgstr "" + +#. module: payment_mercado_pago +#: model:ir.model.fields,help:payment_mercado_pago.field_payment_provider__code +msgid "The technical code of this payment provider." +msgstr "" diff --git a/addons/payment_mercado_pago/models/payment_transaction.py b/addons/payment_mercado_pago/models/payment_transaction.py index 731cf13f8ea..847e1fbb94c 100644 --- a/addons/payment_mercado_pago/models/payment_transaction.py +++ b/addons/payment_mercado_pago/models/payment_transaction.py @@ -6,7 +6,7 @@ import pprint from werkzeug import urls from odoo import _, models -from odoo.exceptions import ValidationError +from odoo.exceptions import UserError, ValidationError from odoo.addons.payment_mercado_pago import const from odoo.addons.payment_mercado_pago.controllers.main import MercadoPagoController @@ -61,6 +61,19 @@ class PaymentTransaction(models.Model): webhook_url = urls.url_join( base_url, f'{MercadoPagoController._webhook_url}/{self.reference}' ) # Append the reference to identify the transaction from the webhook notification data. + + # In the case where we are issuing a preference request in CLP or COP, we must ensure that + # the price unit is an integer because these currencies do not have a minor unit. + unit_price = self.amount + if self.currency_id.name in ('CLP', 'COP'): + rounded_unit_price = int(self.amount) + if rounded_unit_price != self.amount: + raise UserError(_( + "Prices in the currency %s must be expressed in integer values.", + self.currency_id.name, + )) + unit_price = rounded_unit_price + return { 'auto_return': 'all', 'back_urls': { @@ -73,7 +86,7 @@ class PaymentTransaction(models.Model): 'title': self.reference, 'quantity': 1, 'currency_id': self.currency_id.name, - 'unit_price': self.amount, + 'unit_price': unit_price, }], 'notification_url': webhook_url, 'payer': {