-
Notifications
You must be signed in to change notification settings - Fork 12
/
signature.go
159 lines (138 loc) · 4.53 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
}
// ValidateSignature validate HMAC signature for notification event
//
// Link: https://docs.adyen.com/development-resources/notifications/verify-hmac-signatures#verify-using-your-own-solution
func (n *NotificationRequestItemData) ValidateSignature(adyen *Adyen) (bool, error) {
var precondition error
providedSig := n.AdditionalData.HmacSignature
if len(providedSig) == 0 {
precondition = errors.New("no HMAC signature in message")
} else if len(adyen.Credentials.Hmac) == 0 {
precondition = errors.New("no HMAC key configured; cannot validate signature")
}
if precondition != nil {
return false, precondition
}
valueInJavaFloatToStringStyle := strconv.FormatFloat(float64(n.Amount.Value), 'f', -1, 32)
valueString := strings.Join([]string{
replaceSpecialChars(n.PspReference),
replaceSpecialChars(n.OriginalReference),
replaceSpecialChars(n.MerchantAccountCode),
replaceSpecialChars(n.MerchantReference),
valueInJavaFloatToStringStyle,
replaceSpecialChars(n.Amount.Currency),
replaceSpecialChars(n.EventCode),
strconv.FormatBool(bool(n.Success)),
}, ":")
src, err := hex.DecodeString(adyen.Credentials.Hmac)
if err != nil {
return false, err
}
mac := hmac.New(sha256.New, src)
if _, err = mac.Write([]byte(valueString)); err != nil {
return false, err
}
expectedSig := base64.StdEncoding.EncodeToString(mac.Sum(nil))
return expectedSig == providedSig, nil
}