payment gateway for rpn cn
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

157 lines
3.9KB

  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. m.logNewLeanworkIn(row)
  79. return row, err
  80. }
  81. func (m *TransactionDB) logNewLeanworkIn(row LeanworkIn) {
  82. var insert_type string
  83. if row.MD5Key == Config.LeanWork.MD5P2P {
  84. insert_type = "P2P"
  85. } else {
  86. insert_type = "FAT"
  87. }
  88. log.Println(insert_type + " INSERT[" + strconv.FormatInt(row.Id, 10) + "]: customerId: " + row.CustomerId + " | orderAmount: " + row.OrderCurrency + " " + row.OrderAmount)
  89. }
  90. func (m *TransactionDB) addRpnOut(r RpnOut) (row RpnOut, err error) {
  91. if err = m.conn(Config); err != nil {
  92. return row, err
  93. }
  94. defer m.close()
  95. sql := `INSERT INTO rpnOut
  96. (order_id, version, sign_type, mid, notify_url, order_amount, order_time,
  97. user_id, user_name, user_cardno, signature, leanwork, ip4 )
  98. VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)`
  99. insForm, err := m.h.Prepare(sql)
  100. if err != nil {
  101. log.Println("cannot insert RpnOut record, sql prepare failed " + err.Error())
  102. return
  103. }
  104. order_id := r.Order_id
  105. version := r.Version
  106. sign_type := r.Sign_type
  107. mid := r.Mid
  108. notify_url := r.Notify_url
  109. order_amount := r.Order_amount
  110. order_time := r.Order_time
  111. user_id := r.User_id
  112. user_name := r.User_name
  113. user_cardno := r.User_cardno
  114. signature := r.Signature
  115. leanwork := r.Leanwork
  116. ip4 := r.Ip4
  117. res, err := insForm.Exec(order_id, version, sign_type, mid, notify_url, order_amount, order_time,
  118. user_id, user_name, user_cardno, signature, leanwork, ip4)
  119. if err != nil {
  120. log.Println("Failed to execute sql statment for insert RpnOut order_id=" + r.Order_id + " " + err.Error())
  121. return
  122. }
  123. id, err := res.LastInsertId()
  124. if err != nil {
  125. log.Println("Cannot get last insert ID with new RpnOut record")
  126. return
  127. }
  128. row = r
  129. row.Id = id
  130. return row, err
  131. }
  132. func (m *TransactionDB) updateRpnOutNameAndCard(orderid string, signature string, name string, card string) (row RpnOut, err error) {
  133. err = nil
  134. row = RpnOut{}
  135. return
  136. }