payment gateway for rpn cn
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

204 lines
5.6KB

  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=" + url.QueryEscape(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. func rpnNotify(w http.ResponseWriter, r *http.Request) {
  145. }
  146. //receive RPN user name and card number
  147. func rpnNameAndCard(w http.ResponseWriter, r *http.Request) {
  148. if r.Method != "POST" {
  149. w.WriteHeader(http.StatusMethodNotAllowed)
  150. fmt.Fprintf(w, "invalid request")
  151. return
  152. }
  153. r.ParseForm()
  154. id := r.FormValue("id")
  155. sign := r.FormValue("sign")
  156. user_name := r.FormValue("name")
  157. user_card := r.FormValue("card")
  158. rpn_type := r.FormValue("rpnType")
  159. if !(id != "" && sign != "" && user_name != "" && user_card != "") {
  160. w.WriteHeader(http.StatusBadRequest)
  161. fmt.Fprintf(w, "missing parameters")
  162. return
  163. }
  164. row, err := getRequestRowByIdAndSign(id, sign)
  165. if err != nil {
  166. w.WriteHeader(http.StatusBadRequest)
  167. fmt.Fprintf(w, "bad parameters")
  168. return
  169. }
  170. ro := RpnReq{}
  171. if rpn_type == "rpnp2p" {
  172. ro.buildReqByLeanworkRequestP2P(row, user_name, user_card)
  173. } else {
  174. ro.buildReqByLeanworkRequestFAT(row, user_name, user_card)
  175. }
  176. //build rpn redirect page and send it
  177. ro.sendRedirect(w, row)
  178. }
  179. func (m *RpnReq) sendRedirect(w http.ResponseWriter, row LeanworkRequest) {
  180. //create db record
  181. m.Url = Config.Rpn.Url
  182. tmpl.ExecuteTemplate(w, "rpnCallOutRedirect", *m)
  183. }