payment gateway for rpn cn
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 satır
3.6KB

  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)
  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) (row LeanworkRequest, 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 = LeanworkRequest{}
  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)
  51. ip4 := getClientIPLong(r)
  52. insForm, err := m.h.Prepare("INSERT INTO request(pickupUrl, receiveUrl, signType, orderNo, orderAmount, orderCurrency, customerId, sign, valid, ip4) 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)
  58. if err != nil {
  59. log.Printf("Error inserting leanwork request 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. log.Println("INSERT[" + strconv.FormatInt(row.Id, 10) + "]: customerId: " + customerId + " | orderAmount: " + orderCurrency + " " + orderAmount)
  78. return row, err
  79. }
  80. func (m *TransactionDB) addRpnOut(r RpnOut) (row RpnOut, err error) {
  81. if err = m.conn(Config); err != nil {
  82. return row, err
  83. }
  84. defer m.close()
  85. sql := `INSERT INTO rpnOut
  86. (order_id, version, sign_type, mid, notify_url, order_amount, order_time,
  87. user_id, user_name, user_cardno, signature, leanwork, ip4 )
  88. VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)`
  89. insForm, err := m.h.Prepare(sql)
  90. if err != nil {
  91. log.Println("cannot insert RpnOut record, sql prepare failed " + err.Error())
  92. return
  93. }
  94. order_id := r.Order_id
  95. version := r.Version
  96. sign_type := r.Sign_type
  97. mid := r.Mid
  98. notify_url := r.Notify_url
  99. order_amount := r.Order_amount
  100. order_time := r.Order_time
  101. user_id := r.User_id
  102. user_name := r.User_name
  103. user_cardno := r.User_cardno
  104. signature := r.Signature
  105. leanwork := r.Leanwork
  106. ip4 := r.Ip4
  107. res, err := insForm.Exec(order_id, version, sign_type, mid, notify_url, order_amount, order_time,
  108. user_id, user_name, user_cardno, signature, leanwork, ip4)
  109. if err != nil {
  110. log.Println("Failed to execute sql statment for insert RpnOut order_id=" + r.Order_id + " " + err.Error())
  111. return
  112. }
  113. id, err := res.LastInsertId()
  114. if err != nil {
  115. log.Println("Cannot get last insert ID with new RpnOut record")
  116. return
  117. }
  118. row = r
  119. row.Id = id
  120. return row, err
  121. }
  122. func (m *TransactionDB) updateRpnOutNameAndCard(orderid string, signature string, name string, card string) (row RpnOut, err error) {
  123. err = nil
  124. row = RpnOut{}
  125. return
  126. }