From e02a9077b99418d144db1cbdd0f80090744f6875 Mon Sep 17 00:00:00 2001 From: Nans Lefebvre Date: Wed, 20 Mar 2019 14:04:56 +0000 Subject: [PATCH] [FIX] payment_adyen: convert the paymentAmount respecting the Adyen API Adyen requires the amount to be multiplied by 10^k, where k depends on the currency code. https://docs.adyen.com/developers/development-resources/currency-codes Provides a map of codes to factors. Before, k was assumed to be 2 (which is correct in most cases). opw 1928918 closes odoo/odoo#31984 Signed-off-by: Nans Lefebvre (len) --- addons/payment_adyen/models/payment.py | 40 ++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/addons/payment_adyen/models/payment.py b/addons/payment_adyen/models/payment.py index 6ed0c938e08..0c4a62d884a 100644 --- a/addons/payment_adyen/models/payment.py +++ b/addons/payment_adyen/models/payment.py @@ -18,6 +18,31 @@ from odoo.tools.pycompat import to_native _logger = logging.getLogger(__name__) +# https://docs.adyen.com/developers/development-resources/currency-codes +CURRENCY_CODE_MAPS = { + "BHD": 3, + "CVE": 0, + "DJF": 0, + "GNF": 0, + "IDR": 0, + "JOD": 3, + "JPY": 0, + "KMF": 0, + "KRW": 0, + "KWD": 3, + "LYD": 3, + "OMR": 3, + "PYG": 0, + "RWF": 0, + "TND": 3, + "UGX": 0, + "VND": 0, + "VUV": 0, + "XAF": 0, + "XOF": 0, + "XPF": 0, +} + class AcquirerAdyen(models.Model): _inherit = 'payment.acquirer' @@ -27,6 +52,16 @@ class AcquirerAdyen(models.Model): adyen_skin_code = fields.Char('Skin Code', required_if_provider='adyen', groups='base.group_user') adyen_skin_hmac_key = fields.Char('Skin HMAC Key', required_if_provider='adyen', groups='base.group_user') + @api.model + def _adyen_convert_amount(self, amount, currency): + """ + Adyen requires the amount to be multiplied by 10^k, + where k depends on the currency code. + """ + k = CURRENCY_CODE_MAPS.get(currency.name, 2) + paymentAmount = int(tools.float_round(amount, k) * (10**k)) + return paymentAmount + @api.model def _get_adyen_urls(self, environment): """ Adyen URLs: yhpp: hosted payment page: pay.shtml for single, select.shtml for multiple """ @@ -120,12 +155,13 @@ class AcquirerAdyen(models.Model): import datetime from dateutil import relativedelta + paymentAmount = self._adyen_convert_amount(values['amount'], values['currency']) if self.provider == 'adyen' and len(self.adyen_skin_hmac_key) == 64: tmp_date = datetime.datetime.today() + relativedelta.relativedelta(days=1) values.update({ 'merchantReference': values['reference'], - 'paymentAmount': '%d' % int(tools.float_round(values['amount'], 2) * 100), + 'paymentAmount': '%d' % paymentAmount, 'currencyCode': values['currency'] and values['currency'].name or '', 'shipBeforeDate': tmp_date.strftime('%Y-%m-%d'), 'skinCode': self.adyen_skin_code, @@ -143,7 +179,7 @@ class AcquirerAdyen(models.Model): values.update({ 'merchantReference': values['reference'], - 'paymentAmount': '%d' % int(tools.float_round(values['amount'], 2) * 100), + 'paymentAmount': '%d' % paymentAmount, 'currencyCode': values['currency'] and values['currency'].name or '', 'shipBeforeDate': tmp_date, 'skinCode': self.adyen_skin_code,