| @@ -3,10 +3,10 @@ package main | |||
| import ( | |||
| "errors" | |||
| "fmt" | |||
| "math" | |||
| "net/http" | |||
| "net/url" | |||
| "strconv" | |||
| "strings" | |||
| "time" | |||
| ) | |||
| @@ -29,24 +29,67 @@ type RpnReq struct { | |||
| url string //where to post entire data structure | |||
| } | |||
| //build request from leanwork request forms | |||
| func (m *RpnReq) buildReqByForm(form url.Values) RpnReq { | |||
| // //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 = "EU85201311P2P" | |||
| r.mid = Config.Rpn.MIDP2P | |||
| 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_id = row.OrderNo | |||
| r.order_amount = m.translateAmountFromLeanwork(row.OrderAmount) | |||
| r.order_time = m.now() | |||
| r.user_id = form["customerId"][0] | |||
| r.user_name = "SuperForex" | |||
| r.user_cardno = "6212262002002377849" | |||
| r.signature = md4RpnFormP2P(r) | |||
| 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") | |||
| @@ -54,33 +97,35 @@ func (m *RpnReq) now() string { | |||
| //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) | |||
| 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 | |||
| @@ -114,31 +159,43 @@ func retrieveFormValue(form url.Values, key string) (r string, err error) { | |||
| //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() | |||
| 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") | |||
| id := r.FormValue("id") | |||
| sign := r.FormValue("sign") | |||
| user_name := r.FormValue("name") | |||
| user_card := r.FormValue("card") | |||
| rpn_type := r.FormValue("rpnType") | |||
| if !(ok1 == nil && ok2 == nil && ok3 == nil && ok4 == nil) { | |||
| if !(id != "" && sign != "" && user_name != "" && user_card != "") { | |||
| w.WriteHeader(http.StatusBadRequest) | |||
| fmt.Fprintf(w, "missing parameters") | |||
| return | |||
| } | |||
| row, err := db.updateRpnOutNameAndCard(order_id, signature, user_name, user_card) | |||
| row, err := getRequestRowByIdAndSign(id, sign) | |||
| if err != nil { | |||
| fmt.Fprintf(w, "cannot update transaction") | |||
| 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 | |||
| row.sendRedirect(w) | |||
| ro.sendRedirect(w, row) | |||
| } | |||
| func (m *RpnReq) sendRedirect(w http.ResponseWriter) { | |||
| func (m *RpnReq) sendRedirect(w http.ResponseWriter, row LeanworkRequest) { | |||
| //create db record | |||
| m.url = Config.Rpn.Url | |||
| tmpl.ExecuteTemplate(w, "rpnCallOutRedirect", *m) | |||
| } | |||