forked from zhutik/adyen-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.go
119 lines (102 loc) · 3.16 KB
/
signature.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package adyen
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"strconv"
"strings"
)
// replaceSpecialChars replace special characters according to Adyen documentation
//
// Link: https://docs.adyen.com/developers/payments/accepting-payments/hmac-signature-calculation
func replaceSpecialChars(value string) string {
temp := strings.Replace(value, "\\", "\\\\", -1)
temp = strings.Replace(temp, ":", "\\:", -1)
return temp
}
// CalculateSignature calculate HMAC signature for request
//
// Link: https://docs.adyen.com/developers/payments/accepting-payments/hmac-signature-calculation
func (r *DirectoryLookupRequest) CalculateSignature(adyen *Adyen) error {
if r.MerchantAccount == "" || r.SkinCode == "" || adyen.Credentials.Hmac == "" {
return errors.New("merchantID, skinCode and HMAC hash need to be specified")
}
keyString := strings.Join([]string{
"countryCode",
"currencyCode",
"merchantAccount",
"merchantReference",
"paymentAmount",
"sessionValidity",
"shipBeforeDate",
"skinCode",
}, ":")
valueString := strings.Join([]string{
replaceSpecialChars(r.CountryCode),
replaceSpecialChars(r.CurrencyCode),
replaceSpecialChars(r.MerchantAccount),
replaceSpecialChars(r.MerchantReference),
replaceSpecialChars(strconv.Itoa(r.PaymentAmount)),
replaceSpecialChars(r.SessionsValidity),
replaceSpecialChars(r.ShipBeforeDate),
replaceSpecialChars(r.SkinCode),
}, ":")
fullString := keyString + ":" + valueString
src, err := hex.DecodeString(adyen.Credentials.Hmac)
if err != nil {
return err
}
mac := hmac.New(sha256.New, src)
if _, err = mac.Write([]byte(fullString)); err != nil {
return err
}
r.MerchantSig = base64.StdEncoding.EncodeToString(mac.Sum(nil))
return nil
}
// CalculateSignature calculate HMAC signature for request
//
// Link: https://docs.adyen.com/developers/payments/accepting-payments/hmac-signature-calculation
func (r *SkipHppRequest) CalculateSignature(adyen *Adyen) error {
if r.MerchantAccount == "" || r.SkinCode == "" || adyen.Credentials.Hmac == "" {
return errors.New("merchantID, skinCode and HMAC hash need to be specified")
}
keyString := strings.Join([]string{
"brandCode",
"countryCode",
"currencyCode",
"issuerId",
"merchantAccount",
"merchantReference",
"paymentAmount",
"sessionValidity",
"shipBeforeDate",
"shopperLocale",
"skinCode",
}, ":")
valueString := strings.Join([]string{
replaceSpecialChars(r.BrandCode),
replaceSpecialChars(r.CountryCode),
replaceSpecialChars(r.CurrencyCode),
replaceSpecialChars(r.IssuerID),
replaceSpecialChars(r.MerchantAccount),
replaceSpecialChars(r.MerchantReference),
replaceSpecialChars(strconv.Itoa(r.PaymentAmount)),
replaceSpecialChars(r.SessionsValidity),
replaceSpecialChars(r.ShipBeforeDate),
replaceSpecialChars(r.ShopperLocale),
replaceSpecialChars(r.SkinCode),
}, ":")
fullString := keyString + ":" + valueString
src, err := hex.DecodeString(adyen.Credentials.Hmac)
if err != nil {
return err
}
mac := hmac.New(sha256.New, src)
if _, err = mac.Write([]byte(fullString)); err != nil {
return err
}
r.MerchantSig = base64.StdEncoding.EncodeToString(mac.Sum(nil))
return nil
}