|
- package main
-
- import (
- "database/sql"
- "log"
- "net/http"
- "strconv"
- )
-
- type TransactionDB struct {
- h *sql.DB
- }
-
- var db TransactionDB
-
- func (m *TransactionDB) conn(c AppConfig) error {
- if m.h != nil {
- return nil //already connected, prevent multiple connection
- }
- dbDriver := c.DB.Driver
- dbUser := c.DB.User
- dbPass := c.DB.Pass
- dbName := c.DB.Schema
- h, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName)
- if err != nil {
- m.h = nil
- panic(err.Error())
- }
- m.h = h
- return err
- }
-
- func (m *TransactionDB) close() {
- if m.h != nil {
- m.h.Close()
- }
- m.h = nil //clear it, very important to prevent multiple opening
- }
-
- func (m *TransactionDB) addRequest(r *http.Request) (row LeanworkRequest, err error) {
- if err = m.conn(Config); err != nil {
- return row, err
- }
- defer m.close()
-
- r.ParseForm()
- //assuming form has been parsed
- row = LeanworkRequest{}
- pickupUrl := r.FormValue("pickupUrl")
- receiveUrl := r.FormValue("receiveUrl")
- signType := r.FormValue("signType")
- orderNo := r.FormValue("orderNo")
- orderAmount := r.FormValue("orderAmount")
- orderCurrency := r.FormValue("orderCurrency")
- customerId := r.FormValue("customerId")
- sign := r.FormValue("sign")
- valid := isLeanworkFormValid(r.Form)
- ip4 := getClientIPLong(r)
-
- insForm, err := m.h.Prepare("INSERT INTO request(pickupUrl, receiveUrl, signType, orderNo, orderAmount, orderCurrency, customerId, sign, valid, ip4) VALUES(?,?,?,?,?,?,?,?,?,?)")
- if err != nil {
- log.Printf("Failed to prepare SQL statment for insert")
- return
- }
-
- res, err := insForm.Exec(pickupUrl, receiveUrl, signType, orderNo, orderAmount, orderCurrency, customerId, sign, valid, ip4)
- if err != nil {
- log.Printf("Error inserting leanwork request with orderNo =%s \n", orderNo)
- return
- }
-
- id, err := res.LastInsertId()
- if err != nil {
- return
- }
-
- row.Id = id
- row.PickupUrl = pickupUrl
- row.ReceiveUrl = receiveUrl
- row.SignType = signType
- row.OrderNo = orderNo
- row.OrderAmount = orderAmount
- row.OrderCurrency = orderCurrency
- row.CustomerId = customerId
- row.Sign = sign
- row.Valid = valid
- row.Ip4 = ip4
- log.Println("INSERT[" + strconv.FormatInt(row.Id, 10) + "]: customerId: " + customerId + " | orderAmount: " + orderCurrency + " " + orderAmount)
-
- return row, err
- }
-
- func (m *TransactionDB) addRpnOut(r RpnOut) (row RpnOut, err error) {
- if err = m.conn(Config); err != nil {
- return row, err
- }
- defer m.close()
-
- sql := `INSERT INTO rpnOut
- (order_id, version, sign_type, mid, notify_url, order_amount, order_time,
- user_id, user_name, user_cardno, signature, leanwork, ip4 )
- VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)`
- insForm, err := m.h.Prepare(sql)
-
- if err != nil {
- log.Println("cannot insert RpnOut record, sql prepare failed " + err.Error())
- return
- }
-
- order_id := r.Order_id
- version := r.Version
- sign_type := r.Sign_type
- mid := r.Mid
- notify_url := r.Notify_url
- order_amount := r.Order_amount
- order_time := r.Order_time
- user_id := r.User_id
- user_name := r.User_name
- user_cardno := r.User_cardno
- signature := r.Signature
- leanwork := r.Leanwork
- ip4 := r.Ip4
-
- res, err := insForm.Exec(order_id, version, sign_type, mid, notify_url, order_amount, order_time,
- user_id, user_name, user_cardno, signature, leanwork, ip4)
- if err != nil {
- log.Println("Failed to execute sql statment for insert RpnOut order_id=" + r.Order_id + " " + err.Error())
- return
- }
- id, err := res.LastInsertId()
- if err != nil {
- log.Println("Cannot get last insert ID with new RpnOut record")
- return
- }
- row = r
- row.Id = id
- return row, err
- }
-
- func (m *TransactionDB) updateRpnOutNameAndCard(orderid string, signature string, name string, card string) (row RpnOut, err error) {
- err = nil
- row = RpnOut{}
- return
- }
|