Skip to content
Permalink
Browse files

[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
  • Loading branch information...
len-odoo committed Mar 20, 2019
1 parent bd63913 commit a5e1e3e3ef740238e6c26594bb6a1e40f36af325
Showing with 38 additions and 2 deletions.
  1. +38 −2 addons/payment_adyen/models/payment.py
@@ -18,6 +18,31 @@

_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 @@ def adyen_form_generate_values(self, values):
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 @@ def adyen_form_generate_values(self, values):

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,

0 comments on commit a5e1e3e

Please sign in to comment.
You can’t perform that action at this time.