feat(hardlink): unify kiosk payments and finalize legacy cleanup
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user