forked from alexabrahall/go-adyen-ces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadyen.go
188 lines (170 loc) · 5.13 KB
/
adyen.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package adyen
import (
"crypto/aes"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/pion/dtls/v2/pkg/crypto/ccm"
"math/rand"
"reflect"
"strings"
"time"
)
type Adyen struct {
rsa *adrsa
prefix string
version string
rsaEncryptAesVal string
//iv
aesKey []byte
_tagSize int
_nonceSize int
_debug bool
aesNonce []byte
}
type Data struct {
Number string `json:"number"`
Cvc string `json:"cvc"`
HolderName string `json:"holderName"`
ExpiryMonth string `json:"expiryMonth"`
ExpiryYear string `json:"expiryYear"`
Generationtime string `json:"generationtime"`
PaymentMethodID string `json:"paymentMethodId"`
CardType string `json:"cardType"`
Referrer string `json:"referrer"`
Activate string `json:"activate"`
Deactivate string `json:"deactivate"`
InitializeCount string `json:"initializeCount"`
LuhnCount string `json:"luhnCount"`
LuhnOkCount string `json:"luhnOkCount"`
LuhnSameLengthCount string `json:"luhnSameLengthCount"`
SjclStrength string `json:"sjclStrength"`
DfValue string `json:"dfValue"`
}
type CardForm struct {
Number string `json:"number" validate:"required"`
Holder string `json:"holder" validate:"required"`
Cvv string `json:"cvv" validate:"required"`
ExpiryMonth string `json:"expiry_month" validate:"required"`
ExpiryYear string `json:"expiry_year" validate:"required"`
CardType string `json:"card_type" validate:"required"`
PaymentMethodId string `json:"payment_method_id" validate:"required"`
Referer string `json:"referrer"`
DfVal string `json:"df_val"` //理论上只指纹数据
}
func NewAdYen(publicKey string) *Adyen {
yen := &Adyen{}
yen.rsa = NewRsa()
yen.prefix = "adyenjs_"
yen.version = "0_1_21"
yen.aesKey = make([]byte, 32)
yen._tagSize = 8
yen._nonceSize = 12
//如果密钥错误直接推出
err := yen.rsa.SetPublicKey(publicKey, 65537)
if err != nil {
panic(err)
}
return yen
}
func (yen *Adyen) marshal(data interface{}) []byte {
if reflect.TypeOf(data).String() == "string" {
return []byte(data.(string))
}
bytes, _ := json.Marshal(data)
return bytes
}
//validate order info
func (yen *Adyen) validate(card CardForm) (Data, error) {
gt := time.Now().UTC().Format("2006-01-02T15:04:05.000Z07:00")
info := Data{
Number: card.Number,
Cvc: card.Cvv,
HolderName: card.Holder,
ExpiryMonth: card.ExpiryMonth,
ExpiryYear: card.ExpiryYear,
Generationtime: gt,
PaymentMethodID: card.PaymentMethodId,
CardType: card.CardType,
Referrer: card.Referer,
Activate: "4",
Deactivate: "4",
InitializeCount: "1",
LuhnCount: "1",
LuhnOkCount: "1",
LuhnSameLengthCount: "1",
SjclStrength: "10",
DfValue: "",
}
//check card info
if err := validateAdYenCardInfo(info); err != nil {
return info, err
}
return info, nil
}
//encrypt
func (yen *Adyen) Encrypt(card CardForm) (string, error) {
// pre validate
data, err := yen.validate(card)
bytes := yen.marshal(data)
//1. init aes random key
yen.init()
//2. create ccm instance
block, err := aes.NewCipher(yen.aesKey)
if err != nil {
return "", err
}
cmer, err := ccm.NewCCM(block, yen._tagSize, len(yen.aesNonce))
if err != nil {
return "", err
}
//3. aes encrypt data
cipherBytes := cmer.Seal(nil, yen.aesNonce, bytes, nil)
cipherBytes = append(yen.aesNonce, cipherBytes...) //追加
cipherText := base64.StdEncoding.EncodeToString(cipherBytes)
if yen._debug {
fmt.Println("aes:", cipherText)
}
//4. rsa encrypt aes.key
rsaCp, err := yen.rsa.Encrypt2(yen.aesKey, "base64")
if err != nil {
return "", err
}
if yen._debug {
fmt.Println("rsa:", rsaCp)
fmt.Println("rsa:", len(rsaCp))
}
//5. append data
prefix := yen.prefix + yen.version + "$"
arr := []string{prefix, rsaCp, "$", cipherText}
return strings.Join(arr, ""), nil
}
func (yen *Adyen) debug() {
yen._debug = true
yen.aesKey = []byte{
211, 230, 56, 196, 255, 13, 107, 44,
124, 11, 172, 57, 108, 47, 222, 207,
139, 212, 162, 56, 51, 163, 147, 100,
195, 176, 241, 192, 75, 86, 32, 68,
}
yen.aesNonce = []byte{
32, 103, 85, 109, 226,
169, 214, 87, 67, 166,
9, 92,
}
}
func (yen *Adyen) init() {
if yen._debug {
return
}
yen.aesKey = yen.random(32)
yen.aesNonce = yen.random(12)
}
func (yen *Adyen) random(len int) []byte {
ak := make([]byte, len)
_, _ = rand.Read(ak)
return ak
}
func validateAdYenCardInfo(data Data) error {
return nil
}