-
Notifications
You must be signed in to change notification settings - Fork 0
/
vendproxy.go
739 lines (603 loc) · 20.7 KB
/
vendproxy.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
package main
import (
_ "crypto/hmac"
"database/sql"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/sessions"
"github.com/oxipay/oxipay-vend/internal/pkg/config"
"github.com/oxipay/oxipay-vend/internal/pkg/oxipay"
"github.com/oxipay/oxipay-vend/internal/pkg/terminal"
"github.com/oxipay/oxipay-vend/internal/pkg/vend"
logrus "github.com/sirupsen/logrus"
"github.com/srinathgs/mysqlstore"
shortid "github.com/ventu-io/go-shortid"
)
// These are the possible sale statuses. // @todo move to vend
const (
statusAccepted = "ACCEPTED"
statusCancelled = "CANCELLED"
statusDeclined = "DECLINED"
statusFailed = "FAILED"
statusTimeout = "TIMEOUT"
statusUnknown = "UNKNOWN"
)
// Response We build a JSON response object that contains important information for
// which step we should send back to Vend to guide the payment flow.
type Response struct {
ID string `json:"id,omitempty"`
Amount string `json:"amount"`
RegisterID string `json:"register_id"`
Status string `json:"status"`
Signature string `json:"-"`
TrackingData string `json:"tracking_data,omitempty"`
Message string `json:"message,omitempty"`
HTTPStatus int `json:"-"`
file string
}
// DbSessionStore is the database session storage manager
var DbSessionStore *mysqlstore.MySQLStore
var log *logrus.Logger
var appConfig *config.HostConfig
var oxipayClient oxipay.Client
var db *sql.DB
var term *terminal.Terminal
func main() {
// default configuration file for prod
configurationFile := "/etc/vendproxy/vendproxy.json"
if os.Getenv("DEV") != "" {
// default configuration file for dev
configurationFile = "../configs/vendproxy.json"
}
// load config
appConfig, err := config.ReadApplicationConfig(configurationFile)
if err != nil {
logrus.Fatal(err)
}
// init our logging framework
level, err := logrus.ParseLevel(appConfig.LogLevel)
if err != nil {
logrus.Fatalf("Level %s is not a valid log level. Try setting 'info' in production ", level)
}
log = initLogger(level)
if err != nil {
log.Fatalf("Configuration Error: %s ", err)
}
db = connectToDatabase(appConfig.Database)
DbSessionStore = initSessionStore(db, appConfig.Session)
// create a reference to the Oxipay Client
oxipayClient = oxipay.NewOxipay(
appConfig.Oxipay.GatewayURL,
appConfig.Oxipay.Version,
log,
)
term = terminal.NewTerminal(db)
// We are hosting all of the content in ./assets, as the resources are
// required by the frontend.
fileServer := http.FileServer(http.Dir("../assets"))
http.Handle("/assets/", http.StripPrefix("/assets/", fileServer))
http.HandleFunc("/", Index)
http.HandleFunc("/pay", PaymentHandler)
http.HandleFunc("/register", RegisterHandler)
http.HandleFunc("/refund", RefundHandler)
// The default port is 500, but one can be specified as an env var if needed.
port := appConfig.Webserver.Port
log.Infof("Starting webserver on port %s \n", port)
//defer sessionStore.Close()
log.Fatal(http.ListenAndServe(":"+port, nil))
// @todo handle shutdowns
}
func initLogger(logLevel logrus.Level) *logrus.Logger {
logger := logrus.New()
logger.Formatter = &logrus.JSONFormatter{}
logger.SetOutput(os.Stdout)
// Only log the warning severity or above.
logger.SetLevel(logLevel)
return logger
}
func initSessionStore(db *sql.DB, sessionConfig config.SessionConfig) *mysqlstore.MySQLStore {
// @todo support multiple keys from the config so that key rotation is possible
store, err := mysqlstore.NewMySQLStoreFromConnection(db, "sessions", "/", 3600, []byte(sessionConfig.Secret))
if err != nil {
log.Warn(err)
}
store.Options = &sessions.Options{
Domain: sessionConfig.Domain,
Path: sessionConfig.Path,
MaxAge: sessionConfig.MaxAge, // 8 hours
HttpOnly: sessionConfig.HTTPOnly, // disable for this demo
}
// register the type VendPaymentRequest so that we can use it later in the session
gob.Register(&vend.PaymentRequest{})
return store
}
func connectToDatabase(params config.DbConnection) *sql.DB {
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true&loc=Local&timeout=%s",
params.Username,
params.Password,
params.Host,
params.Name,
params.Timeout,
)
log.Infof("Attempting to connect to database %s\n", dsn)
// connect to the database
// @todo grab config
db, err := sql.Open("mysql", dsn)
if err != nil {
log.Error("Unable to connect")
log.Fatal(err)
}
err = retry(30, time.Duration(10), db.Ping)
// test to make sure it's all good
if err != nil {
log.Errorf("Unable to connect to database: %s on %s", params.Name, params.Host)
log.Warn(err)
}
log.Info("Database Connected")
return db
}
func retry(attempts int, sleep time.Duration, f func() error) error {
log.Info("Attempting DB connection")
if err := f(); err != nil {
log.Warn(err)
if attempts--; attempts > 0 {
jitter := time.Duration(rand.Int63n(int64(sleep)))
sleep = sleep + jitter/2
time.Sleep(sleep)
log.Warning("Unsuccessful Retrying")
return retry(attempts, 2*sleep, f)
}
return err
}
return nil
}
func getPaymentRequestFromSession(r *http.Request) (*vend.PaymentRequest, error) {
var err error
var session *sessions.Session
vendPaymentRequest := &vend.PaymentRequest{}
session, err = getSession(r, "oxipay")
if err != nil {
log.Println(err.Error())
_ = session
return nil, err
}
// get the vendRequest from the session
vReq := session.Values["vReq"]
vendPaymentRequest, ok := vReq.(*vend.PaymentRequest)
if !ok {
msg := "Can't get vRequest from session"
return nil, errors.New(msg)
}
return vendPaymentRequest, err
}
func getSession(r *http.Request, sessionName string) (*sessions.Session, error) {
if DbSessionStore == nil {
log.Error("Can't get session store")
return nil, errors.New("Can't get session store")
}
// ensure that we have a session
session, err := DbSessionStore.Get(r, sessionName)
if err != nil {
return session, err
}
return session, nil
}
// RegisterHandler GET request. Prompt for the Merchant ID and Device Token
func RegisterHandler(w http.ResponseWriter, r *http.Request) {
logRequest(r)
browserResponse := &Response{}
switch r.Method {
case http.MethodPost:
// Bind the request from the browser to an Oxipay Registration Payload
registrationPayload, err := bindToRegistrationPayload(r)
if err != nil {
browserResponse.HTTPStatus = http.StatusBadRequest
browserResponse.Message = err.Error()
sendResponse(w, r, browserResponse)
}
err = registrationPayload.Validate()
if err != nil {
browserResponse.HTTPStatus = http.StatusBadRequest
browserResponse.Message = err.Error()
sendResponse(w, r, browserResponse)
return
}
vendPaymentRequest, err := getPaymentRequestFromSession(r)
if err == nil {
// sign the message
registrationPayload.Signature = oxipay.SignMessage(oxipay.GeneratePlainTextSignature(registrationPayload), registrationPayload.DeviceToken)
// submit to oxipay
response, err := oxipayClient.RegisterPosDevice(registrationPayload)
if err != nil {
log.Error(err)
browserResponse.Message = "We are unable to process this request "
browserResponse.HTTPStatus = http.StatusBadGateway
}
// ensure the response came from Oxipay
signedResponse, err := response.Authenticate(registrationPayload.DeviceToken)
if !signedResponse || err != nil {
browserResponse.Message = "The signature returned from Oxipay does not match the expected signature"
browserResponse.HTTPStatus = http.StatusBadRequest
} else {
// process the response
browserResponse = processOxipayResponse(response, oxipay.Registration, "")
if browserResponse.Status == statusAccepted {
log.Info("Device Successfully Registered in Oxipay")
register := terminal.NewRegister(
response.Key,
registrationPayload.DeviceID,
registrationPayload.MerchantID,
vendPaymentRequest.Origin,
vendPaymentRequest.RegisterID,
)
_, err := term.Save("vend-proxy", register)
if err != nil {
log.Error(err)
browserResponse.Message = "Unable to process request"
browserResponse.HTTPStatus = http.StatusServiceUnavailable
} else {
browserResponse.file = "../assets/templates/register_success.html"
}
}
}
} else {
log.Error(err.Error())
browserResponse.Message = "Sorry. We are unable to process this registration. Please contact support"
browserResponse.HTTPStatus = http.StatusBadRequest
}
default:
browserResponse.HTTPStatus = http.StatusOK
browserResponse.file = "../assets/templates/register.html"
}
log.Print(browserResponse.Message)
sendResponse(w, r, browserResponse)
return
}
func processOxipayResponse(oxipayResponse *oxipay.Response, responseType oxipay.ResponseType, amount string) *Response {
// Specify an external transaction ID. This value can be sent back to Vend with
// the "ACCEPT" step as the JSON key "transaction_id".
// shortID, _ := shortid.Generate()
// Build our response content, including the amount approved and the Vend
// register that originally sent the payment.
response := &Response{}
var oxipayResponseCode *oxipay.ResponseCode
switch responseType {
case oxipay.Authorisation:
oxipayResponseCode = oxipay.ProcessAuthorisationResponses()(oxipayResponse.Code)
case oxipay.Adjustment:
oxipayResponseCode = oxipay.ProcessSalesAdjustmentResponse()(oxipayResponse.Code)
case oxipay.Registration:
oxipayResponseCode = oxipay.ProcessRegistrationResponse()(oxipayResponse.Code)
}
if oxipayResponseCode == nil || oxipayResponseCode.TxnStatus == "" {
response.Message = "Unable to estabilish communication with Oxipay"
response.HTTPStatus = http.StatusBadRequest
return response
}
switch oxipayResponseCode.TxnStatus {
case oxipay.StatusApproved:
log.Infof("Status: %f", oxipayResponseCode.LogMessage)
response.Amount = amount
response.ID = oxipayResponse.PurchaseNumber
response.Status = statusAccepted
response.HTTPStatus = http.StatusOK
response.Message = oxipayResponseCode.CustomerMessage
case oxipay.StatusDeclined:
response.HTTPStatus = http.StatusOK
response.ID = ""
response.Status = statusDeclined
response.Message = oxipayResponseCode.CustomerMessage
case oxipay.StatusFailed:
response.HTTPStatus = http.StatusOK
response.ID = ""
response.Status = statusFailed
response.Message = oxipayResponseCode.CustomerMessage
default:
// default to fail...not sure if this is right
response.HTTPStatus = http.StatusOK
response.ID = ""
response.Status = statusFailed
response.Message = oxipayResponseCode.CustomerMessage
}
return response
}
func bindToRegistrationPayload(r *http.Request) (*oxipay.RegistrationPayload, error) {
if err := r.ParseForm(); err != nil {
log.Errorf("Unable to bind registration payload: %s", err)
return nil, err
}
uniqueID, _ := shortid.Generate()
deviceToken := r.Form.Get("DeviceToken")
merchantID := r.Form.Get("MerchantID")
FxlDeviceID := deviceToken + "-" + uniqueID
register := &oxipay.RegistrationPayload{
MerchantID: merchantID,
DeviceID: FxlDeviceID,
DeviceToken: deviceToken,
OperatorID: "unknown",
FirmwareVersion: "version " + oxipayClient.GetVersion(),
POSVendor: "Vend-Proxy",
}
return register, nil
}
func logRequest(r *http.Request) {
dump, _ := httputil.DumpRequest(r, true)
log.Debugf("%q ", dump)
// if r.Body != nil {
// // we need to copy the bytes out of the buffer
// // we we can inspect the contents without draining the buffer
// body, _ := ioutil.ReadAll(r.Body)
// log.Printf(colour.GDarkGray("Body: %s \n"), body)
// }
if r.RequestURI != "" {
query := r.RequestURI
log.Debugf("Query %s", query)
}
}
// Index displays the main payment processing page, giving the user options of
// which outcome they would like the Pay Example to simulate.
func Index(w http.ResponseWriter, r *http.Request) {
logRequest(r)
var err error
if err := r.ParseForm(); err != nil {
log.Errorf("Index error parsing form: %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
origin := r.Form.Get("origin")
origin, _ = url.PathUnescape(origin)
vReq := &vend.PaymentRequest{
Amount: r.Form.Get("amount"),
Origin: origin,
RegisterID: r.Form.Get("register_id"),
}
log.Debugf("Received %s from %s for register %s", vReq.Amount, vReq.Origin, vReq.RegisterID)
vReq, err = validPaymentRequest(vReq)
if err != nil {
w.Write([]byte("Not a valid request"))
w.WriteHeader(http.StatusBadRequest)
return
}
// we just want to ensure there is a terminal available
_, err = term.GetRegister(vReq.Origin, vReq.RegisterID)
// register the device if needed
if err != nil {
saveToSession(w, r, vReq)
// redirect
http.Redirect(w, r, "/register", http.StatusFound)
return
}
// refunds are triggered by a negative amount
if vReq.AmountFloat > 0 {
// payment
http.ServeFile(w, r, "../assets/templates/index.html")
} else {
// save the details of the original request
saveToSession(w, r, vReq)
// refund
http.ServeFile(w, r, "../assets/templates/refund.html")
}
}
func saveToSession(w http.ResponseWriter, r *http.Request, vReq *vend.PaymentRequest) {
session, err := getSession(r, "oxipay")
if err != nil {
log.Error(err)
}
session.Values["vReq"] = vReq
err = sessions.Save(r, w)
if err != nil {
log.Error(err)
}
log.Infof("Session initiated: %s ", session.ID)
}
func bindToPaymentPayload(r *http.Request) (*vend.PaymentRequest, error) {
r.ParseForm()
origin := r.Form.Get("origin")
origin, _ = url.PathUnescape(origin)
vReq := &vend.PaymentRequest{
Amount: r.Form.Get("amount"),
Origin: origin,
SaleID: strings.Trim(r.Form.Get("sale_id"), ""),
RegisterID: r.Form.Get("register_id"),
Code: strings.Trim(r.Form.Get("paymentcode"), ""),
}
log.Debugf("Payment: %s from %s for register %s", vReq.Amount, vReq.Origin, vReq.RegisterID)
vReq, err := validPaymentRequest(vReq)
return vReq, err
}
// RefundHandler handles performing a refund
func RefundHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
cxFields := logrus.Fields{
"module": "proxy",
"call": "RefundHandler",
}
cxLog := log.WithFields(cxFields)
// create the default response so that we always have something to send to the browser
browserResponse := new(Response)
var err error
x, err := getPaymentRequestFromSession(r)
if err != nil {
cxLog.Error("Unable to get the Refund Request from the session")
cxLog.Error(err)
http.Error(w, "There was a problem processing the request", http.StatusBadRequest)
return
}
vReq := vend.RefundRequest{
Amount: x.Amount,
Origin: x.Origin,
SaleID: strings.Trim(r.Form.Get("sale_id"), ""),
PurchaseNumber: strings.Trim(r.Form.Get("purchaseno"), ""),
RegisterID: x.RegisterID,
AmountFloat: x.AmountFloat,
}
cxFields["register_id"] = x.RegisterID
cxFields["origin"] = x.Origin
terminal := terminal.NewTerminal(db)
register, err := terminal.GetRegister(vReq.Origin, vReq.RegisterID)
if err != nil {
cxLog.Info("Register Not Found, redirecting to /register")
// redirect to registration page
http.Redirect(w, r, "/register", http.StatusFound)
return
}
cxFields["merchant_id"] = register.FxlSellerID
txnRef, err := shortid.Generate()
var oxipayPayload = &oxipay.SalesAdjustmentPayload{
Amount: strings.Replace(vReq.Amount, "-", "", 1),
MerchantID: register.FxlSellerID,
DeviceID: register.FxlRegisterID,
FirmwareVersion: "vend_integration_v0.0.1",
OperatorID: "Vend",
PurchaseRef: vReq.PurchaseNumber,
PosTransactionRef: txnRef, // @todo see if vend has a uniqueID for this also
}
// generate the plaintext for the signature
plainText := oxipay.GeneratePlainTextSignature(oxipayPayload)
log.Infof("Oxipay plain text: %s \n", plainText)
// sign the message
oxipayPayload.Signature = oxipay.SignMessage(plainText, register.FxlDeviceSigningKey)
log.Infof("Oxipay signature: %s \n", oxipayPayload.Signature)
// send authorisation to oxipay
oxipayResponse, err := oxipayClient.ProcessSalesAdjustment(oxipayPayload)
if err != nil {
// log the raw response
log.Errorf("Error Processing: %s", oxipayResponse)
return
}
// ensure the response has come from Oxipay
var validSignature bool
validSignature, err = oxipayResponse.Authenticate(register.FxlDeviceSigningKey)
if !validSignature || err != nil {
browserResponse.Message = "The signature does not match the expected signature"
browserResponse.HTTPStatus = http.StatusBadRequest
} else {
// Return a response to the browser bases on the response from Oxipay
browserResponse = processOxipayResponse(oxipayResponse, oxipay.Adjustment, oxipayPayload.Amount)
browserResponse.Amount = "0" // this is set because the payload
}
sendResponse(w, r, browserResponse)
return
}
// PaymentHandler receives the payment request from Vend and sends it to the
// payment gateway.
func PaymentHandler(w http.ResponseWriter, r *http.Request) {
var vReq *vend.PaymentRequest
var err error
browserResponse := new(Response)
logRequest(r)
vReq, err = bindToPaymentPayload(r)
if err != nil {
log.Error(err)
http.Error(w, "There was a problem processing the request", http.StatusBadRequest)
}
// looks up the database to get the fake Oxipay terminal
// so that we can issue this against Oxipay
// if the seller has correctly configured the gateway they will not hit this
// directly but it's here as safeguard
terminal, err := term.GetRegister(vReq.Origin, vReq.RegisterID)
if err != nil {
// redirect
http.Redirect(w, r, "/register", http.StatusFound)
return
}
log.Infof("Processing Payment using Oxipay register %s ", terminal.FxlRegisterID)
// send off to Oxipay
//var oxipayPayload
var oxipayPayload = &oxipay.AuthorisationPayload{
DeviceID: terminal.FxlRegisterID,
MerchantID: terminal.FxlSellerID,
PosTransactionRef: vReq.SaleID,
FinanceAmount: vReq.Amount,
FirmwareVersion: "vend_integration_v0.0.1",
OperatorID: "Vend",
PurchaseAmount: vReq.Amount,
PreApprovalCode: vReq.Code,
}
// generate the plaintext for the signature
plainText := oxipay.GeneratePlainTextSignature(oxipayPayload)
log.Debugf("Oxipay plain text: %s \n", plainText)
// sign the message
oxipayPayload.Signature = oxipay.SignMessage(plainText, terminal.FxlDeviceSigningKey)
log.Debugf("Oxipay signature: %s \n", oxipayPayload.Signature)
// send authorisation to the Oxipay POS API
oxipayResponse, err := oxipayClient.ProcessAuthorisation(oxipayPayload)
if err != nil {
http.Error(w, "There was a problem processing the request", http.StatusInternalServerError)
// log the raw response
msg := fmt.Sprintf("Error Processing: %s", oxipayResponse)
log.Error(msg)
return
}
// ensure the response has come from Oxipay
validSignature, err := oxipayResponse.Authenticate(terminal.FxlDeviceSigningKey)
if !validSignature || err != nil {
browserResponse.Message = "The signature does not match the expected signature"
browserResponse.HTTPStatus = http.StatusBadRequest
} else {
// Return a response to the browser bases on the response from Oxipay
browserResponse = processOxipayResponse(oxipayResponse, oxipay.Authorisation, oxipayPayload.PurchaseAmount)
}
sendResponse(w, r, browserResponse)
return
}
func sendResponse(w http.ResponseWriter, r *http.Request, response *Response) {
if len(response.file) > 0 {
// serve up the success page
// @todo check file exists
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
absFile, err := filepath.Abs(response.file)
if err != nil {
log.Warnf("Unable to find file %s: %e", response.file, err)
} else {
log.Infof("Serving file : %s ", absFile)
}
http.ServeFile(w, r, response.file)
return
}
// Marshal our response into JSON.
responseJSON, err := json.Marshal(response)
if err != nil {
log.Errorf("Failed to marshal response json: %s ", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
log.Debugf("Sending Response: %s \n to the browser \n", responseJSON)
if response.HTTPStatus == 0 {
response.HTTPStatus = http.StatusInternalServerError
}
w.WriteHeader(response.HTTPStatus)
w.Write(responseJSON)
return
}
func validPaymentRequest(req *vend.PaymentRequest) (*vend.PaymentRequest, error) {
var err error
// convert the amount to cents and then go back to a string for
// the checksum
if len(req.Amount) < 1 {
return req, errors.New("Amount is required")
}
req.AmountFloat, err = strconv.ParseFloat(req.Amount, 64)
if err != nil {
return req, err
}
// Oxipay deals with cents.
// Probably not great that we are mutating the value directly
// If it gets problematic we can return a copy
req.Amount = strconv.FormatFloat((req.AmountFloat * 100), 'f', 0, 64)
return req, err
}