Skip to content

Commit a40bcb5

Browse files
committed
small fixes
1 parent 1482a35 commit a40bcb5

File tree

3 files changed

+99
-11
lines changed

3 files changed

+99
-11
lines changed

helper.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strconv"
7+
8+
"github.com/noebs/emv-qrcode/emv/mpm"
9+
)
10+
11+
//QR response to be used in Cashq and other products
12+
type QR struct {
13+
MerchantID string `json:"merchant_id,omitempty"`
14+
MerchantName string `json:"merchant_name,omitempty"`
15+
MerchantBankID string `json:"merchant_bank_id,omitempty"`
16+
Amount float64 `json:"amount,omitempty"`
17+
AcquirerID string `json:"acquirer_id,omitempty"`
18+
RawQR *mpm.EMVQR
19+
}
20+
21+
func (qr *QR) parseAccount(e *mpm.EMVQR) error {
22+
// We have to iterate through all *possible* tags. Previously it was 26
23+
// Merchant ids are from 2...50
24+
25+
if id, ok := e.MerchantAccountInformation["26"]; ok { // for the common case
26+
p := id.Value.PaymentNetworkSpecific
27+
// 0 = acquirer id, 1 = merchant account
28+
//Acquirer ID and merchant ID are mandatory per EMV Book 4 QR specs
29+
if len(p) < 2 {
30+
return errors.New("specs are wrong")
31+
}
32+
qr.AcquirerID = p[0].Value
33+
qr.MerchantBankID = p[1].Value
34+
qr.MerchantID = p[1].Value
35+
} else {
36+
// Iterate through EVERY variable!
37+
for i := 2; i <= 51; i++ {
38+
parsedIndex := fmt.Sprintf("%02d", i)
39+
if id, ok := e.MerchantAccountInformation[mpm.ID(parsedIndex)]; ok { // for the common case
40+
p := id.Value.PaymentNetworkSpecific
41+
// 0 = acquirer id, 1 = merchant account
42+
//Acquirer ID and merchant ID are mandatory per EMV Book 4 QR specs
43+
if len(p) < 2 {
44+
return errors.New("specs are wrong")
45+
}
46+
qr.AcquirerID = p[0].Value
47+
qr.MerchantBankID = p[1].Value
48+
qr.MerchantID = p[1].Value
49+
50+
}
51+
}
52+
}
53+
return nil
54+
}
55+
56+
func (qr *QR) init(e *mpm.EMVQR) error {
57+
var err error
58+
qr.RawQR = e
59+
60+
qr.MerchantName = e.MerchantName.Value
61+
qr.Amount, err = strconv.ParseFloat(e.TransactionAmount.Value, 32)
62+
if err != nil {
63+
return err
64+
}
65+
if err = qr.parseAccount(e); err != nil {
66+
return err
67+
}
68+
return nil
69+
70+
}

main.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func main() {
1616
var qr QRCheck
1717

1818
http.HandleFunc("/encode", qr.DecodeHandler)
19+
http.HandleFunc("/decode", qr.DecodeHandler)
1920
http.HandleFunc("/", qr.DecodeHtml)
2021
http.ListenAndServe(":8012", nil)
2122

@@ -46,12 +47,20 @@ func (q QRCheck) DecodeHandler(w http.ResponseWriter, r *http.Request) {
4647
w.WriteHeader(http.StatusBadRequest)
4748
return
4849
}
49-
res, err := json.Marshal(qrencoded)
50+
51+
qr := QR{}
52+
if err := qr.init(qrencoded); err != nil {
53+
log.Printf("Error in parsing QR: %v", err)
54+
w.WriteHeader(http.StatusBadRequest)
55+
return
56+
}
57+
res, err := json.Marshal(qr)
5058
if err != nil {
5159
log.Printf("Error in parsing QR: %v", err)
5260
w.WriteHeader(http.StatusBadRequest)
5361
return
5462
}
63+
5564
w.WriteHeader(http.StatusOK)
5665
w.Write(res)
5766

@@ -73,7 +82,13 @@ func (q QRCheck) DecodeHtml(w http.ResponseWriter, r *http.Request) {
7382
w.WriteHeader(http.StatusBadRequest)
7483
return
7584
}
76-
tmpl.Execute(w, qrencoded)
85+
qr := QR{}
86+
if err := qr.init(qrencoded); err != nil {
87+
log.Printf("Error in parsing QR: %v", err)
88+
w.WriteHeader(http.StatusBadRequest)
89+
return
90+
}
91+
tmpl.Execute(w, qr)
7792

7893
}
7994

static/qr.html

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,25 @@
1616
<thead>
1717
<tr>
1818

19-
<th scope="col">MerchantName</th>
20-
<th scope="col">TransactionAmount</th>
21-
<th scope="col">TransactionCurrency</th>
22-
<th scope="col">MerchantCity</th>
19+
<th scope="row">Name</th>
20+
<th scope="row">Amount</th>
21+
<th scope="row">Currency</th>
22+
<th scope="row">City</th>
23+
<th scope="row">Account ID</th>
24+
<th scope="row">Acquirer ID</th>
2325

2426

2527
</tr>
2628
</thead>
2729
<tbody>
2830

2931
<tr>
30-
<td>{{.MerchantName.Value}}</td>
31-
<td>{{.TransactionAmount.Value}}</td>
32-
<td>{{.TransactionCurrency.Value}}</td>
33-
<td>{{.MerchantCity.Value}}</td>
34-
32+
<td>{{.MerchantName}}</td>
33+
<td>{{.Amount}}</td>
34+
<td>{{.RawQR.TransactionCurrency.Value}}</td>
35+
<td>{{.RawQR.MerchantCity.Value}}</td>
36+
<td>{{.MerchantBankID}}</td> <!--For merchant ID-->
37+
<td>{{.AcquirerID}}</td>
3538

3639
</tr>
3740

0 commit comments

Comments
 (0)