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.

122 lignes
3.0KB

  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. OrderNo string
  15. OrderAmount string
  16. OrderCurrency string
  17. TransactionId string
  18. Status string
  19. Sign string
  20. Ts time.Time
  21. LeanworkResp string
  22. }
  23. //inform Leanwork of final result, retry 5 times if not successful
  24. func (m *LeanworkOut) DoHttp() (retry bool, err error) {
  25. retry = true
  26. form := url.Values{}
  27. form.Set("signType", "MD5")
  28. form.Set("orderNo", m.OrderNo)
  29. form.Set("orderAmount", m.OrderAmount)
  30. form.Set("orderCurrency", m.OrderCurrency)
  31. form.Set("transactionId", m.TransactionId)
  32. form.Set("sign", m.Sign)
  33. lin, err := getLeanworkInById(m.Leanwork) //which is very unlikely
  34. if err != nil {
  35. log.Println("Fatal: Cannot get LeanworkIn by ID =" + strconv.FormatInt(m.Leanwork, 10) + "error " + err.Error())
  36. retry = false
  37. }
  38. resp, err := http.PostForm(lin.ReceiveUrl, form)
  39. if err != nil {
  40. log.Println("Leanwork Server Error, give bad response, " + err.Error())
  41. return
  42. }
  43. defer resp.Body.Close()
  44. if resp.StatusCode == http.StatusOK {
  45. bodyBytes, err := ioutil.ReadAll(resp.Body)
  46. if err != nil {
  47. log.Println("Fatal: Cannot read leanwork Http Response " + err.Error())
  48. }
  49. bodyString := string(bodyBytes)
  50. if strings.Contains(strings.ToLower(bodyString), "success") {
  51. retry = false
  52. } else {
  53. log.Println("Leanwork response without success word : " + bodyString)
  54. }
  55. }
  56. return
  57. }
  58. func (m *LeanworkOut) startCallBack() {
  59. for i := 1; i <= 5; i++ {
  60. retry, err := m.DoHttp()
  61. if !retry {
  62. break
  63. }
  64. time.Sleep(5 * time.Minute) //sleep 5 minute and try again
  65. log.Printf("Trying(%d) to report leanwork about transaction status %+v, encountered error %s \n", i, *m, err.Error())
  66. }
  67. }
  68. func (m *LeanworkOut) add2db() (ret LeanworkOut, err error) {
  69. if err = db.conn(Config); err != nil {
  70. return
  71. }
  72. defer db.close()
  73. q := `INSERT INTO leanworkOut(
  74. leanwork, orderNo, orderAmount, orderCurrency,
  75. transactionId, status, sign, leanworkResp)
  76. VALUES(?,?,?,?,?,?,?,?)
  77. `
  78. insForm, err := db.h.Prepare(q)
  79. if err != nil {
  80. log.Printf("Failed to prepare SQL statment for insert leanworkOut" + err.Error())
  81. return
  82. }
  83. res, err := insForm.Exec(
  84. m.Leanwork, m.OrderNo, m.OrderAmount, m.OrderCurrency,
  85. m.TransactionId, m.Status, m.Sign, m.LeanworkResp)
  86. if err != nil {
  87. log.Printf("Error inserting leanworkOut with orderNo =%s, %s \n", m.OrderNo, err.Error())
  88. return
  89. }
  90. id, err := res.LastInsertId()
  91. if err != nil {
  92. log.Printf("Cannot retrieve lastInsertId for orderID %s", m.OrderNo)
  93. return
  94. }
  95. ret, err = getLeanWorkOutById(id)
  96. if err == nil {
  97. *m = ret
  98. }
  99. return
  100. }
  101. func getLeanWorkOutById(id int64) (row LeanworkOut, err error) {
  102. db.conn(Config)
  103. defer db.close()
  104. err = db.h.QueryRow("SELECT * FROM leanworkOut WHERE id=? ", id).Scan(
  105. &row.Id, &row.Leanwork, &row.OrderNo, &row.OrderAmount,
  106. &row.OrderCurrency, &row.TransactionId, &row.Status, &row.Sign,
  107. &row.Ts, &row.LeanworkResp)
  108. return
  109. }