added functionality to commit transactions

This commit is contained in:
2025-08-30 23:03:39 +01:00
parent 1bceb55285
commit f1dc0ccce4
3 changed files with 130 additions and 48 deletions
+80 -26
View File
@@ -16,6 +16,11 @@ import (
)
const (
// Transaction types
SaleTransactionType = "sale"
AccountVerificationType = "account verification"
// Transaction results
ResultApproved = "approved"
ResultDeclined = "declined"
ResultCancelled = "cancelled"
@@ -23,6 +28,24 @@ const (
ResultError = "error"
CheckinSuccessfulEndpoint = "/successful" // Endpoint to send guest to after successful payment
CheckinUnsuccessfulEndpoint = "/unsuccessful"
// Response map keys
CardReference = "CARD_REFERENCE"
CardHash = "CARD_HASH"
Errors = "ERRORS"
ReceiptData = "RECEIPT_DATA"
ReceiptDataMerchant = "RECEIPT_DATA_MERCHANT"
ReceiptDataCardholder = "RECEIPT_DATA_CARDHOLDER"
Reference = "REFERENCE"
TransactionResult = "TRANSACTION_RESULT"
TransactionType = "TRANSACTION_TYPE"
ConfirmResult = "CONFIRM_RESULT"
ConfirmErrors = "CONFIRM_ERRORS"
// Log field keys
LogFieldError = "error"
LogFieldDescription = "description"
LogResult = "transactionResult"
)
// XML parsing structs
@@ -42,6 +65,14 @@ type (
Key string `xml:"Key"`
Value string `xml:"Value"`
}
TransactionConfirmation struct {
XMLName xml.Name `xml:"TransactionConfirmation"`
Result string `xml:"Result"`
Errors string `xml:"Errors"`
ErrorDescription string `xml:"ErrorDescription"`
ReceiptDataCardholder string `xml:"ReceiptDataCardholder"`
}
)
// ParseTransactionResult parses the XML into entries.
@@ -217,47 +248,70 @@ func nullableFloatArg(nf sql.NullFloat64) interface{} {
return nil
}
// BuildRedirectURL builds the redirect URL to send the guest to after payment.
func BuildRedirectURL(result map[string]string) string {
var msgType, description string
q := url.Values{}
res := result[TransactionResult]
tType := result[TransactionType]
res := strings.ToLower(result["TRANSACTION_RESULT"])
// Transaction approved?
if strings.EqualFold(res, ResultApproved) {
switch {
// Transaction type AccountVerification?
case strings.EqualFold(tType, AccountVerificationType):
log.WithField(LogResult, result[TransactionResult]).
Info("Account verification approved")
if res == ResultApproved {
q.Set("TxnReference", result["REFERENCE"])
q.Set("CardHash", hex.EncodeToString([]byte(result["CARD_HASH"])))
q.Set("CardReference", hex.EncodeToString([]byte(result["CARD_REFERENCE"])))
u := url.URL{
Path: CheckinSuccessfulEndpoint,
RawQuery: q.Encode(),
return buildSuccessURL(result)
// Transaction type Sale?
case strings.EqualFold(tType, SaleTransactionType):
// Transaction confirmed?
if strings.EqualFold(result[ConfirmResult], ResultApproved) {
log.WithField(LogResult, result[ConfirmResult]).
Info("Transaction approved and confirmed")
return buildSuccessURL(result)
}
// Not confirmed
log.WithFields(log.Fields{LogFieldError: result[ConfirmResult], LogFieldDescription: result[ConfirmErrors]}).
Error("Transaction approved but not confirmed")
return BuildFailureURL(result[ConfirmResult], result[ConfirmErrors])
}
return u.String()
}
msgType = ResultError
if res != "" {
msgType = res
}
// Not approved
return BuildFailureURL(res, result[Errors])
}
errors, ok := result["ERRORS"]
if ok && errors != "" {
description = errors
}
errors, ok = result["ERROR"]
if ok && errors != "" {
description += " " + errors
func buildSuccessURL(result map[string]string) string {
q := url.Values{}
q.Set("TxnReference", result[Reference])
q.Set("CardHash", hex.EncodeToString([]byte(result[CardHash])))
q.Set("CardReference", hex.EncodeToString([]byte(result[CardReference])))
return (&url.URL{
Path: CheckinSuccessfulEndpoint,
RawQuery: q.Encode(),
}).String()
}
func BuildFailureURL(msgType, description string) string {
q := url.Values{}
if msgType == "" {
msgType = ResultError
}
if description == "" {
description = "Transaction failed"
}
log.Errorf("Transaction %s: %s", msgType, description)
log.WithFields(log.Fields{LogFieldError: msgType, LogFieldDescription: description}).
Error("Transaction failed")
q.Set("MsgType", msgType)
q.Set("Description", description)
u := url.URL{
return (&url.URL{
Path: CheckinUnsuccessfulEndpoint,
RawQuery: q.Encode(),
}
return u.String()
}).String()
}