Added payment terminal status streaming
This commit is contained in:
+260
-15
@@ -8,11 +8,13 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/paymentsvc"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/paymentstatus"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -33,6 +35,29 @@ type Client struct {
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
type terminalSessionWithUpdates struct {
|
||||
terminalSessionResponse
|
||||
|
||||
NotificationEvents []terminalNotificationEvent `json:"notificationEvents"`
|
||||
StatusEvents []terminalStatusEvent `json:"statusEvents"`
|
||||
}
|
||||
|
||||
type terminalNotificationEvent struct {
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
NotificationType string `json:"notificationType"`
|
||||
}
|
||||
|
||||
type terminalStatusEvent struct {
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
type terminalDisplayEvent struct {
|
||||
createdAt time.Time
|
||||
code string
|
||||
sessionStatus string
|
||||
}
|
||||
|
||||
func NewClient(cfg Config) (*Client, error) {
|
||||
if cfg.BaseURL == "" {
|
||||
return nil, fmt.Errorf("dojo base_url is required")
|
||||
@@ -60,11 +85,17 @@ func NewClient(cfg Config) (*Client, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
sendPaymentStatus(onStatus, paymentstatus.Starting)
|
||||
|
||||
intent, err := c.createPaymentIntent(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -75,7 +106,7 @@ func (c *Client) Sale(ctx context.Context, req paymentsvc.SaleRequest) (*payment
|
||||
return nil, err
|
||||
}
|
||||
|
||||
session, err = c.waitForTerminalSession(ctx, session.ID)
|
||||
session, err = c.waitForTerminalSession(ctx, session.ID, onStatus)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -150,10 +181,17 @@ func (c *Client) createTerminalSession(ctx context.Context, paymentIntentID stri
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func (c *Client) waitForTerminalSession(ctx context.Context, terminalSessionID string) (*terminalSessionResponse, error) {
|
||||
func (c *Client) waitForTerminalSession(
|
||||
ctx context.Context,
|
||||
terminalSessionID string,
|
||||
onStatus paymentsvc.StatusHandler,
|
||||
) (*terminalSessionResponse, error) {
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
notificationCount := 0
|
||||
statusCount := 0
|
||||
lastSessionStatus := ""
|
||||
signatureRejected := false
|
||||
|
||||
for {
|
||||
@@ -164,12 +202,40 @@ func (c *Client) waitForTerminalSession(ctx context.Context, terminalSessionID s
|
||||
|
||||
log.Println("session status:", session.Status)
|
||||
|
||||
switch strings.ToLower(session.Status) {
|
||||
case types.ResultInitiateRequested, types.ResultInitiated, types.ResultAuthorized, types.ResultCancelRequested:
|
||||
emitNewTerminalSessionEvents(
|
||||
session,
|
||||
¬ificationCount,
|
||||
&statusCount,
|
||||
&lastSessionStatus,
|
||||
onStatus,
|
||||
)
|
||||
|
||||
currentStatus := strings.ToLower(session.Status)
|
||||
if currentStatus != lastSessionStatus {
|
||||
lastSessionStatus = currentStatus
|
||||
sendPaymentStatus(
|
||||
onStatus,
|
||||
mapDojoSessionStatus(session.Status),
|
||||
)
|
||||
}
|
||||
|
||||
switch currentStatus {
|
||||
case types.ResultInitiateRequested,
|
||||
types.ResultInitiated,
|
||||
types.ResultAuthorized,
|
||||
types.ResultCancelRequested:
|
||||
|
||||
case types.ResultSignatureRequired:
|
||||
if !signatureRejected {
|
||||
rejectedSession, err := c.rejectSignature(ctx, terminalSessionID)
|
||||
sendPaymentStatus(
|
||||
onStatus,
|
||||
paymentstatus.SignatureRejecting,
|
||||
)
|
||||
|
||||
rejectedSession, err := c.rejectSignature(
|
||||
ctx,
|
||||
terminalSessionID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -177,30 +243,206 @@ func (c *Client) waitForTerminalSession(ctx context.Context, terminalSessionID s
|
||||
signatureRejected = true
|
||||
|
||||
if rejectedSession != nil {
|
||||
switch strings.ToLower(rejectedSession.Status) {
|
||||
case types.ResultCaptured, types.ResultCancelled, types.ResultCanceled, types.ResultDeclined,
|
||||
types.ResultExpired, types.ResultSignatureAccepted, types.ResultSignatureRejected:
|
||||
rejectedStatus := strings.ToLower(
|
||||
rejectedSession.Status,
|
||||
)
|
||||
|
||||
if rejectedStatus != lastSessionStatus {
|
||||
lastSessionStatus = rejectedStatus
|
||||
sendPaymentStatus(
|
||||
onStatus,
|
||||
mapDojoSessionStatus(
|
||||
rejectedSession.Status,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
switch rejectedStatus {
|
||||
case types.ResultCaptured,
|
||||
types.ResultCancelled,
|
||||
types.ResultCanceled,
|
||||
types.ResultDeclined,
|
||||
types.ResultExpired,
|
||||
types.ResultSignatureAccepted,
|
||||
types.ResultSignatureRejected:
|
||||
return rejectedSession, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case types.ResultCaptured, types.ResultCancelled, types.ResultCanceled, types.ResultDeclined,
|
||||
types.ResultExpired, types.ResultSignatureAccepted, types.ResultSignatureRejected:
|
||||
return session, nil
|
||||
case types.ResultCaptured,
|
||||
types.ResultCancelled,
|
||||
types.ResultCanceled,
|
||||
types.ResultDeclined,
|
||||
types.ResultExpired,
|
||||
types.ResultSignatureAccepted,
|
||||
types.ResultSignatureRejected:
|
||||
return &session.terminalSessionResponse, nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected Dojo terminal session status %q", session.Status)
|
||||
return nil, fmt.Errorf(
|
||||
"unexpected Dojo terminal session status %q",
|
||||
session.Status,
|
||||
)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
|
||||
case <-ticker.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func emitNewTerminalSessionEvents(
|
||||
session *terminalSessionWithUpdates,
|
||||
notificationCount *int,
|
||||
statusCount *int,
|
||||
lastSessionStatus *string,
|
||||
onStatus paymentsvc.StatusHandler,
|
||||
) {
|
||||
if *notificationCount > len(session.NotificationEvents) {
|
||||
*notificationCount = len(session.NotificationEvents)
|
||||
}
|
||||
if *statusCount > len(session.StatusEvents) {
|
||||
*statusCount = len(session.StatusEvents)
|
||||
}
|
||||
|
||||
newEventCount := len(session.NotificationEvents) - *notificationCount +
|
||||
len(session.StatusEvents) - *statusCount
|
||||
events := make([]terminalDisplayEvent, 0, newEventCount)
|
||||
|
||||
// Add status events first so a notification wins when timestamps are equal.
|
||||
for *statusCount < len(session.StatusEvents) {
|
||||
event := session.StatusEvents[*statusCount]
|
||||
*statusCount = *statusCount + 1
|
||||
|
||||
events = append(events, terminalDisplayEvent{
|
||||
createdAt: event.CreatedAt,
|
||||
code: mapDojoSessionStatus(event.Status),
|
||||
sessionStatus: event.Status,
|
||||
})
|
||||
}
|
||||
|
||||
for *notificationCount < len(session.NotificationEvents) {
|
||||
event := session.NotificationEvents[*notificationCount]
|
||||
*notificationCount = *notificationCount + 1
|
||||
|
||||
events = append(events, terminalDisplayEvent{
|
||||
createdAt: event.CreatedAt,
|
||||
code: mapDojoNotification(event.NotificationType),
|
||||
})
|
||||
}
|
||||
|
||||
sort.SliceStable(events, func(i, j int) bool {
|
||||
return events[i].createdAt.Before(events[j].createdAt)
|
||||
})
|
||||
|
||||
for _, event := range events {
|
||||
if event.sessionStatus != "" {
|
||||
*lastSessionStatus = strings.ToLower(event.sessionStatus)
|
||||
}
|
||||
|
||||
sendPaymentStatus(onStatus, event.code)
|
||||
}
|
||||
}
|
||||
|
||||
func sendPaymentStatus(
|
||||
handler paymentsvc.StatusHandler,
|
||||
code string,
|
||||
) {
|
||||
if handler == nil || code == "" {
|
||||
return
|
||||
}
|
||||
|
||||
handler(paymentsvc.StatusUpdate{
|
||||
Code: code,
|
||||
})
|
||||
}
|
||||
|
||||
func mapDojoNotification(notification string) string {
|
||||
switch notification {
|
||||
case "PresentCard":
|
||||
return paymentstatus.PresentCard
|
||||
|
||||
case "InsertCard":
|
||||
return paymentstatus.InsertCard
|
||||
|
||||
case "SwipeCard":
|
||||
return paymentstatus.SwipeCard
|
||||
|
||||
case "EnterPin":
|
||||
return paymentstatus.EnterPIN
|
||||
|
||||
case "RemoveCard":
|
||||
return paymentstatus.RemoveCard
|
||||
|
||||
case "PleaseWait":
|
||||
return paymentstatus.PleaseWait
|
||||
|
||||
default:
|
||||
return dojoFallbackStatusCode(
|
||||
paymentstatus.DojoNotificationPrefix,
|
||||
notification,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func mapDojoSessionStatus(status string) string {
|
||||
switch strings.ToLower(status) {
|
||||
case types.ResultInitiateRequested:
|
||||
return paymentstatus.Starting
|
||||
|
||||
case types.ResultInitiated:
|
||||
return paymentstatus.Started
|
||||
|
||||
case types.ResultAuthorized:
|
||||
return paymentstatus.Authorized
|
||||
|
||||
case types.ResultCancelRequested:
|
||||
return paymentstatus.Cancelling
|
||||
|
||||
case types.ResultSignatureRequired:
|
||||
return paymentstatus.SignatureRequired
|
||||
|
||||
case types.ResultCaptured, types.ResultSignatureAccepted:
|
||||
return paymentstatus.Approved
|
||||
|
||||
case types.ResultCancelled, types.ResultCanceled:
|
||||
return paymentstatus.Cancelled
|
||||
|
||||
case types.ResultDeclined:
|
||||
return paymentstatus.Declined
|
||||
|
||||
case types.ResultExpired:
|
||||
return paymentstatus.Expired
|
||||
|
||||
case types.ResultSignatureRejected:
|
||||
return paymentstatus.SignatureRejected
|
||||
|
||||
default:
|
||||
return dojoFallbackStatusCode(
|
||||
paymentstatus.DojoStatusPrefix,
|
||||
status,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func dojoFallbackStatusCode(prefix, value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
replacer := strings.NewReplacer(
|
||||
" ", "_",
|
||||
"-", "_",
|
||||
)
|
||||
|
||||
return prefix + strings.ToUpper(replacer.Replace(value))
|
||||
}
|
||||
|
||||
func (c *Client) rejectSignature(ctx context.Context, terminalSessionID string) (*terminalSessionResponse, error) {
|
||||
payload := signatureVerificationRequest{
|
||||
Accepted: false,
|
||||
@@ -216,8 +458,11 @@ func (c *Client) rejectSignature(ctx context.Context, terminalSessionID string)
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func (c *Client) getTerminalSession(ctx context.Context, terminalSessionID string) (*terminalSessionResponse, error) {
|
||||
var response terminalSessionResponse
|
||||
func (c *Client) getTerminalSession(
|
||||
ctx context.Context,
|
||||
terminalSessionID string,
|
||||
) (*terminalSessionWithUpdates, error) {
|
||||
var response terminalSessionWithUpdates
|
||||
path := "/terminal-sessions/" + url.PathEscape(terminalSessionID)
|
||||
if err := c.doJSON(ctx, http.MethodGet, path, nil, true, &response); err != nil {
|
||||
return nil, fmt.Errorf("get Dojo terminal session: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user