From adbd1f24136023f5110f8fafdd7e261a586c0652 Mon Sep 17 00:00:00 2001 From: patrick Date: Tue, 10 Mar 2020 00:04:02 +1100 Subject: [PATCH] let user choose what methods need to make payment --- db.go | 60 +++++++++++++++++++++++++++++++++------------ form/StartPay.tmpl | 12 +++++++++ leanwork_request.go | 15 ++++++++++++ purchase.go | 29 +++++++++++++--------- 4 files changed, 88 insertions(+), 28 deletions(-) create mode 100644 form/StartPay.tmpl create mode 100644 leanwork_request.go diff --git a/db.go b/db.go index 515a87c..f3c230b 100644 --- a/db.go +++ b/db.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "net/http" + "strconv" ) type TransactionDB struct { @@ -31,30 +32,57 @@ func (m *TransactionDB) close() { defer m.h.Close() } -func (m *TransactionDB) addRequest(r *http.Request) error { - if err := m.conn(Config); err != nil { - return err +func (m *TransactionDB) addRequest(r *http.Request) (row LeanworkRequest, err error) { + if err = m.conn(Config); err != nil { + return row, err } + defer m.close() + r.ParseForm() //assuming form has been parsed - pickupUrl := r.Form["pickupUrl"][0] - receiveUrl := r.Form["receiveUrl"][0] - signType := r.Form["signType"][0] - orderNo := r.Form["orderNo"][0] - orderAmount := r.Form["orderAmount"][0] - orderCurrency := r.Form["orderCurrency"][0] - customerId := r.Form["customerId"][0] - sign := r.Form["sign"][0] + row = LeanworkRequest{} + pickupUrl, _ := retrieveFormValue(r.Form, "pickupUrl") + receiveUrl, _ := retrieveFormValue(r.Form, "receiveUrl") + signType, _ := retrieveFormValue(r.Form, "signType") + orderNo, _ := retrieveFormValue(r.Form, "orderNo") + orderAmount, _ := retrieveFormValue(r.Form, "orderAmount") + orderCurrency, _ := retrieveFormValue(r.Form, "orderCurrency") + customerId, _ := retrieveFormValue(r.Form, "customerId") + sign, _ := retrieveFormValue(r.Form, "sign") valid := isLeanworkFormValid(r.Form) ip4 := getClientIPLong(r) insForm, err := m.h.Prepare("INSERT INTO request(pickupUrl, receiveUrl, signType, orderNo, orderAmount, orderCurrency, customerId, sign, valid, ip4) VALUES(?,?,?,?,?,?,?,?,?,?)") - if err == nil { - insForm.Exec(pickupUrl, receiveUrl, signType, orderNo, orderAmount, orderCurrency, customerId, sign, valid, ip4) - log.Println("INSERT: customerId: " + customerId + " | orderAmount: " + orderCurrency + " " + orderAmount) + if err != nil { + log.Printf("Failed to prepare SQL statment for insert") + return } - m.close() - return err + + res, err := insForm.Exec(pickupUrl, receiveUrl, signType, orderNo, orderAmount, orderCurrency, customerId, sign, valid, ip4) + if err != nil { + log.Printf("Error inserting leanwork request with orderNo =%s \n", orderNo) + return + } + + id, err := res.LastInsertId() + if err != nil { + return + } + + row.Id = id + row.PickupUrl = pickupUrl + row.ReceiveUrl = receiveUrl + row.SignType = signType + row.OrderNo = orderNo + row.OrderAmount = orderAmount + row.OrderCurrency = orderCurrency + row.CustomerId = customerId + row.Sign = sign + row.Valid = valid + row.Ip4 = ip4 + log.Println("INSERT[" + strconv.FormatInt(row.Id, 10) + "]: customerId: " + customerId + " | orderAmount: " + orderCurrency + " " + orderAmount) + + return row, err } func (m *TransactionDB) addRpnOut(r RpnReq) error { diff --git a/form/StartPay.tmpl b/form/StartPay.tmpl new file mode 100644 index 0000000..3430517 --- /dev/null +++ b/form/StartPay.tmpl @@ -0,0 +1,12 @@ +{{ define "StartPay" }} + + select a form of payment +
+ + + + + +
+ +{{ end }} \ No newline at end of file diff --git a/leanwork_request.go b/leanwork_request.go new file mode 100644 index 0000000..5968c9b --- /dev/null +++ b/leanwork_request.go @@ -0,0 +1,15 @@ +package main + +type LeanworkRequest struct { + Id int64 + PickupUrl string + ReceiveUrl string + SignType string + OrderNo string + OrderAmount string + OrderCurrency string + CustomerId string + Sign string + Valid bool + Ip4 uint32 +} diff --git a/purchase.go b/purchase.go index ece141f..85fda43 100644 --- a/purchase.go +++ b/purchase.go @@ -9,24 +9,25 @@ import ( func StartPay(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { - fmt.Fprintf(w, "invalid request") + w.WriteHeader(http.StatusMethodNotAllowed) + fmt.Fprintf(w, "invalid method") return } r.ParseForm() - db.addRequest(r) - if !isLeanworkFormValid(r.Form) { - fmt.Fprintf(w, "invalid request") + + row, err := db.addRequest(r) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("Cannot initiate database transaction")) return } - for key, value := range r.Form { - fmt.Printf("%s= %s\n", key, value) - //fmt.Fprintf(w, "%s= %s\n", key, value) + if !isLeanworkFormValid(r.Form) { + w.WriteHeader(http.StatusMethodNotAllowed) + fmt.Fprintf(w, "invalid request") + return } - sign := md5LeanworkForm(r.Form) - fmt.Printf("my md5=%s, valid = %t", sign, isLeanworkFormValid(r.Form)) - - askForPaymentInfo(w, r) + askForPaymentSelection(w, row) return // m := RpnReq{} @@ -43,8 +44,12 @@ func StartPay(w http.ResponseWriter, r *http.Request) { } -func askForPaymentInfo(w http.ResponseWriter, r *http.Request) { +func askForPaymentSelection(w http.ResponseWriter, row LeanworkRequest) { + tmpl.ExecuteTemplate(w, "StartPay", row) +} + +func askForPaymentInfo(w http.ResponseWriter, r *http.Request) { t := template.Must(template.New("askForPaymentInfo").ParseGlob("form/*.tmpl")) t.ExecuteTemplate(os.Stdout, "rpnAskNameAndCard", r.Form) t.ExecuteTemplate(w, "rpnAskNameAndCard", r.Form)