package main import ( "errors" "fmt" "net/http" "net/url" "strconv" "strings" "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 = md4RpnFormP2P(r) *m = r return r } 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) { 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" { fmt.Fprintf(w, "invalid request") return } r.ParseForm() order_id, ok1 := retrieveFormValue(r.Form, "id") signature, ok2 := retrieveFormValue(r.Form, "si") user_name, ok3 := retrieveFormValue(r.Form, "name") user_card, ok4 := retrieveFormValue(r.Form, "card") if !(ok1 == nil && ok2 == nil && ok3 == nil && ok4 == nil) { fmt.Fprintf(w, "missing parameters") return } row, err := db.updateRpnOutNameAndCard(order_id, signature, user_name, user_card) if err != nil { fmt.Fprintf(w, "cannot update transaction") return } //build rpn redirect page and send it row.sendRedirect(w) } func (m *RpnReq) sendRedirect(w http.ResponseWriter) { m.url = Config.Rpn.Url tmpl.ExecuteTemplate(w, "rpnCallOutRedirect", *m) }