-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtransactionprocessor.go
116 lines (85 loc) · 2.13 KB
/
transactionprocessor.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
package main
import (
"fmt"
"github.com/greenboxal/emv-kernel/emv"
"sort"
)
type terminalPinAsker struct{}
func (t *terminalPinAsker) RetrievePin() (string, error) {
pin := ""
fmt.Printf("Enter the card PIN\n")
fmt.Printf("Please note that this COULD block your card\n")
fmt.Printf("PIN: ")
fmt.Scanf("%s\n", &pin)
return pin, nil
}
type TransactionProcessor struct {
card *emv.Card
ctx *emv.Context
}
func NewTransactionProcessor(card *emv.Card) *TransactionProcessor {
return &TransactionProcessor{
card: card,
}
}
func (t *TransactionProcessor) Initialize() error {
t.ctx = emv.NewContext(t.card, &emv.ContextConfig{
Terminal: emv.Terminal{
CountryCode: []byte{0x00, 0x76},
},
}, &fileCertificateManager{"./certs"})
err := t.ctx.Initialize()
if err != nil {
return err
}
info, err := t.selectApplication()
if err != nil {
return err
}
_, err = t.ctx.SelectApplication(info.Name)
if err != nil {
return err
}
raw, _ := t.ctx.CardInformation.Raw.EncodeTlv()
fmt.Printf("%x\n", raw)
_, err = t.ctx.Authenticate()
if err != nil {
return err
}
return nil
}
func (t *TransactionProcessor) selectApplication() (*emv.ApplicationInformation, error) {
applications, err := t.ctx.ListApplications(false, hints)
if err != nil {
return nil, err
}
sort.Sort(ApplicationSorter(applications))
if len(applications) == 0 {
return nil, fmt.Errorf("no application available")
}
selected := 0
if len(applications) == 1 && applications[0].Priority&0x80 == 0 {
selected = 1
} else {
fmt.Printf("Available applications:\n")
fmt.Printf("\t00: Cancel\n")
for i, app := range applications {
fmt.Printf("\t%02d: %s (%10x)\n", i+1, app.Label, app.Name)
}
fmt.Printf("\n")
fmt.Printf("Enter the wanted application: ")
fmt.Scanf("%d\n", &selected)
}
if selected == 0 {
return nil, fmt.Errorf("operation was cancelled")
}
if selected > len(applications) {
return nil, fmt.Errorf("invalid application selected")
}
app := applications[selected-1]
fmt.Printf("Selected %s (%10x)\n", app.Label, app.Name)
return app, nil
}
func (t *TransactionProcessor) Process() error {
return nil
}