92 lines
3.6 KiB
Go
92 lines
3.6 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 }
|
|
|
|
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"
|
|
}
|
|
start := func(client *http.Client) (creditcall.TransactionResultXML, error) {
|
|
return callChipDNATransactionStream(context.WithoutCancel(ctx), client, transaction, func(code string) {
|
|
if status != nil {
|
|
status(paymentsvc.StatusUpdate{Code: code})
|
|
}
|
|
})
|
|
}
|
|
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()
|
|
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
|
|
}
|
|
}
|
|
}
|
|
return &paymentsvc.Result{Terminal: final}, nil
|
|
}
|
|
|
|
func (outcome creditCallSaleOutcome) streamResult() 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]
|
|
} else {
|
|
switch strings.ToLower(outcome.FailureType) {
|
|
case "declined":
|
|
final.Outcome, final.Retryable, final.Message = "declined", true, "Transaction declined"
|
|
case "cancelled", "canceled":
|
|
final.Outcome, final.Retryable, final.Message = "cancelled", true, "Transaction cancelled"
|
|
}
|
|
}
|
|
return final
|
|
}
|