-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvietqr_test.go
190 lines (186 loc) · 4.2 KB
/
vietqr_test.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
189
190
package vietqr
import (
"testing"
)
func TestEncode(t *testing.T) {
type args struct {
ti TransferInfo
}
tests := []struct {
name string
args args
want string
}{
{
name: "only beneficiary info",
args: args{
TransferInfo{
BankCode: CAKE,
BankNo: "0905555999",
},
},
want: "00020101021138540010A00000072701240006546034011009055559990208QRIBFTTA53037045802VN6304B2A0",
},
{
name: "VNPAY",
args: args{
TransferInfo{
merchantID: "VNP-123456",
},
},
want: "00020101021138400010A0000007750110VNP-1234560208QRIBFTTA53037045802VN6304E56C",
},
{
name: "with amount",
args: args{
TransferInfo{
BankCode: ACB,
BankNo: "13579",
Amount: 120000,
},
},
want: "00020101021238490010A000000727011900069704160105135790208QRIBFTTA530370454061200005802VN63049A71",
},
{
name: "with message",
args: args{
TransferInfo{
BankCode: TECHCOMBANK,
BankNo: "9796868",
Message: "gen by sunary/vietqr",
},
},
want: "00020101021138510010A00000072701210006970407010797968680208QRIBFTTA53037045802VN62240820gen by sunary/vietqr6304BE74",
},
{
name: "full info",
args: args{
TransferInfo{
BankCode: VPBANK,
BankNo: "19372",
Amount: 152000,
Message: "gen by go-vietqr",
},
},
want: "00020101021238490010A000000727011900069704320105193720208QRIBFTTA530370454061520005802VN62200816gen by go-vietqr63040ED4",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Encode(tt.args.ti); got != tt.want {
t.Errorf("Encode() = %v, want %v", got, tt.want)
}
})
}
}
func TestDecode(t *testing.T) {
equalTransferInfoFn := func(t1, t2 *TransferInfo) bool {
if t1 == nil && t2 == nil {
return true
}
if t1 == nil || t2 == nil {
return false
}
return t1.merchantID == t2.merchantID &&
t1.BankCode == t2.BankCode &&
t1.BankNo == t2.BankNo &&
t1.Amount == t2.Amount &&
t1.Message == t2.Message
}
type args struct {
s string
}
tests := []struct {
name string
args args
want *TransferInfo
wantErr bool
}{
{
name: "wrong crc",
args: args{
"00020101021138540010A00000072701240006546034011009055559990208QRIBFTTA53037045802VN6304xxxx",
},
want: nil,
wantErr: true,
},
{
name: "guid VNPAY",
args: args{
"00020101021138400010A0000007750110VNP-1234560208QRIBFTTA53037045802VN6304E56C",
},
want: &TransferInfo{
merchantID: "VNP-123456",
},
wantErr: false,
},
{
name: "missing crc",
args: args{
"00020101021138540010A00000072701240006546034011009055559990208QRIBFTTA53037045802VN6304",
},
want: nil,
wantErr: true,
},
{
name: "correct data: only beneficiary info",
args: args{
"00020101021138540010A00000072701240006546034011009055559990208QRIBFTTA53037045802VN6304B2A0",
},
want: &TransferInfo{
BankCode: CAKE,
BankNo: "0905555999",
},
wantErr: false,
},
{
name: "with amount",
args: args{
"00020101021238490010A000000727011900069704160105135790208QRIBFTTA530370454061200005802VN63049A71",
},
want: &TransferInfo{
BankCode: ACB,
BankNo: "13579",
Amount: 120000,
},
wantErr: false,
},
{
name: "correct data: with message",
args: args{
"00020101021138510010A00000072701210006970407010797968680208QRIBFTTA53037045802VN62240820gen by sunary/vietqr6304BE74",
},
want: &TransferInfo{
BankCode: TECHCOMBANK,
BankNo: "9796868",
Message: "gen by sunary/vietqr",
},
wantErr: false,
},
{
name: "correct data: full info",
args: args{
"00020101021238490010A000000727011900069704320105193720208QRIBFTTA530370454061520005802VN62200816gen by go-vietqr63040ED4",
},
want: &TransferInfo{
BankCode: VPBANK,
BankNo: "19372",
Amount: 152000,
Message: "gen by go-vietqr",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Decode(tt.args.s)
if (err != nil) != tt.wantErr {
t.Errorf("Decode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !equalTransferInfoFn(got, tt.want) {
t.Errorf("Decode() = %v, want %v", got, tt.want)
}
})
}
}