payment gateway for rpn cn
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

202 lignes
5.5KB

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "math"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  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 = md5RpnFormP2P(r)
  43. // *m = r
  44. // return r
  45. // }
  46. func (m *RpnReq) buildReqByLeanworkRequestP2P(row LeanworkRequest, user_name string, user_cardno string) RpnReq {
  47. r := RpnReq{}
  48. r.version = "1.1"
  49. r.sign_type = "MD5"
  50. r.mid = Config.Rpn.MIDP2P
  51. r.notify_url = "http://rpn.supertraderfx.ayvwd8em49pvoa3g.com/rpn_notify"
  52. r.order_id = row.OrderNo
  53. r.order_amount = m.translateAmountFromLeanwork(row.OrderAmount)
  54. r.order_time = m.now()
  55. r.user_id = row.CustomerId
  56. r.user_name = user_name
  57. r.user_cardno = user_cardno
  58. r.signature = md5RpnFormP2P(r)
  59. *m = r
  60. return r
  61. }
  62. func (m *RpnReq) buildReqByLeanworkRequestFAT(row LeanworkRequest, user_name string, user_cardno string) RpnReq {
  63. r := RpnReq{}
  64. r.version = "1.1"
  65. r.sign_type = "MD5"
  66. r.mid = Config.Rpn.MIDFAT
  67. r.notify_url = "http://rpn.supertraderfx.ayvwd8em49pvoa3g.com/rpn_notify"
  68. r.order_id = row.OrderNo
  69. r.order_amount = m.translateAmountFromLeanwork(row.OrderAmount)
  70. r.order_time = m.now()
  71. r.user_id = row.CustomerId
  72. r.user_name = user_name
  73. r.user_cardno = user_cardno
  74. r.signature = md5RpnFormFAT(r)
  75. *m = r
  76. return r
  77. }
  78. //from 0.1 to 10 cents
  79. func (m *RpnReq) translateAmountFromLeanwork(from string) string {
  80. f, _ := strconv.ParseFloat(from, 32)
  81. f = f * 100 //convert to cents
  82. t := int(math.Ceil(f))
  83. s := strconv.Itoa(t)
  84. return s
  85. }
  86. func (m *RpnReq) now() string {
  87. t := time.Now()
  88. return t.Format("20060102150405")
  89. }
  90. //send request to RPN to initiate transaction
  91. func (m *RpnReq) SendReq(form url.Values) (*http.Response, error) {
  92. return nil, nil
  93. // r := m.buildReqByForm(form)
  94. // hc := http.Client{Timeout: 15 * time.Second}
  95. // myForm := url.Values{}
  96. // myForm.Set("version", r.version)
  97. // myForm.Set("sign_type", r.sign_type)
  98. // myForm.Set("mid", r.mid)
  99. // myForm.Set("notify_url", r.notify_url)
  100. // myForm.Set("order_id", r.order_id)
  101. // myForm.Set("order_amount", r.order_amount)
  102. // myForm.Set("order_time", r.order_time)
  103. // myForm.Set("user_id", r.user_id)
  104. // myForm.Set("user_name", r.user_name)
  105. // myForm.Set("user_cardno", r.user_cardno)
  106. // myForm.Set("signature", r.signature)
  107. // //req, err := http.NewRequest("POST", "https://lawipac.com/dumprequest.php", strings.NewReader(myForm.Encode()))
  108. // req, err := http.NewRequest("POST", Config.Rpn.UrlTest, strings.NewReader(m.encode()))
  109. // if err != nil {
  110. // panic("wrong")
  111. // }
  112. // req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  113. // req.Header.Add("content-Length", strconv.Itoa(len(form.Encode())))
  114. // return hc.Do(req)
  115. //Config.Rpn.UrlTest
  116. // return http.PostForm(Config.Rpn.UrlTest, myForm)
  117. //return http.PostForm("https://lawipac.com/dumprequest.php", myForm)
  118. }
  119. //encode without disturbing it's original order
  120. func (m *RpnReq) encode() string {
  121. s := "version=" + m.version
  122. s += "&sign_type=" + m.sign_type
  123. s += "&mid=" + m.mid
  124. s += "&notify_url=" + url.QueryEscape(m.notify_url)
  125. s += "&order_id=" + m.order_id
  126. s += "&order_amount=" + m.order_amount
  127. s += "&order_time=" + m.order_time
  128. s += "&user_id=" + m.user_id
  129. s += "&user_name=" + m.user_name
  130. s += "&user_cardno=" + m.user_cardno
  131. s += "&signature=" + m.signature
  132. return s
  133. }
  134. func retrieveFormValue(form url.Values, key string) (r string, err error) {
  135. if _, ok := form[key]; ok {
  136. r = form[key][0]
  137. err = nil
  138. } else {
  139. r = ""
  140. err = errors.New("Key [" + key + "] not found in HTTP request")
  141. }
  142. return r, err
  143. }
  144. //receive RPN user name and card number
  145. func rpnNameAndCard(w http.ResponseWriter, r *http.Request) {
  146. if r.Method != "POST" {
  147. w.WriteHeader(http.StatusMethodNotAllowed)
  148. fmt.Fprintf(w, "invalid request")
  149. return
  150. }
  151. r.ParseForm()
  152. id := r.FormValue("id")
  153. sign := r.FormValue("sign")
  154. user_name := r.FormValue("name")
  155. user_card := r.FormValue("card")
  156. rpn_type := r.FormValue("rpnType")
  157. if !(id != "" && sign != "" && user_name != "" && user_card != "") {
  158. w.WriteHeader(http.StatusBadRequest)
  159. fmt.Fprintf(w, "missing parameters")
  160. return
  161. }
  162. row, err := getRequestRowByIdAndSign(id, sign)
  163. if err != nil {
  164. w.WriteHeader(http.StatusBadRequest)
  165. fmt.Fprintf(w, "bad parameters")
  166. return
  167. }
  168. ro := RpnReq{}
  169. if rpn_type == "rpnp2p" {
  170. ro.buildReqByLeanworkRequestP2P(row, user_name, user_card)
  171. } else {
  172. ro.buildReqByLeanworkRequestFAT(row, user_name, user_card)
  173. }
  174. //build rpn redirect page and send it
  175. ro.sendRedirect(w, row)
  176. }
  177. func (m *RpnReq) sendRedirect(w http.ResponseWriter, row LeanworkRequest) {
  178. //create db record
  179. m.url = Config.Rpn.Url
  180. tmpl.ExecuteTemplate(w, "rpnCallOutRedirect", *m)
  181. }