|
- package main
-
- import (
- "database/sql"
- "fmt"
- "log"
- "net/http"
- "strconv"
- )
-
- type TransactionDB struct {
- h *sql.DB
- }
-
- var db TransactionDB
-
- func (m *TransactionDB) conn(c AppConfig) error {
- 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 {
- panic(err.Error())
- }
- m.h = h
- fmt.Printf("%x", h)
- return err
- }
-
- func (m *TransactionDB) close() {
- defer m.h.Close()
- }
-
- 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 RpnReq) error {
- return nil
- }
-
- func (m *TransactionDB) updateRpnOutNameAndCard(orderid string, signature string, name string, card string) (row RpnReq, err error) {
- err = nil
- row = RpnReq{}
- return
- }
|