package main import ( "database/sql" "log" "math" "net/http" "net/url" "strconv" "time" "github.com/go-sql-driver/mysql" ) type RpnOut struct { Version string Sign_type string Mid string Notify_url string Order_amount string Order_time string //YYYYMMDDHHMMSS Order_id string User_id string User_name string User_cardno string Signature string //template Url string //where to post entire data structure //database specific Id int64 Leanwork int64 Ip4 uint32 Ip4location sql.NullString Ts mysql.NullTime } func (m *RpnOut) buildReqByLeanworkINP2P(row LeanworkIn, user_name string, user_cardno string) RpnOut { m.Version = "1.1" m.Sign_type = "MD5" m.Mid = Config.Rpn.MIDP2P m.Notify_url = Config.Rpn.UrlCallBack m.Order_id = row.OrderNo m.Order_amount = m.translateAmountFromLeanwork(row.OrderAmount) m.Order_time = m.now() m.User_id = row.CustomerId m.User_name = user_name m.User_cardno = user_cardno m.Signature = md5RpnFormP2P(*m) m.Leanwork = row.Id return *m } func (m *RpnOut) buildReqByLeanworkINFAT(row LeanworkIn, user_name string, user_cardno string) RpnOut { m.Version = "1.1" m.Sign_type = "MD5" m.Mid = Config.Rpn.MIDFAT m.Notify_url = Config.Rpn.UrlCallBack m.Order_id = row.OrderNo m.Order_amount = m.translateAmountFromLeanwork(row.OrderAmount) m.Order_time = m.now() m.User_id = row.CustomerId m.User_name = user_name m.User_cardno = user_cardno m.Signature = md5RpnFormFAT(*m) m.Leanwork = row.Id return *m } //from 0.1 to 10 cents func (m *RpnOut) translateAmountFromLeanwork(from string) string { f, _ := strconv.ParseFloat(from, 32) f = f * 100 //convert to cents t := int(math.Ceil(f)) s := strconv.Itoa(t) return s } func (m *RpnOut) now() string { t := time.Now() return t.Format("20060102150405") } //encode without disturbing it's original order func (m *RpnOut) encode() string { s := "version=" + m.Version s += "&sign_type=" + m.Sign_type s += "&mid=" + m.Mid s += "¬ify_url=" + url.QueryEscape(m.Notify_url) s += "&order_id=" + m.Order_id s += "&order_amount=" + m.Order_amount s += "&order_time=" + m.Order_time s += "&user_id=" + m.User_id s += "&user_name=" + url.QueryEscape(m.User_name) s += "&user_cardno=" + m.User_cardno s += "&signature=" + m.Signature return s } func (m *RpnOut) sendRedirect(w http.ResponseWriter, row LeanworkIn) { //execute redirect m.Url = Config.Rpn.Url tmpl.ExecuteTemplate(w, "rpnCallOutRedirect", *m) } func (m *RpnOut) getMD5Key() string { if m.Mid == Config.Rpn.MIDP2P { return Config.Rpn.MD5P2P } else if m.Mid == Config.Rpn.MIDFAT { return Config.Rpn.MD5FAT } else { log.Println("WARNING: rpnOut::getMD5Key() has bad Mid = " + m.Mid + " cannot determin P2P or FAT, MID not match config") return "" } } func getRpnOutByOrderId(order_id string) (ret RpnOut, err error) { if err = db.conn(Config); err != nil { return } defer db.close() q := "SELECT * FROM rpnOut WHERE order_id = ? ORDER BY id DESC LIMIT 1" err = db.h.QueryRow(q, order_id).Scan( &ret.Id, &ret.Leanwork, &ret.Version, &ret.Sign_type, &ret.Mid, &ret.Notify_url, &ret.Order_amount, &ret.Order_time, &ret.Order_id, &ret.User_id, &ret.User_name, &ret.User_cardno, &ret.Signature, &ret.Ip4, &ret.Ip4location, &ret.Ts) if err != nil { if err == sql.ErrNoRows { log.Println("trying to retrieve rpnOut(order_id=" + order_id + ") but not found") } else { log.Println("Error retrieving rpnOut(order_id=" + order_id + ") encountered : " + err.Error()) } } return }