feat(hardlink): unify kiosk payments and finalize legacy cleanup
This commit is contained in:
+93
-3
@@ -13,13 +13,16 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/creditcall"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/paymentsvc"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/paymentapi"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/paymentstatus"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
PrintReceipt func(string)
|
||||
BaseURL string
|
||||
APIKey string
|
||||
SoftwareHouseID string
|
||||
@@ -28,6 +31,7 @@ type Config struct {
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
printReceipt func(string)
|
||||
baseURL string
|
||||
apiKey string
|
||||
version string
|
||||
@@ -91,6 +95,7 @@ func NewClient(cfg Config) (*Client, error) {
|
||||
}
|
||||
|
||||
return &Client{
|
||||
printReceipt: cfg.PrintReceipt,
|
||||
baseURL: strings.TrimRight(cfg.BaseURL, "/"),
|
||||
apiKey: cfg.APIKey,
|
||||
version: cfg.Version,
|
||||
@@ -104,7 +109,49 @@ func (c *Client) Sale(
|
||||
ctx context.Context,
|
||||
req paymentsvc.SaleRequest,
|
||||
onStatus paymentsvc.StatusHandler,
|
||||
) (*paymentsvc.Result, error) {
|
||||
) (returned *paymentsvc.Result, resultErr error) {
|
||||
stage := "create_payment_intent"
|
||||
var intentID, sessionID, transactionID string
|
||||
observeSession := func(session *terminalSessionResponse) {
|
||||
if session != nil {
|
||||
if session.ID != "" {
|
||||
sessionID = session.ID
|
||||
}
|
||||
if session.PaymentDetails != nil && session.PaymentDetails.TransactionID != "" {
|
||||
transactionID = session.PaymentDetails.TransactionID
|
||||
}
|
||||
}
|
||||
}
|
||||
defer func() {
|
||||
if returned != nil {
|
||||
returned.Terminal = dojoTerminalResult(returned)
|
||||
if c.printReceipt != nil {
|
||||
c.printReceipt(returned.CustomerReceipt)
|
||||
}
|
||||
}
|
||||
if resultErr != nil || (returned != nil && returned.Terminal.Outcome == "unknown") {
|
||||
fields := log.Fields{
|
||||
"provider": "dojo", "operation": "sale", "stage": stage,
|
||||
"request_id": req.RequestID, "transaction_reference": req.RequestID,
|
||||
"error_category": paymentsvc.SafeErrorCategory(resultErr),
|
||||
}
|
||||
if intentID != "" {
|
||||
fields["payment_intent_id"] = intentID
|
||||
}
|
||||
if sessionID != "" {
|
||||
fields["terminal_session_id"] = sessionID
|
||||
}
|
||||
if transactionID != "" {
|
||||
fields["provider_transaction_id"] = transactionID
|
||||
}
|
||||
var responseError *httpResponseError
|
||||
if errors.As(resultErr, &responseError) {
|
||||
fields["http_status"] = responseError.StatusCode
|
||||
fields["error_category"] = "http_error"
|
||||
}
|
||||
log.WithFields(fields).Warn("Dojo payment requires reconciliation")
|
||||
}
|
||||
}()
|
||||
if req.Currency == "" {
|
||||
req.Currency = "GBP"
|
||||
}
|
||||
@@ -115,7 +162,12 @@ func (c *Client) Sale(
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
intentID = intent.ID
|
||||
if intent.PaymentDetails != nil {
|
||||
transactionID = intent.PaymentDetails.TransactionID
|
||||
}
|
||||
|
||||
stage = "create_terminal_session"
|
||||
session, err := c.createTerminalSession(ctx, intent.ID)
|
||||
if err != nil {
|
||||
var responseErr *httpResponseError
|
||||
@@ -125,6 +177,8 @@ func (c *Client) Sale(
|
||||
"operation": "createTerminalSession",
|
||||
"status": responseErr.StatusCode,
|
||||
"terminal_unavailable": true,
|
||||
"request_id": req.RequestID, "transaction_reference": req.RequestID,
|
||||
"payment_intent_id": intentID,
|
||||
}).Warn(dojoTerminalUnavailableMessage)
|
||||
|
||||
sendPaymentStatus(onStatus, paymentstatus.TerminalUnavailable)
|
||||
@@ -135,18 +189,24 @@ func (c *Client) Sale(
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
observeSession(session)
|
||||
|
||||
session, err = c.waitForTerminalSession(ctx, session.ID, onStatus)
|
||||
stage = "read_terminal_session"
|
||||
session, err = c.waitForTerminalSession(ctx, session.ID, onStatus, observeSession)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch strings.ToLower(session.Status) {
|
||||
case types.ResultCaptured, types.ResultSignatureAccepted:
|
||||
stage = "read_payment_intent"
|
||||
intent, err = c.getPaymentIntent(ctx, intent.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if intent.PaymentDetails != nil && intent.PaymentDetails.TransactionID != "" {
|
||||
transactionID = intent.PaymentDetails.TransactionID
|
||||
}
|
||||
if strings.ToLower(intent.Status) != types.ResultCaptured {
|
||||
return nil, fmt.Errorf("Dojo terminal session is %s but payment intent is %s", session.Status, intent.Status)
|
||||
}
|
||||
@@ -215,6 +275,7 @@ func (c *Client) waitForTerminalSession(
|
||||
ctx context.Context,
|
||||
terminalSessionID string,
|
||||
onStatus paymentsvc.StatusHandler,
|
||||
observeSession func(*terminalSessionResponse),
|
||||
) (*terminalSessionResponse, error) {
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
@@ -230,7 +291,9 @@ func (c *Client) waitForTerminalSession(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Println("session status:", session.Status)
|
||||
observeSession(&session.terminalSessionResponse)
|
||||
log.WithFields(log.Fields{"provider": "dojo", "terminal_session_id": terminalSessionID,
|
||||
"status": mapDojoSessionStatus(session.Status)}).Info("Dojo terminal session observed")
|
||||
|
||||
emitNewTerminalSessionEvents(
|
||||
session,
|
||||
@@ -273,6 +336,7 @@ func (c *Client) waitForTerminalSession(
|
||||
signatureRejected = true
|
||||
|
||||
if rejectedSession != nil {
|
||||
observeSession(rejectedSession)
|
||||
rejectedStatus := strings.ToLower(
|
||||
rejectedSession.Status,
|
||||
)
|
||||
@@ -672,3 +736,29 @@ func normalizeReceiptText(value string) string {
|
||||
|
||||
return replacer.Replace(value)
|
||||
}
|
||||
|
||||
func (*Client) ValidatePreauth(paymentsvc.SaleRequest) error { return paymentsvc.ErrPreauthUnsupported }
|
||||
func (*Client) Preauth(context.Context, paymentsvc.SaleRequest, paymentsvc.StatusHandler) (*paymentsvc.Result, error) {
|
||||
return nil, paymentsvc.ErrPreauthUnsupported
|
||||
}
|
||||
|
||||
func dojoTerminalResult(r *paymentsvc.Result) paymentapi.Result {
|
||||
final := paymentapi.Result{Outcome: "unknown", HTTPStatus: http.StatusOK, Provider: "dojo", AmountMinor: r.Amount, CurrencyCode: r.Currency, TransactionReference: r.RequestID, CardType: r.CardType, MaskedCardNumber: creditcall.MaskedCardNumber(r.CardNumber), ExpiryDate: r.ExpiryDate, CardHash: r.CardHash, CardReference: r.CardReference, AuthorizationCode: r.AuthCode, LastFour: r.LastFourDigits, Message: "Payment outcome uncertain; contact reception"}
|
||||
if r.Success && strings.EqualFold(r.Status, "APPROVED") {
|
||||
final.Outcome, final.Message = "approved", "Payment approved"
|
||||
return final
|
||||
}
|
||||
switch strings.ToUpper(r.Status) {
|
||||
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"
|
||||
case "SIGNATUREVERIFICATIONREJECTED", "SIGNATURE_VERIFICATION_REJECTED":
|
||||
final.Outcome, final.Reason, final.Message = "declined", "signature_verification", "Signature verification declined"
|
||||
final.Retryable = true
|
||||
case dojoTerminalUnavailableStatus:
|
||||
final.Outcome, final.Reason, final.Message = "error", "terminal_unavailable", dojoTerminalUnavailableMessage
|
||||
final.Retryable = true
|
||||
}
|
||||
return final
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user