feat(hardlink): unify kiosk payments and finalize legacy cleanup
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package paymentsvc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
)
|
||||
|
||||
// SafeErrorCategory describes a failure without exposing URLs, payloads or credentials.
|
||||
// It is diagnostic only and must not determine financial outcomes or retry eligibility.
|
||||
func SafeErrorCategory(err error) string {
|
||||
if err == nil {
|
||||
return "unresolved_result"
|
||||
}
|
||||
switch {
|
||||
case errors.Is(err, context.DeadlineExceeded):
|
||||
return "timeout"
|
||||
case errors.Is(err, context.Canceled):
|
||||
return "cancelled"
|
||||
case errors.Is(err, io.EOF), errors.Is(err, io.ErrUnexpectedEOF):
|
||||
return "response_lost"
|
||||
}
|
||||
var networkError net.Error
|
||||
if errors.As(err, &networkError) {
|
||||
if networkError.Timeout() {
|
||||
return "timeout"
|
||||
}
|
||||
return "connection_failure"
|
||||
}
|
||||
var syntaxError *json.SyntaxError
|
||||
var typeError *json.UnmarshalTypeError
|
||||
if errors.As(err, &syntaxError) || errors.As(err, &typeError) {
|
||||
return "invalid_response"
|
||||
}
|
||||
return "provider_error"
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package paymentsvc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSafeErrorCategory(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
want string
|
||||
}{
|
||||
{"missing result", nil, "unresolved_result"},
|
||||
{"timeout", fmt.Errorf("private URL: %w", context.DeadlineExceeded), "timeout"},
|
||||
{"cancel", fmt.Errorf("private token: %w", context.Canceled), "cancelled"},
|
||||
{"eof", fmt.Errorf("raw response: %w", io.EOF), "response_lost"},
|
||||
{"truncated", io.ErrUnexpectedEOF, "response_lost"},
|
||||
{"connection", &url.Error{Op: "Post", URL: "https://user:secret@synthetic.invalid", Err: &net.OpError{Op: "dial", Net: "tcp", Err: errors.New("private credential")}}, "connection_failure"},
|
||||
{"json syntax", &json.SyntaxError{Offset: 1}, "invalid_response"},
|
||||
{"json type", &json.UnmarshalTypeError{Value: "private card", Field: "secret"}, "invalid_response"},
|
||||
{"other", errors.New("raw provider response with sensitive content"), "provider_error"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := SafeErrorCategory(tc.err); got != tc.want {
|
||||
t.Errorf("SafeErrorCategory(%s)=%q, want %q", tc.name, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
var (
|
||||
ErrPaymentInProgress = errors.New("payment is already in progress")
|
||||
ErrProviderNotConfigured = errors.New("payment provider is not configured")
|
||||
ErrPreauthUnsupported = errors.New("configured payment provider does not support preauthorisation")
|
||||
)
|
||||
|
||||
type StatusUpdate struct {
|
||||
@@ -23,15 +24,21 @@ type Provider interface {
|
||||
req SaleRequest,
|
||||
onStatus StatusHandler,
|
||||
) (*Result, error)
|
||||
Preauth(context.Context, SaleRequest, StatusHandler) (*Result, error)
|
||||
ValidatePreauth(SaleRequest) error
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
provider Provider
|
||||
currency string
|
||||
|
||||
mu sync.Mutex
|
||||
busy bool
|
||||
}
|
||||
|
||||
// SetCurrency supplies the currency from the existing CMS hotel response at startup.
|
||||
func (s *Service) SetCurrency(currency string) { s.currency = currency }
|
||||
|
||||
func NewService(provider Provider) *Service {
|
||||
return &Service{
|
||||
provider: provider,
|
||||
@@ -43,9 +50,32 @@ func (s *Service) Sale(
|
||||
req SaleRequest,
|
||||
onStatus StatusHandler,
|
||||
) (*Result, error) {
|
||||
return s.run(ctx, req, onStatus, false)
|
||||
}
|
||||
|
||||
// Preauth uses the same terminal ownership as Sale.
|
||||
func (s *Service) Preauth(ctx context.Context, req SaleRequest, onStatus StatusHandler) (*Result, error) {
|
||||
return s.run(ctx, req, onStatus, true)
|
||||
}
|
||||
|
||||
func (s *Service) run(ctx context.Context, req SaleRequest, onStatus StatusHandler, preauth bool) (*Result, error) {
|
||||
if s == nil || s.provider == nil {
|
||||
return nil, ErrProviderNotConfigured
|
||||
}
|
||||
if s.currency != "" {
|
||||
req.Currency = s.currency
|
||||
}
|
||||
if preauth {
|
||||
if err := s.provider.ValidatePreauth(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if req.RequiredPreauth != nil {
|
||||
hold := req
|
||||
hold.Amount, hold.CheckoutDate = req.RequiredPreauth.Amount, req.RequiredPreauth.CheckoutDate
|
||||
if err := s.provider.ValidatePreauth(hold); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
if s.busy {
|
||||
@@ -61,5 +91,8 @@ func (s *Service) Sale(
|
||||
s.mu.Unlock()
|
||||
}()
|
||||
|
||||
if preauth {
|
||||
return s.provider.Preauth(ctx, req, onStatus)
|
||||
}
|
||||
return s.provider.Sale(ctx, req, onStatus)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package paymentsvc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/paymentapi"
|
||||
)
|
||||
|
||||
type testProcessor struct {
|
||||
entered chan SaleRequest
|
||||
release chan struct{}
|
||||
saleCalls atomic.Int32
|
||||
preauthCalls atomic.Int32
|
||||
validation func(SaleRequest) error
|
||||
}
|
||||
|
||||
func (p *testProcessor) ValidatePreauth(req SaleRequest) error {
|
||||
if p.validation != nil {
|
||||
return p.validation(req)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (p *testProcessor) Sale(_ context.Context, req SaleRequest, _ StatusHandler) (*Result, error) {
|
||||
p.saleCalls.Add(1)
|
||||
if p.entered != nil {
|
||||
p.entered <- req
|
||||
}
|
||||
if p.release != nil {
|
||||
<-p.release
|
||||
}
|
||||
return &Result{Terminal: paymentapi.Result{Outcome: "approved", HTTPStatus: 200}}, nil
|
||||
}
|
||||
func (p *testProcessor) Preauth(context.Context, SaleRequest, StatusHandler) (*Result, error) {
|
||||
p.preauthCalls.Add(1)
|
||||
return &Result{Terminal: paymentapi.Result{Outcome: "approved", HTTPStatus: 200}}, nil
|
||||
}
|
||||
|
||||
func TestSaleAndPreauthShareAdmission(t *testing.T) {
|
||||
p := &testProcessor{entered: make(chan SaleRequest, 1), release: make(chan struct{})}
|
||||
s := NewService(p)
|
||||
done := make(chan error, 1)
|
||||
go func() { _, err := s.Sale(context.Background(), SaleRequest{Amount: 100}, nil); done <- err }()
|
||||
<-p.entered
|
||||
if _, err := s.Preauth(context.Background(), SaleRequest{Amount: 50}, nil); !errors.Is(err, ErrPaymentInProgress) {
|
||||
t.Errorf("Preauth(during Sale) error=%v, want ErrPaymentInProgress", err)
|
||||
}
|
||||
close(p.release)
|
||||
if err := <-done; err != nil {
|
||||
t.Fatalf("Sale() error=%v, want nil", err)
|
||||
}
|
||||
if _, err := s.Preauth(context.Background(), SaleRequest{Amount: 50}, nil); err != nil {
|
||||
t.Errorf("Preauth(after Sale) error=%v, want nil", err)
|
||||
}
|
||||
if p.saleCalls.Load() != 1 || p.preauthCalls.Load() != 1 {
|
||||
t.Errorf("Provider calls=%d/%d, want Sale=1/PREAUTH=1", p.saleCalls.Load(), p.preauthCalls.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequiredPreauthValidatedBeforeSale(t *testing.T) {
|
||||
p := &testProcessor{validation: func(req SaleRequest) error {
|
||||
if req.Amount != 2500 || req.CheckoutDate != "SYNTH-DATE" {
|
||||
t.Errorf("ValidatePreauth() request=%+v, want planned 2500 hold/date", req)
|
||||
}
|
||||
return ErrPreauthUnsupported
|
||||
}}
|
||||
_, err := NewService(p).Sale(context.Background(), SaleRequest{Amount: 10000, RequiredPreauth: &paymentapi.PreauthRequirement{Amount: 2500, CheckoutDate: "SYNTH-DATE"}}, nil)
|
||||
if !errors.Is(err, ErrPreauthUnsupported) || p.saleCalls.Load() != 0 || p.preauthCalls.Load() != 0 {
|
||||
t.Errorf("Sale(required unsupported hold) error/calls=%v/%d/%d, want unsupported/no dispatch", err, p.saleCalls.Load(), p.preauthCalls.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCMSCurrencyReachesProviderWithoutLocalDefault(t *testing.T) {
|
||||
p := &testProcessor{entered: make(chan SaleRequest, 1)}
|
||||
s := NewService(p)
|
||||
s.SetCurrency("EUR")
|
||||
if _, err := s.Sale(context.Background(), SaleRequest{Amount: 100, Currency: "GBP"}, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := (<-p.entered).Currency; got != "EUR" {
|
||||
t.Errorf("Sale() provider currency=%q, want CMS EUR", got)
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,25 @@
|
||||
package paymentsvc
|
||||
|
||||
import "gitea.futuresens.co.uk/futuresens/hardlink/paymentapi"
|
||||
|
||||
type SaleRequest struct {
|
||||
RequestID string `json:"requestId"`
|
||||
Reference string `json:"reference"`
|
||||
Amount int64 `json:"amount"`
|
||||
Currency string `json:"currency"`
|
||||
RequestID string `json:"requestId"`
|
||||
Reference string `json:"reference"`
|
||||
Amount int64 `json:"amount"`
|
||||
Currency string `json:"currency"`
|
||||
CheckoutDate string `json:"checkoutDate,omitempty"`
|
||||
RequiredPreauth *paymentapi.PreauthRequirement `json:"requiredPreauth,omitempty"`
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
Success bool `json:"success"`
|
||||
RequestID string `json:"requestId,omitempty"`
|
||||
Operation string `json:"operation,omitempty"`
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message,omitempty"`
|
||||
ErrorMessage string `json:"errorMessage,omitempty"`
|
||||
// Terminal is the provider-independent result exposed by the modern endpoints.
|
||||
Terminal paymentapi.Result `json:"-"`
|
||||
Success bool `json:"success"`
|
||||
RequestID string `json:"requestId,omitempty"`
|
||||
Operation string `json:"operation,omitempty"`
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message,omitempty"`
|
||||
ErrorMessage string `json:"errorMessage,omitempty"`
|
||||
|
||||
TransactionID string `json:"transactionId,omitempty"`
|
||||
ReferenceNumber string `json:"referenceNumber,omitempty"`
|
||||
|
||||
Reference in New Issue
Block a user