72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"encoding/xml"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
|
"gitea.futuresens.co.uk/futuresens/hardlink/payment"
|
|
"gitea.futuresens.co.uk/futuresens/hardlink/types"
|
|
"gitea.futuresens.co.uk/futuresens/logging"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func writeTransactionResult(w http.ResponseWriter, status int, theResponse cmstypes.ResponseRec) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
if err := json.NewEncoder(w).Encode(theResponse); err != nil {
|
|
logging.Error(types.ServiceName, err.Error(), "JSON encode error", "startTransaction", "", "", 0)
|
|
}
|
|
}
|
|
|
|
func callChipDNA(client *http.Client, url string, payload []byte) ([]byte, error) {
|
|
|
|
resp, err := client.Post(url, "text/xml", bytes.NewBuffer(payload))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
return io.ReadAll(resp.Body)
|
|
}
|
|
|
|
func confirmWithRetry(client *http.Client, req payment.ConfirmTransactionRequest, attempts int) ([]byte, error) {
|
|
|
|
payload, err := xml.Marshal(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var lastErr error
|
|
|
|
for i := 1; i <= attempts; i++ {
|
|
|
|
resp, err := client.Post(types.LinkConfirmTransaction, "text/xml", bytes.NewBuffer(payload))
|
|
if err != nil {
|
|
lastErr = err
|
|
} else {
|
|
|
|
body, readErr := io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
|
|
if readErr != nil {
|
|
lastErr = readErr
|
|
} else {
|
|
return body, nil
|
|
}
|
|
}
|
|
|
|
log.Warnf("ConfirmTransaction attempt %d/%d failed: %v", i, attempts, lastErr)
|
|
|
|
if i < attempts {
|
|
time.Sleep(2 * time.Second)
|
|
}
|
|
}
|
|
|
|
return nil, lastErr
|
|
}
|