feat(hardlink): unify kiosk payments and finalize legacy cleanup
This commit is contained in:
@@ -3,19 +3,24 @@ package paybridge
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"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"
|
||||
"github.com/gorilla/websocket"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
PrintReceipt func(string)
|
||||
URL string
|
||||
APIKey string
|
||||
TimeoutSeconds int
|
||||
@@ -46,10 +51,57 @@ func (c *Client) Sale(ctx context.Context, req paymentsvc.SaleRequest, onStatus
|
||||
TimeoutSeconds: c.TimeoutSeconds,
|
||||
}
|
||||
|
||||
return c.doPayment(ctx, payBridgeReq, onStatus)
|
||||
result, err := c.doPayment(ctx, payBridgeReq, onStatus)
|
||||
if result != nil {
|
||||
result.Terminal = payBridgeTerminalResult(result)
|
||||
if c.PrintReceipt != nil {
|
||||
c.PrintReceipt(result.CustomerReceipt)
|
||||
}
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus paymentsvc.StatusHandler) (*paymentsvc.Result, error) {
|
||||
func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus paymentsvc.StatusHandler) (returned *paymentsvc.Result, resultErr error) {
|
||||
stage := "configure_connection"
|
||||
var httpStatus int
|
||||
defer func() {
|
||||
if resultErr != nil || (returned != nil && payBridgeTerminalResult(returned).Outcome == "unknown") {
|
||||
fields := log.Fields{
|
||||
"provider": "paybridge", "operation": "sale", "stage": stage,
|
||||
"request_id": req.RequestID, "transaction_reference": req.RequestID,
|
||||
"error_category": paymentsvc.SafeErrorCategory(resultErr),
|
||||
}
|
||||
if returned != nil {
|
||||
if returned.RequestID != "" {
|
||||
fields["transaction_reference"] = returned.RequestID
|
||||
}
|
||||
if returned.TransactionID != "" {
|
||||
fields["provider_transaction_id"] = returned.TransactionID
|
||||
}
|
||||
if returned.ReferenceNumber != "" {
|
||||
fields["provider_reference"] = returned.ReferenceNumber
|
||||
}
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
fields["error_category"] = paymentsvc.SafeErrorCategory(ctx.Err())
|
||||
} else if errors.Is(resultErr, ErrConnectionFailed) {
|
||||
fields["error_category"] = "connection_failure"
|
||||
} else if errors.Is(resultErr, ErrAuthFailed) {
|
||||
fields["error_category"] = "authentication_failed"
|
||||
} else if errors.Is(resultErr, ErrUnexpectedMessage) {
|
||||
fields["error_category"] = "invalid_response"
|
||||
}
|
||||
var closeError *websocket.CloseError
|
||||
if errors.As(resultErr, &closeError) {
|
||||
fields["error_category"] = "response_lost"
|
||||
fields["websocket_close_code"] = closeError.Code
|
||||
}
|
||||
if httpStatus != 0 {
|
||||
fields["http_status"] = httpStatus
|
||||
}
|
||||
log.WithFields(fields).Warn("PayBridge payment requires reconciliation")
|
||||
}
|
||||
}()
|
||||
emitStatus(onStatus, paymentstatus.Starting)
|
||||
|
||||
connectURL, err := c.connectURL()
|
||||
@@ -57,8 +109,15 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus pay
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ws, _, err := websocket.DefaultDialer.DialContext(ctx, connectURL, nil)
|
||||
stage = "connect"
|
||||
ws, response, err := websocket.DefaultDialer.DialContext(ctx, connectURL, nil)
|
||||
if err != nil {
|
||||
if response != nil {
|
||||
httpStatus = response.StatusCode
|
||||
if response.Body != nil {
|
||||
response.Body.Close()
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("%w: %v", ErrConnectionFailed, err)
|
||||
}
|
||||
defer ws.Close()
|
||||
@@ -74,6 +133,7 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus pay
|
||||
}
|
||||
}()
|
||||
|
||||
stage = "authenticate"
|
||||
jwt, err := c.readAuthSuccess(ws)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -82,6 +142,7 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus pay
|
||||
log.Info("PayBridge authentication successful")
|
||||
emitStatus(onStatus, paymentstatus.Started)
|
||||
|
||||
stage = "send_payment"
|
||||
if err := ws.WriteJSON(Envelope{
|
||||
Type: types.MesTypePaymentRequest,
|
||||
JWT: jwt,
|
||||
@@ -101,6 +162,7 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus pay
|
||||
emitStatus(onStatus, paymentstatus.RequestSent)
|
||||
|
||||
for {
|
||||
stage = "read_result"
|
||||
_, raw, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
@@ -122,26 +184,20 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus pay
|
||||
|
||||
switch messageType {
|
||||
case types.MesTypePaymentStatusUpdate:
|
||||
stage = "decode_status"
|
||||
var update StatusUpdateEnvelope
|
||||
if err := json.Unmarshal(raw, &update); err != nil {
|
||||
return nil, fmt.Errorf("decode payment_status_update: %w", err)
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"status": update.Data.Status,
|
||||
"code": update.Data.Code,
|
||||
}).Info("PayBridge status update")
|
||||
|
||||
statusCode, known := mapPayBridgeStatus(
|
||||
update.Data.Status,
|
||||
update.Data.Code,
|
||||
)
|
||||
log.WithField("status", statusCode).Info("PayBridge status update")
|
||||
|
||||
if !known {
|
||||
log.WithFields(log.Fields{
|
||||
"status": update.Data.Status,
|
||||
"code": update.Data.Code,
|
||||
}).Warn("Unknown PayBridge status update")
|
||||
log.Warn("Unknown PayBridge status update")
|
||||
}
|
||||
|
||||
emitStatus(onStatus, statusCode)
|
||||
@@ -151,6 +207,7 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus pay
|
||||
emitStatus(onStatus, paymentstatus.Accepted)
|
||||
|
||||
case types.MesTypePaymentResult:
|
||||
stage = "decode_result"
|
||||
var result PaymentResultEnvelope
|
||||
// log.Println("PayBridge payment result raw:", string(raw))
|
||||
if err := json.Unmarshal(raw, &result); err != nil {
|
||||
@@ -162,7 +219,7 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus pay
|
||||
log.WithFields(log.Fields{
|
||||
"requestId": mapped.RequestID,
|
||||
"transactionId": mapped.TransactionID,
|
||||
"status": mapped.Status,
|
||||
"status": mapPayBridgeFinalStatus(mapped.Status, mapped.Success),
|
||||
"success": mapped.Success,
|
||||
}).Info("PayBridge payment result received")
|
||||
|
||||
@@ -174,6 +231,7 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus pay
|
||||
return mapped, nil
|
||||
|
||||
case types.MesTypePaymentError:
|
||||
stage = "decode_payment_error"
|
||||
var paymentErr PaymentErrorEnvelope
|
||||
if err := json.Unmarshal(raw, &paymentErr); err != nil {
|
||||
return nil, fmt.Errorf("decode payment_error: %w", err)
|
||||
@@ -182,10 +240,10 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus pay
|
||||
mapped := mapPaymentError(req, paymentErr)
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"requestId": mapped.RequestID,
|
||||
"transactionId": mapped.TransactionID,
|
||||
"status": mapped.Status,
|
||||
"error": mapped.ErrorMessage,
|
||||
"requestId": mapped.RequestID,
|
||||
"transactionId": mapped.TransactionID,
|
||||
"status": mapPayBridgeFinalStatus(mapped.Status, false),
|
||||
"error_category": "provider_error",
|
||||
}).Warn("PayBridge payment error received")
|
||||
|
||||
emitStatus(
|
||||
@@ -196,6 +254,7 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus pay
|
||||
return mapped, nil
|
||||
|
||||
case types.ResultError:
|
||||
stage = "provider_error"
|
||||
var genericErr struct {
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
@@ -211,10 +270,8 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus pay
|
||||
)
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"code": genericErr.Error.Code,
|
||||
"message": genericErr.Error.Message,
|
||||
}).Error("PayBridge error message received")
|
||||
log.WithFields(log.Fields{"provider": "paybridge", "request_id": req.RequestID,
|
||||
"error_category": "provider_error"}).Error("PayBridge error message received")
|
||||
|
||||
emitStatus(onStatus, paymentstatus.Error)
|
||||
|
||||
@@ -237,10 +294,7 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus pay
|
||||
log.Info("Additional PayBridge auth_success message received")
|
||||
|
||||
default:
|
||||
log.WithField(
|
||||
"messageType",
|
||||
head.Type,
|
||||
).Warn("Unknown PayBridge intermediate message type")
|
||||
log.Warn("Unknown PayBridge intermediate message type")
|
||||
|
||||
emitStatus(onStatus, paymentstatus.Processing)
|
||||
}
|
||||
@@ -447,3 +501,23 @@ func mapPayBridgeFinalStatus(
|
||||
return paymentstatus.Error
|
||||
}
|
||||
}
|
||||
|
||||
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 payBridgeTerminalResult(r *paymentsvc.Result) paymentapi.Result {
|
||||
final := paymentapi.Result{Outcome: "unknown", HTTPStatus: http.StatusOK, Provider: "paybridge", 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, payBridgeFinalStatusApproved) {
|
||||
final.Outcome, final.Message = "approved", "Payment approved"
|
||||
return final
|
||||
}
|
||||
switch strings.ToUpper(r.Status) {
|
||||
case payBridgeFinalStatusDeclined:
|
||||
final.Outcome, final.Retryable, final.Message = "declined", true, "Transaction declined"
|
||||
case payBridgeFinalStatusCancelled:
|
||||
final.Outcome, final.Retryable, final.Message = "cancelled", true, "Transaction cancelled"
|
||||
}
|
||||
return final
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user