-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add amount helper with auto adjusting decimal points
- Loading branch information
Showing
4 changed files
with
126 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package adyen | ||
|
||
import "math" | ||
|
||
// Amount value/currency representation | ||
type Amount struct { | ||
Value float32 `json:"value"` | ||
Currency string `json:"currency"` | ||
} | ||
|
||
var ( | ||
// DefaultCurrencyDecimals - default currency decimals | ||
DefaultCurrencyDecimals uint = 2 | ||
|
||
// CurrencyDecimals - 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, | ||
} | ||
) | ||
|
||
// NewAmount - creates Amount instance | ||
// | ||
// Automatically adjust decimal points for the float value | ||
// Link - https://docs.adyen.com/developers/development-resources/currency-codes | ||
func NewAmount(currency string, amount float32) *Amount { | ||
decimals, ok := CurrencyDecimals[currency] | ||
if !ok { | ||
decimals = DefaultCurrencyDecimals | ||
} | ||
|
||
if decimals == 0 { | ||
return &Amount{ | ||
Currency: currency, | ||
Value: amount, | ||
} | ||
} | ||
|
||
coef := float32(math.Pow(10, float64(decimals))) | ||
|
||
return &Amount{ | ||
Currency: currency, | ||
Value: amount * coef, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package adyen | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestNewAmount(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
currency string | ||
amount float32 | ||
expected Amount | ||
}{ | ||
{ | ||
name: "Test EUR currency", | ||
currency: "EUR", | ||
amount: 10.50, | ||
expected: Amount{Currency: "EUR", Value: 1050}, | ||
}, | ||
{ | ||
name: "Test EUR currency, zero case", | ||
currency: "EUR", | ||
amount: 0, | ||
expected: Amount{Currency: "EUR", Value: 0}, | ||
}, | ||
{ | ||
name: "Test unknown (UKN) currency, default should be used", | ||
currency: "UKN", | ||
amount: 10.60, | ||
expected: Amount{Currency: "UKN", Value: 1060}, | ||
}, | ||
{ | ||
name: "Test CVE currency with zero decimal adjustment", | ||
currency: "CVE", | ||
amount: 150, | ||
expected: Amount{Currency: "CVE", Value: 150}, | ||
}, | ||
{ | ||
name: "Test BHD currency with 3 decimal adjustment points", | ||
currency: "BHD", | ||
amount: 150.050, | ||
expected: Amount{Currency: "BHD", Value: 150050}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
a := NewAmount(c.currency, c.amount) | ||
|
||
equals(t, c.expected, *a) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters