76 lines
3.0 KiB
Go
76 lines
3.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
|
"gitea.futuresens.co.uk/futuresens/hardlink/v2/internal/creditcall"
|
|
"gitea.futuresens.co.uk/futuresens/hardlink/v2/internal/types"
|
|
"gitea.futuresens.co.uk/futuresens/logging"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func (app *App) preauthPayment(w http.ResponseWriter, r *http.Request) {
|
|
app.streamPayment(w, r, true)
|
|
}
|
|
|
|
// executeCreditCallPreauth finalizes the existing unconfirmed transaction.
|
|
// The outcome container and its wire projections are shared with SALE; its execution is not.
|
|
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"},
|
|
}
|
|
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
|
|
outcome.Approved = approved
|
|
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
|
|
}
|