From 395c104c69a4df015ef2b7398be997c8c9c5223a Mon Sep 17 00:00:00 2001 From: patrick Date: Wed, 11 Mar 2020 03:46:06 +1100 Subject: [PATCH] add signType to LeanworkOut --- leanworkOut.go | 58 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/leanworkOut.go b/leanworkOut.go index 2bfa0bf..a5f55c6 100644 --- a/leanworkOut.go +++ b/leanworkOut.go @@ -13,6 +13,7 @@ import ( type LeanworkOut struct { Id int64 Leanwork int64 + SignType string OrderNo string OrderAmount string OrderCurrency string @@ -42,6 +43,8 @@ func (m *LeanworkOut) DoHttp() (retry bool, err error) { resp, err := http.PostForm(lin.ReceiveUrl, form) if err != nil { log.Println("Leanwork Server Error, give bad response, " + err.Error()) + m.LeanworkResp = "Http Error: " + err.Error() + m.add2db() return } defer resp.Body.Close() @@ -61,15 +64,46 @@ func (m *LeanworkOut) DoHttp() (retry bool, err error) { return } -func (m *LeanworkOut) startCallBack() { +func startLeanworkCallBack(ri RpnIn) { + lo, err := buildLeanworkOutByRpnIn(ri) + if err != nil { + log.Printf("Fatal: cannot inform Leanwork %+v , \n\t%s\n", ri, err.Error()) + return + } + + if ri.Pay_result != "3" { + log.Printf("Warning: RpnIn is processing not informing leanwork %+v", ri) + return + } + for i := 1; i <= 5; i++ { - retry, err := m.DoHttp() + retry, err := lo.DoHttp() if !retry { break } time.Sleep(5 * time.Minute) //sleep 5 minute and try again - log.Printf("Trying(%d) to report leanwork about transaction status %+v, encountered error %s \n", i, *m, err.Error()) + log.Printf("Trying(%d) to report leanwork about transaction status %+v, encountered error %s \n", i, lo, err.Error()) + } +} + +func buildLeanworkOutByRpnIn(ri RpnIn) (ret LeanworkOut, err error) { + li, err := getLeanworkInById(ri.Leanwork) + if err != nil { + return + } + ret.Leanwork = ri.Leanwork + ret.SignType = "MD5" + ret.OrderNo = li.OrderNo + ret.OrderAmount = li.OrderAmount + ret.OrderCurrency = li.OrderCurrency + ret.TransactionId = ri.Deal_id + if ri.Pay_result == "3" { + ret.Status = "success" + } else if ri.Pay_result == "1" { + ret.Status = "processing" } + + return } func (m *LeanworkOut) add2db() (ret LeanworkOut, err error) { @@ -79,9 +113,9 @@ func (m *LeanworkOut) add2db() (ret LeanworkOut, err error) { defer db.close() q := `INSERT INTO leanworkOut( - leanwork, orderNo, orderAmount, orderCurrency, + leanwork, signType, orderNo, orderAmount, orderCurrency, transactionId, status, sign, leanworkResp) - VALUES(?,?,?,?,?,?,?,?) + VALUES(?,?,?,?,?,?,?,?,?) ` insForm, err := db.h.Prepare(q) if err != nil { @@ -89,7 +123,7 @@ func (m *LeanworkOut) add2db() (ret LeanworkOut, err error) { return } res, err := insForm.Exec( - m.Leanwork, m.OrderNo, m.OrderAmount, m.OrderCurrency, + m.Leanwork, m.SignType, m.OrderNo, m.OrderAmount, m.OrderCurrency, m.TransactionId, m.Status, m.Sign, m.LeanworkResp) if err != nil { @@ -114,7 +148,17 @@ func getLeanWorkOutById(id int64) (row LeanworkOut, err error) { db.conn(Config) defer db.close() err = db.h.QueryRow("SELECT * FROM leanworkOut WHERE id=? ", id).Scan( - &row.Id, &row.Leanwork, &row.OrderNo, &row.OrderAmount, + &row.Id, &row.Leanwork, &row.SignType, &row.OrderNo, &row.OrderAmount, + &row.OrderCurrency, &row.TransactionId, &row.Status, &row.Sign, + &row.Ts, &row.LeanworkResp) + return +} + +func getLeanWorkOutByLeanworkId(id int64) (row LeanworkOut, err error) { + db.conn(Config) + defer db.close() + err = db.h.QueryRow("SELECT * FROM leanworkOut WHERE leanwork=? ORDER BY id DESC LIMIT 1", id).Scan( + &row.Id, &row.Leanwork, &row.SignType, &row.OrderNo, &row.OrderAmount, &row.OrderCurrency, &row.TransactionId, &row.Status, &row.Sign, &row.Ts, &row.LeanworkResp) return