Skip to content

Commit

Permalink
Add amount helper with auto adjusting decimal points
Browse files Browse the repository at this point in the history
  • Loading branch information
zhutik committed Sep 17, 2018
1 parent 6ef980b commit bbb8b29
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 17 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ instance := adyen.New(
os.Getenv("ADYEN_PASSWORD"),
)

req := &adyen.AuthoriseEncrypted{
Amount: &adyen.Amount{
amount := &adyen.Amount{
Value: 1000, // amount * 100, f.e. 10,30 EUR = 1030
Currency: "EUR" // or use instance.Currency
},
}

// or amount := adyen.NewAmount("EUR", 10), in this case decimal points would be adjusted automatically

req := &adyen.AuthoriseEncrypted{
Amount: amount,
MerchantAccount: os.Getenv("ADYEN_ACCOUNT"), // your merchant account in Adyen
AdditionalData: &adyen.AdditionalData{Content: "encryptedData"}, // encrypted data from a form
Reference: "your-order-number",
Expand Down Expand Up @@ -95,10 +99,7 @@ instance.MerchantAccount = "TEST_MERCHANT_ACCOUNT"

// futher, information could be retrieved to populate request
req := &adyen.AuthoriseEncrypted{
Amount: &adyen.Amount{
Value: 1000,
Currency: instance.Currency
},
Amount: adyen.NewAmount(instance.Currency, 10.00),
MerchantAccount: instance.MerchantAccount,
AdditionalData: &adyen.AdditionalData{Content: "encryptedData"}, // encrypted data from a form
Reference: "your-order-number",
Expand Down
65 changes: 65 additions & 0 deletions amount.go
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,
}
}
53 changes: 53 additions & 0 deletions amount_test.go
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)
})
}
}
10 changes: 0 additions & 10 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ type Address struct {
Street string `json:"street"`
}

/*********
* Amount *
*********/

// Amount value/currency representation
type Amount struct {
Value float32 `json:"value"`
Currency string `json:"currency"`
}

/*******
* Card *
*******/
Expand Down

0 comments on commit bbb8b29

Please sign in to comment.