package main import ( "errors" "fmt" "math" "net/http" "net/url" "strconv" "time" ) // var url = "https://deposit.paylomo.net/pay.php?r=payEasy" //production // var url = "https://deposit-mac.chinapaytech.com/pay.php?r=payEasy" //test // var md5p2p = "370296119874502" // var md5fat = "207841502473198" type RpnReq struct { version string sign_type string mid string notify_url string order_id string order_amount string order_time string //YYYYMMDDHHMMSS user_id string user_name string user_cardno string signature string url string //where to post entire data structure } // //build request from leanwork request forms // func (m *RpnReq) buildReqByForm(form url.Values) RpnReq { // r := RpnReq{} // r.version = "1.1" // r.sign_type = "MD5" // r.mid = "EU85201311P2P" // r.notify_url = "http://rpn.supertraderfx.ayvwd8em49pvoa3g.com/rpn_notify" // r.order_id = form["orderNo"][0] // r.order_amount = form["orderAmount"][0] //cents // r.order_time = m.now() // r.user_id = form["customerId"][0] // r.user_name = "SuperForex" // r.user_cardno = "6212262002002377849" // r.signature = md5RpnFormP2P(r) // *m = r // return r // } func (m *RpnReq) buildReqByLeanworkRequestP2P(row LeanworkRequest, user_name string, user_cardno string) RpnReq { r := RpnReq{} r.version = "1.1" r.sign_type = "MD5" r.mid = Config.Rpn.MIDP2P r.notify_url = "http://rpn.supertraderfx.ayvwd8em49pvoa3g.com/rpn_notify" r.order_id = row.OrderNo r.order_amount = m.translateAmountFromLeanwork(row.OrderAmount) r.order_time = m.now() r.user_id = row.CustomerId r.user_name = user_name r.user_cardno = user_cardno r.signature = md5RpnFormP2P(r) *m = r return r } func (m *RpnReq) buildReqByLeanworkRequestFAT(row LeanworkRequest, user_name string, user_cardno string) RpnReq { r := RpnReq{} r.version = "1.1" r.sign_type = "MD5" r.mid = Config.Rpn.MIDFAT r.notify_url = "http://rpn.supertraderfx.ayvwd8em49pvoa3g.com/rpn_notify" r.order_id = row.OrderNo r.order_amount = m.translateAmountFromLeanwork(row.OrderAmount) r.order_time = m.now() r.user_id = row.CustomerId r.user_name = user_name r.user_cardno = user_cardno r.signature = md5RpnFormFAT(r) *m = r return r } //from 0.1 to 10 cents func (m *RpnReq) 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 *RpnReq) now() string { t := time.Now() return t.Format("20060102150405") } //send request to RPN to initiate transaction func (m *RpnReq) SendReq(form url.Values) (*http.Response, error) { return nil, nil // r := m.buildReqByForm(form) // hc := http.Client{Timeout: 15 * time.Second} // myForm := url.Values{} // myForm.Set("version", r.version) // myForm.Set("sign_type", r.sign_type) // myForm.Set("mid", r.mid) // myForm.Set("notify_url", r.notify_url) // myForm.Set("order_id", r.order_id) // myForm.Set("order_amount", r.order_amount) // myForm.Set("order_time", r.order_time) // myForm.Set("user_id", r.user_id) // myForm.Set("user_name", r.user_name) // myForm.Set("user_cardno", r.user_cardno) // myForm.Set("signature", r.signature) // //req, err := http.NewRequest("POST", "https://lawipac.com/dumprequest.php", strings.NewReader(myForm.Encode())) // req, err := http.NewRequest("POST", Config.Rpn.UrlTest, strings.NewReader(m.encode())) // if err != nil { // panic("wrong") // } // req.Header.Add("Content-Type", "application/x-www-form-urlencoded") // req.Header.Add("content-Length", strconv.Itoa(len(form.Encode()))) // return hc.Do(req) //Config.Rpn.UrlTest // return http.PostForm(Config.Rpn.UrlTest, myForm) //return http.PostForm("https://lawipac.com/dumprequest.php", myForm) } //encode without disturbing it's original order func (m *RpnReq) 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=" + m.user_name s += "&user_cardno=" + m.user_cardno s += "&signature=" + m.signature return s } func retrieveFormValue(form url.Values, key string) (r string, err error) { if _, ok := form[key]; ok { r = form[key][0] err = nil } else { r = "" err = errors.New("Key [" + key + "] not found in HTTP request") } return r, err } //receive RPN user name and card number func rpnNameAndCard(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { w.WriteHeader(http.StatusMethodNotAllowed) fmt.Fprintf(w, "invalid request") return } r.ParseForm() id := r.FormValue("id") sign := r.FormValue("sign") user_name := r.FormValue("name") user_card := r.FormValue("card") rpn_type := r.FormValue("rpnType") if !(id != "" && sign != "" && user_name != "" && user_card != "") { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "missing parameters") return } row, err := getRequestRowByIdAndSign(id, sign) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "bad parameters") return } ro := RpnReq{} if rpn_type == "rpnp2p" { ro.buildReqByLeanworkRequestP2P(row, user_name, user_card) } else { ro.buildReqByLeanworkRequestFAT(row, user_name, user_card) } //build rpn redirect page and send it ro.sendRedirect(w, row) } func (m *RpnReq) sendRedirect(w http.ResponseWriter, row LeanworkRequest) { //create db record m.url = Config.Rpn.Url tmpl.ExecuteTemplate(w, "rpnCallOutRedirect", *m) }