zhutik/adyen-api-go

Implement currency code list and automated amount calculation

zhutik opened this issue · 3 comments

Adyen provides a list of supported currencies with the number of decimal points to adjust.

F.e.:

  • 15,60 EUR should be submitted as 1560
  • 10 BHD as 10000

Docs and list of codes:
https://docs.adyen.com/developers/currency-codes

We did something like this already :)

import "math"

var (
	DefaultCurrencyDecimals uint = 2

	// https://docs.adyen.com/developers/currency-codes
	// currencies with 2 decimals stripped out
	CurrencyDecimals = map[string]uint{
		"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,
	}
)

func GetCurrencyDecimals(currency string) uint {
	decimals, ok := CurrencyDecimals[currency]
	if !ok {
		return DefaultCurrencyDecimals
	}

	return decimals
}

func GetMinorUnitsAmount(amount float32, currency string) int {
	decimals := GetCurrencyDecimals(currency)

	if decimals == 0 {
		return int(amount)
	}

	coef := float32(math.Pow(10, float64(decimals)))
	return int(amount * coef)
}

Nice!

Can you please do a pull request with the changes? I think it would be nice to have this functionality from a library, so clients do not have to know the formatting logic.

I'm quite under pressure, so that won't be instant, but i'll try to do it 👍