forked from dongri/emv-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
70 lines (61 loc) · 1.82 KB
/
helper.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
package main
import (
"errors"
"fmt"
"strconv"
"github.com/noebs/emv-qrcode/emv/mpm"
)
//QR response to be used in Cashq and other products
type QR struct {
MerchantID string `json:"merchant_id,omitempty"`
MerchantName string `json:"merchant_name,omitempty"`
MerchantBankID string `json:"merchant_bank_id,omitempty"`
Amount float64 `json:"amount,omitempty"`
AcquirerID string `json:"acquirer_id,omitempty"`
RawQR *mpm.EMVQR
}
func (qr *QR) parseAccount(e *mpm.EMVQR) error {
// We have to iterate through all *possible* tags. Previously it was 26
// Merchant ids are from 2...50
if id, ok := e.MerchantAccountInformation["26"]; ok { // for the common case
p := id.Value.PaymentNetworkSpecific
// 0 = acquirer id, 1 = merchant account
//Acquirer ID and merchant ID are mandatory per EMV Book 4 QR specs
if len(p) < 2 {
return errors.New("specs are wrong")
}
qr.AcquirerID = p[0].Value
qr.MerchantBankID = p[1].Value
qr.MerchantID = p[1].Value
} else {
// Iterate through EVERY variable!
for i := 2; i <= 51; i++ {
parsedIndex := fmt.Sprintf("%02d", i)
if id, ok := e.MerchantAccountInformation[mpm.ID(parsedIndex)]; ok { // for the common case
p := id.Value.PaymentNetworkSpecific
// 0 = acquirer id, 1 = merchant account
//Acquirer ID and merchant ID are mandatory per EMV Book 4 QR specs
if len(p) < 2 {
return errors.New("specs are wrong")
}
qr.AcquirerID = p[0].Value
qr.MerchantBankID = p[1].Value
qr.MerchantID = p[1].Value
}
}
}
return nil
}
func (qr *QR) init(e *mpm.EMVQR) error {
var err error
qr.RawQR = e
qr.MerchantName = e.MerchantName.Value
qr.Amount, err = strconv.ParseFloat(e.TransactionAmount.Value, 32)
if err != nil {
return err
}
if err = qr.parseAccount(e); err != nil {
return err
}
return nil
}