|
|
|
|
|
|
|
|
package main |
|
|
package main |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
|
"io/ioutil" |
|
|
|
|
|
"log" |
|
|
|
|
|
"net/http" |
|
|
|
|
|
"net/url" |
|
|
|
|
|
"strconv" |
|
|
|
|
|
"strings" |
|
|
|
|
|
"time" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type LeanworkOut struct { |
|
|
|
|
|
Id int64 |
|
|
|
|
|
Leanwork int64 |
|
|
|
|
|
OrderNo string |
|
|
|
|
|
OrderAmount string |
|
|
|
|
|
OrderCurrency string |
|
|
|
|
|
TransactionId string |
|
|
|
|
|
Status string |
|
|
|
|
|
Sign string |
|
|
|
|
|
Ts time.Time |
|
|
|
|
|
LeanworkResp string |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//inform Leanwork of final result, retry 5 times if not successful |
|
|
|
|
|
func (m *LeanworkOut) DoHttp() (retry bool, err error) { |
|
|
|
|
|
retry = true |
|
|
|
|
|
form := url.Values{} |
|
|
|
|
|
form.Set("signType", "MD5") |
|
|
|
|
|
form.Set("orderNo", m.OrderNo) |
|
|
|
|
|
form.Set("orderAmount", m.OrderAmount) |
|
|
|
|
|
form.Set("orderCurrency", m.OrderCurrency) |
|
|
|
|
|
form.Set("transactionId", m.TransactionId) |
|
|
|
|
|
form.Set("sign", m.Sign) |
|
|
|
|
|
|
|
|
|
|
|
lin, err := getLeanworkInById(m.Leanwork) //which is very unlikely |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Println("Fatal: Cannot get LeanworkIn by ID =" + strconv.FormatInt(m.Leanwork, 10) + "error " + err.Error()) |
|
|
|
|
|
retry = false |
|
|
|
|
|
} |
|
|
|
|
|
resp, err := http.PostForm(lin.ReceiveUrl, form) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Println("Leanwork Server Error, give bad response, " + err.Error()) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
defer resp.Body.Close() |
|
|
|
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusOK { |
|
|
|
|
|
bodyBytes, err := ioutil.ReadAll(resp.Body) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Println("Fatal: Cannot read leanwork Http Response " + err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
bodyString := string(bodyBytes) |
|
|
|
|
|
if strings.Contains(strings.ToLower(bodyString), "success") { |
|
|
|
|
|
retry = false |
|
|
|
|
|
} else { |
|
|
|
|
|
log.Println("Leanwork response without success word : " + bodyString) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (m *LeanworkOut) startCallBack() { |
|
|
|
|
|
for i := 1; i <= 5; i++ { |
|
|
|
|
|
retry, err := m.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()) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (m *LeanworkOut) add2db() (ret LeanworkOut, err error) { |
|
|
|
|
|
if err = db.conn(Config); err != nil { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
defer db.close() |
|
|
|
|
|
|
|
|
|
|
|
q := `INSERT INTO leanworkOut( |
|
|
|
|
|
leanwork, orderNo, orderAmount, orderCurrency, |
|
|
|
|
|
transactionId, status, sign, leanworkResp) |
|
|
|
|
|
VALUES(?,?,?,?,?,?,?,?) |
|
|
|
|
|
` |
|
|
|
|
|
insForm, err := db.h.Prepare(q) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Printf("Failed to prepare SQL statment for insert leanworkOut" + err.Error()) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
res, err := insForm.Exec( |
|
|
|
|
|
m.Leanwork, m.OrderNo, m.OrderAmount, m.OrderCurrency, |
|
|
|
|
|
m.TransactionId, m.Status, m.Sign, m.LeanworkResp) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Printf("Error inserting leanworkOut with orderNo =%s, %s \n", m.OrderNo, err.Error()) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
id, err := res.LastInsertId() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.Printf("Cannot retrieve lastInsertId for orderID %s", m.OrderNo) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ret, err = getLeanWorkOutById(id) |
|
|
|
|
|
if err == nil { |
|
|
|
|
|
*m = ret |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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.OrderCurrency, &row.TransactionId, &row.Status, &row.Sign, |
|
|
|
|
|
&row.Ts, &row.LeanworkResp) |
|
|
|
|
|
return |
|
|
|
|
|
} |