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.

213 lignes
5.6KB

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "math"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "time"
  10. "github.com/go-sql-driver/mysql"
  11. )
  12. type RpnReq struct {
  13. Order_id string
  14. Version string
  15. Sign_type string
  16. Mid string
  17. Notify_url string
  18. Order_amount string
  19. Order_time string //YYYYMMDDHHMMSS
  20. User_id string
  21. User_name string
  22. User_cardno string
  23. Signature string
  24. //template
  25. Url string //where to post entire data structure
  26. //database specific
  27. Id int64
  28. Leanwork int64
  29. Callback int64
  30. Ip4 uint32
  31. Ip4location string
  32. Ts mysql.NullTime
  33. }
  34. // //build request from leanwork request forms
  35. // func (m *RpnReq) buildReqByForm(form url.Values) RpnReq {
  36. // r := RpnReq{}
  37. // r.version = "1.1"
  38. // r.sign_type = "MD5"
  39. // r.mid = "EU85201311P2P"
  40. // r.notify_url = "http://rpn.supertraderfx.ayvwd8em49pvoa3g.com/rpn_notify"
  41. // r.order_id = form["orderNo"][0]
  42. // r.order_amount = form["orderAmount"][0] //cents
  43. // r.order_time = m.now()
  44. // r.user_id = form["customerId"][0]
  45. // r.user_name = "SuperForex"
  46. // r.user_cardno = "6212262002002377849"
  47. // r.signature = md5RpnFormP2P(r)
  48. // *m = r
  49. // return r
  50. // }
  51. func (m *RpnReq) buildReqByLeanworkRequestP2P(row LeanworkRequest, user_name string, user_cardno string) RpnReq {
  52. m.Version = "1.1"
  53. m.Sign_type = "MD5"
  54. m.Mid = Config.Rpn.MIDP2P
  55. m.Notify_url = Config.Rpn.UrlCallBack
  56. m.Order_id = row.OrderNo
  57. m.Order_amount = m.translateAmountFromLeanwork(row.OrderAmount)
  58. m.Order_time = m.now()
  59. m.User_id = row.CustomerId
  60. m.User_name = user_name
  61. m.User_cardno = user_cardno
  62. m.Signature = md5RpnFormP2P(*m)
  63. m.Leanwork = row.Id
  64. return *m
  65. }
  66. func (m *RpnReq) buildReqByLeanworkRequestFAT(row LeanworkRequest, user_name string, user_cardno string) RpnReq {
  67. m.Version = "1.1"
  68. m.Sign_type = "MD5"
  69. m.Mid = Config.Rpn.MIDFAT
  70. m.Notify_url = Config.Rpn.UrlCallBack
  71. m.Order_id = row.OrderNo
  72. m.Order_amount = m.translateAmountFromLeanwork(row.OrderAmount)
  73. m.Order_time = m.now()
  74. m.User_id = row.CustomerId
  75. m.User_name = user_name
  76. m.User_cardno = user_cardno
  77. m.Signature = md5RpnFormFAT(*m)
  78. m.Leanwork = row.Id
  79. return *m
  80. }
  81. //from 0.1 to 10 cents
  82. func (m *RpnReq) translateAmountFromLeanwork(from string) string {
  83. f, _ := strconv.ParseFloat(from, 32)
  84. f = f * 100 //convert to cents
  85. t := int(math.Ceil(f))
  86. s := strconv.Itoa(t)
  87. return s
  88. }
  89. func (m *RpnReq) now() string {
  90. t := time.Now()
  91. return t.Format("20060102150405")
  92. }
  93. //send request to RPN to initiate transaction
  94. func (m *RpnReq) SendReq(form url.Values) (*http.Response, error) {
  95. return nil, nil
  96. // r := m.buildReqByForm(form)
  97. // hc := http.Client{Timeout: 15 * time.Second}
  98. // myForm := url.Values{}
  99. // myForm.Set("version", r.version)
  100. // myForm.Set("sign_type", r.sign_type)
  101. // myForm.Set("mid", r.mid)
  102. // myForm.Set("notify_url", r.notify_url)
  103. // myForm.Set("order_id", r.order_id)
  104. // myForm.Set("order_amount", r.order_amount)
  105. // myForm.Set("order_time", r.order_time)
  106. // myForm.Set("user_id", r.user_id)
  107. // myForm.Set("user_name", r.user_name)
  108. // myForm.Set("user_cardno", r.user_cardno)
  109. // myForm.Set("signature", r.signature)
  110. // //req, err := http.NewRequest("POST", "https://lawipac.com/dumprequest.php", strings.NewReader(myForm.Encode()))
  111. // req, err := http.NewRequest("POST", Config.Rpn.UrlTest, strings.NewReader(m.encode()))
  112. // if err != nil {
  113. // panic("wrong")
  114. // }
  115. // req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  116. // req.Header.Add("content-Length", strconv.Itoa(len(form.Encode())))
  117. // return hc.Do(req)
  118. //Config.Rpn.UrlTest
  119. // return http.PostForm(Config.Rpn.UrlTest, myForm)
  120. //return http.PostForm("https://lawipac.com/dumprequest.php", myForm)
  121. }
  122. //encode without disturbing it's original order
  123. func (m *RpnReq) encode() string {
  124. s := "version=" + m.Version
  125. s += "&sign_type=" + m.Sign_type
  126. s += "&mid=" + m.Mid
  127. s += "&notify_url=" + url.QueryEscape(m.Notify_url)
  128. s += "&order_id=" + m.Order_id
  129. s += "&order_amount=" + m.Order_amount
  130. s += "&order_time=" + m.Order_time
  131. s += "&user_id=" + m.User_id
  132. s += "&user_name=" + url.QueryEscape(m.User_name)
  133. s += "&user_cardno=" + m.User_cardno
  134. s += "&signature=" + m.Signature
  135. return s
  136. }
  137. func retrieveFormValue(form url.Values, key string) (r string, err error) {
  138. if _, ok := form[key]; ok {
  139. r = form[key][0]
  140. err = nil
  141. } else {
  142. r = ""
  143. err = errors.New("Key [" + key + "] not found in HTTP request")
  144. }
  145. return r, err
  146. }
  147. func rpnNotify(w http.ResponseWriter, r *http.Request) {
  148. fmt.Fprint(w, "ok notify")
  149. }
  150. //receive RPN user name and card number
  151. func rpnNameAndCard(w http.ResponseWriter, r *http.Request) {
  152. if r.Method != "POST" {
  153. errPage(w, http.StatusMethodNotAllowed, "invalid request")
  154. return
  155. }
  156. r.ParseForm()
  157. id := r.FormValue("id")
  158. sign := r.FormValue("sign")
  159. user_name := r.FormValue("name")
  160. user_card := r.FormValue("card")
  161. rpn_type := r.FormValue("rpnType")
  162. if !(id != "" && sign != "" && user_name != "" && user_card != "") {
  163. errPage(w, http.StatusBadRequest, "missing parameters")
  164. return
  165. }
  166. row, err := getRequestRowByIdAndSign(id, sign)
  167. if err != nil {
  168. w.WriteHeader(http.StatusBadRequest)
  169. fmt.Fprintf(w, "bad parameters")
  170. return
  171. }
  172. ro := RpnReq{}
  173. ro.Ip4 = getClientIPLong(r)
  174. ro.Leanwork = row.Id
  175. if rpn_type == "rpnp2p" {
  176. ro.buildReqByLeanworkRequestP2P(row, user_name, user_card)
  177. } else {
  178. ro.buildReqByLeanworkRequestFAT(row, user_name, user_card)
  179. }
  180. //create db record
  181. db.addRpnOut(ro)
  182. //build rpn redirect page and send it
  183. ro.sendRedirect(w, row)
  184. }
  185. func (m *RpnReq) sendRedirect(w http.ResponseWriter, row LeanworkRequest) {
  186. //execute redirect
  187. m.Url = Config.Rpn.Url
  188. tmpl.ExecuteTemplate(w, "rpnCallOutRedirect", *m)
  189. }