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.

145 line
3.9KB

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