| s += "user_name=" + r.User_name + "|" | s += "user_name=" + r.User_name + "|" | ||||
| s += "user_cardno=" + r.User_cardno + "|" | s += "user_cardno=" + r.User_cardno + "|" | ||||
| s += "key=" + md5key | s += "key=" + md5key | ||||
| fmt.Println(s) | |||||
| fmt.Println(md5str(s)) | |||||
| // fmt.Println(s) | |||||
| // fmt.Println(md5str(s)) | |||||
| return md5str(s) | return md5str(s) | ||||
| } | } |
| dbUser := c.DB.User | dbUser := c.DB.User | ||||
| dbPass := c.DB.Pass | dbPass := c.DB.Pass | ||||
| dbName := c.DB.Schema | dbName := c.DB.Schema | ||||
| h, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName+"?parseTime=true") | |||||
| dbURL := dbUser + ":" + dbPass + "@/" + dbName + "?parseTime=true" | |||||
| h, err := sql.Open(dbDriver, dbURL) | |||||
| if err != nil { | if err != nil { | ||||
| m.h = nil | m.h = nil | ||||
| log.Println("ERROR: open database " + dbDriver + " " + dbURL + " " + err.Error()) | |||||
| panic(err.Error()) | panic(err.Error()) | ||||
| } | } | ||||
| m.h = h | m.h = h | ||||
| insForm, err := m.h.Prepare("INSERT INTO leanworkIn (pickupUrl, receiveUrl, signType, orderNo, orderAmount, orderCurrency, customerId, sign, valid, ip4, md5key) VALUES(?,?,?,?,?,?,?,?,?,?,?)") | insForm, err := m.h.Prepare("INSERT INTO leanworkIn (pickupUrl, receiveUrl, signType, orderNo, orderAmount, orderCurrency, customerId, sign, valid, ip4, md5key) VALUES(?,?,?,?,?,?,?,?,?,?,?)") | ||||
| if err != nil { | if err != nil { | ||||
| log.Printf("Failed to prepare SQL statment for insert") | |||||
| log.Printf("Failed to prepare SQL statment for insert LeanworkIn") | |||||
| return | return | ||||
| } | } | ||||
| res, err := insForm.Exec(pickupUrl, receiveUrl, signType, orderNo, orderAmount, orderCurrency, customerId, sign, valid, ip4, md5key) | res, err := insForm.Exec(pickupUrl, receiveUrl, signType, orderNo, orderAmount, orderCurrency, customerId, sign, valid, ip4, md5key) | ||||
| if err != nil { | if err != nil { | ||||
| log.Printf("Error inserting leanworkIn with orderNo =%s \n", orderNo) | |||||
| log.Printf("Error inserting leanworkIn with orderNo =%s \n%+v\n%s", orderNo, r.Form, err.Error()) | |||||
| return | return | ||||
| } | } | ||||
| id, err := res.LastInsertId() | id, err := res.LastInsertId() | ||||
| if err != nil { | if err != nil { | ||||
| log.Printf("Cannot Get lastinsert ID for %+v, error= %s\n", r.Form, err.Error()) | |||||
| return | return | ||||
| } | } | ||||
| func (m *TransactionDB) addRpnOut(r RpnOut) (row RpnOut, err error) { | func (m *TransactionDB) addRpnOut(r RpnOut) (row RpnOut, err error) { | ||||
| if err = m.conn(Config); err != nil { | if err = m.conn(Config); err != nil { | ||||
| log.Printf("ERROR adding RpnOut Failed, DB error, RPNOut %+v, Error: %s\n", r, err.Error()) | |||||
| return row, err | return row, err | ||||
| } | } | ||||
| defer m.close() | defer m.close() | ||||
| user_id, user_name, user_cardno, signature, leanwork, ip4) | user_id, user_name, user_cardno, signature, leanwork, ip4) | ||||
| if err != nil { | if err != nil { | ||||
| log.Println("Failed to execute sql statment for insert RpnOut order_id=" + r.Order_id + " " + err.Error()) | log.Println("Failed to execute sql statment for insert RpnOut order_id=" + r.Order_id + " " + err.Error()) | ||||
| log.Printf("%+v", r) | |||||
| return | return | ||||
| } | } | ||||
| id, err := res.LastInsertId() | id, err := res.LastInsertId() | ||||
| if err != nil { | if err != nil { | ||||
| log.Println("Cannot get last insert ID with new RpnOut record") | log.Println("Cannot get last insert ID with new RpnOut record") | ||||
| log.Printf("%+v\n", r) | |||||
| return | return | ||||
| } | } | ||||
| row = r | row = r |
| package main | package main | ||||
| import ( | import ( | ||||
| "fmt" | |||||
| "log" | "log" | ||||
| ) | ) | ||||
| func logRequestDebug(data []byte, err error) { | func logRequestDebug(data []byte, err error) { | ||||
| if err == nil { | if err == nil { | ||||
| fmt.Printf("%s\n\n", string(data)) | |||||
| log.Printf("%s\n\n", string(data)) | |||||
| } else { | } else { | ||||
| log.Fatalf("%s\n\n", err) | log.Fatalf("%s\n\n", err) | ||||
| } | } |
| import ( | import ( | ||||
| "errors" | "errors" | ||||
| "log" | |||||
| "time" | "time" | ||||
| ) | ) | ||||
| defer db.close() | defer db.close() | ||||
| selDB, err := db.h.Query("SELECT * FROM leanworkIn WHERE id=? and sign=?", id, sign) | selDB, err := db.h.Query("SELECT * FROM leanworkIn WHERE id=? and sign=?", id, sign) | ||||
| if err != nil { | if err != nil { | ||||
| log.Printf("Trying to get LeanworkIn id=%s, sign=%s, with Error:%s\n", id, sign, err.Error()) | |||||
| return | return | ||||
| } | } | ||||
| count := 0 | count := 0 | ||||
| &row.OrderNo, &row.OrderAmount, &row.OrderCurrency, &row.CustomerId, | &row.OrderNo, &row.OrderAmount, &row.OrderCurrency, &row.CustomerId, | ||||
| &row.Sign, &row.Valid, &row.Ts, &row.Ip4, &row.Ip4Location, &row.MD5Key) | &row.Sign, &row.Valid, &row.Ts, &row.Ip4, &row.Ip4Location, &row.MD5Key) | ||||
| if err != nil { | if err != nil { | ||||
| log.Printf("Cannot Retrieve LeanworkIn Record for id=%s, sign=%s, Error=%s", id, sign, err.Error()) | |||||
| return | return | ||||
| } | } | ||||
| if count >= 1 { | |||||
| log.Printf("WARNING: LeanworkIn has %d id=%s and sign=%s \n", count, id, sign) | |||||
| } | |||||
| count++ | count++ | ||||
| } | } | ||||
| bodyBytes, err := ioutil.ReadAll(resp.Body) | bodyBytes, err := ioutil.ReadAll(resp.Body) | ||||
| if err != nil { | if err != nil { | ||||
| log.Println("Fatal: Cannot read leanwork Http Response " + err.Error()) | log.Println("Fatal: Cannot read leanwork Http Response " + err.Error()) | ||||
| } | |||||
| bodyString := string(bodyBytes) | |||||
| m.LeanworkResp = bodyString | |||||
| m.add2db() | |||||
| if strings.Contains(strings.ToLower(bodyString), "success") { | |||||
| retry = false | |||||
| } else { | } else { | ||||
| log.Println("Leanwork response without success word : " + bodyString) | |||||
| bodyString := string(bodyBytes) | |||||
| m.LeanworkResp = bodyString | |||||
| m.add2db() | |||||
| if strings.Contains(strings.ToLower(bodyString), "success") { | |||||
| log.Printf("Leanwork Out [%d]: orderNo=%s, %s %s success\n", m.Leanwork, m.OrderNo, m.OrderCurrency, m.OrderAmount) | |||||
| retry = false | |||||
| } else { | |||||
| log.Println("Leanwork response without 'success' : " + bodyString) | |||||
| } | |||||
| } | } | ||||
| retry = false | retry = false | ||||
| } | } | ||||
| func buildLeanworkOutByRpnIn(ri RpnIn) (ret LeanworkOut, err error) { | func buildLeanworkOutByRpnIn(ri RpnIn) (ret LeanworkOut, err error) { | ||||
| li, err := getLeanworkInById(ri.Leanwork) | li, err := getLeanworkInById(ri.Leanwork) | ||||
| if err != nil { | if err != nil { | ||||
| log.Println("cannot get LeanworkIn by ID %s, %s", ri.Leanwork, err.Error()) | |||||
| return | return | ||||
| } | } | ||||
| ret.Leanwork = ri.Leanwork | ret.Leanwork = ri.Leanwork | ||||
| func (m *LeanworkOut) getLeanworkMD5Key() string { | func (m *LeanworkOut) getLeanworkMD5Key() string { | ||||
| li, err := getLeanworkInById(m.Leanwork) | li, err := getLeanworkInById(m.Leanwork) | ||||
| if err != nil { | if err != nil { | ||||
| log.Printf("Error determin P2P or FAT, leanworkIn = %d", m.Leanwork) | |||||
| log.Printf("ERROR unknown P2P or FAT, leanworkIn id= %d", m.Leanwork) | |||||
| return "" | return "" | ||||
| } | } | ||||
| return li.MD5Key | return li.MD5Key | ||||
| func (m *LeanworkOut) add2db() (ret LeanworkOut, err error) { | func (m *LeanworkOut) add2db() (ret LeanworkOut, err error) { | ||||
| if err = db.conn(Config); err != nil { | if err = db.conn(Config); err != nil { | ||||
| log.Println("ERROR cannot addd LeanwokOut, db conn error " + err.Error()) | |||||
| return | return | ||||
| } | } | ||||
| defer db.close() | defer db.close() | ||||
| m.TransactionId, m.Status, m.Sign, m.LeanworkResp) | m.TransactionId, m.Status, m.Sign, m.LeanworkResp) | ||||
| if err != nil { | if err != nil { | ||||
| log.Printf("Error inserting leanworkOut with orderNo =%s, %s \n", m.OrderNo, err.Error()) | |||||
| log.Printf("Error inserting leanworkOut with orderNo =%s, %s %s %s \n", m.OrderNo, m.OrderCurrency, m.OrderAmount, err.Error()) | |||||
| return | return | ||||
| } | } | ||||
| id, err := res.LastInsertId() | id, err := res.LastInsertId() | ||||
| if err != nil { | if err != nil { | ||||
| log.Printf("Cannot retrieve lastInsertId for orderID %s", m.OrderNo) | |||||
| log.Printf("Cannot retrieve lastInsertId for orderID %s, %s %s", m.OrderNo, m.OrderCurrency, m.OrderAmount) | |||||
| return | return | ||||
| } | } | ||||
| http.HandleFunc("/", fs) | http.HandleFunc("/", fs) | ||||
| //start listening | //start listening | ||||
| log.Println("Server started on: http://localhost:8080") | |||||
| http.ListenAndServe(Config.Server.Host+":"+Config.Server.Port, nil) | |||||
| svr := Config.Server.Host + ":" + Config.Server.Port | |||||
| log.Println("Server started on: " + svr) | |||||
| http.ListenAndServe(svr, nil) | |||||
| //program never reach here. | //program never reach here. | ||||
| } | } |
| "fmt" | "fmt" | ||||
| "log" | "log" | ||||
| "net/http" | "net/http" | ||||
| "net/http/httputil" | |||||
| "text/template" | "text/template" | ||||
| ) | ) | ||||
| //leanwork in for alipay | //leanwork in for alipay | ||||
| func leanworkInFAT(w http.ResponseWriter, r *http.Request) { | func leanworkInFAT(w http.ResponseWriter, r *http.Request) { | ||||
| if r.Method != "POST" { | if r.Method != "POST" { | ||||
| log.Println("Invalid Request expect POST for leanworkInFAT") | |||||
| logRequestDebug(httputil.DumpRequest(r, true)) | |||||
| errPage(w, http.StatusMethodNotAllowed, "invalid method") | errPage(w, http.StatusMethodNotAllowed, "invalid method") | ||||
| return | return | ||||
| } | } | ||||
| row, err := db.addRequest(r, Config.LeanWork.MD5FAT) | row, err := db.addRequest(r, Config.LeanWork.MD5FAT) | ||||
| if err != nil { | if err != nil { | ||||
| log.Printf("ERROR failed to add leanworkIn %+v \n", r.Form) | |||||
| logRequestDebug(httputil.DumpRequest(r, true)) | |||||
| errPage(w, http.StatusInternalServerError, "Cannot initiate database transaction for incoming request") | errPage(w, http.StatusInternalServerError, "Cannot initiate database transaction for incoming request") | ||||
| return | return | ||||
| } | } | ||||
| // w.WriteHeader(http.StatusMethodNotAllowed) | // w.WriteHeader(http.StatusMethodNotAllowed) | ||||
| // fmt.Fprintf(w, "invalid request") | // fmt.Fprintf(w, "invalid request") | ||||
| errPage(w, http.StatusBadRequest, "validation of FAT input parameters failed") | errPage(w, http.StatusBadRequest, "validation of FAT input parameters failed") | ||||
| log.Printf("Invalid Request received, Signature is not correct") | |||||
| logRequestDebug(httputil.DumpRequest(r, true)) | |||||
| return | return | ||||
| } | } | ||||
| func leanworkInP2P(w http.ResponseWriter, r *http.Request) { | func leanworkInP2P(w http.ResponseWriter, r *http.Request) { | ||||
| if r.Method != "POST" { | if r.Method != "POST" { | ||||
| log.Println("Invalid P2P Request expect POST for leanworkInP2P") | |||||
| logRequestDebug(httputil.DumpRequest(r, true)) | |||||
| errPage(w, http.StatusMethodNotAllowed, "invalid method") | errPage(w, http.StatusMethodNotAllowed, "invalid method") | ||||
| return | return | ||||
| } | } | ||||
| // w.WriteHeader(http.StatusMethodNotAllowed) | // w.WriteHeader(http.StatusMethodNotAllowed) | ||||
| // fmt.Fprintf(w, "invalid request") | // fmt.Fprintf(w, "invalid request") | ||||
| errPage(w, http.StatusBadRequest, "validation of P2P input parameters failed") | errPage(w, http.StatusBadRequest, "validation of P2P input parameters failed") | ||||
| log.Printf("Invalid Request received, Signature is not correct") | |||||
| logRequestDebug(httputil.DumpRequest(r, true)) | |||||
| return | return | ||||
| } | } | ||||
| return | return | ||||
| } | } | ||||
| //directly send leanwork success without contacting RPN | |||||
| func debugStartLeanworkCallBack(li LeanworkIn) { | func debugStartLeanworkCallBack(li LeanworkIn) { | ||||
| lo := buildLeanworkOutByIn(li) | lo := buildLeanworkOutByIn(li) | ||||
| "net/http/httputil" | "net/http/httputil" | ||||
| ) | ) | ||||
| //RPN callback us about payment result | |||||
| func rpnNotify(w http.ResponseWriter, r *http.Request) { | func rpnNotify(w http.ResponseWriter, r *http.Request) { | ||||
| logRequestDebug(httputil.DumpRequest(r, true)) | |||||
| if r.Method != "POST" { | if r.Method != "POST" { | ||||
| errPage(w, http.StatusMethodNotAllowed, "invalid request") | errPage(w, http.StatusMethodNotAllowed, "invalid request") | ||||
| log.Println("ERROR RPN request,not HTTP POST") | |||||
| logRequestDebug(httputil.DumpRequest(r, true)) | |||||
| return | return | ||||
| } | } | ||||
| ri, err := GetRpnInFromHTTPRequest(r) //ParseForm called | ri, err := GetRpnInFromHTTPRequest(r) //ParseForm called | ||||
| if err != nil { | if err != nil { | ||||
| errPage(w, http.StatusBadRequest, "invalid parameters "+err.Error()) | errPage(w, http.StatusBadRequest, "invalid parameters "+err.Error()) | ||||
| log.Println("ERROR bad request, err=" + err.Error()) | |||||
| logRequestDebug(httputil.DumpRequest(r, true)) | |||||
| return | return | ||||
| } | } | ||||
| _, err = ri.add2db() //TODO:check error add | _, err = ri.add2db() //TODO:check error add | ||||
| if err != nil { | if err != nil { | ||||
| log.Printf("failed to add rpnIn %+v , error is %s", ri, err.Error()) | log.Printf("failed to add rpnIn %+v , error is %s", ri, err.Error()) | ||||
| logRequestDebug(httputil.DumpRequest(r, true)) | |||||
| } | } | ||||
| fmt.Fprintf(w, "[SUCCESS]") | fmt.Fprintf(w, "[SUCCESS]") | ||||
| //receive RPN user name and card number | //receive RPN user name and card number | ||||
| func rpnNameAndCard(w http.ResponseWriter, r *http.Request) { | func rpnNameAndCard(w http.ResponseWriter, r *http.Request) { | ||||
| if r.Method != "POST" { | if r.Method != "POST" { | ||||
| log.Println("ERROR collecting Name and Card Number,not HTTP POST") | |||||
| logRequestDebug(httputil.DumpRequest(r, true)) | |||||
| errPage(w, http.StatusMethodNotAllowed, "invalid request") | errPage(w, http.StatusMethodNotAllowed, "invalid request") | ||||
| return | return | ||||
| } | } | ||||
| if !(id != "" && sign != "" && user_name != "" && user_card != "") { | if !(id != "" && sign != "" && user_name != "" && user_card != "") { | ||||
| errPage(w, http.StatusBadRequest, "missing parameters") | errPage(w, http.StatusBadRequest, "missing parameters") | ||||
| log.Printf("WARNING collecting Name and Card Number, require non-empty value, id=%s, sign=%s, user_name=%s, user_card=%s\n", id, sign, user_name, user_card) | |||||
| return | return | ||||
| } | } | ||||
| row, err := getRequestRowByIdAndSign(id, sign) | row, err := getRequestRowByIdAndSign(id, sign) | ||||
| if err != nil { | if err != nil { | ||||
| w.WriteHeader(http.StatusBadRequest) | |||||
| fmt.Fprintf(w, "bad parameters") | |||||
| errPage(w, http.StatusBadRequest, "bad parameters") | |||||
| log.Printf("WARNING: collecting Name and Card Number, signature not correct %+f\n", r.Form) | |||||
| return | return | ||||
| } | } | ||||
| "errors" | "errors" | ||||
| "log" | "log" | ||||
| "net/http" | "net/http" | ||||
| "net/http/httputil" | |||||
| "strconv" | "strconv" | ||||
| "time" | "time" | ||||
| ) | ) | ||||
| func (m *RpnIn) md5key() string { | func (m *RpnIn) md5key() string { | ||||
| if m.Order_id == "" { | if m.Order_id == "" { | ||||
| log.Println("WARN: rpnIn:md5key() trying to get MD5Key with empty orderID") | |||||
| return "" | return "" | ||||
| } | } | ||||
| ro, err := getRpnOutByOrderId(ret.Order_id) | ro, err := getRpnOutByOrderId(ret.Order_id) | ||||
| if err != nil || ro.Order_id != ret.Order_id { | if err != nil || ro.Order_id != ret.Order_id { | ||||
| log.Println("Cannot get RpnOut by order_id=" + ret.Order_id) | log.Println("Cannot get RpnOut by order_id=" + ret.Order_id) | ||||
| logRequestDebug(httputil.DumpRequest(r, true)) | |||||
| return | return | ||||
| } | } | ||||
| if ret.Signature != ret.signature() { | if ret.Signature != ret.signature() { | ||||
| log.Println("Invalid RpnIn Signature") | log.Println("Invalid RpnIn Signature") | ||||
| err = errors.New("Invalid signauture for RpnIn") | err = errors.New("Invalid signauture for RpnIn") | ||||
| logRequestDebug(httputil.DumpRequest(r, true)) | |||||
| return | return | ||||
| } | } | ||||
| return | return |
| } else if m.Mid == Config.Rpn.MIDFAT { | } else if m.Mid == Config.Rpn.MIDFAT { | ||||
| return Config.Rpn.MD5FAT | return Config.Rpn.MD5FAT | ||||
| } else { | } else { | ||||
| log.Println("WARNING: rpnOut::getMD5Key() has bad Mid = " + m.Mid + " cannot determin P2P or FAT, MID not match config") | |||||
| return "" | return "" | ||||
| } | } | ||||
| } | } |