payment gateway for rpn cn
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 line
3.6KB

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. // var url = "https://deposit.paylomo.net/pay.php?r=payEasy" //production
  11. // var url = "https://deposit-mac.chinapaytech.com/pay.php?r=payEasy" //test
  12. // var md5p2p = "370296119874502"
  13. // var md5fat = "207841502473198"
  14. type RpnReq struct {
  15. version string
  16. sign_type string
  17. mid string
  18. notify_url string
  19. order_id string
  20. order_amount string
  21. order_time string //YYYYMMDDHHMMSS
  22. user_id string
  23. user_name string
  24. user_cardno string
  25. signature string
  26. url string //where to post entire data structure
  27. }
  28. //build request from leanwork request forms
  29. func (m *RpnReq) buildReqByForm(form url.Values) RpnReq {
  30. r := RpnReq{}
  31. r.version = "1.1"
  32. r.sign_type = "MD5"
  33. r.mid = "EU85201311P2P"
  34. r.notify_url = "http://rpn.supertraderfx.ayvwd8em49pvoa3g.com/rpn_notify"
  35. r.order_id = form["orderNo"][0]
  36. r.order_amount = form["orderAmount"][0] //cents
  37. r.order_time = m.now()
  38. r.user_id = form["customerId"][0]
  39. r.user_name = "SuperForex"
  40. r.user_cardno = "6212262002002377849"
  41. r.signature = md4RpnFormP2P(r)
  42. *m = r
  43. return r
  44. }
  45. func (m *RpnReq) now() string {
  46. t := time.Now()
  47. return t.Format("20060102150405")
  48. }
  49. //send request to RPN to initiate transaction
  50. func (m *RpnReq) SendReq(form url.Values) (*http.Response, error) {
  51. r := m.buildReqByForm(form)
  52. hc := http.Client{Timeout: 15 * time.Second}
  53. myForm := url.Values{}
  54. myForm.Set("version", r.version)
  55. myForm.Set("sign_type", r.sign_type)
  56. myForm.Set("mid", r.mid)
  57. myForm.Set("notify_url", r.notify_url)
  58. myForm.Set("order_id", r.order_id)
  59. myForm.Set("order_amount", r.order_amount)
  60. myForm.Set("order_time", r.order_time)
  61. myForm.Set("user_id", r.user_id)
  62. myForm.Set("user_name", r.user_name)
  63. myForm.Set("user_cardno", r.user_cardno)
  64. myForm.Set("signature", r.signature)
  65. //req, err := http.NewRequest("POST", "https://lawipac.com/dumprequest.php", strings.NewReader(myForm.Encode()))
  66. req, err := http.NewRequest("POST", Config.Rpn.UrlTest, strings.NewReader(m.encode()))
  67. if err != nil {
  68. panic("wrong")
  69. }
  70. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  71. req.Header.Add("content-Length", strconv.Itoa(len(form.Encode())))
  72. return hc.Do(req)
  73. //Config.Rpn.UrlTest
  74. // return http.PostForm(Config.Rpn.UrlTest, myForm)
  75. //return http.PostForm("https://lawipac.com/dumprequest.php", myForm)
  76. }
  77. //encode without disturbing it's original order
  78. func (m *RpnReq) encode() string {
  79. s := "version=" + m.version
  80. s += "&sign_type=" + m.sign_type
  81. s += "&mid=" + m.mid
  82. s += "&notify_url=" + url.QueryEscape(m.notify_url)
  83. s += "&order_id=" + m.order_id
  84. s += "&order_amount=" + m.order_amount
  85. s += "&order_time=" + m.order_time
  86. s += "&user_id=" + m.user_id
  87. s += "&user_name=" + m.user_name
  88. s += "&user_cardno=" + m.user_cardno
  89. s += "&signature=" + m.signature
  90. return s
  91. }
  92. //receive RPN user name and card number
  93. func rpnNameAndCard(w http.ResponseWriter, r *http.Request) {
  94. if r.Method != "POST" {
  95. fmt.Fprintf(w, "invalid request")
  96. return
  97. }
  98. r.ParseForm()
  99. order_id, ok1 := r.Form["id"][0]
  100. signature, ok2 := r.Form["si"][0]
  101. user_name, ok3 := r.Form["name"][0]
  102. user_card, ok4 := r.Form["card"][0]
  103. if !(ok1 && ok2 && ok3 && ok4) {
  104. fmt.Fprintf(w, "missing parameters")
  105. return
  106. }
  107. row, err := db.updateRpnOutNameAndCard(order_id, signature, user_name, user_card)
  108. if err != nil {
  109. fmt.Fprintf(w, "cannot update transaction")
  110. return
  111. }
  112. //build rpn redirect page and send it
  113. row.sendRedirect(w)
  114. }
  115. func (m *RpnReq) sendRedirect(w http.ResponseWriter) {
  116. m.url = Config.Rpn.Url
  117. tmpl.ExecuteTemplate(w, "rpnCallOutRedirect", *m)
  118. }