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.

166 lignes
4.2KB

  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type LeanworkOut struct {
  12. Id int64
  13. Leanwork int64
  14. SignType string
  15. OrderNo string
  16. OrderAmount string
  17. OrderCurrency string
  18. TransactionId string
  19. Status string
  20. Sign string
  21. Ts time.Time
  22. LeanworkResp string
  23. }
  24. //inform Leanwork of final result, retry 5 times if not successful
  25. func (m *LeanworkOut) DoHttp() (retry bool, err error) {
  26. retry = true
  27. form := url.Values{}
  28. form.Set("signType", "MD5")
  29. form.Set("orderNo", m.OrderNo)
  30. form.Set("orderAmount", m.OrderAmount)
  31. form.Set("orderCurrency", m.OrderCurrency)
  32. form.Set("transactionId", m.TransactionId)
  33. form.Set("sign", m.Sign)
  34. lin, err := getLeanworkInById(m.Leanwork) //which is very unlikely
  35. if err != nil {
  36. log.Println("Fatal: Cannot get LeanworkIn by ID =" + strconv.FormatInt(m.Leanwork, 10) + "error " + err.Error())
  37. retry = false
  38. }
  39. resp, err := http.PostForm(lin.ReceiveUrl, form)
  40. if err != nil {
  41. log.Println("Leanwork Server Error, give bad response, " + err.Error())
  42. m.LeanworkResp = "Http Error: " + err.Error()
  43. m.add2db()
  44. return
  45. }
  46. defer resp.Body.Close()
  47. if resp.StatusCode == http.StatusOK {
  48. bodyBytes, err := ioutil.ReadAll(resp.Body)
  49. if err != nil {
  50. log.Println("Fatal: Cannot read leanwork Http Response " + err.Error())
  51. }
  52. bodyString := string(bodyBytes)
  53. if strings.Contains(strings.ToLower(bodyString), "success") {
  54. retry = false
  55. } else {
  56. log.Println("Leanwork response without success word : " + bodyString)
  57. }
  58. }
  59. return
  60. }
  61. func startLeanworkCallBack(ri RpnIn) {
  62. lo, err := buildLeanworkOutByRpnIn(ri)
  63. if err != nil {
  64. log.Printf("Fatal: cannot inform Leanwork %+v , \n\t%s\n", ri, err.Error())
  65. return
  66. }
  67. if ri.Pay_result != "3" {
  68. log.Printf("Warning: RpnIn is processing not informing leanwork %+v", ri)
  69. return
  70. }
  71. for i := 1; i <= 5; i++ {
  72. retry, err := lo.DoHttp()
  73. if !retry {
  74. break
  75. }
  76. time.Sleep(5 * time.Minute) //sleep 5 minute and try again
  77. log.Printf("Trying(%d) to report leanwork about transaction status %+v, encountered error %s \n", i, lo, err.Error())
  78. }
  79. }
  80. func buildLeanworkOutByRpnIn(ri RpnIn) (ret LeanworkOut, err error) {
  81. li, err := getLeanworkInById(ri.Leanwork)
  82. if err != nil {
  83. return
  84. }
  85. ret.Leanwork = ri.Leanwork
  86. ret.SignType = "MD5"
  87. ret.OrderNo = li.OrderNo
  88. ret.OrderAmount = li.OrderAmount
  89. ret.OrderCurrency = li.OrderCurrency
  90. ret.TransactionId = ri.Deal_id
  91. if ri.Pay_result == "3" {
  92. ret.Status = "success"
  93. } else if ri.Pay_result == "1" {
  94. ret.Status = "processing"
  95. }
  96. return
  97. }
  98. func (m *LeanworkOut) add2db() (ret LeanworkOut, err error) {
  99. if err = db.conn(Config); err != nil {
  100. return
  101. }
  102. defer db.close()
  103. q := `INSERT INTO leanworkOut(
  104. leanwork, signType, orderNo, orderAmount, orderCurrency,
  105. transactionId, status, sign, leanworkResp)
  106. VALUES(?,?,?,?,?,?,?,?,?)
  107. `
  108. insForm, err := db.h.Prepare(q)
  109. if err != nil {
  110. log.Printf("Failed to prepare SQL statment for insert leanworkOut" + err.Error())
  111. return
  112. }
  113. res, err := insForm.Exec(
  114. m.Leanwork, m.SignType, m.OrderNo, m.OrderAmount, m.OrderCurrency,
  115. m.TransactionId, m.Status, m.Sign, m.LeanworkResp)
  116. if err != nil {
  117. log.Printf("Error inserting leanworkOut with orderNo =%s, %s \n", m.OrderNo, err.Error())
  118. return
  119. }
  120. id, err := res.LastInsertId()
  121. if err != nil {
  122. log.Printf("Cannot retrieve lastInsertId for orderID %s", m.OrderNo)
  123. return
  124. }
  125. ret, err = getLeanWorkOutById(id)
  126. if err == nil {
  127. *m = ret
  128. }
  129. return
  130. }
  131. func getLeanWorkOutById(id int64) (row LeanworkOut, err error) {
  132. db.conn(Config)
  133. defer db.close()
  134. err = db.h.QueryRow("SELECT * FROM leanworkOut WHERE id=? ", id).Scan(
  135. &row.Id, &row.Leanwork, &row.SignType, &row.OrderNo, &row.OrderAmount,
  136. &row.OrderCurrency, &row.TransactionId, &row.Status, &row.Sign,
  137. &row.Ts, &row.LeanworkResp)
  138. return
  139. }
  140. func getLeanWorkOutByLeanworkId(id int64) (row LeanworkOut, err error) {
  141. db.conn(Config)
  142. defer db.close()
  143. err = db.h.QueryRow("SELECT * FROM leanworkOut WHERE leanwork=? ORDER BY id DESC LIMIT 1", id).Scan(
  144. &row.Id, &row.Leanwork, &row.SignType, &row.OrderNo, &row.OrderAmount,
  145. &row.OrderCurrency, &row.TransactionId, &row.Status, &row.Sign,
  146. &row.Ts, &row.LeanworkResp)
  147. return
  148. }