-
Notifications
You must be signed in to change notification settings - Fork 1
/
qr.go
289 lines (249 loc) · 7.94 KB
/
qr.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package vnqrpay
import (
"errors"
"fmt"
"strconv"
"strings"
)
func MakeCRC(content string) string {
sum := CRC16CCITT(content)
hex := "0000" + strconv.FormatInt(int64(sum), 16)
result := strings.ToUpper(hex[len(hex)-4:])
return result
}
func VerifyCRC(content string) bool {
value := content[:len(content)-4]
extracted := strings.ToUpper(content[len(content)-4:])
crc := MakeCRC(value)
return crc == extracted
}
func Parse(content string) (qr *QRPay, err error) {
qr = &QRPay{}
if len(content) < 4 {
return qr, errors.New("invalid QR: too short")
}
qr.IsValid = VerifyCRC(content)
if !qr.IsValid {
return qr, errors.New("invalid QR: CRC is invalid")
}
qr.parseQRContent(content)
return
}
func (qr *QRPay) Build() (content string, err error) {
version := combineFieldData(FieldVersion, defaultStrValue(qr.Version, "01"))
initMethod := combineFieldData(FieldInitMethod, defaultStrValue(qr.Version, "11"))
guid := combineFieldData(QRFieldID(ProviderFieldGUID), string(qr.Provider.GUID))
providerDataContent := ""
if qr.Provider.GUID == VietQRGUID {
bankBin := combineFieldData(QRFieldID(VietQRBankBin), qr.Consumer.BankBin)
bankNumber := combineFieldData(QRFieldID(VietQRBankNumber), qr.Consumer.BankNumber)
providerDataContent = bankBin + bankNumber
} else if qr.Provider.GUID == VNPayGUID {
providerDataContent = defaultStrValue(qr.Merchant.ID, "")
}
provider := combineFieldData(QRFieldID(ProviderFieldData), providerDataContent)
service := combineFieldData(QRFieldID(ProviderFieldService), fmt.Sprint(qr.Provider.Service))
providerData := combineFieldData(qr.Provider.FieldID, guid+provider+service)
category := combineFieldData(FieldCategory, qr.Category)
currency := combineFieldData(FieldCurrency, defaultStrValue(qr.Currency, "704"))
amount := combineFieldData(FieldAmount, qr.Amount)
tipAndFeeType := combineFieldData(FieldTipAndFeeType, qr.TipAndFeeType)
tipAndFeeAmount := combineFieldData(FieldTipAndFeeAmount, qr.TipAndFeeAmount)
tipAndFeePercent := combineFieldData(FieldTipAndFeePercent, qr.TipAndFeePercent)
nation := combineFieldData(FieldNation, defaultStrValue(qr.Nation, "VN"))
merchantName := combineFieldData(FieldMerchantName, qr.Merchant.Name)
city := combineFieldData(FieldCity, qr.City)
zipCode := combineFieldData(FieldZipCode, qr.ZipCode)
buildNumber := combineFieldData(QRFieldID(AdditionalDataBillNumber), qr.AdditionalData.BillNumber)
mobileNumber := combineFieldData(QRFieldID(AdditionalDataMobileNumber), qr.AdditionalData.MobileNumber)
storeLabel := combineFieldData(QRFieldID(AdditionalDataStoreLabel), qr.AdditionalData.Store)
loyaltyNumber := combineFieldData(QRFieldID(AdditionalDataLoyaltyNumber), qr.AdditionalData.LoyaltyNumber)
reference := combineFieldData(QRFieldID(AdditionalDataReferenceLabel), qr.AdditionalData.Reference)
customerLabel := combineFieldData(QRFieldID(AdditionalDataCustomerLabel), qr.AdditionalData.CustomerLabel)
terminal := combineFieldData(QRFieldID(AdditionalDataTerminalLabel), qr.AdditionalData.Terminal)
purpose := combineFieldData(QRFieldID(AdditionalDataPurposeOfTransaction), qr.AdditionalData.Purpose)
dataRequest := combineFieldData(QRFieldID(AdditionalDataAdditionalConsumerDataRequest), qr.AdditionalData.DataRequest)
additionalDataContent := buildNumber + mobileNumber + storeLabel + loyaltyNumber + reference + customerLabel + terminal + purpose + dataRequest
additionalData := combineFieldData(FieldAdditionalData, additionalDataContent)
content = version + initMethod + providerData + category + currency + amount + tipAndFeeType + tipAndFeeAmount + tipAndFeePercent + nation + merchantName + city + zipCode + additionalData + string(FieldCRC) + "04"
crc := MakeCRC(content)
content += crc
return
}
func CreateVietQR(opts VietQROptions) (qr *QRPay, err error) {
qr = &QRPay{}
if opts.Amount != "" {
qr.InitMethod = "12"
} else {
qr.InitMethod = "11"
}
qr.Provider.FieldID = FieldVietQR
qr.Provider.GUID = VietQRGUID
qr.Provider.Name = VietQRProvider
if opts.Service == "" {
qr.Provider.Service = VietQRByAccountNumber
} else {
qr.Provider.Service = opts.Service
}
qr.Consumer = opts.Consumer
qr.Amount = opts.Amount
qr.AdditionalData.Purpose = opts.Purpose
return
}
func CreateVNPay(opts VNPayOptions) (qr *QRPay, err error) {
qr = &QRPay{}
qr.Merchant.ID = opts.Merchant.ID
qr.Merchant.Name = opts.Merchant.Name
qr.Provider.FieldID = FieldVNPayQR
qr.Provider.GUID = VNPayGUID
qr.Provider.Name = VNPayProvider
qr.Amount = opts.Amount
qr.AdditionalData = opts.AdditionalData
return
}
func (qr *QRPay) parseQRContent(content string) error {
id, value, nextValue, err := sliceContent(content)
if err != nil {
return err
}
switch QRFieldID(id) {
case FieldVersion:
qr.Version = value
case FieldInitMethod:
qr.InitMethod = value
case FieldVietQR:
qr.Provider.FieldID = QRFieldID(id)
err = qr.parseProviderInfo(value)
if err != nil {
return err
}
case FieldVNPayQR:
qr.Provider.FieldID = QRFieldID(id)
err = qr.parseProviderInfo(value)
if err != nil {
return err
}
case FieldCategory:
qr.Category = value
case FieldCurrency:
qr.Currency = value
case FieldAmount:
qr.Amount = value
case FieldTipAndFeeAmount:
qr.TipAndFeeAmount = value
case FieldTipAndFeeType:
qr.TipAndFeeType = value
case FieldTipAndFeePercent:
qr.TipAndFeePercent = value
case FieldNation:
qr.Nation = value
case FieldMerchantName:
qr.Merchant.Name = value
case FieldCity:
qr.City = value
case FieldZipCode:
qr.ZipCode = value
case FieldAdditionalData:
err = qr.parseAdditionalData(value)
if err != nil {
return err
}
case FieldCRC:
qr.CRC = value
}
if len(nextValue) > 4 {
qr.parseQRContent(nextValue)
return err
}
return err
}
func defaultStrValue(val, defaultVal string) string {
if val == "" {
return defaultVal
}
return val
}
func combineFieldData(id QRFieldID, value string) string {
if len(id) != 2 || len(value) == 0 {
return ""
}
length := fmt.Sprintf("%02d", len(value))
return fmt.Sprintf("%v%v%v", id, length, value)
}
func sliceContent(content string) (string, string, string, error) {
id := content[:2]
length, err := strconv.Atoi(content[2:4])
if err != nil {
return id, "", "", err
}
value := content[4 : 4+length]
nextValue := content[4+length:]
return id, value, nextValue, nil
}
func (qr *QRPay) parseProviderInfo(content string) error {
id, value, nextValue, err := sliceContent(content)
if err != nil {
return err
}
switch QRProviderFieldID(id) {
case ProviderFieldGUID:
qr.Provider.GUID = QRProviderGUID(value)
case ProviderFieldData:
if qr.Provider.GUID == VNPayGUID {
qr.Provider.Name = VNPayProvider
qr.Merchant.ID = value
} else if qr.Provider.GUID == VietQRGUID {
qr.Provider.Name = VietQRProvider
err := qr.parseVietQRConsumer(value)
if err != nil {
return err
}
}
case ProviderFieldService:
qr.Provider.Service = VietQRService(value)
}
if len(nextValue) > 4 {
qr.parseProviderInfo(nextValue)
}
return nil
}
func (qr *QRPay) parseVietQRConsumer(content string) error {
id, value, nextValue, err := sliceContent(content)
if err != nil {
return err
}
switch VietQRConsumerID(id) {
case VietQRBankBin:
qr.Consumer.BankBin = value
case VietQRBankNumber:
qr.Consumer.BankNumber = value
}
if len(nextValue) > 4 {
qr.parseVietQRConsumer(nextValue)
}
return nil
}
func (qr *QRPay) parseAdditionalData(content string) error {
id, value, nextValue, err := sliceContent(content)
if err != nil {
return err
}
switch AdditionalDataID(id) {
case AdditionalDataPurposeOfTransaction:
qr.AdditionalData.Purpose = value
case AdditionalDataBillNumber:
qr.AdditionalData.BillNumber = value
case AdditionalDataMobileNumber:
qr.AdditionalData.MobileNumber = value
case AdditionalDataReferenceLabel:
qr.AdditionalData.Reference = value
case AdditionalDataStoreLabel:
qr.AdditionalData.Store = value
case AdditionalDataTerminalLabel:
qr.AdditionalData.Terminal = value
}
if len(nextValue) > 4 {
qr.parseAdditionalData(nextValue)
}
return nil
}