54 lines
2.0 KiB
Go
54 lines
2.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
|
"gitea.futuresens.co.uk/futuresens/hardlink/internal/creditcall"
|
|
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
|
"gitea.futuresens.co.uk/futuresens/logging"
|
|
)
|
|
|
|
type creditCallPreauthRequest struct {
|
|
Amount string `json:"amount"`
|
|
TransactionType string `json:"transactionType"`
|
|
CheckoutDate string `json:"checkoutDate"`
|
|
}
|
|
|
|
func (app *App) streamCreditCallPreauth(w http.ResponseWriter, r *http.Request) {
|
|
app.streamCreditCall(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")
|
|
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)
|
|
outcome.FailureType = types.ResultError
|
|
outcome.FailureDescription = "No response from payment processor"
|
|
return outcome
|
|
}
|
|
var result creditcall.PaymentResult
|
|
result.FillFromTransactionResult(transaction)
|
|
app.printCreditCallReceipt(result.CardholderReceipt)
|
|
approved, persist := creditcall.PreauthDecision(result.Fields)
|
|
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 {
|
|
go app.persistPreauth(ctx, result.Fields, request.CheckoutDate)
|
|
}
|
|
return outcome
|
|
}
|