| type LeanworkOut struct { | type LeanworkOut struct { | ||||
| Id int64 | Id int64 | ||||
| Leanwork int64 | Leanwork int64 | ||||
| SignType string | |||||
| OrderNo string | OrderNo string | ||||
| OrderAmount string | OrderAmount string | ||||
| OrderCurrency string | OrderCurrency string | ||||
| resp, err := http.PostForm(lin.ReceiveUrl, form) | resp, err := http.PostForm(lin.ReceiveUrl, form) | ||||
| if err != nil { | if err != nil { | ||||
| log.Println("Leanwork Server Error, give bad response, " + err.Error()) | log.Println("Leanwork Server Error, give bad response, " + err.Error()) | ||||
| m.LeanworkResp = "Http Error: " + err.Error() | |||||
| m.add2db() | |||||
| return | return | ||||
| } | } | ||||
| defer resp.Body.Close() | defer resp.Body.Close() | ||||
| return | 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++ { | for i := 1; i <= 5; i++ { | ||||
| retry, err := m.DoHttp() | |||||
| retry, err := lo.DoHttp() | |||||
| if !retry { | if !retry { | ||||
| break | break | ||||
| } | } | ||||
| time.Sleep(5 * time.Minute) //sleep 5 minute and try again | 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) { | func (m *LeanworkOut) add2db() (ret LeanworkOut, err error) { | ||||
| defer db.close() | defer db.close() | ||||
| q := `INSERT INTO leanworkOut( | q := `INSERT INTO leanworkOut( | ||||
| leanwork, orderNo, orderAmount, orderCurrency, | |||||
| leanwork, signType, orderNo, orderAmount, orderCurrency, | |||||
| transactionId, status, sign, leanworkResp) | transactionId, status, sign, leanworkResp) | ||||
| VALUES(?,?,?,?,?,?,?,?) | |||||
| VALUES(?,?,?,?,?,?,?,?,?) | |||||
| ` | ` | ||||
| insForm, err := db.h.Prepare(q) | insForm, err := db.h.Prepare(q) | ||||
| if err != nil { | if err != nil { | ||||
| return | return | ||||
| } | } | ||||
| res, err := insForm.Exec( | 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) | m.TransactionId, m.Status, m.Sign, m.LeanworkResp) | ||||
| if err != nil { | if err != nil { | ||||
| db.conn(Config) | db.conn(Config) | ||||
| defer db.close() | defer db.close() | ||||
| err = db.h.QueryRow("SELECT * FROM leanworkOut WHERE id=? ", id).Scan( | 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.OrderCurrency, &row.TransactionId, &row.Status, &row.Sign, | ||||
| &row.Ts, &row.LeanworkResp) | &row.Ts, &row.LeanworkResp) | ||||
| return | return |