forked from noebs/noebs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
345 lines (304 loc) · 9.18 KB
/
helpers.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strconv"
"strings"
"github.com/adonese/noebs/consumer"
"github.com/adonese/noebs/ebs_fields"
"github.com/go-playground/validator/v10"
"github.com/go-redis/redis/v7"
"github.com/google/uuid"
ginprometheus "github.com/zsais/go-gin-prometheus"
)
type redisPurchaseFields map[string]interface{}
func structFields(s interface{}) (fields []map[string]interface{}) {
val := reflect.Indirect(reflect.ValueOf(s))
// val is a reflect.Type now
t := val.Type()
for i := 0; i <= t.NumField(); i++ {
f := t.Field(i)
name := f.Tag.Get("json")
tag := f.Tag.Get("binding")
sType := f.Type
if tag == "" {
tag = "optional"
}
field := map[string]interface{}{
"field": name,
"is_required": tag,
"type": sType,
}
fields = append(fields, field)
}
return fields
}
//endpointToFields the corresponding struct field for the endpoint string.
// we use simple string matching to capture the results
func endpointToFields(e string) interface{} {
if strings.Contains(e, "cashIn") {
return ebs_fields.CashInFields{}
}
if strings.Contains(e, "cashOut") {
return ebs_fields.CashOutFields{}
}
if strings.Contains(e, "balance") {
return ebs_fields.BalanceFields{}
}
if strings.Contains(e, "billPayment") {
return ebs_fields.BillPaymentFields{}
}
if strings.Contains(e, "cardTransfer") {
return ebs_fields.CardTransferFields{}
}
if strings.Contains(e, "changePin") {
return ebs_fields.ChangePINFields{}
}
if strings.Contains(e, "purchase") {
return ebs_fields.PurchaseFields{}
}
return ebs_fields.GenericEBSResponseFields{}
}
//generateDoc generates API doc for this particular field
func generateDoc(e string) []map[string]interface{} {
fields := endpointToFields(e)
scheme := structFields(&fields)
return scheme
}
//getAllRoutes gets all routes for this particular engine
// perhaps, it could better be rewritten to explicitly show that
func getAllRoutes() []map[string]string {
e := GetMainEngine()
endpoints := e.Routes()
var allRoutes []map[string]string
for _, r := range endpoints {
name := strings.TrimPrefix(r.Path, "/")
mapping := map[string]string{
"http_method": r.Method,
"path": name,
}
allRoutes = append(allRoutes, mapping)
}
return allRoutes
}
var response = ebs_fields.GenericEBSResponseFields{
ResponseMessage: "Successful",
ResponseStatus: "Successful",
ResponseCode: 0,
ReferenceNumber: "094930",
ApprovalCode: "0032",
TranDateTime: "190613085100",
TerminalID: "19000019",
SystemTraceAuditNumber: 0,
ClientID: "ACTS",
PAN: "92220817",
}
func MockEBSServer() *httptest.Server {
f := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
// write the Generic ResponseBody onto the response writer
b, err := json.Marshal(&response)
if err != nil {
log.Errorf("theres an error")
}
w.Write(b)
}
return httptest.NewServer(http.HandlerFunc(f))
}
func urlToMock(url string) interface{} {
if url == ebs_fields.EBSMerchantIP+ebs_fields.BalanceEndpoint {
return mockPurchaseResponse{}
} else if url == ebs_fields.EBSMerchantIP+ebs_fields.PurchaseEndpoint {
return mockPurchaseResponse{}
} else if url == ebs_fields.EBSMerchantIP+ebs_fields.MiniStatementEndpoint {
return mockMiniStatementResponse{}
} else if url == ebs_fields.EBSMerchantIP+ebs_fields.WorkingKeyEndpoint {
fmt.Printf("i'm here..")
return mockWorkingKeyResponse{}
}
return mockWorkingKeyResponse{}
}
func Metrics() []*ginprometheus.Metric {
metrics := []*ginprometheus.Metric{
{
ID: "1234", // optional string
Name: "test_metric", // required string
Description: "Counter test metric", // required string
Type: "counter", // required string
},
{
ID: "1235", // Identifier
Name: "test_metric_2", // Metric Name
Description: "Summary test metric", // Help Description
Type: "summary", // type associated with prometheus collector
},
{
ID: "1235", // Identifier
Name: "test_metric_3", // Metric Name
Description: "Summary test metric", // Help Description
Type: "counter_vec", // type associated with prometheus collector
},
{
ID: "1236", // Identifier
Name: "test_metric_4", // Metric Name
Description: "Summary test metric", // Help Description
Type: "histogram_vec", // type associated with prometheus collector
},
// Type Options:
// counter, counter_vec, gauge, gauge_vec,
// histogram, histogram_vec, summary, summary_vec
}
return metrics
}
func validateRequest(v validator.ValidationErrors) ebs_fields.ErrorDetails {
var details []ebs_fields.ErrDetails
for _, err := range v {
details = append(details, ebs_fields.ErrorToString(err))
}
payload := ebs_fields.ErrorDetails{Details: details, Code: 400, Message: "Request fields validation error", Status: ebs_fields.BadRequest}
return payload
}
func generateUUID() string {
return uuid.New().String()
}
func handleChan(r *redis.Client) {
// when getting redis results, ALWAYS json.Marshal them
for {
select {
case c := <-consumer.BillChan:
if c.PayeeID == necPayment {
var m necBill
//FIXME there is a bug here
//mapFields, _ := additionalFieldsToHash(c.BillInfo)
m.NewFromMap(c.BillInfo)
r.HSet("meters", m.MeterNumber, m.CustomerName)
}
//} else if c.PayeeID == mtnTopUp {
// var m mtnBill
// mapFields, _ := additionalFieldsToHash(c.AdditionalData)
// m.NewFromMap(mapFields)
//} else if c.PayeeID == sudaniTopUp {
// var m sudaniBill
// mapFields, _ := additionalFieldsToHash(c.AdditionalData)
// m.NewFromMap(mapFields)
//}
}
}
}
func additionalFieldsToHash(a string) (map[string]string, error) {
fields := strings.Split(a, ";")
if len(fields) < 2 {
return nil, errors.New("index out of range")
}
out := make(map[string]string)
for _, v := range fields {
f := strings.Split(v, "=")
out[f[0]] = f[1]
}
return out, nil
}
type test struct {
AdditionalData string
PayeeID string
}
type mtnBill struct {
PaidAmount float64 `json:"PaidAmount"`
SubNewBalance float64 `json:"SubNewBalance"`
}
func (m *mtnBill) MarshalBinary() (data []byte, err error) {
d, err := json.Marshal(m)
return d, err
}
func (m *mtnBill) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, m)
}
func (m *mtnBill) NewFromMap(f map[string]string) {
m.PaidAmount, _ = strconv.ParseFloat(f["PaidAmount"], 32)
m.SubNewBalance, _ = strconv.ParseFloat(f["SubNewBalance"], 32)
}
type sudaniBill struct {
Status string `json:"Status"`
}
func (s *sudaniBill) MarshalBinary() (data []byte, err error) {
d, err := json.Marshal(s)
return d, err
}
func (s *sudaniBill) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, s)
}
func (s *sudaniBill) NewFromMap(f map[string]string) {
s.Status = f["Status"]
}
type necBill struct {
SalesAmount float64 `json:"SalesAmount"`
FixedFee float64 `json:"FixedFee"`
Token string `json:"Token"`
MeterNumber string `json:"MeterNumber"`
CustomerName string `json:"CustomerName"`
}
func (n *necBill) MarshalBinary() (data []byte, err error) {
d, err := json.Marshal(n)
return d, err
}
func (n *necBill) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, n)
}
func (n *necBill) NewFromMap(f map[string]interface{}) {
/*
"accountNo": "AM042111907231",
"customerName": "ALSAFIE BAKHIEYT HEMYDAN",
"meterFees": "0",
"meterNumber": "04203594959",
"netAmount": "10",
"opertorMessage": "Credit Purchase",
"token": "07246305192693082213",
"unitsInKWh": "66.7",
"waterFees": "0.00"
*/
n.SalesAmount, _ = strconv.ParseFloat(f["netAmount"].(string), 32)
n.CustomerName = f["customerName"].(string)
n.FixedFee, _ = strconv.ParseFloat(f["meterFees"].(string), 32)
n.MeterNumber = f["meterNumber"].(string)
n.Token = f["token"].(string)
}
const (
zainBillInquiry = "0010010002"
zainBillPayment = "0010010002"
zainTopUp = "0010010001"
mtnBillInquiry = "0010010004"
mtnBillPayment = "0010010004"
mtnTopUp = "0010010003"
necPayment = "0010020001"
sudaniInquiryPayment = "0010010006"
sudaniBillPayment = "0010010006"
sudaniTopUp = "0010030002"
moheBillInquiry = "0010030002"
moheBillPayment = "0010030002"
customsBillInquiry = "0010030003"
customsBillPayment = "0010030003"
moheArabBillInquiry = "0010030004"
moheArabBillPayment = "0010030004"
e15BillInquiry = "0010050001"
e15BillPayment = "0010050001"
)
func idToInterface(id string) (interface{}, bool) {
if id == mtnTopUp {
return &mtnBill{}, true
} else if id == sudaniTopUp {
return &sudaniBill{}, true
} else if id == necPayment {
return &necBill{}, true
}
return "", false
}
func generateFields() *ebs_fields.GenericEBSResponseFields {
f := &ebs_fields.GenericEBSResponseFields{}
f.AdditionalData = "SalesAmount=10.3;FixedFee=22.3;Token=23232;MeterNumber=12345;CustomerName=mohamed"
f.PayeeID = "0010020001"
return f
}