Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
28aea4aab2 |
@@ -33,7 +33,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
buildVersion = "v2.1.3"
|
||||
buildVersion = "v2.1.4"
|
||||
serviceName = "hardlink"
|
||||
pollingFrequency = 8 * time.Second
|
||||
)
|
||||
|
||||
+1
-1
@@ -89,7 +89,7 @@ func InsertPreauth(ctx context.Context, db *sql.DB, m map[string]string, checkou
|
||||
if err != nil {
|
||||
return fmt.Errorf("InsertPreauth: parsing checkoutDate %q: %w", checkoutDate, err)
|
||||
}
|
||||
releaseDate := checkOutDate.Add(48 * time.Hour)
|
||||
releaseDate := checkOutDate.Add(96 * time.Hour)
|
||||
|
||||
const stmt = `
|
||||
INSERT INTO dbo.Preauthorizations
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/creditcall"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
||||
"gitea.futuresens.co.uk/futuresens/logging"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type creditCallPreauthRequest struct {
|
||||
@@ -25,6 +26,11 @@ func (app *App) streamCreditCallPreauth(w http.ResponseWriter, r *http.Request)
|
||||
func (app *App) executeCreditCallPreauth(ctx context.Context, request cmstypes.TransactionRec,
|
||||
start func(*http.Client) (creditcall.TransactionResultXML, error)) creditCallSaleOutcome {
|
||||
const op = logging.Op("takePreauthorization")
|
||||
entry := log.WithFields(log.Fields{
|
||||
"operation": "preauth", "hotel": app.cfg.Hotel, "kiosk": app.cfg.Kiosk,
|
||||
"requestedType": request.TransactionType, "requestedAmount": request.AmountMinorUnits,
|
||||
})
|
||||
entry.Info("CreditCall PREAUTH started")
|
||||
outcome := creditCallSaleOutcome{
|
||||
HTTPStatus: http.StatusBadGateway,
|
||||
Status: cmstypes.StatusRec{Code: http.StatusInternalServerError, Message: "500 Internal server error"},
|
||||
@@ -32,14 +38,32 @@ func (app *App) executeCreditCallPreauth(ctx context.Context, request cmstypes.T
|
||||
transaction, err := start(app.creditCallClient())
|
||||
if err != nil {
|
||||
logging.Error(types.ServiceName, err.Error(), "Preauth processing error", string(op), "", "", 0)
|
||||
entry.Error("CreditCall PREAUTH ended without a processor result")
|
||||
outcome.FailureType = types.ResultError
|
||||
outcome.FailureDescription = "No response from payment processor"
|
||||
return outcome
|
||||
}
|
||||
var result creditcall.PaymentResult
|
||||
result.FillFromTransactionResult(transaction)
|
||||
entry = entry.WithFields(log.Fields{
|
||||
"reference": result.Fields[types.Reference],
|
||||
"transactionResult": result.Fields[types.TransactionResult],
|
||||
"transactionType": result.Fields[types.TransactionType],
|
||||
"totalAmount": result.Fields[types.TotalAmount],
|
||||
"receiptPresent": result.CardholderReceipt != "",
|
||||
})
|
||||
entry.Info("CreditCall PREAUTH processor result received")
|
||||
if result.Fields[types.TransactionType] == "" {
|
||||
entry.Warn("CreditCall PREAUTH result has no TRANSACTION_TYPE")
|
||||
}
|
||||
app.printCreditCallReceipt(result.CardholderReceipt)
|
||||
approved, persist := creditcall.PreauthDecision(result.Fields)
|
||||
decision := entry.WithFields(log.Fields{"approved": approved, "persist": persist})
|
||||
if approved {
|
||||
decision.Info("CreditCall PREAUTH decision")
|
||||
} else {
|
||||
decision.Warn("CreditCall PREAUTH decision")
|
||||
}
|
||||
outcome.HTTPStatus = http.StatusOK
|
||||
outcome.Status = result.Status
|
||||
outcome.Payment = result
|
||||
@@ -47,6 +71,10 @@ func (app *App) executeCreditCallPreauth(ctx context.Context, request cmstypes.T
|
||||
outcome.FailureType = result.Fields[types.TransactionResult]
|
||||
outcome.FailureDescription = result.Fields[types.Errors]
|
||||
if persist {
|
||||
if result.Fields[types.TotalAmount] == "" {
|
||||
entry.Warn("CreditCall PREAUTH persistence requested without TOTAL_AMOUNT")
|
||||
}
|
||||
entry.Info("CreditCall PREAUTH persistence scheduled")
|
||||
go app.persistPreauth(ctx, result.Fields, request.CheckoutDate)
|
||||
}
|
||||
return outcome
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/config"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/creditcall"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
||||
log "github.com/sirupsen/logrus"
|
||||
logtest "github.com/sirupsen/logrus/hooks/test"
|
||||
)
|
||||
|
||||
func capturePreauthLogs(t *testing.T) *logtest.Hook {
|
||||
t.Helper()
|
||||
logger := log.StandardLogger()
|
||||
previousHooks := logger.ReplaceHooks(make(log.LevelHooks))
|
||||
previousLevel := logger.GetLevel()
|
||||
logger.SetLevel(log.InfoLevel)
|
||||
hook := logtest.NewGlobal()
|
||||
t.Cleanup(func() {
|
||||
logger.ReplaceHooks(previousHooks)
|
||||
logger.SetLevel(previousLevel)
|
||||
})
|
||||
return hook
|
||||
}
|
||||
|
||||
func requirePreauthLog(t *testing.T, hook *logtest.Hook, message string, level log.Level) *log.Entry {
|
||||
t.Helper()
|
||||
for _, entry := range hook.AllEntries() {
|
||||
if entry.Message == message {
|
||||
if entry.Level != level {
|
||||
t.Errorf("log %q level=%v, want %v", message, entry.Level, level)
|
||||
}
|
||||
return entry
|
||||
}
|
||||
}
|
||||
t.Fatalf("missing log %q", message)
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestCreditCallPreauthDecisionLogging(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, result, kind string
|
||||
approved bool
|
||||
level log.Level
|
||||
}{
|
||||
{"verification", "APPROVED", "ACCOUNT VERIFICATION", true, log.InfoLevel},
|
||||
{"missing type", "APPROVED", "", false, log.WarnLevel},
|
||||
{"declined", "DECLINED", "SALE", false, log.WarnLevel},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
hook := capturePreauthLogs(t)
|
||||
app := &App{cfg: &config.ConfigRec{}, creditCallReceipt: func(string) {}}
|
||||
outcome := app.executeCreditCallPreauth(context.Background(), cmstypes.TransactionRec{TransactionType: "AccountVerification"},
|
||||
func(*http.Client) (creditcall.TransactionResultXML, error) {
|
||||
return creditcall.TransactionResultXML{Entries: []creditcall.EntryXML{
|
||||
{Key: types.TransactionResult, Value: tc.result},
|
||||
{Key: types.TransactionType, Value: tc.kind},
|
||||
{Key: types.Reference, Value: "log-test-ref"},
|
||||
{Key: types.PanMasked, Value: "sensitive-card-sentinel"},
|
||||
{Key: types.CardHash, Value: "sensitive-hash-sentinel"},
|
||||
{Key: types.ReceiptDataCardholder, Value: "sensitive-receipt-sentinel"},
|
||||
}}, nil
|
||||
})
|
||||
if outcome.Approved != tc.approved {
|
||||
t.Errorf("approved=%t, want %t", outcome.Approved, tc.approved)
|
||||
}
|
||||
requirePreauthLog(t, hook, "CreditCall PREAUTH started", log.InfoLevel)
|
||||
provider := requirePreauthLog(t, hook, "CreditCall PREAUTH processor result received", log.InfoLevel)
|
||||
if provider.Data["transactionType"] != tc.kind || provider.Data["transactionResult"] != tc.result || provider.Data["receiptPresent"] != true {
|
||||
t.Errorf("provider log=%v, want type=%q result=%q receiptPresent=true", provider.Data, tc.kind, tc.result)
|
||||
}
|
||||
decision := requirePreauthLog(t, hook, "CreditCall PREAUTH decision", tc.level)
|
||||
if decision.Data["approved"] != tc.approved || decision.Data["persist"] != false || decision.Data["reference"] != "log-test-ref" {
|
||||
t.Errorf("decision log=%v, want approved=%t persist=false reference=log-test-ref", decision.Data, tc.approved)
|
||||
}
|
||||
if tc.kind == "" {
|
||||
requirePreauthLog(t, hook, "CreditCall PREAUTH result has no TRANSACTION_TYPE", log.WarnLevel)
|
||||
}
|
||||
for _, entry := range hook.AllEntries() {
|
||||
for key, value := range entry.Data {
|
||||
if value == "sensitive-card-sentinel" || value == "sensitive-hash-sentinel" || value == "sensitive-receipt-sentinel" {
|
||||
t.Errorf("log %q exposed sensitive field %q", entry.Message, key)
|
||||
}
|
||||
}
|
||||
if entry.Message == "CreditCall PREAUTH persistence scheduled" {
|
||||
t.Error("non-persisting result logged persistence scheduling")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreditCallPreauthStartFailureLogging(t *testing.T) {
|
||||
hook := capturePreauthLogs(t)
|
||||
app := &App{cfg: &config.ConfigRec{}}
|
||||
outcome := app.executeCreditCallPreauth(context.Background(), cmstypes.TransactionRec{},
|
||||
func(*http.Client) (creditcall.TransactionResultXML, error) {
|
||||
return creditcall.TransactionResultXML{}, errors.New("test transport failure")
|
||||
})
|
||||
if outcome.HTTPStatus != http.StatusBadGateway {
|
||||
t.Errorf("HTTPStatus=%d, want 502", outcome.HTTPStatus)
|
||||
}
|
||||
requirePreauthLog(t, hook, "CreditCall PREAUTH started", log.InfoLevel)
|
||||
requirePreauthLog(t, hook, "CreditCall PREAUTH ended without a processor result", log.ErrorLevel)
|
||||
}
|
||||
|
||||
func TestCreditCallPreauthPersistenceLogging(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, amount, message string
|
||||
level log.Level
|
||||
breakSpool bool
|
||||
}{
|
||||
{"SQL", "3100", "CreditCall PREAUTH SQL persistence completed", log.InfoLevel, false},
|
||||
{"spool", "", "CreditCall PREAUTH saved to spool; SQL persistence pending", log.WarnLevel, false},
|
||||
{"spool failure", "", "failed to spool preauth", log.ErrorLevel, true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
hook := capturePreauthLogs(t)
|
||||
app := &App{cfg: &config.ConfigRec{}}
|
||||
preauthTestDatabase(t, app)
|
||||
if tc.breakSpool {
|
||||
path := filepath.Join(app.cfg.LogDir, "not-a-directory")
|
||||
if err := os.WriteFile(path, nil, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
app.cfg.LogDir = path
|
||||
}
|
||||
app.persistPreauth(context.Background(), map[string]string{
|
||||
types.Reference: "persist-test-ref", types.TotalAmount: tc.amount,
|
||||
}, "2026-10-21 00:00:00 +0000")
|
||||
entry := requirePreauthLog(t, hook, tc.message, tc.level)
|
||||
if entry.Data["reference"] != "persist-test-ref" || entry.Data["operation"] != "preauth" {
|
||||
t.Errorf("persistence log=%v, want reference=persist-test-ref operation=preauth", entry.Data)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/db"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -74,18 +75,23 @@ func (app *App) spoolPath() string {
|
||||
// It never returns an error to the caller (so your HTTP flow stays simple),
|
||||
// but it logs failures.
|
||||
func (app *App) persistPreauth(ctx context.Context, fields map[string]string, checkoutDate string) {
|
||||
entry := log.WithFields(log.Fields{
|
||||
"operation": "preauth", "reference": fields[types.Reference],
|
||||
"hotel": app.cfg.Hotel, "kiosk": app.cfg.Kiosk,
|
||||
})
|
||||
// First, try DB (with your reconnect logic inside getDB)
|
||||
dbConn, err := app.getDB(ctx)
|
||||
if err == nil && dbConn != nil {
|
||||
if err := db.InsertPreauth(ctx, dbConn, fields, checkoutDate); err == nil {
|
||||
entry.Info("CreditCall PREAUTH SQL persistence completed")
|
||||
// opportunistic drain once DB is alive
|
||||
go app.drainPreauthSpool(context.Background())
|
||||
return
|
||||
} else {
|
||||
log.WithError(err).Warn("DB insert failed; will spool preauth")
|
||||
entry.WithError(err).Warn("DB insert failed; will spool preauth")
|
||||
}
|
||||
} else {
|
||||
log.WithError(err).Warn("DB unavailable; will spool preauth")
|
||||
entry.WithError(err).Warn("DB unavailable; will spool preauth")
|
||||
}
|
||||
|
||||
// Fallback: spool to file
|
||||
@@ -95,7 +101,9 @@ func (app *App) persistPreauth(ctx context.Context, fields map[string]string, ch
|
||||
Fields: fields,
|
||||
}
|
||||
if spErr := app.spoolPreauth(rec); spErr != nil {
|
||||
log.WithError(spErr).Error("failed to spool preauth")
|
||||
entry.WithError(spErr).Error("failed to spool preauth")
|
||||
} else {
|
||||
entry.Warn("CreditCall PREAUTH saved to spool; SQL persistence pending")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
builtVersion is a const in main.go
|
||||
|
||||
#### v2.1.4 - 19 October 2026
|
||||
added detailed logging for preauth payment processing and result streaming
|
||||
|
||||
#### v2.1.3 - 30 September 2026
|
||||
feat(logging): add weekly log rotation and retention
|
||||
|
||||
|
||||
Reference in New Issue
Block a user