diff --git a/checksum.go b/checksum.go index 09b55c8..874827c 100644 --- a/checksum.go +++ b/checksum.go @@ -4,6 +4,7 @@ import ( "crypto/md5" "fmt" "io" + "net/url" ) func md5str(s string) string { @@ -11,3 +12,41 @@ func md5str(s string) string { io.WriteString(h, s) return fmt.Sprintf("%x", h.Sum(nil)) } + +func md5LeanworkForm(form url.Values) string { + s := "" + if _, ok := form["pickupUrl"]; ok { + s += form["pickupUrl"][0] + s += form["receiveUrl"][0] + s += form["signType"][0] + s += form["orderNo"][0] + s += form["orderAmount"][0] + s += form["orderCurrency"][0] + s += form["customerId"][0] + s += md5key + } + return md5str(s) +} + +func isLeanworkFormValid(form url.Values) bool { + r := md5LeanworkForm(form) + sign := form["sign"][0] + return r == sign +} + +func md5RpnForm(form url.Values) string { + s := "" + if _, ok := form["version"]; ok { + s += "sign_type=" + form["sign_type"][0] + "|" + s += "mid=" + form["mid"][0] + "|" + s += "notify_url=" + form["notify_url"][0] + "|" + s += "order_id=" + form["order_id"][0] + "|" + s += "order_amount=" + form["order_amount"][0] + "|" + s += "order_time=" + form["order_time"][0] + "|" + s += "user_id=" + form["user_id"][0] + "|" + s += "user_name=" + form["user_name"][0] + "|" + s += "user_cardno=" + form["user_cardno"][0] + "|" + s += "key=" + md5key + } + return md5str(s) +} diff --git a/checksum_test.go b/checksum_test.go index eb90ec4..d106639 100644 --- a/checksum_test.go +++ b/checksum_test.go @@ -16,7 +16,7 @@ func TestMD5Sum(t *testing.T) { t.Errorf("something is wrong %d ", 1) } -func TestRequestForm(t *testing.T) { +func buildForm() url.Values { // receiveUrl= [http://publicapi.lwork.com:8080/notify/default_notify] // orderAmount= [1200] // customerId= [123] @@ -36,6 +36,11 @@ func TestRequestForm(t *testing.T) { form.Add("orderCurrency", "CNY") form.Add("customerId", "123") form.Add("sign", "06bcbd40cf6b914ef8ea6596730571ba") + return form +} + +func TestRequestForm(t *testing.T) { + form := buildForm() md5key := "492815086935204" expected := "06bcbd40cf6b914ef8ea6596730571ba" @@ -54,3 +59,13 @@ func TestRequestForm(t *testing.T) { assert := assert.New(t) assert.Equal(expected, md5str(s), "the md5 result should be equal") } + +func TestMd5Form(t *testing.T) { + form := buildForm() + expected := "06bcbd40cf6b914ef8ea6596730571ba" + expectedkey := "492815086935204" + assert := assert.New(t) + assert.Equal(expectedkey, md5key, "md5key should be 492815086935204") + result := md5LeanworkForm(form) + assert.Equal(expected, result, "expected signature should be equal") +} diff --git a/config.go b/config.go new file mode 100644 index 0000000..4df84af --- /dev/null +++ b/config.go @@ -0,0 +1,44 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" +) + +type AppConfig struct { + Rpn struct { + Url string `json:Url` + UrlTest string `json:UrlTest` + MD5P2P string `json:MD5P2P` + MD5FAT string `json:MD5FAT` + } `json:Rpn` + LeanWork struct { + MD5Key string `json:MD5Key` + } `json:LeanWork` +} + +var Config AppConfig + +func readConfig() error { + log.Printf("Read configration from config.json") + body, err := ioutil.ReadFile("config.json") + if err != nil { + log.Fatal("Fatal error Cannot read config ..") + return err + } + err = json.Unmarshal(body, &Config) + if err != nil { + log.Fatal("cannot read config into json") + return err + } + + j, err := json.MarshalIndent(Config, "", "\t") + if err != nil { + log.Fatal("cannot print back to json") + return err + } + fmt.Println(string(j)) + return err +} diff --git a/config.json b/config.json new file mode 100644 index 0000000..166eeb9 --- /dev/null +++ b/config.json @@ -0,0 +1,11 @@ +{ + "Rpn": { + "Url" : "https://deposit.paylomo.net/pay.php?r=payEasy", + "UrlTest" : "https://deposit-mac.chinapaytech.com/pay.php?r=payEasy", + "MD5P2P" : "370296119874502", + "MD5FAT" : "207841502473198" + }, + "LeanWork" :{ + "MD5Key" : "492815086935204" + } +} \ No newline at end of file diff --git a/main.go b/main.go index 75a9a07..35043a3 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "crypto/md5" "database/sql" "fmt" "html/template" @@ -158,31 +157,28 @@ func StartPay(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "fuck") return } - s := "" r.ParseForm() + if !isLeanworkFormValid(r.Form) { + fmt.Fprintf(w, "invalid request") + return + } + for key, value := range r.Form { fmt.Printf("%s= %s\n", key, value) fmt.Fprintf(w, "%s= %s\n", key, value) } - if _, ok := r.Form["pickupUrl"]; ok { - s += r.Form["pickupUrl"][0] - s += r.Form["receiveUrl"][0] - s += r.Form["signType"][0] - s += r.Form["orderNo"][0] - s += r.Form["orderAmount"][0] - s += r.Form["orderCurrency"][0] - s += r.Form["customerId"][0] - s += md5key - } - //var h = md5.New() - data := []byte(s) - fmt.Printf("my md5=%x", md5.Sum(data)) - fmt.Fprintf(w, "my md5=%x", md5.Sum(data)) + sign := md5LeanworkForm(r.Form) + + fmt.Printf("my md5=%s, valid = %t", sign, isLeanworkFormValid(r.Form)) + fmt.Fprintf(w, "my md5=%s", sign) } func main() { + readConfig() + return + log.Println("Server started on: http://localhost:8080") http.HandleFunc("/", StartPay) //http.HandleFunc("/", Index) diff --git a/rpn.go b/rpn.go new file mode 100644 index 0000000..aa04f85 --- /dev/null +++ b/rpn.go @@ -0,0 +1,13 @@ +package main + +import "net/url" + +// 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" + +//send request to RPN to initiate transaction +func sendReq(form url.Values) { + +}