-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr.go
126 lines (101 loc) · 2.62 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
package finserv
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"golang.org/x/net/context/ctxhttp"
)
// QRCaptureResult is a result after capture a transaction
type QRCaptureResult struct {
transactionID string
err error
}
// NewQRCaptureResult is a constructor for doing test
func NewQRCaptureResult(transactionID string, err error) *QRCaptureResult {
return &QRCaptureResult{
transactionID: transactionID,
err: err,
}
}
// Transaction will return transaction id and error
func (qrcr *QRCaptureResult) Transaction() (string, error) {
return qrcr.transactionID, qrcr.err
}
type qrClient struct {
client *http.Client
host string
secret string
}
// NewQRClient is a constructor
func NewQRClient(httpClient *http.Client, host, secret string) QRClient {
return &qrClient{
client: httpClient,
host: host,
secret: secret,
}
}
func (q *qrClient) do(ctx context.Context, resultChan chan<- *QRCaptureResult, client *http.Client, req *http.Request) {
defer close(resultChan)
res, err := ctxhttp.Do(ctx, client, req)
result := new(QRCaptureResult)
if err != nil {
if err == context.DeadlineExceeded {
result.err = ErrTimeout
resultChan <- result
return
}
result.err = ErrClient
resultChan <- result
return
}
if res.StatusCode != http.StatusOK {
result.err = ErrInternalServer
resultChan <- result
return
}
defer res.Body.Close()
resBody, _ := ioutil.ReadAll(res.Body)
var e qrEnvlope
if err := json.Unmarshal(resBody, &e); err != nil {
result.err = ErrInternalServer
resultChan <- result
return
}
switch e.RC {
case rcDBError:
result.err = ErrDatabase
break
case rcFormError:
result.err = ErrInvalidParameter
break
case rcSuccess:
result.err = nil
result.transactionID = e.TransactionID
break
default:
result.err = ErrInternalServer
break
}
resultChan <- result
return
}
// Capture is a function to send QRPayload fo finserv server.
// qrHost is a parameter that contains url that point to finserv qr service,
// if the value is not empty or "", it will replace the current host.
func (q *qrClient) Capture(ctx context.Context, qrp *QRPayload) *QRCaptureResult {
resultChan := make(chan *QRCaptureResult)
url := fmt.Sprintf("%s%s", q.host, qrCapturePath)
qrp.TransactionType = QRPayment
qrp.DateTime = time.Now().Format(DateFormat)
qrpByte, _ := json.Marshal(qrp)
req, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader(qrpByte))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", q.secret)
go q.do(ctx, resultChan, q.client, req)
result := <-resultChan
return result
}