157 lines
5.7 KiB
Go
157 lines
5.7 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
|
"gitea.futuresens.co.uk/futuresens/hardlink/v2/internal/creditcall"
|
|
"gitea.futuresens.co.uk/futuresens/hardlink/v2/internal/paymentsvc"
|
|
"gitea.futuresens.co.uk/futuresens/hardlink/v2/internal/types"
|
|
"gitea.futuresens.co.uk/futuresens/hardlink/v2/paymentapi"
|
|
)
|
|
|
|
// creditCallProcessor delegates to the unchanged legacy execution/finalization core.
|
|
type creditCallProcessor struct{ app *App }
|
|
|
|
type creditCallInitialKind uint8
|
|
|
|
const (
|
|
creditCallInitialUnknown creditCallInitialKind = iota
|
|
creditCallInitialApproved
|
|
creditCallInitialDeclined
|
|
creditCallInitialCancelled
|
|
creditCallInitialTimeout
|
|
)
|
|
|
|
// creditCallInitialResult describes only the completed initial SDK response.
|
|
type creditCallInitialResult struct {
|
|
kind creditCallInitialKind
|
|
reason string
|
|
}
|
|
|
|
func (*creditCallProcessor) ValidatePreauth(req paymentsvc.SaleRequest) error {
|
|
if req.Amount < 0 {
|
|
return paymentsvc.ErrPreauthUnsupported
|
|
}
|
|
return nil
|
|
}
|
|
func (p *creditCallProcessor) Sale(ctx context.Context, req paymentsvc.SaleRequest, status paymentsvc.StatusHandler) (*paymentsvc.Result, error) {
|
|
return p.run(ctx, req, status, false)
|
|
}
|
|
func (p *creditCallProcessor) Preauth(ctx context.Context, req paymentsvc.SaleRequest, status paymentsvc.StatusHandler) (*paymentsvc.Result, error) {
|
|
return p.run(ctx, req, status, true)
|
|
}
|
|
func (p *creditCallProcessor) run(ctx context.Context, req paymentsvc.SaleRequest, status paymentsvc.StatusHandler, preauth bool) (*paymentsvc.Result, error) {
|
|
transaction := cmstypes.TransactionRec{AmountMinorUnits: strconv.FormatInt(req.Amount, 10), TransactionType: "Sale", CheckoutDate: req.CheckoutDate}
|
|
if preauth && req.Amount == 0 {
|
|
transaction.AmountMinorUnits = ""
|
|
transaction.TransactionType = "AccountVerification"
|
|
}
|
|
var initial creditCallInitialResult
|
|
var diagnostic creditCallDiagnostic
|
|
start := func(client *http.Client) (creditcall.TransactionResultXML, error) {
|
|
result, err := callChipDNATransactionStream(context.WithoutCancel(ctx), client, transaction, func(code string) {
|
|
if status != nil {
|
|
status(paymentsvc.StatusUpdate{Code: code})
|
|
}
|
|
})
|
|
if err == nil {
|
|
initial = classifyCreditCallInitialResult(result)
|
|
diagnostic = creditCallInitialDiagnostic(result)
|
|
}
|
|
return result, err
|
|
}
|
|
var outcome creditCallSaleOutcome
|
|
if preauth {
|
|
// The existing asynchronous PREAUTH persistence must outlive response delivery.
|
|
outcome = p.app.executeCreditCallPreauth(context.WithoutCancel(ctx), transaction, start)
|
|
} else {
|
|
outcome = p.app.executeCreditCallSale(transaction, start)
|
|
}
|
|
final := outcome.streamResult(initial)
|
|
final.AmountMinor, final.CurrencyCode = req.Amount, req.Currency
|
|
if outcome.Approved {
|
|
if raw := outcome.Payment.Fields[types.TotalAmount]; raw != "" {
|
|
amount, err := strconv.ParseInt(raw, 10, 64)
|
|
if err != nil || amount < 0 {
|
|
final.Outcome, final.Retryable = "unknown", false
|
|
final.AmountMinor = 0
|
|
final.Reason = "amount_unverified"
|
|
final.Message = "Payment amount uncertain; contact reception"
|
|
} else {
|
|
final.AmountMinor = amount
|
|
}
|
|
}
|
|
}
|
|
if outcome.confirmationDiagnostic.reason != "" {
|
|
diagnostic = outcome.confirmationDiagnostic
|
|
}
|
|
diagnostic.apply(&final)
|
|
return &paymentsvc.Result{Terminal: final}, nil
|
|
}
|
|
|
|
func classifyCreditCallInitialResult(result creditcall.TransactionResultXML) creditCallInitialResult {
|
|
var transactionResult, sdkError string
|
|
for _, entry := range result.Entries {
|
|
switch entry.Key {
|
|
case types.TransactionResult:
|
|
transactionResult = entry.Value
|
|
case types.Errors:
|
|
sdkError = entry.Value
|
|
}
|
|
}
|
|
transactionResult = strings.ToLower(transactionResult)
|
|
switch transactionResult {
|
|
case types.ResultApproved:
|
|
return creditCallInitialResult{kind: creditCallInitialApproved}
|
|
case "cancelled", "canceled":
|
|
return creditCallInitialResult{kind: creditCallInitialCancelled}
|
|
case "", types.ResultError, types.ResultDeclined:
|
|
switch sdkError {
|
|
case "CardHolderActionTimedOut":
|
|
return creditCallInitialResult{kind: creditCallInitialTimeout, reason: "cardholder_timeout"}
|
|
case "PinPadUserCancelled":
|
|
return creditCallInitialResult{kind: creditCallInitialCancelled, reason: "user_cancelled"}
|
|
}
|
|
if transactionResult == types.ResultDeclined {
|
|
return creditCallInitialResult{kind: creditCallInitialDeclined}
|
|
}
|
|
}
|
|
return creditCallInitialResult{}
|
|
}
|
|
|
|
func (outcome creditCallSaleOutcome) streamResult(initial creditCallInitialResult) paymentapi.Result {
|
|
final := paymentapi.Result{Outcome: "unknown", HTTPStatus: outcome.HTTPStatus, Provider: "creditcall", Message: "Payment outcome uncertain; contact reception"}
|
|
if final.HTTPStatus == 0 {
|
|
final.HTTPStatus = http.StatusOK
|
|
}
|
|
if outcome.Approved {
|
|
fields := outcome.Payment.Fields
|
|
final.Outcome, final.Message = "approved", "Payment approved"
|
|
final.TransactionReference = fields[types.Reference]
|
|
final.CardType = fields[types.CardType]
|
|
final.MaskedCardNumber = creditcall.MaskedCardNumber(fields[types.PanMasked])
|
|
final.ExpiryDate = fields[types.ExpiryDate]
|
|
final.CardHash, final.CardReference = fields[types.CardHash], fields[types.CardReference]
|
|
return final
|
|
}
|
|
// Initial approval followed by unsuccessful confirmation must not permit another charge.
|
|
if initial.kind == creditCallInitialApproved {
|
|
return final
|
|
}
|
|
switch initial.kind {
|
|
case creditCallInitialDeclined:
|
|
final.Outcome, final.Retryable, final.Message = "declined", true, "Transaction declined"
|
|
case creditCallInitialCancelled:
|
|
final.Outcome, final.Retryable = "cancelled", true
|
|
final.Reason, final.Message = initial.reason, "Transaction cancelled"
|
|
case creditCallInitialTimeout:
|
|
final.Outcome, final.Retryable = "timeout", true
|
|
final.Reason, final.Message = initial.reason, "Cardholder action timed out"
|
|
}
|
|
return final
|
|
}
|