Added payment terminal status streaming
This commit is contained in:
+291
-25
@@ -10,7 +10,9 @@ import (
|
||||
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/paymentsvc"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/paymentstatus"
|
||||
"github.com/gorilla/websocket"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
@@ -31,7 +33,7 @@ func NewClient(url, apiKey string, timeoutSeconds int) *Client {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Sale(ctx context.Context, req paymentsvc.SaleRequest) (*paymentsvc.Result, error) {
|
||||
func (c *Client) Sale(ctx context.Context, req paymentsvc.SaleRequest, onStatus paymentsvc.StatusHandler) (*paymentsvc.Result, error) {
|
||||
if req.Currency == "" {
|
||||
req.Currency = "GBP"
|
||||
}
|
||||
@@ -44,10 +46,12 @@ func (c *Client) Sale(ctx context.Context, req paymentsvc.SaleRequest) (*payment
|
||||
TimeoutSeconds: c.TimeoutSeconds,
|
||||
}
|
||||
|
||||
return c.doPayment(ctx, payBridgeReq)
|
||||
return c.doPayment(ctx, payBridgeReq, onStatus)
|
||||
}
|
||||
|
||||
func (c *Client) doPayment(ctx context.Context, req PaymentRequest) (*paymentsvc.Result, error) {
|
||||
func (c *Client) doPayment(ctx context.Context, req PaymentRequest, onStatus paymentsvc.StatusHandler) (*paymentsvc.Result, error) {
|
||||
emitStatus(onStatus, paymentstatus.Starting)
|
||||
|
||||
connectURL, err := c.connectURL()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -75,6 +79,9 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest) (*paymentsvc
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Info("PayBridge authentication successful")
|
||||
emitStatus(onStatus, paymentstatus.Started)
|
||||
|
||||
if err := ws.WriteJSON(Envelope{
|
||||
Type: types.MesTypePaymentRequest,
|
||||
JWT: jwt,
|
||||
@@ -84,36 +91,108 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest) (*paymentsvc
|
||||
return nil, fmt.Errorf("send PayBridge payment_request: %w", err)
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"requestId": req.RequestID,
|
||||
"operation": req.Operation,
|
||||
"amount": req.Amount,
|
||||
"currency": req.Currency,
|
||||
}).Info("PayBridge payment request sent")
|
||||
|
||||
emitStatus(onStatus, paymentstatus.RequestSent)
|
||||
|
||||
for {
|
||||
_, raw, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("read PayBridge message: %w", err)
|
||||
}
|
||||
|
||||
var head struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(raw, &head); err != nil {
|
||||
return nil, fmt.Errorf("decode PayBridge message header: %w", err)
|
||||
}
|
||||
|
||||
switch strings.ToLower(head.Type) {
|
||||
messageType := strings.ToLower(head.Type)
|
||||
|
||||
switch messageType {
|
||||
case types.MesTypePaymentStatusUpdate:
|
||||
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,
|
||||
)
|
||||
|
||||
if !known {
|
||||
log.WithFields(log.Fields{
|
||||
"status": update.Data.Status,
|
||||
"code": update.Data.Code,
|
||||
}).Warn("Unknown PayBridge status update")
|
||||
}
|
||||
|
||||
emitStatus(onStatus, statusCode)
|
||||
|
||||
case types.MesTypePaymentAccepted:
|
||||
log.Info("PayBridge payment request accepted")
|
||||
emitStatus(onStatus, paymentstatus.Accepted)
|
||||
|
||||
case types.MesTypePaymentResult:
|
||||
var result PaymentResultEnvelope
|
||||
if err := json.Unmarshal(raw, &result); err != nil {
|
||||
return nil, fmt.Errorf("decode payment_result: %w", err)
|
||||
}
|
||||
return mapPaymentResult(result), nil
|
||||
|
||||
mapped := mapPaymentResult(result)
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"requestId": mapped.RequestID,
|
||||
"transactionId": mapped.TransactionID,
|
||||
"status": mapped.Status,
|
||||
"success": mapped.Success,
|
||||
}).Info("PayBridge payment result received")
|
||||
|
||||
emitStatus(
|
||||
onStatus,
|
||||
mapPayBridgeFinalStatus(mapped.Status, mapped.Success),
|
||||
)
|
||||
|
||||
return mapped, nil
|
||||
|
||||
case types.MesTypePaymentError:
|
||||
var paymentErr PaymentErrorEnvelope
|
||||
if err := json.Unmarshal(raw, &paymentErr); err != nil {
|
||||
return nil, fmt.Errorf("decode payment_error: %w", err)
|
||||
}
|
||||
return mapPaymentError(req, paymentErr), nil
|
||||
|
||||
mapped := mapPaymentError(req, paymentErr)
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"requestId": mapped.RequestID,
|
||||
"transactionId": mapped.TransactionID,
|
||||
"status": mapped.Status,
|
||||
"error": mapped.ErrorMessage,
|
||||
}).Warn("PayBridge payment error received")
|
||||
|
||||
emitStatus(
|
||||
onStatus,
|
||||
mapPayBridgeFinalStatus(mapped.Status, false),
|
||||
)
|
||||
|
||||
return mapped, nil
|
||||
|
||||
case types.ResultError:
|
||||
var genericErr struct {
|
||||
@@ -122,20 +201,47 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest) (*paymentsvc
|
||||
Message string `json:"message"`
|
||||
} `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &genericErr); err != nil {
|
||||
return nil, fmt.Errorf("%w: %s", ErrUnexpectedMessage, string(raw))
|
||||
}
|
||||
if genericErr.Error.Message != "" {
|
||||
return nil, fmt.Errorf("%w: %s: %s", ErrUnexpectedMessage, genericErr.Error.Code, genericErr.Error.Message)
|
||||
}
|
||||
return nil, fmt.Errorf("%w: %s", ErrUnexpectedMessage, string(raw))
|
||||
|
||||
case types.MesTypePaymentStatusUpdate, types.MesTypePaymentAccepted, types.MesTypeAuthSuccess:
|
||||
// Intermediate message. Keep waiting for payment_result or payment_error.
|
||||
if err := json.Unmarshal(raw, &genericErr); err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"%w: %s",
|
||||
ErrUnexpectedMessage,
|
||||
string(raw),
|
||||
)
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"code": genericErr.Error.Code,
|
||||
"message": genericErr.Error.Message,
|
||||
}).Error("PayBridge error message received")
|
||||
|
||||
emitStatus(onStatus, paymentstatus.Error)
|
||||
|
||||
if genericErr.Error.Message != "" {
|
||||
return nil, fmt.Errorf(
|
||||
"%w: %s: %s",
|
||||
ErrUnexpectedMessage,
|
||||
genericErr.Error.Code,
|
||||
genericErr.Error.Message,
|
||||
)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf(
|
||||
"%w: %s",
|
||||
ErrUnexpectedMessage,
|
||||
string(raw),
|
||||
)
|
||||
|
||||
case types.MesTypeAuthSuccess:
|
||||
log.Info("Additional PayBridge auth_success message received")
|
||||
|
||||
default:
|
||||
// PayBridge may introduce additional intermediate message types.
|
||||
// Ignore them and continue waiting for the final result.
|
||||
log.WithField(
|
||||
"messageType",
|
||||
head.Type,
|
||||
).Warn("Unknown PayBridge intermediate message type")
|
||||
|
||||
emitStatus(onStatus, paymentstatus.Processing)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,7 +249,10 @@ func (c *Client) doPayment(ctx context.Context, req PaymentRequest) (*paymentsvc
|
||||
func (c *Client) connectURL() (string, error) {
|
||||
u, err := url.Parse(c.URL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("parse PayBridge WebSocket URL: %w", err)
|
||||
return "", fmt.Errorf(
|
||||
"parse PayBridge WebSocket URL: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
q := u.Query()
|
||||
@@ -154,29 +263,186 @@ func (c *Client) connectURL() (string, error) {
|
||||
}
|
||||
|
||||
func (c *Client) readAuthSuccess(ws *websocket.Conn) (string, error) {
|
||||
if err := ws.SetReadDeadline(time.Now().Add(10 * time.Second)); err != nil {
|
||||
return "", fmt.Errorf("%w: set auth read deadline: %v", ErrAuthFailed, err)
|
||||
if err := ws.SetReadDeadline(
|
||||
time.Now().Add(10 * time.Second),
|
||||
); err != nil {
|
||||
return "", fmt.Errorf(
|
||||
"%w: set auth read deadline: %v",
|
||||
ErrAuthFailed,
|
||||
err,
|
||||
)
|
||||
}
|
||||
defer ws.SetReadDeadline(time.Time{})
|
||||
|
||||
_, raw, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%w: read auth_success: %v", ErrAuthFailed, err)
|
||||
return "", fmt.Errorf(
|
||||
"%w: read auth_success: %v",
|
||||
ErrAuthFailed,
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
var auth struct {
|
||||
Type string `json:"type"`
|
||||
JWT string `json:"jwt"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(raw, &auth); err != nil {
|
||||
return "", fmt.Errorf("%w: decode auth_success: %v", ErrAuthFailed, err)
|
||||
return "", fmt.Errorf(
|
||||
"%w: decode auth_success: %v",
|
||||
ErrAuthFailed,
|
||||
err,
|
||||
)
|
||||
}
|
||||
if !strings.EqualFold(auth.Type, types.MesTypeAuthSuccess) {
|
||||
return "", fmt.Errorf("%w: expected auth_success, got %s: %s", ErrAuthFailed, auth.Type, string(raw))
|
||||
|
||||
if !strings.EqualFold(
|
||||
auth.Type,
|
||||
types.MesTypeAuthSuccess,
|
||||
) {
|
||||
return "", fmt.Errorf(
|
||||
"%w: expected auth_success, got %s: %s",
|
||||
ErrAuthFailed,
|
||||
auth.Type,
|
||||
string(raw),
|
||||
)
|
||||
}
|
||||
|
||||
if auth.JWT == "" {
|
||||
return "", fmt.Errorf("%w: auth_success did not contain jwt", ErrAuthFailed)
|
||||
return "", fmt.Errorf(
|
||||
"%w: auth_success did not contain jwt",
|
||||
ErrAuthFailed,
|
||||
)
|
||||
}
|
||||
|
||||
return auth.JWT, nil
|
||||
}
|
||||
|
||||
func emitStatus(
|
||||
onStatus paymentsvc.StatusHandler,
|
||||
code string,
|
||||
) {
|
||||
if onStatus == nil || code == "" {
|
||||
return
|
||||
}
|
||||
|
||||
onStatus(paymentsvc.StatusUpdate{
|
||||
Code: code,
|
||||
})
|
||||
}
|
||||
|
||||
func mapPayBridgeStatus(
|
||||
status string,
|
||||
code string,
|
||||
) (string, bool) {
|
||||
normalizedStatus := strings.ToLower(
|
||||
strings.TrimSpace(status),
|
||||
)
|
||||
|
||||
switch normalizedStatus {
|
||||
case payBridgeMessageInsertOrSwipeCard,
|
||||
payBridgeMessageInsertSwipeOrPresentCard:
|
||||
return paymentstatus.PresentCard, true
|
||||
|
||||
case payBridgeMessageInsertCard:
|
||||
return paymentstatus.InsertCard, true
|
||||
|
||||
case payBridgeMessagePleaseWait:
|
||||
return paymentstatus.PleaseWait, true
|
||||
|
||||
case payBridgeMessageDoNotRemoveCard:
|
||||
return paymentstatus.DoNotRemoveCard, true
|
||||
|
||||
case payBridgeMessageTryAnotherInterface:
|
||||
return paymentstatus.TryAnotherInterface, true
|
||||
|
||||
case payBridgeMessagePIN:
|
||||
return paymentstatus.EnterPIN, true
|
||||
|
||||
case payBridgeMessagePINAgain:
|
||||
return paymentstatus.EnterPINAgain, true
|
||||
|
||||
case payBridgeMessageTransactionCancelled:
|
||||
return paymentstatus.Cancelled, true
|
||||
|
||||
case payBridgeMessageProcessing:
|
||||
return paymentstatus.Processing, true
|
||||
|
||||
case payBridgeMessageApproved:
|
||||
// Это ещё промежуточный статус.
|
||||
// Финальный успех определяется только по payment_result.
|
||||
return paymentstatus.Authorized, true
|
||||
|
||||
case payBridgeMessageDeclined:
|
||||
return paymentstatus.Declined, true
|
||||
}
|
||||
|
||||
// Fallback по фактически обнаруженным кодам терминала.
|
||||
switch strings.TrimSpace(code) {
|
||||
case payBridgeCodeInsertCard:
|
||||
return paymentstatus.InsertCard, true
|
||||
|
||||
case payBridgeCodeEnterPIN:
|
||||
return paymentstatus.EnterPIN, true
|
||||
|
||||
case payBridgeCodeCancelled:
|
||||
return paymentstatus.Cancelled, true
|
||||
|
||||
case payBridgeCodePleaseWait:
|
||||
return paymentstatus.PleaseWait, true
|
||||
|
||||
case payBridgeCodePresentCard, payBridgeCodePresentCardAlternate:
|
||||
return paymentstatus.PresentCard, true
|
||||
|
||||
case payBridgeCodeEnterPINAgain:
|
||||
return paymentstatus.EnterPINAgain, true
|
||||
|
||||
case payBridgeCodeTryAnotherInterface:
|
||||
return paymentstatus.TryAnotherInterface, true
|
||||
|
||||
case payBridgeCodeDoNotRemoveCard:
|
||||
return paymentstatus.DoNotRemoveCard, true
|
||||
}
|
||||
|
||||
// Пока назначение кодов 200, 201, 205 и 210 неизвестно.
|
||||
// Они останутся в логах, но на экране будет общий статус.
|
||||
return paymentstatus.Processing, false
|
||||
}
|
||||
|
||||
func mapPayBridgeFinalStatus(
|
||||
status string,
|
||||
success bool,
|
||||
) string {
|
||||
if success && strings.EqualFold(status, payBridgeFinalStatusApproved) {
|
||||
return paymentstatus.Approved
|
||||
}
|
||||
|
||||
switch strings.ToUpper(status) {
|
||||
case payBridgeFinalStatusDeclined:
|
||||
return paymentstatus.Declined
|
||||
|
||||
case payBridgeFinalStatusCancelled:
|
||||
return paymentstatus.Cancelled
|
||||
|
||||
case payBridgeFinalStatusTimeout:
|
||||
return paymentstatus.Timeout
|
||||
|
||||
case payBridgeFinalStatusVoided:
|
||||
return paymentstatus.Voided
|
||||
|
||||
case payBridgeFinalStatusVoidedDailyLimitExceeded:
|
||||
return paymentstatus.DailyLimitExceeded
|
||||
|
||||
case payBridgeFinalStatusDailyLimitVoidFailed:
|
||||
return paymentstatus.VoidFailed
|
||||
|
||||
case payBridgeFinalStatusDailyLimitValidationError:
|
||||
return paymentstatus.LimitValidationError
|
||||
|
||||
case payBridgeFinalStatusError, payBridgeFinalStatusFailed:
|
||||
return paymentstatus.Error
|
||||
|
||||
default:
|
||||
return paymentstatus.Error
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user