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.

146 lignes
3.7KB

  1. package main
  2. import (
  3. "database/sql"
  4. "log"
  5. "net/http"
  6. "strconv"
  7. )
  8. type TransactionDB struct {
  9. h *sql.DB
  10. }
  11. var db TransactionDB
  12. func (m *TransactionDB) conn(c AppConfig) error {
  13. if m.h != nil {
  14. return nil //already connected, prevent multiple connection
  15. }
  16. dbDriver := c.DB.Driver
  17. dbUser := c.DB.User
  18. dbPass := c.DB.Pass
  19. dbName := c.DB.Schema
  20. h, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName+"?parseTime=true")
  21. if err != nil {
  22. m.h = nil
  23. panic(err.Error())
  24. }
  25. m.h = h
  26. return err
  27. }
  28. func (m *TransactionDB) close() {
  29. if m.h != nil {
  30. m.h.Close()
  31. }
  32. m.h = nil //clear it, very important to prevent multiple opening
  33. }
  34. func (m *TransactionDB) addRequest(r *http.Request, md5key string) (row LeanworkIn, err error) {
  35. if err = m.conn(Config); err != nil {
  36. return row, err
  37. }
  38. defer m.close()
  39. r.ParseForm()
  40. //assuming form has been parsed
  41. row = LeanworkIn{}
  42. pickupUrl := r.FormValue("pickupUrl")
  43. receiveUrl := r.FormValue("receiveUrl")
  44. signType := r.FormValue("signType")
  45. orderNo := r.FormValue("orderNo")
  46. orderAmount := r.FormValue("orderAmount")
  47. orderCurrency := r.FormValue("orderCurrency")
  48. customerId := r.FormValue("customerId")
  49. sign := r.FormValue("sign")
  50. valid := isLeanworkFormValid(r.Form, md5key)
  51. ip4 := getClientIPLong(r)
  52. insForm, err := m.h.Prepare("INSERT INTO leanworkIn (pickupUrl, receiveUrl, signType, orderNo, orderAmount, orderCurrency, customerId, sign, valid, ip4, md5key) VALUES(?,?,?,?,?,?,?,?,?,?,?)")
  53. if err != nil {
  54. log.Printf("Failed to prepare SQL statment for insert")
  55. return
  56. }
  57. res, err := insForm.Exec(pickupUrl, receiveUrl, signType, orderNo, orderAmount, orderCurrency, customerId, sign, valid, ip4, md5key)
  58. if err != nil {
  59. log.Printf("Error inserting leanworkIn with orderNo =%s \n", orderNo)
  60. return
  61. }
  62. id, err := res.LastInsertId()
  63. if err != nil {
  64. return
  65. }
  66. row.Id = id
  67. row.PickupUrl = pickupUrl
  68. row.ReceiveUrl = receiveUrl
  69. row.SignType = signType
  70. row.OrderNo = orderNo
  71. row.OrderAmount = orderAmount
  72. row.OrderCurrency = orderCurrency
  73. row.CustomerId = customerId
  74. row.Sign = sign
  75. row.Valid = valid
  76. row.Ip4 = ip4
  77. row.MD5Key = md5key
  78. log.Println("INSERT[" + strconv.FormatInt(row.Id, 10) + "]: customerId: " + customerId + " | orderAmount: " + orderCurrency + " " + orderAmount)
  79. return row, err
  80. }
  81. func (m *TransactionDB) addRpnOut(r RpnOut) (row RpnOut, err error) {
  82. if err = m.conn(Config); err != nil {
  83. return row, err
  84. }
  85. defer m.close()
  86. sql := `INSERT INTO rpnOut
  87. (order_id, version, sign_type, mid, notify_url, order_amount, order_time,
  88. user_id, user_name, user_cardno, signature, leanwork, ip4 )
  89. VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)`
  90. insForm, err := m.h.Prepare(sql)
  91. if err != nil {
  92. log.Println("cannot insert RpnOut record, sql prepare failed " + err.Error())
  93. return
  94. }
  95. order_id := r.Order_id
  96. version := r.Version
  97. sign_type := r.Sign_type
  98. mid := r.Mid
  99. notify_url := r.Notify_url
  100. order_amount := r.Order_amount
  101. order_time := r.Order_time
  102. user_id := r.User_id
  103. user_name := r.User_name
  104. user_cardno := r.User_cardno
  105. signature := r.Signature
  106. leanwork := r.Leanwork
  107. ip4 := r.Ip4
  108. res, err := insForm.Exec(order_id, version, sign_type, mid, notify_url, order_amount, order_time,
  109. user_id, user_name, user_cardno, signature, leanwork, ip4)
  110. if err != nil {
  111. log.Println("Failed to execute sql statment for insert RpnOut order_id=" + r.Order_id + " " + err.Error())
  112. return
  113. }
  114. id, err := res.LastInsertId()
  115. if err != nil {
  116. log.Println("Cannot get last insert ID with new RpnOut record")
  117. return
  118. }
  119. row = r
  120. row.Id = id
  121. return row, err
  122. }
  123. func (m *TransactionDB) updateRpnOutNameAndCard(orderid string, signature string, name string, card string) (row RpnOut, err error) {
  124. err = nil
  125. row = RpnOut{}
  126. return
  127. }