Compare commits
1 Commits
developmen
...
cardEncode
| Author | SHA1 | Date | |
|---|---|---|---|
| 975707de4c |
2
.gitignore
vendored
2
.gitignore
vendored
@ -42,6 +42,6 @@ _cgo_export.*
|
|||||||
|
|
||||||
_testmain.go
|
_testmain.go
|
||||||
|
|
||||||
*.exe*
|
*.exe
|
||||||
*.test
|
*.test
|
||||||
*.prof
|
*.prof
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/config"
|
"gitea.futuresens.co.uk/futuresens/hardlink/config"
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/db"
|
"gitea.futuresens.co.uk/futuresens/hardlink/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
func OpenDB(cfg *config.ConfigRec) (*sql.DB, error) {
|
func OpenDB(cfg *config.ConfigRec) (*sql.DB, error) {
|
||||||
@ -1,318 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/xml"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"os/signal"
|
|
||||||
"strings"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/tarm/serial"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/cms"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/config"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/bootstrap"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/creditcall"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/dispenser"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/dojo"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/errorhandlers"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/handlers"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/lockserver"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/logging"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/mail"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/paybridge"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/paymentsvc"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/printer"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
buildVersion = "v1.3.5"
|
|
||||||
serviceName = "hardlink"
|
|
||||||
pollingFrequency = 8 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// Load config
|
|
||||||
cfg := config.ReadHardlinkConfig()
|
|
||||||
printer.Layout = readTicketLayout()
|
|
||||||
printer.PrinterName = cfg.PrinterName
|
|
||||||
lockserver.Cert = cfg.Cert
|
|
||||||
lockserver.LockServerURL = cfg.LockserverURL
|
|
||||||
mail.SendErrorEmails = cfg.SendErrorEmails
|
|
||||||
|
|
||||||
// Root context for background goroutines
|
|
||||||
// rootCtx, rootCancel := context.WithCancel(context.Background())
|
|
||||||
// defer rootCancel()
|
|
||||||
|
|
||||||
var (
|
|
||||||
dispPort *serial.Port
|
|
||||||
disp *dispenser.Client
|
|
||||||
cardWellStatus string
|
|
||||||
)
|
|
||||||
|
|
||||||
// Setup logging and get file handle
|
|
||||||
logFile, err := logging.SetupLogging(cfg.LogDir, serviceName, buildVersion)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to set up logging: %v\n", err)
|
|
||||||
}
|
|
||||||
if logFile != nil {
|
|
||||||
defer logFile.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize dispenser
|
|
||||||
if !cfg.TestMode {
|
|
||||||
dispenser.SerialPort = cfg.DispenserPort
|
|
||||||
dispenser.Address = []byte(cfg.DispenserAdrr)
|
|
||||||
|
|
||||||
dispPort, err = dispenser.InitializeDispenser()
|
|
||||||
if err != nil {
|
|
||||||
errorhandlers.FatalErrorWithMail(cfg.Hotel, cfg.Kiosk, "Dispenser Initialization Error", fmt.Errorf("failed to initialize dispenser: %v", err))
|
|
||||||
}
|
|
||||||
defer dispPort.Close()
|
|
||||||
|
|
||||||
disp = dispenser.NewClient(dispPort, 32)
|
|
||||||
defer disp.Close()
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
cardWellStatus, err = disp.DispenserPrepare(ctx)
|
|
||||||
if err != nil {
|
|
||||||
err = fmt.Errorf("%s; wrong dispenser address: %s", err, cfg.DispenserAdrr)
|
|
||||||
errorhandlers.FatalErrorWithMail(cfg.Hotel, cfg.Kiosk, "Dispenser Preparation Error", err)
|
|
||||||
}
|
|
||||||
fmt.Println(cardWellStatus)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test lock-server connection
|
|
||||||
switch strings.ToLower(cfg.LockType) {
|
|
||||||
case lockserver.TLJ:
|
|
||||||
// TLJ uses HTTP - skip TCP probe here
|
|
||||||
default:
|
|
||||||
lockConn, err := lockserver.InitializeServerConnection(cfg.LockserverURL)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
log.Errorf(err.Error())
|
|
||||||
mail.SendEmailOnError(cfg.Hotel, cfg.Kiosk, "Lock Server Connection Error", err.Error())
|
|
||||||
} else {
|
|
||||||
fmt.Printf("Connected to the lock server successfuly at %s\n", cfg.LockserverURL)
|
|
||||||
log.Infof("Connected to the lock server successfuly at %s", cfg.LockserverURL)
|
|
||||||
lockConn.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
database, err := bootstrap.OpenDB(&cfg)
|
|
||||||
if err != nil {
|
|
||||||
log.Warnf("DB init failed: %v", err)
|
|
||||||
}
|
|
||||||
if database != nil {
|
|
||||||
defer database.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create App and wire routes
|
|
||||||
app := handlers.NewApp(disp, cfg.LockType, cfg.EncoderAddress, cardWellStatus, database, &cfg)
|
|
||||||
|
|
||||||
if cfg.IsPayment {
|
|
||||||
fmt.Println("Payment processing is enabled")
|
|
||||||
log.Info("Payment processing is enabled")
|
|
||||||
|
|
||||||
var provider paymentsvc.Provider
|
|
||||||
reservationSystem, err := cms.ReadHotel(cfg.Hotel, cfg.CMSBaseURL)
|
|
||||||
if err != nil {
|
|
||||||
errorhandlers.FatalErrorWithMail(cfg.Hotel, cfg.Kiosk, "Failed to read hotel from CMS", fmt.Errorf("failed to read hotel from CMS: %v", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
paymentProvider := reservationSystem.PaymentSystem
|
|
||||||
if paymentProvider == 0 {
|
|
||||||
paymentProvider = cms.PaymentSystemIndex(cfg.PaymentProvider)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch paymentProvider {
|
|
||||||
case cmstypes.PaySystemCreditCall:
|
|
||||||
// CreditCall keeps using the existing /takepayment and /takepreauth endpoints.
|
|
||||||
startChipDnaClient()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
time.Sleep(30 * time.Second)
|
|
||||||
|
|
||||||
pdqStatus, err := creditcall.ReadPdqStatus(cfg.Hotel, cfg.Kiosk)
|
|
||||||
if err != nil {
|
|
||||||
mail.SendEmailOnError(cfg.Hotel, cfg.Kiosk, "PDQ Status Read Error", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("\nPDQ available: %v\n", pdqStatus.IsAvailable)
|
|
||||||
log.Infof("PDQ available: %v", pdqStatus.IsAvailable)
|
|
||||||
}()
|
|
||||||
|
|
||||||
log.Info("CreditCall payment provider enabled")
|
|
||||||
fmt.Println("CreditCall payment provider enabled")
|
|
||||||
|
|
||||||
case cmstypes.PaySystemPayBridge:
|
|
||||||
if reservationSystem.PaymentGatewayURL == "" {
|
|
||||||
errorhandlers.FatalErrorWithMail(cfg.Hotel, cfg.Kiosk, "Payment Gateway URL not found", fmt.Errorf("payment provider paybridge requires paybridge.websocket_url"))
|
|
||||||
}
|
|
||||||
if reservationSystem.PaymentGatewayAPIKeyWeb == "" {
|
|
||||||
errorhandlers.FatalErrorWithMail(cfg.Hotel, cfg.Kiosk, "Payment Gateway API Key not found", fmt.Errorf("payment provider paybridge requires paybridge.api_key"))
|
|
||||||
}
|
|
||||||
|
|
||||||
provider = paybridge.NewClient(
|
|
||||||
reservationSystem.PaymentGatewayURL,
|
|
||||||
reservationSystem.PaymentGatewayAPIKeyWeb,
|
|
||||||
cfg.TimeoutSeconds,
|
|
||||||
)
|
|
||||||
|
|
||||||
case cmstypes.PaySystemDojo:
|
|
||||||
var TerminalID string
|
|
||||||
for _, item := range reservationSystem.PDQs {
|
|
||||||
if item.Kiosk != cfg.Kiosk {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
TerminalID = item.Serial
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
dojoClient, err := dojo.NewClient(dojo.Config{
|
|
||||||
BaseURL: reservationSystem.PaymentGatewayURL,
|
|
||||||
APIKey: reservationSystem.PaymentGatewayAPIKeyWeb,
|
|
||||||
SoftwareHouseID: reservationSystem.PaymentGatewaySiteIDEPOS,
|
|
||||||
Version: reservationSystem.PaymentGatewayPasswordEPOS,
|
|
||||||
TerminalID: TerminalID,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
errorhandlers.FatalErrorWithMail(cfg.Hotel, cfg.Kiosk, "Failed to create Dojo client", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
provider = dojoClient
|
|
||||||
|
|
||||||
case cmstypes.PaySystemNone:
|
|
||||||
errorhandlers.FatalErrorWithMail(cfg.Hotel, cfg.Kiosk, "No payment provider selected", fmt.Errorf("payment processing is enabled, but no payment provider selected; expected creditcall, paybridge or dojo"))
|
|
||||||
|
|
||||||
default:
|
|
||||||
errorhandlers.FatalErrorWithMail(cfg.Hotel, cfg.Kiosk, "Unsupported Payment Provider", fmt.Errorf(
|
|
||||||
"unsupported payment provider %q; expected creditcall, paybridge or dojo",
|
|
||||||
cfg.PaymentProvider,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only PayBridge and Dojo use POST /api/payment/sale.
|
|
||||||
if provider != nil {
|
|
||||||
app.SetPaymentService(paymentsvc.NewService(provider))
|
|
||||||
log.Infof("Payment provider enabled for POST /api/payment/sale: %s", cmstypes.PaySystemNames[paymentProvider])
|
|
||||||
fmt.Printf("Payment provider enabled for POST /api/payment/sale: %s\n", cmstypes.PaySystemNames[paymentProvider])
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fmt.Println("Payment processing is disabled")
|
|
||||||
log.Info("Payment processing is disabled")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update cardWellStatus when dispenser status changes
|
|
||||||
if !cfg.TestMode && disp != nil {
|
|
||||||
// Set initial cardWellStatus
|
|
||||||
app.SetCardWellStatus(cardWellStatus)
|
|
||||||
|
|
||||||
// Set up callback to update cardWellStatus when dispenser status changes
|
|
||||||
disp.OnStockUpdate(func(stock string) {
|
|
||||||
app.SetCardWellStatus(stock)
|
|
||||||
})
|
|
||||||
|
|
||||||
// Start polling for dispenser status every 10 seconds
|
|
||||||
disp.StartPolling(pollingFrequency)
|
|
||||||
}
|
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
|
||||||
app.RegisterRoutes(mux)
|
|
||||||
|
|
||||||
addr := fmt.Sprintf(":%d", cfg.Port)
|
|
||||||
log.Infof("Starting HTTP server on http://localhost%s", addr)
|
|
||||||
fmt.Printf("Starting HTTP server on http://localhost%s", addr)
|
|
||||||
|
|
||||||
if err := http.ListenAndServe(addr, mux); err != nil {
|
|
||||||
errorhandlers.FatalError(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func readTicketLayout() printer.LayoutOptions {
|
|
||||||
const layoutName = "TicketLayout.xml"
|
|
||||||
var layout printer.LayoutOptions
|
|
||||||
|
|
||||||
// 1) Read the file
|
|
||||||
data, err := os.ReadFile(layoutName)
|
|
||||||
if err != nil {
|
|
||||||
errorhandlers.FatalError(fmt.Errorf("failed to read %s: %v", layoutName, err))
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2) Unmarshal into your struct
|
|
||||||
if err := xml.Unmarshal(data, &layout); err != nil {
|
|
||||||
errorhandlers.FatalError(fmt.Errorf("failed to parse %s: %v", layoutName, err))
|
|
||||||
}
|
|
||||||
|
|
||||||
return layout
|
|
||||||
}
|
|
||||||
|
|
||||||
func startChipDnaClient() {
|
|
||||||
startClient := func() (*exec.Cmd, error) {
|
|
||||||
cmd := exec.Command("./ChipDNAClient/ChipDnaClient.exe")
|
|
||||||
err := cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to start ChipDnaClient: %v", err)
|
|
||||||
}
|
|
||||||
log.Infof("ChipDnaClient started with PID %d", cmd.Process.Pid)
|
|
||||||
return cmd, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd, err := startClient()
|
|
||||||
if err != nil {
|
|
||||||
errorhandlers.FatalError(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Restart loop
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
err := cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("ChipDnaClient exited unexpectedly: %v", err)
|
|
||||||
fmt.Printf("ChipDnaClient exited unexpectedly: %v", err)
|
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
cmd, err = startClient()
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Restart failed: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Info("ChipDnaClient restarted successfully")
|
|
||||||
fmt.Printf("ChipDnaClient restarted successfully")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Handle shutdown signals
|
|
||||||
sigs := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
||||||
go func() {
|
|
||||||
<-sigs
|
|
||||||
log.Info("Shutting down...")
|
|
||||||
if cmd.Process != nil {
|
|
||||||
log.Info("Sending SIGTERM to ChipDnaClient...")
|
|
||||||
_ = cmd.Process.Signal(syscall.SIGTERM)
|
|
||||||
// wait up to 5s for graceful shutdown
|
|
||||||
done := make(chan error, 1)
|
|
||||||
go func() { done <- cmd.Wait() }()
|
|
||||||
select {
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
log.Warn("ChipDnaClient did not exit in time, killing...")
|
|
||||||
_ = cmd.Process.Kill()
|
|
||||||
case err := <-done:
|
|
||||||
log.Infof("ChipDnaClient exited cleanly: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
os.Exit(0)
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
120
cms/cms.go
120
cms/cms.go
@ -1,120 +0,0 @@
|
|||||||
// Package cms provides functions to read hotel records from the CMS and retrieve their reservation system configuration.
|
|
||||||
package cms
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ReadHotel gets the hotel record from CMS and returns its reservation system
|
|
||||||
// configuration if the record has been updated since the last read.
|
|
||||||
func ReadHotel(hotelCode, CMSBaseURL string) (cmstypes.ReservationSystemRec, error) {
|
|
||||||
var reservationSystem cmstypes.ReservationSystemRec
|
|
||||||
|
|
||||||
if hotelCode == "" {
|
|
||||||
return reservationSystem, errors.New("hotel code is empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if CMSBaseURL == "" {
|
|
||||||
return reservationSystem, errors.New("CMS base URL is empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
var request cmstypes.RequestRec
|
|
||||||
|
|
||||||
request.Auth.ID = hotelCode
|
|
||||||
request.Auth.APIKey = cmstypes.APIKey
|
|
||||||
request.Auth.Hotel = hotelCode
|
|
||||||
request.Data = hotelCode
|
|
||||||
|
|
||||||
requestData, err := json.Marshal(request)
|
|
||||||
if err != nil {
|
|
||||||
return reservationSystem, fmt.Errorf(
|
|
||||||
"marshal CMS hotel request: %w",
|
|
||||||
err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
requestURL := CMSBaseURL + cmstypes.APIHotelDetails
|
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, requestURL, bytes.NewReader(requestData))
|
|
||||||
if err != nil {
|
|
||||||
return reservationSystem, fmt.Errorf(
|
|
||||||
"create CMS hotel request: %w",
|
|
||||||
err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Set("Accept", "application/json")
|
|
||||||
|
|
||||||
client := &http.Client{
|
|
||||||
Timeout: 15 * time.Second,
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return reservationSystem, fmt.Errorf(
|
|
||||||
"perform CMS hotel request: %w",
|
|
||||||
err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return reservationSystem, fmt.Errorf(
|
|
||||||
"read CMS hotel response: %w",
|
|
||||||
err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode < http.StatusOK ||
|
|
||||||
resp.StatusCode >= http.StatusMultipleChoices {
|
|
||||||
return reservationSystem, fmt.Errorf(
|
|
||||||
"CMS hotel request returned HTTP %s: %s",
|
|
||||||
resp.Status,
|
|
||||||
string(body),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
var hotelResponse cmstypes.HotelResponseRec
|
|
||||||
|
|
||||||
if err := json.Unmarshal(body, &hotelResponse); err != nil {
|
|
||||||
return reservationSystem, fmt.Errorf(
|
|
||||||
"unmarshal CMS hotel response: %w",
|
|
||||||
err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if hotelResponse.Status.Code != cmstypes.StatusSuccessCode {
|
|
||||||
return reservationSystem, fmt.Errorf(
|
|
||||||
"CMS hotel request failed: %s",
|
|
||||||
hotelResponse.Status.Message,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if hotelResponse.TheHotel.Updated <= -1 {
|
|
||||||
return reservationSystem, errors.New(
|
|
||||||
"hotel record not updated since last read",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return hotelResponse.TheHotel.ReservationSystem, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func PaymentSystemIndex(name string) int {
|
|
||||||
for i, paySystemName := range cmstypes.PaySystemNames {
|
|
||||||
if strings.EqualFold(paySystemName, name) {
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
233
cms/cms_test.go
233
cms/cms_test.go
@ -1,233 +0,0 @@
|
|||||||
package cms
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestReadHotelSuccess(t *testing.T) {
|
|
||||||
const hotelCode = "gb-test-hotel"
|
|
||||||
|
|
||||||
expected := cmstypes.ReservationSystemRec{}
|
|
||||||
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(
|
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
t.Errorf(
|
|
||||||
"expected method %s, got %s",
|
|
||||||
http.MethodPost,
|
|
||||||
r.Method,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.URL.Path != cmstypes.APIHotelDetails {
|
|
||||||
t.Errorf(
|
|
||||||
"expected path %q, got %q",
|
|
||||||
cmstypes.APIHotelDetails,
|
|
||||||
r.URL.Path,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if contentType := r.Header.Get("Content-Type"); contentType != "application/json" {
|
|
||||||
t.Errorf(
|
|
||||||
"expected Content-Type application/json, got %q",
|
|
||||||
contentType,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if accept := r.Header.Get("Accept"); accept != "application/json" {
|
|
||||||
t.Errorf(
|
|
||||||
"expected Accept application/json, got %q",
|
|
||||||
accept,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
var request cmstypes.RequestRec
|
|
||||||
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
|
||||||
t.Errorf("decode request: %v", err)
|
|
||||||
http.Error(w, "invalid request", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if request.Auth.ID != hotelCode {
|
|
||||||
t.Errorf(
|
|
||||||
"expected auth ID %q, got %q",
|
|
||||||
hotelCode,
|
|
||||||
request.Auth.ID,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if request.Auth.Hotel != hotelCode {
|
|
||||||
t.Errorf(
|
|
||||||
"expected auth hotel %q, got %q",
|
|
||||||
hotelCode,
|
|
||||||
request.Auth.Hotel,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if request.Auth.APIKey != cmstypes.APIKey {
|
|
||||||
t.Errorf(
|
|
||||||
"expected API key %q, got %q",
|
|
||||||
cmstypes.APIKey,
|
|
||||||
request.Auth.APIKey,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if request.Data != hotelCode {
|
|
||||||
t.Errorf(
|
|
||||||
"expected request data %q, got %q",
|
|
||||||
hotelCode,
|
|
||||||
request.Data,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
response := cmstypes.HotelResponseRec{}
|
|
||||||
response.Status.Code = cmstypes.StatusSuccessCode
|
|
||||||
response.TheHotel.Updated = 1
|
|
||||||
response.TheHotel.ReservationSystem = expected
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
||||||
t.Errorf("encode response: %v", err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
))
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
got, err := ReadHotel(hotelCode, server.URL)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("ReadHotel returned an unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(got, expected) {
|
|
||||||
t.Errorf(
|
|
||||||
"unexpected reservation system:\ngot: %+v\nwant: %+v",
|
|
||||||
got,
|
|
||||||
expected,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadHotelValidation(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
hotelCode string
|
|
||||||
cmsBaseURL string
|
|
||||||
wantMessage string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "empty hotel code",
|
|
||||||
hotelCode: "",
|
|
||||||
cmsBaseURL: "http://example.com",
|
|
||||||
wantMessage: "hotel code is empty",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "empty CMS base URL",
|
|
||||||
hotelCode: "gb-test-hotel",
|
|
||||||
cmsBaseURL: "",
|
|
||||||
wantMessage: "CMS base URL is empty",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
_, err := ReadHotel(test.hotelCode, test.cmsBaseURL)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected an error, got nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err.Error() != test.wantMessage {
|
|
||||||
t.Errorf(
|
|
||||||
"expected error %q, got %q",
|
|
||||||
test.wantMessage,
|
|
||||||
err.Error(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadHotelNotUpdated(t *testing.T) {
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(
|
|
||||||
func(w http.ResponseWriter, _ *http.Request) {
|
|
||||||
response := cmstypes.HotelResponseRec{}
|
|
||||||
response.Status.Code = cmstypes.StatusSuccessCode
|
|
||||||
response.TheHotel.Updated = -1
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
||||||
t.Errorf("encode response: %v", err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
))
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
_, err := ReadHotel("gb-test-hotel", server.URL)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected an error, got nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
const expected = "hotel record not updated since last read"
|
|
||||||
|
|
||||||
if err.Error() != expected {
|
|
||||||
t.Errorf("expected error %q, got %q", expected, err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadHotelHTTPError(t *testing.T) {
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(
|
|
||||||
func(w http.ResponseWriter, _ *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
|
|
||||||
if _, err := io.WriteString(w, "CMS unavailable"); err != nil {
|
|
||||||
t.Errorf("write response: %v", err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
))
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
_, err := ReadHotel("gb-test-hotel", server.URL)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected an error, got nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(err.Error(), "HTTP 500 Internal Server Error") {
|
|
||||||
t.Errorf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(err.Error(), "CMS unavailable") {
|
|
||||||
t.Errorf("expected response body in error, got: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadHotelInvalidJSON(t *testing.T) {
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(
|
|
||||||
func(w http.ResponseWriter, _ *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
if _, err := io.WriteString(w, `{invalid JSON`); err != nil {
|
|
||||||
t.Errorf("write response: %v", err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
))
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
_, err := ReadHotel("gb-test-hotel", server.URL)
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected an error, got nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(err.Error(), "unmarshal CMS hotel response") {
|
|
||||||
t.Errorf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,4 +1,3 @@
|
|||||||
// Package config handles reading and parsing configuration from config.yml.
|
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -6,41 +5,35 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/errorhandlers"
|
"gitea.futuresens.co.uk/futuresens/hardlink/handlers"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
yaml "gopkg.in/yaml.v3"
|
yaml "gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConfigRec holds values from config.yml.
|
// configRec holds values from config.yml.
|
||||||
type ConfigRec struct {
|
type ConfigRec struct {
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
LockserverURL string `yaml:"lockservUrl"`
|
LockserverUrl string `yaml:"lockservUrl"`
|
||||||
LockType string `yaml:"lockType"`
|
LockType string `yaml:"lockType"`
|
||||||
EncoderAddress string `yaml:"encoderAddr"`
|
EncoderAddress string `yaml:"encoderAddr"`
|
||||||
Cert string `yaml:"cert"`
|
Cert string `yaml:"cert"`
|
||||||
DispenserPort string `yaml:"dispensPort"`
|
DispenserPort string `yaml:"dispensPort"`
|
||||||
DispenserAdrr string `yaml:"dispensAddr"`
|
DispenserAdrr string `yaml:"dispensAddr"`
|
||||||
PrinterName string `yaml:"printerName"`
|
PrinterName string `yaml:"printerName"`
|
||||||
LogDir string `yaml:"logdir"`
|
LogDir string `yaml:"logdir"`
|
||||||
Dbport int `yaml:"dbport"`
|
Dbport int `yaml:"dbport"` // Port for the database connection
|
||||||
Dbname string `yaml:"dbname"`
|
Dbname string `yaml:"dbname"` // Database name for the connection
|
||||||
Dbuser string `yaml:"dbuser"`
|
Dbuser string `yaml:"dbuser"` // User for the database connection
|
||||||
Dbpassword string `yaml:"dbpassword"`
|
Dbpassword string `yaml:"dbpassword"` // Password for the database connection
|
||||||
CMSBaseURL string `yaml:"cmsurl"`
|
IsPayment bool `yaml:"isPayment"`
|
||||||
IsPayment bool `yaml:"isPayment"`
|
TestMode bool `yaml:"testMode"`
|
||||||
TestMode bool `yaml:"testMode"`
|
|
||||||
Hotel string `yaml:"hotel"`
|
|
||||||
Kiosk int `yaml:"kiosk"`
|
|
||||||
SendErrorEmails []string `yaml:"senderroremails"`
|
|
||||||
PaymentProvider string `yaml:"paymentProvider"`
|
|
||||||
TimeoutSeconds int `yaml:"timeoutSeconds"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadHardlinkConfig reads config.yml and applies defaults.
|
// ReadConfig reads config.yml and applies defaults.
|
||||||
func ReadHardlinkConfig() ConfigRec {
|
func ReadHardlinkConfig() ConfigRec {
|
||||||
var cfg ConfigRec
|
var cfg ConfigRec
|
||||||
const configName = "config.yml"
|
const configName = "config.yml"
|
||||||
const defaultPort = 9091
|
defaultPort := 9091
|
||||||
sep := string(os.PathSeparator)
|
sep := string(os.PathSeparator)
|
||||||
|
|
||||||
data, err := os.ReadFile(configName)
|
data, err := os.ReadFile(configName)
|
||||||
@ -55,7 +48,8 @@ func ReadHardlinkConfig() ConfigRec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cfg.LockType == "" {
|
if cfg.LockType == "" {
|
||||||
errorhandlers.FatalError(fmt.Errorf("LockType is required in %s", configName))
|
err = fmt.Errorf("LockType is required in %s", configName)
|
||||||
|
handlers.FatalError(err)
|
||||||
}
|
}
|
||||||
cfg.LockType = strings.ToLower(cfg.LockType)
|
cfg.LockType = strings.ToLower(cfg.LockType)
|
||||||
|
|
||||||
@ -66,13 +60,8 @@ func ReadHardlinkConfig() ConfigRec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Dbport <= 0 || cfg.Dbuser == "" || cfg.Dbname == "" || cfg.Dbpassword == "" {
|
if cfg.Dbport <= 0 || cfg.Dbuser == "" || cfg.Dbname == "" || cfg.Dbpassword == "" {
|
||||||
log.Warnf("Database config (dbport, dbuser, dbname, dbpassword) are required in %s", configName)
|
err = fmt.Errorf("Database config (dbport, dbuser, dbname, dbpassword) are required in %s", configName)
|
||||||
}
|
log.Warnf(err.Error())
|
||||||
|
|
||||||
cfg.PaymentProvider = strings.ToLower(strings.TrimSpace(cfg.PaymentProvider))
|
|
||||||
|
|
||||||
if cfg.TimeoutSeconds <= 0 {
|
|
||||||
cfg.TimeoutSeconds = 300
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cfg
|
return cfg
|
||||||
@ -91,7 +80,8 @@ func ReadPreauthReleaserConfig() ConfigRec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Dbport <= 0 || cfg.Dbuser == "" || cfg.Dbname == "" || cfg.Dbpassword == "" {
|
if cfg.Dbport <= 0 || cfg.Dbuser == "" || cfg.Dbname == "" || cfg.Dbpassword == "" {
|
||||||
errorhandlers.FatalErrorWithMail(cfg.Hotel, cfg.Kiosk, "PreauthReleaser Database Configuration Error", fmt.Errorf("Database config (dbport, dbuser, dbname, dbpassword) are required in %s", configName))
|
err = fmt.Errorf("Database config (dbport, dbuser, dbname, dbpassword) are required in %s", configName)
|
||||||
|
handlers.FatalError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.LogDir == "" {
|
if cfg.LogDir == "" {
|
||||||
|
|||||||
@ -12,7 +12,7 @@ import (
|
|||||||
mssqldb "github.com/denisenkom/go-mssqldb" // for error inspection
|
mssqldb "github.com/denisenkom/go-mssqldb" // for error inspection
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
"gitea.futuresens.co.uk/futuresens/hardlink/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InitMSSQL opens and pings the SQL Server instance (keeps your original behaviour)
|
// InitMSSQL opens and pings the SQL Server instance (keeps your original behaviour)
|
||||||
@ -198,4 +198,3 @@ WHERE TxnReference = @TxnReference AND Released = 0;
|
|||||||
log.Infof("Marked preauth %s released at %s", txnReference, releasedAt.Format(time.RFC3339))
|
log.Infof("Marked preauth %s released at %s", txnReference, releasedAt.Format(time.RFC3339))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
294
dispenser/dispenser.go
Normal file
294
dispenser/dispenser.go
Normal file
@ -0,0 +1,294 @@
|
|||||||
|
package dispenser
|
||||||
|
|
||||||
|
import (
|
||||||
|
// "encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
// "log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/tarm/serial"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Control characters.
|
||||||
|
const (
|
||||||
|
STX = 0x02 // Start of Text
|
||||||
|
ETX = 0x03 // End of Text
|
||||||
|
ACK = 0x06 // Positive response
|
||||||
|
NAK = 0x15 // Negative response
|
||||||
|
ENQ = 0x05 // Enquiry from host
|
||||||
|
space = 0x00 // Space character
|
||||||
|
baudRate = 9600 // Baud rate for serial communication
|
||||||
|
delay = 500 * time.Millisecond // Delay for processing commands
|
||||||
|
)
|
||||||
|
|
||||||
|
// type (
|
||||||
|
// configRec struct {
|
||||||
|
// SerialPort string `yaml:"port"`
|
||||||
|
// Address string `yaml:"addr"`
|
||||||
|
// }
|
||||||
|
// )
|
||||||
|
|
||||||
|
var (
|
||||||
|
SerialPort string
|
||||||
|
Address []byte
|
||||||
|
commandFC7 = []byte{ETX, 0x46, 0x43, 0x37} // "FC7" command dispense card at read card position
|
||||||
|
commandFC0 = []byte{ETX, 0x46, 0x43, 0x30} // "FC0" command dispense card out of card mouth command
|
||||||
|
|
||||||
|
statusPos0 = map[byte]string{
|
||||||
|
0x38: "Keep",
|
||||||
|
0x34: "Command cannot execute",
|
||||||
|
0x32: "Preparing card fails",
|
||||||
|
0x31: "Preparing card",
|
||||||
|
0x30: "Normal", // Default if none of the above
|
||||||
|
}
|
||||||
|
|
||||||
|
statusPos1 = map[byte]string{
|
||||||
|
0x38: "Dispensing card",
|
||||||
|
0x34: "Capturing card",
|
||||||
|
0x32: "Dispense card error",
|
||||||
|
0x31: "Capture card error",
|
||||||
|
0x30: "Normal",
|
||||||
|
}
|
||||||
|
|
||||||
|
statusPos2 = map[byte]string{
|
||||||
|
0x38: "No captured card",
|
||||||
|
0x34: "Card overlapped",
|
||||||
|
0x32: "Card jammed",
|
||||||
|
0x31: "Card pre-empty",
|
||||||
|
0x30: "Normal",
|
||||||
|
}
|
||||||
|
|
||||||
|
statusPos3 = map[byte]string{
|
||||||
|
0x38: "Card empty",
|
||||||
|
0x34: "Card ready position",
|
||||||
|
0x33: "Card at encoder position",
|
||||||
|
0x32: "Card at hold card position",
|
||||||
|
0x31: "Card out of card mouth position",
|
||||||
|
0x30: "Normal",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func checkStatus(statusResp []byte) ([]string, error) {
|
||||||
|
if len(statusResp) > 3 {
|
||||||
|
statusBytes := statusResp[7:11] // Extract the relevant bytes from the response
|
||||||
|
// For each position, get the ASCII character, hex value, and mapped meaning.
|
||||||
|
posStatus := []struct {
|
||||||
|
pos int
|
||||||
|
value byte
|
||||||
|
mapper map[byte]string
|
||||||
|
}{
|
||||||
|
{pos: 1, value: statusBytes[0], mapper: statusPos0},
|
||||||
|
{pos: 2, value: statusBytes[1], mapper: statusPos1},
|
||||||
|
{pos: 3, value: statusBytes[2], mapper: statusPos2},
|
||||||
|
{pos: 4, value: statusBytes[3], mapper: statusPos3},
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]string, len(posStatus))
|
||||||
|
for _, p := range posStatus {
|
||||||
|
statusMsg, exists := p.mapper[p.value]
|
||||||
|
if !exists {
|
||||||
|
statusMsg = "Unknown status"
|
||||||
|
}
|
||||||
|
if p.value != 0x30 {
|
||||||
|
result = append(result, fmt.Sprintf("Status: %s; ", statusMsg))
|
||||||
|
}
|
||||||
|
if p.pos == 4 && p.value == 0x38 {
|
||||||
|
return nil, fmt.Errorf("Card well empty")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if len(statusResp) == 3 && statusResp[0] == ACK && statusResp[1] == Address[0] && statusResp[2] == Address[1] {
|
||||||
|
return "active;", nil
|
||||||
|
} else if len(statusResp) > 0 && statusResp[0] == NAK {
|
||||||
|
return "", fmt.Errorf("negative response from dispenser")
|
||||||
|
} else {
|
||||||
|
return "", fmt.Errorf("unexpected response status: % X", statusResp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculateBCC computes the Block Check Character (BCC) as the XOR of all bytes from STX to ETX.
|
||||||
|
func calculateBCC(data []byte) byte {
|
||||||
|
var bcc byte
|
||||||
|
for _, b := range data {
|
||||||
|
bcc ^= b
|
||||||
|
}
|
||||||
|
return bcc
|
||||||
|
}
|
||||||
|
|
||||||
|
func createPacket(address []byte, command []byte) []byte {
|
||||||
|
packet := []byte{STX}
|
||||||
|
packet = append(packet, address...) // Address bytes
|
||||||
|
packet = append(packet, space) // Space character
|
||||||
|
packet = append(packet, command...)
|
||||||
|
packet = append(packet, ETX)
|
||||||
|
bcc := calculateBCC(packet)
|
||||||
|
packet = append(packet, bcc)
|
||||||
|
return packet
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildCheckRF(address []byte) []byte {
|
||||||
|
return createPacket(address, []byte{STX, 0x52, 0x46})
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildCheckAP(address []byte) []byte {
|
||||||
|
return createPacket(address, []byte{STX, 0x41, 0x50})
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendAndReceive(port *serial.Port, packet []byte, delay time.Duration) ([]byte, error) {
|
||||||
|
n, err := port.Write(packet)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error writing to port: %w", err)
|
||||||
|
}
|
||||||
|
// log.Printf("TX %d bytes: % X", n, packet[:n])
|
||||||
|
|
||||||
|
time.Sleep(delay) // Wait for the dispenser to process the command
|
||||||
|
|
||||||
|
buf := make([]byte, 128)
|
||||||
|
n, err = port.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error reading from port: %w", err)
|
||||||
|
}
|
||||||
|
resp := buf[:n]
|
||||||
|
// log.Printf("RX %d bytes: % X", n, buf[:n])
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitializeDispenser() (*serial.Port, error) {
|
||||||
|
const funcName = "initializeDispenser"
|
||||||
|
serialConfig := &serial.Config{
|
||||||
|
Name: SerialPort,
|
||||||
|
Baud: baudRate,
|
||||||
|
ReadTimeout: time.Second * 2,
|
||||||
|
}
|
||||||
|
port, err := serial.OpenPort(serialConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error opening dispenser COM port: %w", err)
|
||||||
|
}
|
||||||
|
return port, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DispenserSequence(port *serial.Port) (string, error) {
|
||||||
|
const funcName = "dispenserSequence"
|
||||||
|
var result string
|
||||||
|
|
||||||
|
// Check dispenser status
|
||||||
|
status, err := CheckDispenserStatus(port)
|
||||||
|
if err != nil {
|
||||||
|
return status, fmt.Errorf("[%s] error checking dispenser status: %v", funcName, err)
|
||||||
|
}
|
||||||
|
result += status
|
||||||
|
|
||||||
|
// Send card to encoder position
|
||||||
|
status, err = CardToEncoderPosition(port)
|
||||||
|
if err != nil {
|
||||||
|
return status, fmt.Errorf("[%s] error sending card to encoder position: %v", funcName, err)
|
||||||
|
}
|
||||||
|
result += "; " + status
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// if dispenser is not responding, I should repeat the command
|
||||||
|
func CheckDispenserStatus(port *serial.Port) (string, error) {
|
||||||
|
const funcName = "checkDispenserStatus"
|
||||||
|
var result string
|
||||||
|
checkCmd := buildCheckAP(Address)
|
||||||
|
enq := append([]byte{ENQ}, Address...)
|
||||||
|
|
||||||
|
// Send check command (AP)
|
||||||
|
statusResp, err := sendAndReceive(port, checkCmd, delay)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error sending check command: %v", err)
|
||||||
|
}
|
||||||
|
if len(statusResp) == 0 {
|
||||||
|
return "", fmt.Errorf("no response from dispenser")
|
||||||
|
}
|
||||||
|
status, err := checkStatus(statusResp)
|
||||||
|
if err != nil {
|
||||||
|
return status, err
|
||||||
|
}
|
||||||
|
result += "; " + status
|
||||||
|
|
||||||
|
// Send ENQ+ADDR to prompt device to execute the command.
|
||||||
|
statusResp, err = sendAndReceive(port, enq, delay)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("error sending ENQ: %v", err)
|
||||||
|
}
|
||||||
|
if len(statusResp) == 0 {
|
||||||
|
return "", fmt.Errorf("no response from dispenser")
|
||||||
|
}
|
||||||
|
status, err = checkStatus(statusResp)
|
||||||
|
if err != nil {
|
||||||
|
return status, err
|
||||||
|
}
|
||||||
|
result += status
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CardToEncoderPosition(port *serial.Port) (string, error) {
|
||||||
|
const funcName = "cartToEncoderPosition"
|
||||||
|
enq := append([]byte{ENQ}, Address...)
|
||||||
|
|
||||||
|
//Send Dispense card to encoder position (FC7) ---
|
||||||
|
dispenseCmd := createPacket(Address, commandFC7)
|
||||||
|
log.Println("Send card to encoder position")
|
||||||
|
statusResp, err := sendAndReceive(port, dispenseCmd, delay)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error sending card to encoder position: %v", err)
|
||||||
|
}
|
||||||
|
_, err = checkStatus(statusResp)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Send ENQ to prompt device ---
|
||||||
|
_, err = port.Write(enq)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error sending ENQ to prompt device: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(delay)
|
||||||
|
|
||||||
|
//Check card position status
|
||||||
|
status, err := CheckDispenserStatus(port)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return status, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CardOutOfMouth(port *serial.Port) (string, error) {
|
||||||
|
const funcName = "CardOutOfMouth"
|
||||||
|
enq := append([]byte{ENQ}, Address...)
|
||||||
|
|
||||||
|
// Send card out of card mouth (FC0) ---
|
||||||
|
dispenseCmd := createPacket(Address, commandFC0)
|
||||||
|
log.Println("Send card to out mouth position")
|
||||||
|
statusResp, err := sendAndReceive(port, dispenseCmd, delay)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error sending out of mouth command: %v", err)
|
||||||
|
}
|
||||||
|
_, err = checkStatus(statusResp)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
//Send ENQ to prompt device ---
|
||||||
|
_, err = port.Write(enq)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error sending ENQ to prompt device: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(delay)
|
||||||
|
|
||||||
|
//Check card position status
|
||||||
|
status, err := CheckDispenserStatus(port)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return status, nil
|
||||||
|
}
|
||||||
5
go.mod
5
go.mod
@ -3,13 +3,10 @@ module gitea.futuresens.co.uk/futuresens/hardlink
|
|||||||
go 1.23.2
|
go 1.23.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
gitea.futuresens.co.uk/futuresens/cmstypes v1.0.200
|
gitea.futuresens.co.uk/futuresens/cmstypes v1.0.190
|
||||||
gitea.futuresens.co.uk/futuresens/logging v1.0.9
|
gitea.futuresens.co.uk/futuresens/logging v1.0.9
|
||||||
github.com/alexbrainman/printer v0.0.0-20200912035444-f40f26f0bdeb
|
github.com/alexbrainman/printer v0.0.0-20200912035444-f40f26f0bdeb
|
||||||
github.com/denisenkom/go-mssqldb v0.12.3
|
github.com/denisenkom/go-mssqldb v0.12.3
|
||||||
github.com/google/uuid v1.6.0
|
|
||||||
github.com/gorilla/websocket v1.5.3
|
|
||||||
github.com/mailjet/mailjet-apiv3-go v0.0.0-20201009050126-c24bc15a9394
|
|
||||||
github.com/sirupsen/logrus v1.9.3
|
github.com/sirupsen/logrus v1.9.3
|
||||||
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07
|
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07
|
||||||
golang.org/x/image v0.27.0
|
golang.org/x/image v0.27.0
|
||||||
|
|||||||
10
go.sum
10
go.sum
@ -1,5 +1,5 @@
|
|||||||
gitea.futuresens.co.uk/futuresens/cmstypes v1.0.200 h1:CRGAuhwecpOwY1CAuC038NFyw6EFulVG554HbUqfezI=
|
gitea.futuresens.co.uk/futuresens/cmstypes v1.0.190 h1:OxP911wT8HQqBJ20KIZcBxi898rsYHhhCkne2u45p1A=
|
||||||
gitea.futuresens.co.uk/futuresens/cmstypes v1.0.200/go.mod h1:ABMUkdm+3VGrkuoCJsXMfPPud9GHDOwBb1NiifFqxes=
|
gitea.futuresens.co.uk/futuresens/cmstypes v1.0.190/go.mod h1:ABMUkdm+3VGrkuoCJsXMfPPud9GHDOwBb1NiifFqxes=
|
||||||
gitea.futuresens.co.uk/futuresens/fscrypto v0.0.0-20221125125050-9acaffd21362 h1:MnhYo7XtsECCU+5yVMo3tZZOOSOKGkl7NpOvTAieBTo=
|
gitea.futuresens.co.uk/futuresens/fscrypto v0.0.0-20221125125050-9acaffd21362 h1:MnhYo7XtsECCU+5yVMo3tZZOOSOKGkl7NpOvTAieBTo=
|
||||||
gitea.futuresens.co.uk/futuresens/fscrypto v0.0.0-20221125125050-9acaffd21362/go.mod h1:p95ouVfK4qyC20D3/k9QLsWSxD2pdweWiY6vcYi9hpM=
|
gitea.futuresens.co.uk/futuresens/fscrypto v0.0.0-20221125125050-9acaffd21362/go.mod h1:p95ouVfK4qyC20D3/k9QLsWSxD2pdweWiY6vcYi9hpM=
|
||||||
gitea.futuresens.co.uk/futuresens/logging v1.0.9 h1:uvCQq/plecB0z/bUWOhFhwyYUWGPkTBZHsYNL+3RFvI=
|
gitea.futuresens.co.uk/futuresens/logging v1.0.9 h1:uvCQq/plecB0z/bUWOhFhwyYUWGPkTBZHsYNL+3RFvI=
|
||||||
@ -19,12 +19,6 @@ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZ
|
|||||||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
|
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
|
||||||
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
|
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
|
||||||
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
|
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
|
||||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
|
||||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
|
||||||
github.com/mailjet/mailjet-apiv3-go v0.0.0-20201009050126-c24bc15a9394 h1:+6kiV40vfmh17TDlZG15C2uGje1/XBGT32j6xKmUkqM=
|
|
||||||
github.com/mailjet/mailjet-apiv3-go v0.0.0-20201009050126-c24bc15a9394/go.mod h1:ogN8Sxy3n5VKLhQxbtSBM3ICG/VgjXS/akQJIoDSrgA=
|
|
||||||
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
|
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
|
||||||
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
|
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
|||||||
382
handlers/handlers.go
Normal file
382
handlers/handlers.go
Normal file
@ -0,0 +1,382 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"encoding/xml"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/tarm/serial"
|
||||||
|
|
||||||
|
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
||||||
|
"gitea.futuresens.co.uk/futuresens/hardlink/db"
|
||||||
|
"gitea.futuresens.co.uk/futuresens/hardlink/dispenser"
|
||||||
|
"gitea.futuresens.co.uk/futuresens/hardlink/lockserver"
|
||||||
|
"gitea.futuresens.co.uk/futuresens/hardlink/payment"
|
||||||
|
"gitea.futuresens.co.uk/futuresens/hardlink/printer"
|
||||||
|
"gitea.futuresens.co.uk/futuresens/hardlink/types"
|
||||||
|
"gitea.futuresens.co.uk/futuresens/logging"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type App struct {
|
||||||
|
dispPort *serial.Port
|
||||||
|
lockserver lockserver.LockServer
|
||||||
|
isPayment bool
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewApp(dispPort *serial.Port, lockType, encoderAddress string, db *sql.DB, isPayment bool) *App {
|
||||||
|
return &App{
|
||||||
|
isPayment: isPayment,
|
||||||
|
dispPort: dispPort,
|
||||||
|
lockserver: lockserver.NewLockServer(lockType, encoderAddress, FatalError),
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) RegisterRoutes(mux *http.ServeMux) {
|
||||||
|
mux.HandleFunc("/issuedoorcard", app.issueDoorCard)
|
||||||
|
mux.HandleFunc("/printroomticket", app.printRoomTicket)
|
||||||
|
mux.HandleFunc("/takepreauth", app.takePreauthorization)
|
||||||
|
mux.HandleFunc("/takepayment", app.takePayment)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) takePreauthorization(w http.ResponseWriter, r *http.Request) {
|
||||||
|
const op = logging.Op("takePreauthorization")
|
||||||
|
var (
|
||||||
|
theResponse cmstypes.ResponseRec
|
||||||
|
theRequest cmstypes.TransactionRec
|
||||||
|
trResult payment.TransactionResultXML
|
||||||
|
result payment.PaymentResult
|
||||||
|
save bool
|
||||||
|
)
|
||||||
|
|
||||||
|
theResponse.Status.Code = http.StatusInternalServerError
|
||||||
|
theResponse.Status.Message = "500 Internal server error"
|
||||||
|
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
if !app.isPayment {
|
||||||
|
theResponse.Data = payment.BuildFailureURL(types.ResultError, "Payment processing is disabled")
|
||||||
|
writeTransactionResult(w, http.StatusServiceUnavailable, theResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Method == http.MethodOptions {
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("takePreauthorization called")
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
theResponse.Data = payment.BuildFailureURL(types.ResultError, "Method not allowed; use POST")
|
||||||
|
writeTransactionResult(w, http.StatusMethodNotAllowed, theResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
if ct := r.Header.Get("Content-Type"); ct != "text/xml" {
|
||||||
|
theResponse.Data = payment.BuildFailureURL(types.ResultError, "Content-Type must be text/xml")
|
||||||
|
writeTransactionResult(w, http.StatusUnsupportedMediaType, theResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
body, _ := io.ReadAll(r.Body)
|
||||||
|
err := xml.Unmarshal(body, &theRequest)
|
||||||
|
if err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "ReadXML", string(op), "", "", 0)
|
||||||
|
theResponse.Data = payment.BuildFailureURL(types.ResultError, "Invalid XML payload")
|
||||||
|
writeTransactionResult(w, http.StatusBadRequest, theResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("Transaction payload: Amount=%s, Type=%s", theRequest.AmountMinorUnits, theRequest.TransactionType)
|
||||||
|
|
||||||
|
client := &http.Client{Timeout: 300 * time.Second}
|
||||||
|
response, err := client.Post(types.LinkTakePreauthorization, "text/xml", bytes.NewBuffer(body))
|
||||||
|
if err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "Payment processing error", string(op), "", "", 0)
|
||||||
|
theResponse.Data = payment.BuildFailureURL(types.ResultError, "No response from payment processor")
|
||||||
|
writeTransactionResult(w, http.StatusBadGateway, theResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
body, err = io.ReadAll(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "Read response body error", string(op), "", "", 0)
|
||||||
|
theResponse.Data = payment.BuildFailureURL(types.ResultError, "Failed to read response body")
|
||||||
|
writeTransactionResult(w, http.StatusInternalServerError, theResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := trResult.ParseTransactionResult(body); err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "Parse transaction result error", string(op), "", "", 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compose JSON from responseEntries
|
||||||
|
result.FillFromTransactionResult(trResult)
|
||||||
|
|
||||||
|
if err := printer.PrintCardholderReceipt(result.CardholderReceipt); err != nil {
|
||||||
|
log.Errorf("PrintCardholderReceipt error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
theResponse.Status = result.Status
|
||||||
|
theResponse.Data, save = payment.BuildPreauthRedirectURL(result.Fields)
|
||||||
|
if save {
|
||||||
|
db.InsertPreauth(r.Context(), app.db, result.Fields, theRequest.CheckoutDate)
|
||||||
|
}
|
||||||
|
writeTransactionResult(w, http.StatusOK, theResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) takePayment(w http.ResponseWriter, r *http.Request) {
|
||||||
|
const op = logging.Op("takePayment")
|
||||||
|
var (
|
||||||
|
theResponse cmstypes.ResponseRec
|
||||||
|
theRequest cmstypes.TransactionRec
|
||||||
|
trResult payment.TransactionResultXML
|
||||||
|
result payment.PaymentResult
|
||||||
|
)
|
||||||
|
|
||||||
|
theResponse.Status.Code = http.StatusInternalServerError
|
||||||
|
theResponse.Status.Message = "500 Internal server error"
|
||||||
|
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
if !app.isPayment {
|
||||||
|
theResponse.Data = payment.BuildFailureURL(types.ResultError, "Payment processing is disabled")
|
||||||
|
writeTransactionResult(w, http.StatusServiceUnavailable, theResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Method == http.MethodOptions {
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("takePayment called")
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
theResponse.Data = payment.BuildFailureURL(types.ResultError, "Method not allowed; use POST")
|
||||||
|
writeTransactionResult(w, http.StatusMethodNotAllowed, theResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
if ct := r.Header.Get("Content-Type"); ct != "text/xml" {
|
||||||
|
theResponse.Data = payment.BuildFailureURL(types.ResultError, "Content-Type must be text/xml")
|
||||||
|
writeTransactionResult(w, http.StatusUnsupportedMediaType, theResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
body, _ := io.ReadAll(r.Body)
|
||||||
|
err := xml.Unmarshal(body, &theRequest)
|
||||||
|
if err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "ReadXML", string(op), "", "", 0)
|
||||||
|
theResponse.Data = payment.BuildFailureURL(types.ResultError, "Invalid XML payload")
|
||||||
|
writeTransactionResult(w, http.StatusBadRequest, theResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("Transaction payload: Amount=%s, Type=%s", theRequest.AmountMinorUnits, theRequest.TransactionType)
|
||||||
|
|
||||||
|
client := &http.Client{Timeout: 300 * time.Second}
|
||||||
|
response, err := client.Post(types.LinkTakePayment, "text/xml", bytes.NewBuffer(body))
|
||||||
|
if err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "Payment processing error", string(op), "", "", 0)
|
||||||
|
theResponse.Data = payment.BuildFailureURL(types.ResultError, "No response from payment processor")
|
||||||
|
writeTransactionResult(w, http.StatusBadGateway, theResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
body, err = io.ReadAll(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "Read response body error", string(op), "", "", 0)
|
||||||
|
theResponse.Data = payment.BuildFailureURL(types.ResultError, "Failed to read response body")
|
||||||
|
writeTransactionResult(w, http.StatusInternalServerError, theResponse)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := trResult.ParseTransactionResult(body); err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "Parse transaction result error", string(op), "", "", 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compose JSON from responseEntries
|
||||||
|
result.FillFromTransactionResult(trResult)
|
||||||
|
|
||||||
|
if err := printer.PrintCardholderReceipt(result.CardholderReceipt); err != nil {
|
||||||
|
log.Errorf("PrintCardholderReceipt error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
theResponse.Status = result.Status
|
||||||
|
theResponse.Data = payment.BuildPaymentRedirectURL(result.Fields)
|
||||||
|
writeTransactionResult(w, http.StatusOK, theResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) issueDoorCard(w http.ResponseWriter, r *http.Request) {
|
||||||
|
const op = logging.Op("issueDoorCard")
|
||||||
|
var (
|
||||||
|
doorReq lockserver.DoorCardRequest
|
||||||
|
theResponse cmstypes.StatusRec
|
||||||
|
)
|
||||||
|
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
if r.Method == http.MethodOptions {
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("issueDoorCard called")
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
writeError(w, http.StatusMethodNotAllowed, "Method not allowed; use POST")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
|
||||||
|
writeError(w, http.StatusUnsupportedMediaType, "Content-Type must be application/json")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&doorReq); err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "ReadJSON", string(op), "", "", 0)
|
||||||
|
writeError(w, http.StatusBadRequest, "Invalid JSON payload: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse times
|
||||||
|
checkIn, err := time.Parse(types.CustomLayout, doorReq.CheckinTime)
|
||||||
|
if err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "Invalid checkinTime format", string(op), "", "", 0)
|
||||||
|
writeError(w, http.StatusBadRequest, "Invalid checkinTime format: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
checkOut, err := time.Parse(types.CustomLayout, doorReq.CheckoutTime)
|
||||||
|
if err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "Invalid checkoutTime format", string(op), "", "", 0)
|
||||||
|
writeError(w, http.StatusBadRequest, "Invalid checkoutTime format: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// card to encoder position if not there already
|
||||||
|
if status, err := dispenser.DispenserSequence(app.dispPort); err != nil {
|
||||||
|
if status != "" {
|
||||||
|
logging.Error(serviceName, status, "Dispense error", string(op), "", "", 0)
|
||||||
|
writeError(w, http.StatusServiceUnavailable, "Dispense error: "+err.Error())
|
||||||
|
} else {
|
||||||
|
logging.Error(serviceName, err.Error(), "Dispense error", string(op), "", "", 0)
|
||||||
|
writeError(w, http.StatusServiceUnavailable, "Dispense error: "+err.Error()+"; check card stock")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
log.Info(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// build lock server command
|
||||||
|
app.lockserver.BuildCommand(doorReq, checkIn, checkOut)
|
||||||
|
|
||||||
|
// lock server sequence
|
||||||
|
err = app.lockserver.LockSequence()
|
||||||
|
if err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "Key encoding", string(op), "", "", 0)
|
||||||
|
writeError(w, http.StatusBadGateway, err.Error())
|
||||||
|
dispenser.CardOutOfMouth(app.dispPort)
|
||||||
|
dispenser.DispenserSequence(app.dispPort)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// card out of mouth
|
||||||
|
if status, err := dispenser.CardOutOfMouth(app.dispPort); err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "Dispenser eject error", string(op), "", "", 0)
|
||||||
|
writeError(w, http.StatusServiceUnavailable, "Dispenser eject error: "+err.Error())
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
log.Info(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// card to encoder position
|
||||||
|
if status, err := dispenser.DispenserSequence(app.dispPort); err != nil {
|
||||||
|
if status != "" {
|
||||||
|
logging.Error(serviceName, status, "Dispense error", string(op), "", "", 0)
|
||||||
|
writeError(w, http.StatusServiceUnavailable, "Dispense error: "+err.Error())
|
||||||
|
} else {
|
||||||
|
logging.Error(serviceName, err.Error(), "Dispense error", string(op), "", "", 0)
|
||||||
|
writeError(w, http.StatusServiceUnavailable, "Dispense error: "+err.Error()+"; check card stock")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
log.Info(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
theResponse.Code = http.StatusOK
|
||||||
|
theResponse.Message = "Card issued successfully"
|
||||||
|
// success! return 200 and any data you like
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
json.NewEncoder(w).Encode(theResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) printRoomTicket(w http.ResponseWriter, r *http.Request) {
|
||||||
|
const op = logging.Op("printRoomTicket")
|
||||||
|
var roomDetails printer.RoomDetailsRec
|
||||||
|
// Allow CORS preflight if needed
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
|
||||||
|
if r.Method == http.MethodOptions {
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println("printRoomTicket called")
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
writeError(w, http.StatusMethodNotAllowed, "Method not allowed; use POST")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ct := r.Header.Get("Content-Type"); !strings.Contains(ct, "xml") {
|
||||||
|
writeError(w, http.StatusUnsupportedMediaType, "Content-Type must be application/xml")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer r.Body.Close()
|
||||||
|
if err := xml.NewDecoder(r.Body).Decode(&roomDetails); err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "ReadXML", string(op), "", "", 0)
|
||||||
|
writeError(w, http.StatusBadRequest, "Invalid XML payload: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := printer.BuildRoomTicket(roomDetails)
|
||||||
|
if err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "BuildRoomTicket", string(op), "", "", 0)
|
||||||
|
writeError(w, http.StatusInternalServerError, "BuildRoomTicket failed: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send to the Windows Epson TM-T82II via the printer package
|
||||||
|
if err := printer.SendToPrinter(data); err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "printRoomTicket", "printRoomTicket", "", "", 0)
|
||||||
|
writeError(w, http.StatusInternalServerError, "Print failed: "+err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Success
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
json.NewEncoder(w).Encode(cmstypes.StatusRec{
|
||||||
|
Code: http.StatusOK,
|
||||||
|
Message: "Print job sent successfully",
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package errorhandlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -7,12 +7,14 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/mail"
|
"gitea.futuresens.co.uk/futuresens/logging"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const serviceName = "hardlink"
|
||||||
|
|
||||||
// writeError is a helper to send a JSON error and HTTP status in one go.
|
// writeError is a helper to send a JSON error and HTTP status in one go.
|
||||||
func WriteError(w http.ResponseWriter, status int, msg string) {
|
func writeError(w http.ResponseWriter, status int, msg string) {
|
||||||
theResponse := cmstypes.StatusRec{
|
theResponse := cmstypes.StatusRec{
|
||||||
Code: status,
|
Code: status,
|
||||||
Message: msg,
|
Message: msg,
|
||||||
@ -22,6 +24,14 @@ func WriteError(w http.ResponseWriter, status int, msg string) {
|
|||||||
json.NewEncoder(w).Encode(theResponse)
|
json.NewEncoder(w).Encode(theResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeTransactionResult(w http.ResponseWriter, status int, theResponse cmstypes.ResponseRec) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(status)
|
||||||
|
if err := json.NewEncoder(w).Encode(theResponse); err != nil {
|
||||||
|
logging.Error(serviceName, err.Error(), "JSON encode error", "startTransaction", "", "", 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func FatalError(err error) {
|
func FatalError(err error) {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
log.Errorf(err.Error())
|
log.Errorf(err.Error())
|
||||||
@ -29,8 +39,3 @@ func FatalError(err error) {
|
|||||||
fmt.Scanln()
|
fmt.Scanln()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FatalErrorWithMail(hotel string, kiosk int, title string, err error) {
|
|
||||||
mail.SendEmailOnError(hotel, kiosk, title, err.Error())
|
|
||||||
FatalError(err)
|
|
||||||
}
|
|
||||||
@ -4,17 +4,16 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/bootstrap"
|
"gitea.futuresens.co.uk/futuresens/hardlink/bootstrap"
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/config"
|
"gitea.futuresens.co.uk/futuresens/hardlink/config"
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/logging"
|
"gitea.futuresens.co.uk/futuresens/hardlink/logging"
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/creditcall"
|
"gitea.futuresens.co.uk/futuresens/hardlink/payment"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
buildVersion = "1.0.2"
|
buildVersion = "1.0.0"
|
||||||
serviceName = "preauth-release"
|
serviceName = "preauth-release"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -32,18 +31,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer database.Close()
|
defer database.Close()
|
||||||
|
|
||||||
if err := creditcall.ReleasePreauthorizations(database); err != nil {
|
if err := payment.ReleasePreauthorizations(database); err != nil {
|
||||||
log.Error(err)
|
log.WithError(err).Fatal("Preauth release failed")
|
||||||
fmt.Println(err)
|
|
||||||
} else {
|
|
||||||
log.Info("Task completed successfully")
|
|
||||||
fmt.Println("Task completed successfully")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 20; i > 0; i-- {
|
log.Info("Task completed successfully")
|
||||||
fmt.Printf("\rExiting in %2d seconds... ", i)
|
fmt.Println(". Press Enter to exit...")
|
||||||
time.Sleep(time.Second)
|
fmt.Scanln()
|
||||||
}
|
|
||||||
fmt.Println("\rExiting now. ")
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,278 +0,0 @@
|
|||||||
package creditcall
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"encoding/xml"
|
|
||||||
"fmt"
|
|
||||||
"html"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
KeyErrors = "ERRORS"
|
|
||||||
KeyVersionInformation = "VERSION_INFORMATION"
|
|
||||||
KeyChipDnaStatus = "CHIPDNA_STATUS"
|
|
||||||
KeyPaymentDeviceStatus = "PAYMENT_DEVICE_STATUS"
|
|
||||||
KeyRequestQueueStatus = "REQUEST_QUEUE_STATUS"
|
|
||||||
KeyTmsStatus = "TMS_STATUS"
|
|
||||||
KeyPaymentPlatform = "PAYMENT_PLATFORM_STATUS"
|
|
||||||
KeyPaymentDeviceModel = "PAYMENT_DEVICE_MODEL"
|
|
||||||
KeyPaymentDeviceIdentifier = "PAYMENT_DEVICE_IDENTIFIER"
|
|
||||||
KeyIsAvailable = "IS_AVAILABLE"
|
|
||||||
KeyAvailabilityError = "AVAILABILITY_ERROR"
|
|
||||||
KeyAvailabilityErrorInformation = "AVAILABILITY_ERROR_INFORMATION"
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
|
||||||
ArrayOfParameter struct {
|
|
||||||
Parameters []Parameter `xml:"Parameter" json:"Parameters"`
|
|
||||||
}
|
|
||||||
|
|
||||||
Parameter struct {
|
|
||||||
Key string `xml:"Key" json:"Key"`
|
|
||||||
Value string `xml:"Value" json:"Value"`
|
|
||||||
}
|
|
||||||
|
|
||||||
ServerStatus struct {
|
|
||||||
IsProcessingTransaction bool `xml:"IsProcessingTransaction" json:"IsProcessingTransaction"`
|
|
||||||
ChipDnaServerIssue string `xml:"ChipDnaServerIssue" json:"ChipDnaServerIssue"`
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayOfPaymentDeviceStatus struct {
|
|
||||||
Items []PaymentDeviceStatus `xml:"PaymentDeviceStatus" json:"Items"`
|
|
||||||
}
|
|
||||||
|
|
||||||
PaymentDeviceStatus struct {
|
|
||||||
ConfiguredDeviceId string `xml:"ConfiguredDeviceId" json:"ConfiguredDeviceId"`
|
|
||||||
ConfiguredDeviceModel string `xml:"ConfiguredDeviceModel" json:"ConfiguredDeviceModel"`
|
|
||||||
ProcessingTransaction bool `xml:"ProcessingTransaction" json:"ProcessingTransaction"`
|
|
||||||
AvailabilityError string `xml:"AvailabilityError" json:"AvailabilityError"`
|
|
||||||
AvailabilityErrorInformation string `xml:"AvailabilityErrorInformation" json:"AvailabilityErrorInformation"`
|
|
||||||
ConfigurationState string `xml:"ConfigurationState" json:"ConfigurationState"`
|
|
||||||
IsAvailable bool `xml:"IsAvailable" json:"IsAvailable"`
|
|
||||||
BatteryPercentage int `xml:"BatteryPercentage" json:"BatteryPercentage"`
|
|
||||||
BatteryChargingStatus string `xml:"BatteryChargingStatus" json:"BatteryChargingStatus"`
|
|
||||||
BatteryStatusUpdateDateTime string `xml:"BatteryStatusUpdateDateTime" json:"BatteryStatusUpdateDateTime"`
|
|
||||||
BatteryStatusUpdateDateTimeFormat string `xml:"BatteryStatusUpdateDateTimeFormat" json:"BatteryStatusUpdateDateTimeFormat"`
|
|
||||||
}
|
|
||||||
|
|
||||||
RequestQueueStatus struct {
|
|
||||||
CreditRequestCount int `xml:"CreditRequestCount" json:"CreditRequestCount"`
|
|
||||||
CreditConfirmRequestCount int `xml:"CreditConfirmRequestCount" json:"CreditConfirmRequestCount"`
|
|
||||||
CreditVoidRequestCount int `xml:"CreditVoidRequestCount" json:"CreditVoidRequestCount"`
|
|
||||||
DebitRequestCount int `xml:"DebitRequestCount" json:"DebitRequestCount"`
|
|
||||||
DebitConfirmRequestCount int `xml:"DebitConfirmRequestCount" json:"DebitConfirmRequestCount"`
|
|
||||||
DebitVoidRequestCount int `xml:"DebitVoidRequestCount" json:"DebitVoidRequestCount"`
|
|
||||||
}
|
|
||||||
|
|
||||||
TmsStatus struct {
|
|
||||||
LastConfigUpdateDateTime string `xml:"LastConfigUpdateDateTime" json:"LastConfigUpdateDateTime"`
|
|
||||||
DaysUntilConfigUpdateIsRequired int `xml:"DaysUntilConfigUpdateIsRequired" json:"DaysUntilConfigUpdateIsRequired"`
|
|
||||||
RequiredConfigUpdateDateTime string `xml:"RequiredConfigUpdateDateTime" json:"RequiredConfigUpdateDateTime"`
|
|
||||||
}
|
|
||||||
|
|
||||||
PaymentPlatformStatus struct {
|
|
||||||
MachineLocalDateTime string `xml:"MachineLocalDateTime" json:"MachineLocalDateTime"`
|
|
||||||
PaymentPlatformLocalDateTime string `xml:"PaymentPlatformLocalDateTime" json:"PaymentPlatformLocalDateTime"`
|
|
||||||
PaymentPlatformLocalDateTimeFormat string `xml:"PaymentPlatformLocalDateTimeFormat" json:"PaymentPlatformLocalDateTimeFormat"`
|
|
||||||
State string `xml:"State" json:"State"`
|
|
||||||
}
|
|
||||||
|
|
||||||
ParsedStatus struct {
|
|
||||||
Errors []string `json:"Errors"`
|
|
||||||
VersionInfo map[string]string `json:"VersionInfo"`
|
|
||||||
ChipDnaStatus *ServerStatus `json:"ChipDnaStatus"`
|
|
||||||
PaymentDevices []PaymentDeviceStatus `json:"PaymentDevices"`
|
|
||||||
RequestQueue *RequestQueueStatus `json:"RequestQueue"`
|
|
||||||
TMS *TmsStatus `json:"TMS"`
|
|
||||||
PaymentPlatform *PaymentPlatformStatus `json:"PaymentPlatform"`
|
|
||||||
Unknown map[string]string `json:"Unknown"`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// ===========================
|
|
||||||
// Parser
|
|
||||||
// ===========================
|
|
||||||
|
|
||||||
func ParseStatusResult(data []byte) (*ParsedStatus, error) {
|
|
||||||
var tr TransactionResultXML
|
|
||||||
if err := tr.ParseTransactionResult(data); err != nil {
|
|
||||||
return nil, fmt.Errorf("unmarshal TransactionResult: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
out := &ParsedStatus{
|
|
||||||
VersionInfo: make(map[string]string),
|
|
||||||
Unknown: make(map[string]string),
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, e := range tr.Entries {
|
|
||||||
switch e.Key {
|
|
||||||
|
|
||||||
// Some responses return plain text (not escaped XML) for ERRORS.
|
|
||||||
case KeyErrors:
|
|
||||||
msg := html.UnescapeString(e.Value) // safe even if not escaped
|
|
||||||
if msg != "" {
|
|
||||||
out.Errors = append(out.Errors, msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Everything below is escaped XML inside <Value>
|
|
||||||
case KeyVersionInformation:
|
|
||||||
unescaped := html.UnescapeString(e.Value)
|
|
||||||
|
|
||||||
var a ArrayOfParameter
|
|
||||||
if err := xml.Unmarshal([]byte(unescaped), &a); err != nil {
|
|
||||||
return nil, fmt.Errorf("unmarshal %s: %w", e.Key, err)
|
|
||||||
}
|
|
||||||
for _, p := range a.Parameters {
|
|
||||||
out.VersionInfo[p.Key] = p.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
case KeyChipDnaStatus:
|
|
||||||
unescaped := html.UnescapeString(e.Value)
|
|
||||||
|
|
||||||
var s ServerStatus
|
|
||||||
if err := xml.Unmarshal([]byte(unescaped), &s); err != nil {
|
|
||||||
return nil, fmt.Errorf("unmarshal %s: %w", e.Key, err)
|
|
||||||
}
|
|
||||||
out.ChipDnaStatus = &s
|
|
||||||
|
|
||||||
case KeyPaymentDeviceStatus:
|
|
||||||
unescaped := html.UnescapeString(e.Value)
|
|
||||||
|
|
||||||
var a ArrayOfPaymentDeviceStatus
|
|
||||||
if err := xml.Unmarshal([]byte(unescaped), &a); err != nil {
|
|
||||||
return nil, fmt.Errorf("unmarshal %s: %w", e.Key, err)
|
|
||||||
}
|
|
||||||
out.PaymentDevices = append(out.PaymentDevices, a.Items...)
|
|
||||||
|
|
||||||
case KeyRequestQueueStatus:
|
|
||||||
unescaped := html.UnescapeString(e.Value)
|
|
||||||
|
|
||||||
var s RequestQueueStatus
|
|
||||||
if err := xml.Unmarshal([]byte(unescaped), &s); err != nil {
|
|
||||||
return nil, fmt.Errorf("unmarshal %s: %w", e.Key, err)
|
|
||||||
}
|
|
||||||
out.RequestQueue = &s
|
|
||||||
|
|
||||||
case KeyTmsStatus:
|
|
||||||
unescaped := html.UnescapeString(e.Value)
|
|
||||||
|
|
||||||
var s TmsStatus
|
|
||||||
if err := xml.Unmarshal([]byte(unescaped), &s); err != nil {
|
|
||||||
return nil, fmt.Errorf("unmarshal %s: %w", e.Key, err)
|
|
||||||
}
|
|
||||||
out.TMS = &s
|
|
||||||
|
|
||||||
case KeyPaymentPlatform:
|
|
||||||
unescaped := html.UnescapeString(e.Value)
|
|
||||||
|
|
||||||
var s PaymentPlatformStatus
|
|
||||||
if err := xml.Unmarshal([]byte(unescaped), &s); err != nil {
|
|
||||||
return nil, fmt.Errorf("unmarshal %s: %w", e.Key, err)
|
|
||||||
}
|
|
||||||
out.PaymentPlatform = &s
|
|
||||||
|
|
||||||
default:
|
|
||||||
// Keep for logging / future additions. Unescape so it's readable XML if it was escaped.
|
|
||||||
out.Unknown[e.Key] = html.UnescapeString(e.Value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func fetchChipDNAStatus() (*ParsedStatus, error) {
|
|
||||||
const op = logging.Op("fetchChipDNAStatus")
|
|
||||||
|
|
||||||
body := []byte{}
|
|
||||||
client := &http.Client{Timeout: 300 * time.Second}
|
|
||||||
response, err := client.Post(types.LinkChipDNAStatus, "text/xml", bytes.NewBuffer(body))
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "error fetching ChipDNA status", string(op), "", "", 0)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer response.Body.Close()
|
|
||||||
|
|
||||||
body, err = io.ReadAll(response.Body)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Read response body error", string(op), "", "", 0)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := ParseStatusResult(body)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Parse ChipDNA status error", string(op), "", "", 0)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReadPdqStatus(hotel string, kiosk int) (PaymentDeviceStatus, error) {
|
|
||||||
const op = logging.Op("readPdqStatus")
|
|
||||||
|
|
||||||
status, err := fetchChipDNAStatus()
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, "pdq_unavailable", "Failed to fetch ChipDNA status: "+err.Error(), string(op), "", hotel, kiosk)
|
|
||||||
return PaymentDeviceStatus{}, fmt.Errorf("error fetch ChipDNA status: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(status.Errors) > 0 {
|
|
||||||
msg := strings.Join(status.Errors, "; ")
|
|
||||||
logging.Error(types.ServiceName, "pdq_unavailable", "ChipDNA status errors: "+msg, string(op), "", hotel, kiosk)
|
|
||||||
return PaymentDeviceStatus{}, fmt.Errorf("ChipDNA status errors: %s", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(status.PaymentDevices) == 0 {
|
|
||||||
logging.Error(types.ServiceName, "pdq_unavailable", "ChipDNA status has no PAYMENT_DEVICE_STATUS items", string(op), "", hotel, kiosk)
|
|
||||||
return PaymentDeviceStatus{}, fmt.Errorf("no payment devices returned")
|
|
||||||
}
|
|
||||||
|
|
||||||
dev := status.PaymentDevices[0]
|
|
||||||
if !dev.IsAvailable {
|
|
||||||
logging.Error(types.ServiceName, "pdq_unavailable", "Payment device unavailable", string(op), "", hotel, kiosk)
|
|
||||||
return dev, fmt.Errorf("device unavailable")
|
|
||||||
}
|
|
||||||
|
|
||||||
return dev, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func StartPdqHourlyCheck(ctx context.Context, hotel string, kiosk int) {
|
|
||||||
// waitUntilNextHour(ctx)
|
|
||||||
|
|
||||||
// First execution exactly at round hour
|
|
||||||
_, _ = ReadPdqStatus(hotel, kiosk)
|
|
||||||
|
|
||||||
ticker := time.NewTicker(10 * time.Second)
|
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-ticker.C:
|
|
||||||
_, _ = ReadPdqStatus(hotel, kiosk)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func waitUntilNextHour(ctx context.Context) {
|
|
||||||
now := time.Now()
|
|
||||||
next := now.Truncate(time.Hour).Add(time.Hour)
|
|
||||||
d := time.Until(next)
|
|
||||||
|
|
||||||
timer := time.NewTimer(d)
|
|
||||||
defer timer.Stop()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
case <-timer.C:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,338 +0,0 @@
|
|||||||
package dispenser
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/tarm/serial"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Control characters.
|
|
||||||
const (
|
|
||||||
STX = 0x02 // Start of Text
|
|
||||||
ETX = 0x03 // End of Text
|
|
||||||
ACK = 0x06 // Positive response
|
|
||||||
NAK = 0x15 // Negative response
|
|
||||||
ENQ = 0x05 // Enquiry from host
|
|
||||||
space = 0x00 // Space character
|
|
||||||
baudRate = 9600 // Baud rate for serial communication
|
|
||||||
delay = 500 * time.Millisecond // Delay for processing commands
|
|
||||||
|
|
||||||
// cache freshness for "continuous status" reads (tune as you wish)
|
|
||||||
defaultStatusTTL = 1500 * time.Millisecond
|
|
||||||
|
|
||||||
CardWellEmptyMessage = "Card well is empty"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrCardWellEmpty = errors.New(CardWellEmptyMessage)
|
|
||||||
|
|
||||||
SerialPort string
|
|
||||||
Address []byte
|
|
||||||
|
|
||||||
commandFC7 = []byte{ETX, 0x46, 0x43, 0x37} // "FC7"
|
|
||||||
commandFC0 = []byte{ETX, 0x46, 0x43, 0x30} // "FC0"
|
|
||||||
|
|
||||||
statusPos0 = map[byte]string{
|
|
||||||
0x38: "Keep",
|
|
||||||
0x34: "Command cannot execute",
|
|
||||||
0x32: "Preparing card fails",
|
|
||||||
0x31: "Preparing card",
|
|
||||||
0x30: "Normal",
|
|
||||||
0x36: "Command cannot execute; Preparing card fails",
|
|
||||||
}
|
|
||||||
statusPos1 = map[byte]string{
|
|
||||||
0x38: "Dispensing card",
|
|
||||||
0x34: "Capturing card",
|
|
||||||
0x32: "Dispense card error",
|
|
||||||
0x31: "Capture card error",
|
|
||||||
0x30: "Normal",
|
|
||||||
}
|
|
||||||
statusPos2 = map[byte]string{
|
|
||||||
0x38: "No captured card",
|
|
||||||
0x34: "Card overlapped",
|
|
||||||
0x32: "Card jammed",
|
|
||||||
0x31: "Card pre-empty",
|
|
||||||
0x30: "Normal",
|
|
||||||
}
|
|
||||||
statusPos3 = map[byte]string{
|
|
||||||
0x38: "Card empty",
|
|
||||||
0x34: "Card ready position",
|
|
||||||
0x33: "Card at encoder position",
|
|
||||||
0x32: "Card at hold card position",
|
|
||||||
0x31: "Card out of card mouth position",
|
|
||||||
0x30: "Normal",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// --------------------
|
|
||||||
// Status helpers
|
|
||||||
// --------------------
|
|
||||||
|
|
||||||
func statusDescription(statusBytes []byte) string {
|
|
||||||
if len(statusBytes) < 4 {
|
|
||||||
return fmt.Sprintf("<invalid len=%d>", len(statusBytes))
|
|
||||||
}
|
|
||||||
|
|
||||||
posStatus := []struct {
|
|
||||||
pos int
|
|
||||||
value byte
|
|
||||||
mapper map[byte]string
|
|
||||||
}{
|
|
||||||
{pos: 1, value: statusBytes[0], mapper: statusPos0},
|
|
||||||
{pos: 2, value: statusBytes[1], mapper: statusPos1},
|
|
||||||
{pos: 3, value: statusBytes[2], mapper: statusPos2},
|
|
||||||
{pos: 4, value: statusBytes[3], mapper: statusPos3},
|
|
||||||
}
|
|
||||||
|
|
||||||
var result strings.Builder
|
|
||||||
for _, p := range posStatus {
|
|
||||||
statusMsg, exists := p.mapper[p.value]
|
|
||||||
if !exists {
|
|
||||||
statusMsg = fmt.Sprintf("Unknown status 0x%X at position %d", p.value, p.pos)
|
|
||||||
}
|
|
||||||
if p.value != 0x30 {
|
|
||||||
result.WriteString(statusMsg + "; ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func logStatus(statusBytes []byte) {
|
|
||||||
log.Infof("Dispenser status: %s", statusDescription(statusBytes))
|
|
||||||
}
|
|
||||||
|
|
||||||
func isAtEncoderPosition(statusBytes []byte) bool {
|
|
||||||
return len(statusBytes) >= 4 && statusBytes[3] == 0x33
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateDispenserStatusData(statusBytes []byte) error {
|
|
||||||
if len(statusBytes) != 4 {
|
|
||||||
return fmt.Errorf("malformed dispenser status: got %d bytes, want 4", len(statusBytes))
|
|
||||||
}
|
|
||||||
|
|
||||||
statusMaps := []map[byte]string{statusPos0, statusPos1, statusPos2, statusPos3}
|
|
||||||
for position, mapper := range statusMaps {
|
|
||||||
if _, ok := mapper[statusBytes[position]]; !ok {
|
|
||||||
return fmt.Errorf("unknown dispenser status 0x%X at position %d", statusBytes[position], position+1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func dispenserStatusError(statusBytes []byte) error {
|
|
||||||
switch statusBytes[0] {
|
|
||||||
case 0x34, 0x32, 0x36:
|
|
||||||
return fmt.Errorf("dispenser error: %s", statusPos0[statusBytes[0]])
|
|
||||||
}
|
|
||||||
switch statusBytes[1] {
|
|
||||||
case 0x32, 0x31:
|
|
||||||
return fmt.Errorf("dispenser error: %s", statusPos1[statusBytes[1]])
|
|
||||||
}
|
|
||||||
switch statusBytes[2] {
|
|
||||||
case 0x34, 0x32:
|
|
||||||
return fmt.Errorf("dispenser error: %s", statusPos2[statusBytes[2]])
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func isPreparationMoving(statusBytes []byte) bool {
|
|
||||||
return len(statusBytes) == 4 &&
|
|
||||||
(statusBytes[0] == 0x31 || statusBytes[1] == 0x38)
|
|
||||||
}
|
|
||||||
|
|
||||||
func hasPreparationDiagnostics(statusBytes []byte) bool {
|
|
||||||
return len(statusBytes) == 4 &&
|
|
||||||
(statusBytes[0] != 0x30 || statusBytes[1] != 0x30 || statusBytes[2] != 0x30)
|
|
||||||
}
|
|
||||||
|
|
||||||
func stockTake(statusBytes []byte) string {
|
|
||||||
if len(statusBytes) < 4 {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
status := ""
|
|
||||||
if statusBytes[0] == 0x32 || statusBytes[0] == 0x36 {
|
|
||||||
status = statusPos0[statusBytes[0]]
|
|
||||||
}
|
|
||||||
if statusBytes[2] != 0x30 {
|
|
||||||
status = statusPos2[statusBytes[2]]
|
|
||||||
}
|
|
||||||
if statusBytes[3] == 0x38 {
|
|
||||||
status = statusPos3[statusBytes[3]]
|
|
||||||
}
|
|
||||||
return status
|
|
||||||
}
|
|
||||||
|
|
||||||
func isCardWellEmpty(statusBytes []byte) bool {
|
|
||||||
return len(statusBytes) >= 4 && statusBytes[3] == 0x38
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkACK(statusResp []byte) error {
|
|
||||||
if len(statusResp) == 3 &&
|
|
||||||
statusResp[0] == ACK &&
|
|
||||||
len(Address) >= 2 &&
|
|
||||||
statusResp[1] == Address[0] &&
|
|
||||||
statusResp[2] == Address[1] {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if len(statusResp) > 0 && statusResp[0] == NAK {
|
|
||||||
return fmt.Errorf("negative response from dispenser")
|
|
||||||
}
|
|
||||||
return fmt.Errorf("unexpected response status: % X", statusResp)
|
|
||||||
}
|
|
||||||
|
|
||||||
// calculateBCC computes BCC as XOR of all bytes from STX to ETX.
|
|
||||||
func calculateBCC(data []byte) byte {
|
|
||||||
var bcc byte
|
|
||||||
for _, b := range data {
|
|
||||||
bcc ^= b
|
|
||||||
}
|
|
||||||
return bcc
|
|
||||||
}
|
|
||||||
|
|
||||||
func createPacket(address []byte, command []byte) []byte {
|
|
||||||
packet := []byte{STX}
|
|
||||||
packet = append(packet, address...)
|
|
||||||
packet = append(packet, space)
|
|
||||||
packet = append(packet, command...)
|
|
||||||
packet = append(packet, ETX)
|
|
||||||
bcc := calculateBCC(packet)
|
|
||||||
packet = append(packet, bcc)
|
|
||||||
return packet
|
|
||||||
}
|
|
||||||
|
|
||||||
func buildCheckAP(address []byte) []byte { return createPacket(address, []byte{STX, 0x41, 0x50}) }
|
|
||||||
|
|
||||||
func sendAndReceive(port *serial.Port, packet []byte, delay time.Duration) ([]byte, error) {
|
|
||||||
_, err := port.Write(packet)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error writing to port: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(delay)
|
|
||||||
|
|
||||||
buf := make([]byte, 128)
|
|
||||||
n, err := port.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error reading from port: %w", err)
|
|
||||||
}
|
|
||||||
return buf[:n], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------
|
|
||||||
// Serial init (3 attempts)
|
|
||||||
// --------------------
|
|
||||||
|
|
||||||
func InitializeDispenser() (*serial.Port, error) {
|
|
||||||
const (
|
|
||||||
funcName = "InitializeDispenser"
|
|
||||||
maxRetries = 3
|
|
||||||
retryDelay = 4 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
if SerialPort == "" {
|
|
||||||
return nil, fmt.Errorf("%s: SerialPort is empty", funcName)
|
|
||||||
}
|
|
||||||
if len(Address) < 2 {
|
|
||||||
return nil, fmt.Errorf("%s: Address must be at least 2 bytes", funcName)
|
|
||||||
}
|
|
||||||
|
|
||||||
serialConfig := &serial.Config{
|
|
||||||
Name: SerialPort,
|
|
||||||
Baud: baudRate,
|
|
||||||
ReadTimeout: 2 * time.Second,
|
|
||||||
}
|
|
||||||
|
|
||||||
var lastErr error
|
|
||||||
for attempt := 1; attempt <= maxRetries; attempt++ {
|
|
||||||
port, err := serial.OpenPort(serialConfig)
|
|
||||||
if err == nil {
|
|
||||||
log.Infof("%s: dispenser opened on %s (attempt %d/%d)", funcName, SerialPort, attempt, maxRetries)
|
|
||||||
return port, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
lastErr = err
|
|
||||||
log.Warnf("%s: failed to open dispenser on %s (attempt %d/%d): %v", funcName, SerialPort, attempt, maxRetries, err)
|
|
||||||
if attempt < maxRetries {
|
|
||||||
time.Sleep(retryDelay)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, fmt.Errorf("%s: failed to open dispenser on %s after %d attempts: %w", funcName, SerialPort, maxRetries, lastErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------
|
|
||||||
// Internal (port-owner only) operations
|
|
||||||
// --------------------
|
|
||||||
|
|
||||||
// checkDispenserStatus talks to the device and returns the 4 status bytes [pos0..pos3].
|
|
||||||
func checkDispenserStatus(port *serial.Port) ([]byte, error) {
|
|
||||||
checkCmd := buildCheckAP(Address)
|
|
||||||
enq := append([]byte{ENQ}, Address...)
|
|
||||||
|
|
||||||
statusResp, err := sendAndReceive(port, checkCmd, delay)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error sending check command: %w", err)
|
|
||||||
}
|
|
||||||
if len(statusResp) == 0 {
|
|
||||||
return nil, fmt.Errorf("no response from dispenser")
|
|
||||||
}
|
|
||||||
if err := checkACK(statusResp); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
statusResp, err = sendAndReceive(port, enq, delay)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error sending ENQ: %w", err)
|
|
||||||
}
|
|
||||||
if len(statusResp) < 13 {
|
|
||||||
return nil, fmt.Errorf("incomplete status response from dispenser: % X", statusResp)
|
|
||||||
}
|
|
||||||
return statusResp[7:11], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func cardToEncoderPosition(port *serial.Port) error {
|
|
||||||
enq := append([]byte{ENQ}, Address...)
|
|
||||||
|
|
||||||
dispenseCmd := createPacket(Address, commandFC7)
|
|
||||||
log.Println("Send card to encoder position")
|
|
||||||
|
|
||||||
statusResp, err := sendAndReceive(port, dispenseCmd, delay)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error sending card to encoder position: %w", err)
|
|
||||||
}
|
|
||||||
if err := checkACK(statusResp); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = port.Write(enq)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error sending ENQ to prompt device: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func cardOutOfMouth(port *serial.Port) error {
|
|
||||||
enq := append([]byte{ENQ}, Address...)
|
|
||||||
|
|
||||||
dispenseCmd := createPacket(Address, commandFC0)
|
|
||||||
log.Println("Send card to out mouth position")
|
|
||||||
|
|
||||||
statusResp, err := sendAndReceive(port, dispenseCmd, delay)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error sending out of mouth command: %w", err)
|
|
||||||
}
|
|
||||||
if err := checkACK(statusResp); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = port.Write(enq)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error sending ENQ to prompt device: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@ -1,448 +0,0 @@
|
|||||||
// Package dispenser provides a queue-based client (single owner of port).
|
|
||||||
package dispenser
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/tarm/serial"
|
|
||||||
)
|
|
||||||
|
|
||||||
type cmdType int
|
|
||||||
|
|
||||||
const (
|
|
||||||
cmdStatus cmdType = iota
|
|
||||||
cmdToEncoder
|
|
||||||
cmdOutOfMouth
|
|
||||||
)
|
|
||||||
|
|
||||||
type cmdReq struct {
|
|
||||||
typ cmdType
|
|
||||||
ctx context.Context
|
|
||||||
respCh chan cmdResp
|
|
||||||
}
|
|
||||||
|
|
||||||
type cmdResp struct {
|
|
||||||
status []byte
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
type sequenceTiming struct {
|
|
||||||
now func() time.Time
|
|
||||||
wait func(context.Context, time.Duration) error
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
sequencePollInterval = time.Second
|
|
||||||
sequenceRetryAfter = 6 * time.Second
|
|
||||||
sequenceTimeout = 12 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
type Client struct {
|
|
||||||
port *serial.Port
|
|
||||||
|
|
||||||
reqCh chan cmdReq
|
|
||||||
done chan struct{}
|
|
||||||
|
|
||||||
sequenceTiming sequenceTiming
|
|
||||||
|
|
||||||
// status cache
|
|
||||||
mu sync.RWMutex
|
|
||||||
lastStatus []byte
|
|
||||||
lastStatusT time.Time
|
|
||||||
statusTTL time.Duration
|
|
||||||
|
|
||||||
// published "stock/cardwell" cache + callback
|
|
||||||
lastStockMu sync.RWMutex
|
|
||||||
lastStock string
|
|
||||||
onStock func(string)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewClient starts the worker that owns the serial port.
|
|
||||||
func NewClient(port *serial.Port, queueSize int) *Client {
|
|
||||||
if queueSize <= 0 {
|
|
||||||
queueSize = 16
|
|
||||||
}
|
|
||||||
c := &Client{
|
|
||||||
port: port,
|
|
||||||
reqCh: make(chan cmdReq, queueSize),
|
|
||||||
done: make(chan struct{}),
|
|
||||||
sequenceTiming: sequenceTiming{
|
|
||||||
now: time.Now,
|
|
||||||
wait: waitForSequence,
|
|
||||||
},
|
|
||||||
statusTTL: defaultStatusTTL,
|
|
||||||
}
|
|
||||||
go c.loop()
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
func waitForSequence(ctx context.Context, duration time.Duration) error {
|
|
||||||
timer := time.NewTimer(duration)
|
|
||||||
defer timer.Stop()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-timer.C:
|
|
||||||
return nil
|
|
||||||
case <-ctx.Done():
|
|
||||||
return ctx.Err()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) Close() {
|
|
||||||
select {
|
|
||||||
case <-c.done:
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
close(c.done)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetStatusTTL sets the duration for which cached status is considered fresh.
|
|
||||||
func (c *Client) SetStatusTTL(d time.Duration) {
|
|
||||||
c.mu.Lock()
|
|
||||||
c.statusTTL = d
|
|
||||||
c.mu.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
// OnStockUpdate registers a callback called whenever polling (or status reads) produce a stock status string.
|
|
||||||
func (c *Client) OnStockUpdate(fn func(string)) {
|
|
||||||
c.lastStockMu.Lock()
|
|
||||||
c.onStock = fn
|
|
||||||
c.lastStockMu.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
// LastStock returns the most recently computed stock/card-well status string.
|
|
||||||
func (c *Client) LastStock() string {
|
|
||||||
c.lastStockMu.RLock()
|
|
||||||
defer c.lastStockMu.RUnlock()
|
|
||||||
return c.lastStock
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) setStock(statusBytes []byte) {
|
|
||||||
stock := stockTake(statusBytes)
|
|
||||||
|
|
||||||
c.lastStockMu.Lock()
|
|
||||||
c.lastStock = stock
|
|
||||||
fn := c.onStock
|
|
||||||
c.lastStockMu.Unlock()
|
|
||||||
|
|
||||||
// call outside lock
|
|
||||||
if fn != nil {
|
|
||||||
fn(stock)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// StartPolling performs a periodic status refresh.
|
|
||||||
// It will NOT interrupt commands: it enqueues only when queue is idle.
|
|
||||||
func (c *Client) StartPolling(interval time.Duration) {
|
|
||||||
if interval <= 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
t := time.NewTicker(interval)
|
|
||||||
defer t.Stop()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-c.done:
|
|
||||||
return
|
|
||||||
case <-t.C:
|
|
||||||
// enqueue only if idle to avoid delaying real commands
|
|
||||||
if len(c.reqCh) != 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
||||||
_, err := c.CheckStatus(ctx)
|
|
||||||
if err != nil {
|
|
||||||
log.Debugf("dispenser polling: %v", err)
|
|
||||||
}
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) loop() {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-c.done:
|
|
||||||
return
|
|
||||||
case req := <-c.reqCh:
|
|
||||||
c.handle(req)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) handle(req cmdReq) {
|
|
||||||
select {
|
|
||||||
case <-req.ctx.Done():
|
|
||||||
req.respCh <- cmdResp{err: req.ctx.Err()}
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
switch req.typ {
|
|
||||||
case cmdStatus:
|
|
||||||
st, err := checkDispenserStatus(c.port)
|
|
||||||
if err == nil && len(st) == 4 {
|
|
||||||
c.mu.Lock()
|
|
||||||
c.lastStatus = append([]byte(nil), st...)
|
|
||||||
c.lastStatusT = time.Now()
|
|
||||||
c.mu.Unlock()
|
|
||||||
|
|
||||||
// publish stock/cardwell
|
|
||||||
c.setStock(st)
|
|
||||||
}
|
|
||||||
req.respCh <- cmdResp{status: st, err: err}
|
|
||||||
|
|
||||||
case cmdToEncoder:
|
|
||||||
err := cardToEncoderPosition(c.port)
|
|
||||||
req.respCh <- cmdResp{err: err}
|
|
||||||
|
|
||||||
case cmdOutOfMouth:
|
|
||||||
err := cardOutOfMouth(c.port)
|
|
||||||
req.respCh <- cmdResp{err: err}
|
|
||||||
|
|
||||||
default:
|
|
||||||
req.respCh <- cmdResp{err: fmt.Errorf("unknown command")}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) do(ctx context.Context, typ cmdType) ([]byte, error) {
|
|
||||||
rch := make(chan cmdResp, 1)
|
|
||||||
req := cmdReq{typ: typ, ctx: ctx, respCh: rch}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case c.reqCh <- req:
|
|
||||||
case <-ctx.Done():
|
|
||||||
return nil, ctx.Err()
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case r := <-rch:
|
|
||||||
return r.status, r.err
|
|
||||||
case <-ctx.Done():
|
|
||||||
return nil, ctx.Err()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CheckStatus returns cached status if fresh, otherwise enqueues a device status read.
|
|
||||||
func (c *Client) CheckStatus(ctx context.Context) ([]byte, error) {
|
|
||||||
c.mu.RLock()
|
|
||||||
ttl := c.statusTTL
|
|
||||||
st := append([]byte(nil), c.lastStatus...)
|
|
||||||
ts := c.lastStatusT
|
|
||||||
c.mu.RUnlock()
|
|
||||||
|
|
||||||
if len(st) == 4 && time.Since(ts) <= ttl {
|
|
||||||
// even when returning cached, keep stock in sync
|
|
||||||
c.setStock(st)
|
|
||||||
return st, nil
|
|
||||||
}
|
|
||||||
return c.do(ctx, cmdStatus)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) ToEncoder(ctx context.Context) error {
|
|
||||||
_, err := c.do(ctx, cmdToEncoder)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) OutOfMouth(ctx context.Context) error {
|
|
||||||
_, err := c.do(ctx, cmdOutOfMouth)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------
|
|
||||||
// Public sequences updated to use Client (queue)
|
|
||||||
// --------------------
|
|
||||||
|
|
||||||
// DispenserPrepare checks status; if empty => ok; else ensure at encoder.
|
|
||||||
func (c *Client) DispenserPrepare(ctx context.Context) (string, error) {
|
|
||||||
const funcName = "DispenserPrepare"
|
|
||||||
stockStatus := ""
|
|
||||||
|
|
||||||
status, err := c.CheckStatus(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return stockStatus, fmt.Errorf("[%s] check status: %w", funcName, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
logStatus(status)
|
|
||||||
stockStatus = stockTake(status)
|
|
||||||
c.setStock(status)
|
|
||||||
|
|
||||||
if isCardWellEmpty(status) {
|
|
||||||
return stockStatus, nil
|
|
||||||
}
|
|
||||||
if isAtEncoderPosition(status) {
|
|
||||||
return stockStatus, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.ToEncoder(ctx); err != nil {
|
|
||||||
return stockStatus, fmt.Errorf("[%s] to encoder: %w", funcName, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(delay)
|
|
||||||
status, err = c.CheckStatus(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return stockStatus, fmt.Errorf("[%s] re-check status: %w", funcName, err)
|
|
||||||
}
|
|
||||||
logStatus(status)
|
|
||||||
stockStatus = stockTake(status)
|
|
||||||
c.setStock(status)
|
|
||||||
|
|
||||||
return stockStatus, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) readSequenceStatus(ctx context.Context, operation string) ([]byte, string, error) {
|
|
||||||
status, err := c.do(ctx, cmdStatus)
|
|
||||||
if err != nil {
|
|
||||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
|
||||||
return nil, "", fmt.Errorf("[%s] read status: %w", operation, ctxErr)
|
|
||||||
}
|
|
||||||
return nil, "", fmt.Errorf("[%s] read status: %w", operation, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
stockStatus := ""
|
|
||||||
if len(status) == 4 {
|
|
||||||
stockStatus = stockTake(status)
|
|
||||||
c.setStock(status)
|
|
||||||
logStatus(status)
|
|
||||||
}
|
|
||||||
return status, stockStatus, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func preparationStatus(operation string, status []byte) (bool, error) {
|
|
||||||
if len(status) != 4 {
|
|
||||||
return false, fmt.Errorf("[%s] %w", operation, validateDispenserStatusData(status))
|
|
||||||
}
|
|
||||||
if isAtEncoderPosition(status) {
|
|
||||||
if hasPreparationDiagnostics(status) {
|
|
||||||
log.Warnf(
|
|
||||||
"[%s] card confirmed at encoder with dispenser diagnostics: %s raw status: % X",
|
|
||||||
operation,
|
|
||||||
statusDescription(status),
|
|
||||||
status,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
if err := validateDispenserStatusData(status); err != nil {
|
|
||||||
return false, fmt.Errorf("[%s] %w", operation, err)
|
|
||||||
}
|
|
||||||
if isCardWellEmpty(status) {
|
|
||||||
return false, fmt.Errorf("[%s] %w", operation, ErrCardWellEmpty)
|
|
||||||
}
|
|
||||||
if isPreparationMoving(status) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
if err := dispenserStatusError(status); err != nil {
|
|
||||||
return false, fmt.Errorf("[%s] %w", operation, err)
|
|
||||||
}
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) pollForEncoderPosition(
|
|
||||||
ctx context.Context,
|
|
||||||
operation string,
|
|
||||||
retryCommand func(context.Context) error,
|
|
||||||
) (string, error) {
|
|
||||||
started := c.sequenceTiming.now()
|
|
||||||
halfway := started.Add(sequenceRetryAfter)
|
|
||||||
deadline := started.Add(sequenceTimeout)
|
|
||||||
retried := false
|
|
||||||
stockStatus := ""
|
|
||||||
|
|
||||||
for {
|
|
||||||
if err := ctx.Err(); err != nil {
|
|
||||||
return stockStatus, fmt.Errorf("[%s] %w", operation, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
now := c.sequenceTiming.now()
|
|
||||||
if !now.Before(deadline) {
|
|
||||||
return stockStatus, fmt.Errorf("[%s] timed out after %s", operation, sequenceTimeout)
|
|
||||||
}
|
|
||||||
|
|
||||||
status, currentStockStatus, err := c.readSequenceStatus(ctx, operation)
|
|
||||||
stockStatus = currentStockStatus
|
|
||||||
if err != nil {
|
|
||||||
return stockStatus, err
|
|
||||||
}
|
|
||||||
ready, err := preparationStatus(operation, status)
|
|
||||||
if err != nil {
|
|
||||||
return stockStatus, err
|
|
||||||
}
|
|
||||||
if ready {
|
|
||||||
return stockStatus, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
now = c.sequenceTiming.now()
|
|
||||||
if !now.Before(deadline) {
|
|
||||||
return stockStatus, fmt.Errorf("[%s] timed out after %s", operation, sequenceTimeout)
|
|
||||||
}
|
|
||||||
if retryCommand != nil && !retried && !now.Before(halfway) {
|
|
||||||
if err := retryCommand(ctx); err != nil {
|
|
||||||
return stockStatus, fmt.Errorf("[%s] retry command: %w", operation, err)
|
|
||||||
}
|
|
||||||
retried = true
|
|
||||||
}
|
|
||||||
|
|
||||||
wait := sequencePollInterval
|
|
||||||
if remaining := deadline.Sub(now); remaining < wait {
|
|
||||||
wait = remaining
|
|
||||||
}
|
|
||||||
if err := c.sequenceTiming.wait(ctx, wait); err != nil {
|
|
||||||
return stockStatus, fmt.Errorf("[%s] %w", operation, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) prepareCardAtEncoder(ctx context.Context, operation string) (string, error) {
|
|
||||||
status, stockStatus, err := c.readSequenceStatus(ctx, operation)
|
|
||||||
if err != nil {
|
|
||||||
return stockStatus, err
|
|
||||||
}
|
|
||||||
ready, err := preparationStatus(operation, status)
|
|
||||||
if err != nil {
|
|
||||||
return stockStatus, err
|
|
||||||
}
|
|
||||||
if ready {
|
|
||||||
return stockStatus, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.ToEncoder(ctx); err != nil {
|
|
||||||
return stockStatus, fmt.Errorf("[%s] to encoder: %w", operation, err)
|
|
||||||
}
|
|
||||||
return c.pollForEncoderPosition(ctx, operation, c.ToEncoder)
|
|
||||||
}
|
|
||||||
|
|
||||||
// PrepareCurrentCard authoritatively places the card to be encoded at the encoder.
|
|
||||||
func (c *Client) PrepareCurrentCard(ctx context.Context) (string, error) {
|
|
||||||
return c.prepareCardAtEncoder(ctx, "PrepareCurrentCard")
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeliverCurrentCard presents the encoded card and confirms command acceptance.
|
|
||||||
func (c *Client) DeliverCurrentCard(ctx context.Context) (string, error) {
|
|
||||||
const operation = "DeliverCurrentCard"
|
|
||||||
|
|
||||||
if err := c.OutOfMouth(ctx); err != nil {
|
|
||||||
return "", fmt.Errorf("[%s] out of mouth: %w", operation, err)
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// BeginPrepareNextCard starts moving the next card to the encoder without waiting for readiness.
|
|
||||||
func (c *Client) BeginPrepareNextCard(ctx context.Context) error {
|
|
||||||
if err := c.ToEncoder(ctx); err != nil {
|
|
||||||
return fmt.Errorf("[BeginPrepareNextCard] to encoder: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// PrepareNextCard places a new card at the encoder for a later issuance attempt.
|
|
||||||
func (c *Client) PrepareNextCard(ctx context.Context) (string, error) {
|
|
||||||
return c.prepareCardAtEncoder(ctx, "PrepareNextCard")
|
|
||||||
}
|
|
||||||
@ -1,404 +0,0 @@
|
|||||||
package dispenser
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
type fakeSequenceDevice struct {
|
|
||||||
statusResponses []cmdResp
|
|
||||||
commandErrors map[cmdType][]error
|
|
||||||
commands []cmdType
|
|
||||||
}
|
|
||||||
|
|
||||||
func newSequenceTestClient(t *testing.T, statusResponses ...cmdResp) (*Client, *fakeSequenceDevice) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
device := &fakeSequenceDevice{
|
|
||||||
statusResponses: append([]cmdResp(nil), statusResponses...),
|
|
||||||
commandErrors: make(map[cmdType][]error),
|
|
||||||
}
|
|
||||||
now := time.Date(2026, time.July, 23, 12, 0, 0, 0, time.UTC)
|
|
||||||
client := &Client{
|
|
||||||
reqCh: make(chan cmdReq),
|
|
||||||
done: make(chan struct{}),
|
|
||||||
sequenceTiming: sequenceTiming{
|
|
||||||
now: func() time.Time {
|
|
||||||
return now
|
|
||||||
},
|
|
||||||
wait: func(ctx context.Context, duration time.Duration) error {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return ctx.Err()
|
|
||||||
default:
|
|
||||||
now = now.Add(duration)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-client.done:
|
|
||||||
return
|
|
||||||
case request := <-client.reqCh:
|
|
||||||
device.commands = append(device.commands, request.typ)
|
|
||||||
if request.typ == cmdStatus {
|
|
||||||
if len(device.statusResponses) == 0 {
|
|
||||||
request.respCh <- cmdResp{err: errors.New("unexpected status read")}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
response := device.statusResponses[0]
|
|
||||||
device.statusResponses = device.statusResponses[1:]
|
|
||||||
request.respCh <- response
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
if queued := device.commandErrors[request.typ]; len(queued) > 0 {
|
|
||||||
err = queued[0]
|
|
||||||
device.commandErrors[request.typ] = queued[1:]
|
|
||||||
}
|
|
||||||
request.respCh <- cmdResp{err: err}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
t.Cleanup(client.Close)
|
|
||||||
|
|
||||||
return client, device
|
|
||||||
}
|
|
||||||
|
|
||||||
func status(position byte) []byte {
|
|
||||||
return []byte{0x30, 0x30, 0x30, position}
|
|
||||||
}
|
|
||||||
|
|
||||||
func commandCount(commands []cmdType, target cmdType) int {
|
|
||||||
count := 0
|
|
||||||
for _, command := range commands {
|
|
||||||
if command == target {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrepareCurrentCardAcceptsEncoderPositionWithStaleDiagnostics(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
status []byte
|
|
||||||
wantStock string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "dispense error and jam",
|
|
||||||
status: []byte{0x30, 0x32, 0x32, 0x33},
|
|
||||||
wantStock: "Card jammed",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "supply diagnostics",
|
|
||||||
status: []byte{0x32, 0x30, 0x31, 0x33},
|
|
||||||
wantStock: "Card pre-empty",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "combined stale jam and supply diagnostics",
|
|
||||||
status: []byte{0x30, 0x32, 0x33, 0x33},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
var logOutput bytes.Buffer
|
|
||||||
standardLogger := log.StandardLogger()
|
|
||||||
previousOutput := standardLogger.Out
|
|
||||||
standardLogger.SetOutput(&logOutput)
|
|
||||||
t.Cleanup(func() {
|
|
||||||
standardLogger.SetOutput(previousOutput)
|
|
||||||
})
|
|
||||||
|
|
||||||
client, device := newSequenceTestClient(t, cmdResp{status: test.status})
|
|
||||||
|
|
||||||
stock, err := client.PrepareCurrentCard(context.Background())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if stock != test.wantStock {
|
|
||||||
t.Fatalf("stock status = %q, want %q", stock, test.wantStock)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdToEncoder); got != 0 {
|
|
||||||
t.Fatalf("to-encoder commands = %d, want 0", got)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdStatus); got != 1 {
|
|
||||||
t.Fatalf("status reads = %d, want 1", got)
|
|
||||||
}
|
|
||||||
logged := logOutput.String()
|
|
||||||
if !strings.Contains(logged, "card confirmed at encoder") ||
|
|
||||||
!strings.Contains(logged, statusDescription(test.status)) ||
|
|
||||||
!strings.Contains(logged, "raw status") {
|
|
||||||
t.Fatalf("encoder diagnostic log = %q", logged)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrepareCurrentCardPollingAllowsTransientMovementWithStaleErrors(t *testing.T) {
|
|
||||||
client, device := newSequenceTestClient(t,
|
|
||||||
cmdResp{status: status(0x34)},
|
|
||||||
cmdResp{status: []byte{0x31, 0x38, 0x32, 0x30}},
|
|
||||||
cmdResp{status: []byte{0x30, 0x32, 0x32, 0x33}},
|
|
||||||
)
|
|
||||||
|
|
||||||
stock, err := client.PrepareCurrentCard(context.Background())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if stock != "Card jammed" {
|
|
||||||
t.Fatalf("stock status = %q, want Card jammed", stock)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdToEncoder); got != 1 {
|
|
||||||
t.Fatalf("to-encoder commands = %d, want 1", got)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdStatus); got != 3 {
|
|
||||||
t.Fatalf("status reads = %d, want 3", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrepareCurrentCardRejectsFailureWithoutEncoderOrMovement(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
response cmdResp
|
|
||||||
wantError string
|
|
||||||
wantStock string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "status read error",
|
|
||||||
response: cmdResp{err: errors.New("serial read failed")},
|
|
||||||
wantError: "serial read failed",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "malformed status",
|
|
||||||
response: cmdResp{status: []byte{0x30, 0x30, 0x31}},
|
|
||||||
wantError: "malformed dispenser status",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "unknown status",
|
|
||||||
response: cmdResp{status: []byte{0x30, 0x30, 0x30, 0x39}},
|
|
||||||
wantError: "unknown dispenser status",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "jammed preparation",
|
|
||||||
response: cmdResp{status: []byte{0x30, 0x30, 0x32, 0x34}},
|
|
||||||
wantError: "Card jammed",
|
|
||||||
wantStock: "Card jammed",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
client, _ := newSequenceTestClient(t, test.response)
|
|
||||||
|
|
||||||
stock, err := client.PrepareCurrentCard(context.Background())
|
|
||||||
if err == nil || !strings.Contains(err.Error(), test.wantError) {
|
|
||||||
t.Fatalf("error = %v, want containing %q", err, test.wantError)
|
|
||||||
}
|
|
||||||
if stock != test.wantStock {
|
|
||||||
t.Fatalf("stock status = %q, want %q", stock, test.wantStock)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrepareCurrentCardReturnsAuthoritativeEmptyWellError(t *testing.T) {
|
|
||||||
client, device := newSequenceTestClient(t, cmdResp{status: status(0x38)})
|
|
||||||
|
|
||||||
stock, err := client.PrepareCurrentCard(context.Background())
|
|
||||||
if !errors.Is(err, ErrCardWellEmpty) {
|
|
||||||
t.Fatalf("error = %v, want ErrCardWellEmpty", err)
|
|
||||||
}
|
|
||||||
if stock != "Card empty" {
|
|
||||||
t.Fatalf("stock status = %q, want Card empty", stock)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdToEncoder); got != 0 {
|
|
||||||
t.Fatalf("to-encoder commands = %d, want 0", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDeliverCurrentCardSucceedsAfterCommandAcceptanceWithoutStatusPolling(t *testing.T) {
|
|
||||||
staleStatus := []byte{0x30, 0x32, 0x32, 0x33}
|
|
||||||
client, device := newSequenceTestClient(t, cmdResp{status: staleStatus})
|
|
||||||
|
|
||||||
stock, err := client.DeliverCurrentCard(context.Background())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if stock != "" {
|
|
||||||
t.Fatalf("stock status = %q, want empty", stock)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdOutOfMouth); got != 1 {
|
|
||||||
t.Fatalf("out-of-mouth commands = %d, want 1", got)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdStatus); got != 0 {
|
|
||||||
t.Fatalf("status reads = %d, want 0", got)
|
|
||||||
}
|
|
||||||
if len(device.statusResponses) != 1 {
|
|
||||||
t.Fatalf("queued status responses = %d, want stale status unread", len(device.statusResponses))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDeliverCurrentCardRejectsCommandFailures(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
err error
|
|
||||||
}{
|
|
||||||
{name: "serial dispatch failure", err: errors.New("serial write failed")},
|
|
||||||
{name: "invalid acknowledgement", err: errors.New("unexpected response status")},
|
|
||||||
{name: "missing acknowledgement", err: errors.New("no response from dispenser")},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
client, device := newSequenceTestClient(t)
|
|
||||||
device.commandErrors[cmdOutOfMouth] = []error{test.err}
|
|
||||||
|
|
||||||
_, err := client.DeliverCurrentCard(context.Background())
|
|
||||||
if err == nil || !strings.Contains(err.Error(), test.err.Error()) {
|
|
||||||
t.Fatalf("error = %v, want containing %q", err, test.err)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdOutOfMouth); got != 1 {
|
|
||||||
t.Fatalf("out-of-mouth commands = %d, want 1", got)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdStatus); got != 0 {
|
|
||||||
t.Fatalf("status reads = %d, want 0", got)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDeliverCurrentCardPreservesContextCancellation(t *testing.T) {
|
|
||||||
client, _ := newSequenceTestClient(t)
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
cancel()
|
|
||||||
|
|
||||||
_, err := client.DeliverCurrentCard(ctx)
|
|
||||||
if !errors.Is(err, context.Canceled) {
|
|
||||||
t.Fatalf("error = %v, want context.Canceled", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrepareCurrentCardRetriesOnceThenTimesOut(t *testing.T) {
|
|
||||||
responses := make([]cmdResp, 13)
|
|
||||||
for index := range responses {
|
|
||||||
responses[index] = cmdResp{status: status(0x34)}
|
|
||||||
}
|
|
||||||
client, device := newSequenceTestClient(t, responses...)
|
|
||||||
|
|
||||||
_, err := client.PrepareCurrentCard(context.Background())
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "timed out") {
|
|
||||||
t.Fatalf("error = %v, want preparation timeout", err)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdToEncoder); got != 2 {
|
|
||||||
t.Fatalf("to-encoder commands = %d, want initial command plus one halfway retry", got)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdStatus); got != 13 {
|
|
||||||
t.Fatalf("status reads = %d, want 13", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrepareCurrentCardPropagatesContextCancellation(t *testing.T) {
|
|
||||||
client, _ := newSequenceTestClient(t,
|
|
||||||
cmdResp{status: status(0x34)},
|
|
||||||
cmdResp{status: status(0x34)},
|
|
||||||
)
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
client.sequenceTiming.wait = func(context.Context, time.Duration) error {
|
|
||||||
cancel()
|
|
||||||
return ctx.Err()
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := client.PrepareCurrentCard(ctx)
|
|
||||||
if !errors.Is(err, context.Canceled) {
|
|
||||||
t.Fatalf("error = %v, want context.Canceled", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBeginPrepareNextCardDispatchesWithoutReadinessPolling(t *testing.T) {
|
|
||||||
client, device := newSequenceTestClient(t)
|
|
||||||
|
|
||||||
if err := client.BeginPrepareNextCard(context.Background()); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdToEncoder); got != 1 {
|
|
||||||
t.Fatalf("to-encoder commands = %d, want 1", got)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdStatus); got != 0 {
|
|
||||||
t.Fatalf("status reads = %d, want 0", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBeginPrepareNextCardReturnsDispatchFailureWithoutPolling(t *testing.T) {
|
|
||||||
client, device := newSequenceTestClient(t)
|
|
||||||
device.commandErrors[cmdToEncoder] = []error{errors.New("dispatch failed")}
|
|
||||||
|
|
||||||
err := client.BeginPrepareNextCard(context.Background())
|
|
||||||
if err == nil || !strings.Contains(err.Error(), "dispatch failed") {
|
|
||||||
t.Fatalf("error = %v, want dispatch failure", err)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdToEncoder); got != 1 {
|
|
||||||
t.Fatalf("to-encoder commands = %d, want 1", got)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdStatus); got != 0 {
|
|
||||||
t.Fatalf("status reads = %d, want 0", got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrepareCardAtEncoderRequiresConfirmedEncoderState(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
responses []cmdResp
|
|
||||||
wantError string
|
|
||||||
wantToEncoder int
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "already at encoder",
|
|
||||||
responses: []cmdResp{{status: status(0x33)}},
|
|
||||||
wantToEncoder: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "moves ready card to encoder",
|
|
||||||
responses: []cmdResp{
|
|
||||||
{status: status(0x34)},
|
|
||||||
{status: status(0x33)},
|
|
||||||
},
|
|
||||||
wantToEncoder: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "empty card well",
|
|
||||||
responses: []cmdResp{{status: status(0x38)}},
|
|
||||||
wantError: CardWellEmptyMessage,
|
|
||||||
wantToEncoder: 0,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
client, device := newSequenceTestClient(t, test.responses...)
|
|
||||||
|
|
||||||
_, err := client.PrepareNextCard(context.Background())
|
|
||||||
if test.wantError == "" && err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if test.wantError != "" && (err == nil || !strings.Contains(err.Error(), test.wantError)) {
|
|
||||||
t.Fatalf("error = %v, want containing %q", err, test.wantError)
|
|
||||||
}
|
|
||||||
if got := commandCount(device.commands, cmdToEncoder); got != test.wantToEncoder {
|
|
||||||
t.Fatalf("to-encoder commands = %d, want %d", got, test.wantToEncoder)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,674 +0,0 @@
|
|||||||
package dojo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"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"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
BaseURL string
|
|
||||||
APIKey string
|
|
||||||
SoftwareHouseID string
|
|
||||||
Version string
|
|
||||||
TerminalID string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Client struct {
|
|
||||||
baseURL string
|
|
||||||
apiKey string
|
|
||||||
version string
|
|
||||||
softwareHouseID string
|
|
||||||
terminalID string
|
|
||||||
httpClient *http.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
dojoTerminalUnavailableStatus = "TERMINAL_UNAVAILABLE"
|
|
||||||
dojoTerminalUnavailableMessage = "Payment terminal is unavailable"
|
|
||||||
)
|
|
||||||
|
|
||||||
type httpResponseError struct {
|
|
||||||
StatusCode int
|
|
||||||
Body string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *httpResponseError) Error() string {
|
|
||||||
return fmt.Sprintf("Dojo returned HTTP %d: %s", e.StatusCode, e.Body)
|
|
||||||
}
|
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
if cfg.APIKey == "" {
|
|
||||||
return nil, fmt.Errorf("dojo api_key is required")
|
|
||||||
}
|
|
||||||
if cfg.Version == "" {
|
|
||||||
cfg.Version = "2026-02-27"
|
|
||||||
}
|
|
||||||
if cfg.SoftwareHouseID == "" {
|
|
||||||
return nil, fmt.Errorf("dojo software_house_id is required")
|
|
||||||
}
|
|
||||||
if cfg.TerminalID == "" {
|
|
||||||
return nil, fmt.Errorf("dojo terminal_id is required")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Client{
|
|
||||||
baseURL: strings.TrimRight(cfg.BaseURL, "/"),
|
|
||||||
apiKey: cfg.APIKey,
|
|
||||||
version: cfg.Version,
|
|
||||||
softwareHouseID: cfg.SoftwareHouseID,
|
|
||||||
terminalID: cfg.TerminalID,
|
|
||||||
httpClient: &http.Client{Timeout: 30 * time.Second},
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
session, err := c.createTerminalSession(ctx, intent.ID)
|
|
||||||
if err != nil {
|
|
||||||
var responseErr *httpResponseError
|
|
||||||
if errors.As(err, &responseErr) && responseErr.StatusCode == http.StatusConflict {
|
|
||||||
log.WithFields(log.Fields{
|
|
||||||
"provider": "Dojo",
|
|
||||||
"operation": "createTerminalSession",
|
|
||||||
"status": responseErr.StatusCode,
|
|
||||||
"terminal_unavailable": true,
|
|
||||||
}).Warn(dojoTerminalUnavailableMessage)
|
|
||||||
|
|
||||||
sendPaymentStatus(onStatus, paymentstatus.TerminalUnavailable)
|
|
||||||
result := c.baseResult(req, nil)
|
|
||||||
result.Status = dojoTerminalUnavailableStatus
|
|
||||||
result.ErrorMessage = dojoTerminalUnavailableMessage
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
session, err = c.waitForTerminalSession(ctx, session.ID, onStatus)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch strings.ToLower(session.Status) {
|
|
||||||
case types.ResultCaptured, types.ResultSignatureAccepted:
|
|
||||||
intent, err = c.getPaymentIntent(ctx, intent.ID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if strings.ToLower(intent.Status) != types.ResultCaptured {
|
|
||||||
return nil, fmt.Errorf("Dojo terminal session is %s but payment intent is %s", session.Status, intent.Status)
|
|
||||||
}
|
|
||||||
result := c.mapCapturedResult(req, intent)
|
|
||||||
result.CustomerReceipt = receiptToText(session.Receipt)
|
|
||||||
return result, nil
|
|
||||||
|
|
||||||
case types.ResultCancelled, types.ResultCanceled, types.ResultDeclined, types.ResultExpired, types.ResultSignatureRejected:
|
|
||||||
return c.mapTerminalFailure(req, session), nil
|
|
||||||
|
|
||||||
case types.ResultSignatureRequired:
|
|
||||||
result := c.baseResult(req, session.PaymentDetails)
|
|
||||||
result.Status = "SIGNATURE_VERIFICATION_REQUIRED"
|
|
||||||
result.ErrorMessage = "Dojo signature verification is required but is not supported"
|
|
||||||
return result, nil
|
|
||||||
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unexpected final Dojo terminal session status %q", session.Status)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) createPaymentIntent(ctx context.Context, req paymentsvc.SaleRequest) (*paymentIntentResponse, error) {
|
|
||||||
payload := createPaymentIntentRequest{
|
|
||||||
Amount: money{
|
|
||||||
Value: req.Amount,
|
|
||||||
CurrencyCode: req.Currency,
|
|
||||||
},
|
|
||||||
Reference: dojoReference(req),
|
|
||||||
CaptureMode: "Auto",
|
|
||||||
}
|
|
||||||
|
|
||||||
var response paymentIntentResponse
|
|
||||||
if err := c.doJSON(ctx, http.MethodPost, "/payment-intents", payload, false, &response); err != nil {
|
|
||||||
return nil, fmt.Errorf("create Dojo payment intent: %w", err)
|
|
||||||
}
|
|
||||||
if response.ID == "" {
|
|
||||||
return nil, fmt.Errorf("create Dojo payment intent: response did not contain id")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &response, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) createTerminalSession(ctx context.Context, paymentIntentID string) (*terminalSessionResponse, error) {
|
|
||||||
payload := createTerminalSessionRequest{
|
|
||||||
TerminalID: c.terminalID,
|
|
||||||
Details: terminalSessionDetails{
|
|
||||||
SessionType: "Sale",
|
|
||||||
Sale: terminalSessionSale{
|
|
||||||
PaymentIntentID: paymentIntentID,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
var response terminalSessionResponse
|
|
||||||
if err := c.doJSON(ctx, http.MethodPost, "/terminal-sessions", payload, true, &response); err != nil {
|
|
||||||
return nil, fmt.Errorf("create Dojo terminal session: %w", err)
|
|
||||||
}
|
|
||||||
if response.ID == "" {
|
|
||||||
return nil, fmt.Errorf("create Dojo terminal session: response did not contain id")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &response, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
session, err := c.getTerminalSession(ctx, terminalSessionID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("session status:", session.Status)
|
|
||||||
|
|
||||||
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 {
|
|
||||||
sendPaymentStatus(
|
|
||||||
onStatus,
|
|
||||||
paymentstatus.SignatureRejecting,
|
|
||||||
)
|
|
||||||
|
|
||||||
rejectedSession, err := c.rejectSignature(
|
|
||||||
ctx,
|
|
||||||
terminalSessionID,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
signatureRejected = true
|
|
||||||
|
|
||||||
if rejectedSession != nil {
|
|
||||||
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.terminalSessionResponse, nil
|
|
||||||
|
|
||||||
default:
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
|
|
||||||
var response terminalSessionResponse
|
|
||||||
path := "/terminal-sessions/" + url.PathEscape(terminalSessionID) + "/signature"
|
|
||||||
|
|
||||||
if err := c.doJSON(ctx, http.MethodPut, path, payload, true, &response); err != nil {
|
|
||||||
return nil, fmt.Errorf("reject Dojo signature verification: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &response, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
return &response, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) getPaymentIntent(ctx context.Context, paymentIntentID string) (*paymentIntentResponse, error) {
|
|
||||||
var response paymentIntentResponse
|
|
||||||
path := "/payment-intents/" + url.PathEscape(paymentIntentID) + "?returnCanceled=true"
|
|
||||||
if err := c.doJSON(ctx, http.MethodGet, path, nil, false, &response); err != nil {
|
|
||||||
return nil, fmt.Errorf("get Dojo payment intent: %w", err)
|
|
||||||
}
|
|
||||||
return &response, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) mapCapturedResult(req paymentsvc.SaleRequest, intent *paymentIntentResponse) *paymentsvc.Result {
|
|
||||||
result := c.baseResult(req, intent.PaymentDetails)
|
|
||||||
result.Success = true
|
|
||||||
result.Status = "APPROVED"
|
|
||||||
|
|
||||||
if intent.Amount.Value != 0 {
|
|
||||||
result.Amount = intent.Amount.Value
|
|
||||||
}
|
|
||||||
if intent.Amount.CurrencyCode != "" {
|
|
||||||
result.Currency = intent.Amount.CurrencyCode
|
|
||||||
}
|
|
||||||
if result.Message == "" {
|
|
||||||
result.Message = "Payment approved"
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) mapTerminalFailure(req paymentsvc.SaleRequest, session *terminalSessionResponse) *paymentsvc.Result {
|
|
||||||
result := c.baseResult(req, session.PaymentDetails)
|
|
||||||
result.Status = strings.ToUpper(session.Status)
|
|
||||||
result.ErrorMessage = "Dojo terminal session ended with status " + session.Status
|
|
||||||
result.CustomerReceipt = receiptToText(session.Receipt)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) baseResult(req paymentsvc.SaleRequest, details *paymentDetails) *paymentsvc.Result {
|
|
||||||
result := &paymentsvc.Result{
|
|
||||||
RequestID: req.RequestID,
|
|
||||||
Operation: "SALE",
|
|
||||||
Amount: req.Amount,
|
|
||||||
Currency: req.Currency,
|
|
||||||
DeviceUsed: c.terminalID,
|
|
||||||
DeviceType: "Dojo Terminal",
|
|
||||||
}
|
|
||||||
|
|
||||||
if details == nil {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
result.TransactionID = details.TransactionID
|
|
||||||
result.ReferenceNumber = req.RequestID
|
|
||||||
result.AuthCode = details.AuthCode
|
|
||||||
result.Message = details.Message
|
|
||||||
result.CardNumber = details.Card.CardNumber
|
|
||||||
result.CardType = details.Card.CardType
|
|
||||||
result.ExpiryDate = details.Card.ExpiryDate
|
|
||||||
result.LastFourDigits = details.Card.Last4PAN
|
|
||||||
if result.LastFourDigits == "" {
|
|
||||||
result.LastFourDigits = lastFour(details.Card.CardNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) doJSON(ctx context.Context, method, path string, payload any, terminalRequest bool, target any) error {
|
|
||||||
var body io.Reader
|
|
||||||
if payload != nil {
|
|
||||||
encoded, err := json.Marshal(payload)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("encode request: %w", err)
|
|
||||||
}
|
|
||||||
body = bytes.NewReader(encoded)
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, body)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("create request: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Set("Authorization", "Basic "+c.apiKey)
|
|
||||||
req.Header.Set("Version", c.version)
|
|
||||||
req.Header.Set("Accept", "application/json")
|
|
||||||
if payload != nil {
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
}
|
|
||||||
if terminalRequest {
|
|
||||||
req.Header.Set("software-house-id", c.softwareHouseID)
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := c.httpClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("send request: %w", err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
responseBody, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("read response: %w", err)
|
|
||||||
}
|
|
||||||
// log.Println("Dojo payment result raw:", string(responseBody))
|
|
||||||
if resp.StatusCode < http.StatusOK ||
|
|
||||||
resp.StatusCode >= http.StatusMultipleChoices {
|
|
||||||
return &httpResponseError{
|
|
||||||
StatusCode: resp.StatusCode,
|
|
||||||
Body: strings.TrimSpace(string(responseBody)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if target == nil || len(responseBody) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(responseBody, target); err != nil {
|
|
||||||
return fmt.Errorf("decode response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func dojoReference(req paymentsvc.SaleRequest) string {
|
|
||||||
reference := req.RequestID
|
|
||||||
if reference == "" {
|
|
||||||
reference = req.Reference
|
|
||||||
}
|
|
||||||
|
|
||||||
runes := []rune(reference)
|
|
||||||
if len(runes) > 60 {
|
|
||||||
runes = runes[:60]
|
|
||||||
}
|
|
||||||
return string(runes)
|
|
||||||
}
|
|
||||||
|
|
||||||
func receiptToText(receipt *receipt) string {
|
|
||||||
if receipt == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
var builder strings.Builder
|
|
||||||
|
|
||||||
for _, line := range receipt.Lines {
|
|
||||||
if line.LineType != "Text" || line.Text == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.WriteString(normalizeReceiptText(line.Text.Value))
|
|
||||||
builder.WriteByte('\n')
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func lastFour(cardNumber string) string {
|
|
||||||
digits := make([]byte, 0, len(cardNumber))
|
|
||||||
for i := 0; i < len(cardNumber); i++ {
|
|
||||||
if cardNumber[i] >= '0' && cardNumber[i] <= '9' {
|
|
||||||
digits = append(digits, cardNumber[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(digits) <= 4 {
|
|
||||||
return string(digits)
|
|
||||||
}
|
|
||||||
return string(digits[len(digits)-4:])
|
|
||||||
}
|
|
||||||
|
|
||||||
func normalizeReceiptText(value string) string {
|
|
||||||
replacer := strings.NewReplacer(
|
|
||||||
"£", "GBP ",
|
|
||||||
"€", "EUR ",
|
|
||||||
"$", "USD ",
|
|
||||||
)
|
|
||||||
|
|
||||||
return replacer.Replace(value)
|
|
||||||
}
|
|
||||||
@ -1,199 +0,0 @@
|
|||||||
package dojo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"sync/atomic"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/paymentsvc"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/paymentstatus"
|
|
||||||
)
|
|
||||||
|
|
||||||
const terminalUnavailableBody = `{"detail":"the terminal is either offline or currently in use","errors":{},"status":409,"title":"terminal unavailable","traceId":"trace-secret","type":"https://docs.dojo.tech/problems/terminal-unavailable"}`
|
|
||||||
|
|
||||||
func TestDoJSONReturnsStructuredHTTPResponseError(t *testing.T) {
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusConflict)
|
|
||||||
_, _ = w.Write([]byte(" " + terminalUnavailableBody + " "))
|
|
||||||
}))
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
client := newDojoTestClient(t, server)
|
|
||||||
err := client.doJSON(context.Background(), http.MethodPost, "/payment-intents", nil, false, nil)
|
|
||||||
|
|
||||||
var responseErr *httpResponseError
|
|
||||||
if !errors.As(err, &responseErr) {
|
|
||||||
t.Fatalf("error = %T %v, want *httpResponseError", err, err)
|
|
||||||
}
|
|
||||||
if responseErr.StatusCode != http.StatusConflict {
|
|
||||||
t.Fatalf("StatusCode = %d, want %d", responseErr.StatusCode, http.StatusConflict)
|
|
||||||
}
|
|
||||||
if responseErr.Body != terminalUnavailableBody {
|
|
||||||
t.Fatalf("Body = %q, want retained trimmed response", responseErr.Body)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSaleMapsCreateTerminalSessionConflictToTerminalUnavailable(t *testing.T) {
|
|
||||||
var paymentIntentRequests atomic.Int32
|
|
||||||
var terminalSessionRequests atomic.Int32
|
|
||||||
var unexpectedRequests atomic.Int32
|
|
||||||
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
switch {
|
|
||||||
case r.Method == http.MethodPost && r.URL.Path == "/payment-intents":
|
|
||||||
paymentIntentRequests.Add(1)
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
_, _ = fmt.Fprintln(w, `{"id":"intent-123"}`)
|
|
||||||
case r.Method == http.MethodPost && r.URL.Path == "/terminal-sessions":
|
|
||||||
terminalSessionRequests.Add(1)
|
|
||||||
w.Header().Set("Content-Type", "application/problem+json")
|
|
||||||
w.WriteHeader(http.StatusConflict)
|
|
||||||
_, _ = fmt.Fprintln(w, terminalUnavailableBody)
|
|
||||||
default:
|
|
||||||
unexpectedRequests.Add(1)
|
|
||||||
http.Error(w, "unexpected request", http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
client := newDojoTestClient(t, server)
|
|
||||||
request := paymentsvc.SaleRequest{
|
|
||||||
RequestID: "REQ-123",
|
|
||||||
Reference: "BOOKING-123",
|
|
||||||
Amount: 10852,
|
|
||||||
Currency: "GBP",
|
|
||||||
}
|
|
||||||
var statuses []string
|
|
||||||
result, err := client.Sale(context.Background(), request, func(update paymentsvc.StatusUpdate) {
|
|
||||||
statuses = append(statuses, update.Code)
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if result == nil {
|
|
||||||
t.Fatal("Sale returned nil result")
|
|
||||||
}
|
|
||||||
if result.Success || result.Status == "APPROVED" {
|
|
||||||
t.Fatalf("result was accidentally successful: %+v", result)
|
|
||||||
}
|
|
||||||
if result.Status != dojoTerminalUnavailableStatus || result.ErrorMessage != dojoTerminalUnavailableMessage {
|
|
||||||
t.Fatalf("terminal-unavailable result = %+v", result)
|
|
||||||
}
|
|
||||||
if result.RequestID != request.RequestID ||
|
|
||||||
result.Operation != "SALE" ||
|
|
||||||
result.Amount != request.Amount ||
|
|
||||||
result.Currency != request.Currency ||
|
|
||||||
result.DeviceUsed != "terminal-1" ||
|
|
||||||
result.DeviceType != "Dojo Terminal" {
|
|
||||||
t.Fatalf("base result fields were not retained: %+v", result)
|
|
||||||
}
|
|
||||||
if result.TransactionID != "" || result.CustomerReceipt != "" || result.MerchantReceipt != "" {
|
|
||||||
t.Fatalf("terminal-unavailable result invented transaction data: %+v", result)
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(statuses, []string{paymentstatus.Starting, paymentstatus.TerminalUnavailable}) {
|
|
||||||
t.Fatalf("statuses = %#v", statuses)
|
|
||||||
}
|
|
||||||
if paymentIntentRequests.Load() != 1 || terminalSessionRequests.Load() != 1 || unexpectedRequests.Load() != 0 {
|
|
||||||
t.Fatalf(
|
|
||||||
"requests: intent=%d terminal=%d unexpected=%d",
|
|
||||||
paymentIntentRequests.Load(),
|
|
||||||
terminalSessionRequests.Load(),
|
|
||||||
unexpectedRequests.Load(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
resultText := fmt.Sprintf("%+v", result)
|
|
||||||
for _, forbidden := range []string{"offline or currently in use", "trace-secret", "docs.dojo.tech", terminalUnavailableBody} {
|
|
||||||
if strings.Contains(resultText, forbidden) {
|
|
||||||
t.Fatalf("result exposed %q: %s", forbidden, resultText)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSaleDoesNotClassifyPaymentIntentConflictAsTerminalUnavailable(t *testing.T) {
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.URL.Path != "/payment-intents" {
|
|
||||||
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
|
|
||||||
}
|
|
||||||
w.WriteHeader(http.StatusConflict)
|
|
||||||
_, _ = fmt.Fprintln(w, terminalUnavailableBody)
|
|
||||||
}))
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
client := newDojoTestClient(t, server)
|
|
||||||
var statuses []string
|
|
||||||
result, err := client.Sale(context.Background(), paymentsvc.SaleRequest{
|
|
||||||
RequestID: "REQ-123",
|
|
||||||
Amount: 10852,
|
|
||||||
Currency: "GBP",
|
|
||||||
}, func(update paymentsvc.StatusUpdate) {
|
|
||||||
statuses = append(statuses, update.Code)
|
|
||||||
})
|
|
||||||
|
|
||||||
if result != nil || err == nil {
|
|
||||||
t.Fatalf("Sale result/error = %+v/%v, want nil generic error", result, err)
|
|
||||||
}
|
|
||||||
var responseErr *httpResponseError
|
|
||||||
if !errors.As(err, &responseErr) || responseErr.StatusCode != http.StatusConflict {
|
|
||||||
t.Fatalf("error = %T %v, want wrapped HTTP 409", err, err)
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(statuses, []string{paymentstatus.Starting}) {
|
|
||||||
t.Fatalf("statuses = %#v, want only PAYMENT_STARTING", statuses)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSaleRetainsGenericCreateTerminalSessionErrors(t *testing.T) {
|
|
||||||
for _, statusCode := range []int{http.StatusBadRequest, http.StatusBadGateway} {
|
|
||||||
t.Run(http.StatusText(statusCode), func(t *testing.T) {
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
switch r.URL.Path {
|
|
||||||
case "/payment-intents":
|
|
||||||
_, _ = fmt.Fprintln(w, `{"id":"intent-123"}`)
|
|
||||||
case "/terminal-sessions":
|
|
||||||
w.WriteHeader(statusCode)
|
|
||||||
_, _ = fmt.Fprintln(w, `{"detail":"provider failure"}`)
|
|
||||||
default:
|
|
||||||
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
client := newDojoTestClient(t, server)
|
|
||||||
result, err := client.Sale(context.Background(), paymentsvc.SaleRequest{
|
|
||||||
RequestID: "REQ-123",
|
|
||||||
Amount: 10852,
|
|
||||||
Currency: "GBP",
|
|
||||||
}, nil)
|
|
||||||
|
|
||||||
if result != nil || err == nil {
|
|
||||||
t.Fatalf("Sale result/error = %+v/%v, want nil generic error", result, err)
|
|
||||||
}
|
|
||||||
var responseErr *httpResponseError
|
|
||||||
if !errors.As(err, &responseErr) || responseErr.StatusCode != statusCode {
|
|
||||||
t.Fatalf("error = %T %v, want wrapped HTTP %d", err, err, statusCode)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newDojoTestClient(t *testing.T, server *httptest.Server) *Client {
|
|
||||||
t.Helper()
|
|
||||||
client, err := NewClient(Config{
|
|
||||||
BaseURL: server.URL,
|
|
||||||
APIKey: "test-api-key",
|
|
||||||
SoftwareHouseID: "software-house-1",
|
|
||||||
TerminalID: "terminal-1",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
client.httpClient = server.Client()
|
|
||||||
return client
|
|
||||||
}
|
|
||||||
@ -1,81 +0,0 @@
|
|||||||
package dojo
|
|
||||||
|
|
||||||
type money struct {
|
|
||||||
Value int64 `json:"value"`
|
|
||||||
CurrencyCode string `json:"currencyCode"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type createPaymentIntentRequest struct {
|
|
||||||
Amount money `json:"amount"`
|
|
||||||
Reference string `json:"reference"`
|
|
||||||
CaptureMode string `json:"captureMode"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type createTerminalSessionRequest struct {
|
|
||||||
TerminalID string `json:"terminalId"`
|
|
||||||
Details terminalSessionDetails `json:"details"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type terminalSessionDetails struct {
|
|
||||||
SessionType string `json:"sessionType"`
|
|
||||||
Sale terminalSessionSale `json:"sale"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type terminalSessionSale struct {
|
|
||||||
PaymentIntentID string `json:"paymentIntentId"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type signatureVerificationRequest struct {
|
|
||||||
Accepted bool `json:"accepted"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type terminalSessionResponse struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
TerminalID string `json:"terminalId"`
|
|
||||||
PaymentDetails *paymentDetails `json:"paymentDetails"`
|
|
||||||
Receipt *receipt `json:"receipt,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type receipt struct {
|
|
||||||
Lines []receiptLine `json:"lines"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type receiptLine struct {
|
|
||||||
LineType string `json:"lineType"`
|
|
||||||
Text *receiptText `json:"text,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type receiptText struct {
|
|
||||||
Align string `json:"align"`
|
|
||||||
EmphasisBold bool `json:"emphasisBold"`
|
|
||||||
Size string `json:"size"`
|
|
||||||
Value string `json:"value"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type paymentIntentResponse struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
Reference string `json:"reference"`
|
|
||||||
Amount money `json:"amount"`
|
|
||||||
PaymentDetails *paymentDetails `json:"paymentDetails"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type paymentDetails struct {
|
|
||||||
TransactionID string `json:"transactionId"`
|
|
||||||
TransactionDateTime string `json:"transactionDateTime"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
AuthCode string `json:"authCode"`
|
|
||||||
Card dojoCard `json:"card"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type dojoCard struct {
|
|
||||||
CardNumber string `json:"cardNumber"`
|
|
||||||
CardName string `json:"cardName"`
|
|
||||||
ExpiryDate string `json:"expiryDate"`
|
|
||||||
CardType string `json:"cardType"`
|
|
||||||
CardFundingType string `json:"cardFundingType"`
|
|
||||||
Last4PAN string `json:"last4PAN"`
|
|
||||||
EntryMode string `json:"entryMode"`
|
|
||||||
VerificationMethod string `json:"verificationMethod"`
|
|
||||||
}
|
|
||||||
@ -1,206 +0,0 @@
|
|||||||
package handlers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"context"
|
|
||||||
"database/sql"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/db"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
type preauthSpoolRecord struct {
|
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
|
||||||
CheckoutDate string `json:"checkoutDate"` // keep as received
|
|
||||||
Fields map[string]string `json:"fields"` // ChipDNA result.Fields
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) getDB(ctx context.Context) (*sql.DB, error) {
|
|
||||||
app.dbMu.Lock()
|
|
||||||
defer app.dbMu.Unlock()
|
|
||||||
|
|
||||||
// Fast path: db exists and is alive
|
|
||||||
if app.db != nil {
|
|
||||||
pingCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
if err := app.db.PingContext(pingCtx); err == nil {
|
|
||||||
return app.db, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// stale handle
|
|
||||||
_ = app.db.Close()
|
|
||||||
app.db = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reconnect once, bounded
|
|
||||||
dialCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
dbConn, err := db.InitMSSQL(
|
|
||||||
app.cfg.Dbport,
|
|
||||||
app.cfg.Dbuser,
|
|
||||||
app.cfg.Dbpassword,
|
|
||||||
app.cfg.Dbname,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
pingCtx, cancel2 := context.WithTimeout(dialCtx, 1*time.Second)
|
|
||||||
defer cancel2()
|
|
||||||
|
|
||||||
if err := dbConn.PingContext(pingCtx); err != nil {
|
|
||||||
_ = dbConn.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
app.db = dbConn
|
|
||||||
return app.db, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) spoolPath() string {
|
|
||||||
// keep it near logs; adjust if you prefer a dedicated dir
|
|
||||||
// ensure LogDir ends with separator in your config loader
|
|
||||||
return filepath.Join(app.cfg.LogDir, "preauth_spool.ndjson")
|
|
||||||
}
|
|
||||||
|
|
||||||
// persistPreauth tries DB first; if DB is down or insert fails, it spools to file.
|
|
||||||
// It never returns an error to the caller (so your HTTP flow stays simple),
|
|
||||||
// but it logs failures.
|
|
||||||
func (app *App) persistPreauth(ctx context.Context, fields map[string]string, checkoutDate string) {
|
|
||||||
// First, try DB (with your reconnect logic inside getDB)
|
|
||||||
dbConn, err := app.getDB(ctx)
|
|
||||||
if err == nil && dbConn != nil {
|
|
||||||
if err := db.InsertPreauth(ctx, dbConn, fields, checkoutDate); err == nil {
|
|
||||||
// opportunistic drain once DB is alive
|
|
||||||
go app.drainPreauthSpool(context.Background())
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
log.WithError(err).Warn("DB insert failed; will spool preauth")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.WithError(err).Warn("DB unavailable; will spool preauth")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback: spool to file
|
|
||||||
rec := preauthSpoolRecord{
|
|
||||||
CreatedAt: time.Now().UTC(),
|
|
||||||
CheckoutDate: checkoutDate,
|
|
||||||
Fields: fields,
|
|
||||||
}
|
|
||||||
if spErr := app.spoolPreauth(rec); spErr != nil {
|
|
||||||
log.WithError(spErr).Error("failed to spool preauth")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// append one line JSON (NDJSON)
|
|
||||||
func (app *App) spoolPreauth(rec preauthSpoolRecord) error {
|
|
||||||
p := app.spoolPath()
|
|
||||||
|
|
||||||
f, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("open spool file: %w", err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
b, err := json.Marshal(rec)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("marshal spool record: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := f.Write(append(b, '\n')); err != nil {
|
|
||||||
return fmt.Errorf("write spool record: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return f.Sync() // ensure it's on disk
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drain spool into DB.
|
|
||||||
// Strategy: read all lines, insert each; keep failures in a temp file; then replace original.
|
|
||||||
func (app *App) drainPreauthSpool(ctx context.Context) {
|
|
||||||
dbConn, err := app.getDB(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return // still down, nothing to do
|
|
||||||
}
|
|
||||||
|
|
||||||
spool := app.spoolPath()
|
|
||||||
in, err := os.Open(spool)
|
|
||||||
if err != nil {
|
|
||||||
// no spool is fine
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer in.Close()
|
|
||||||
|
|
||||||
tmp := spool + ".tmp"
|
|
||||||
out, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
|
|
||||||
if err != nil {
|
|
||||||
log.WithError(err).Warn("drain spool: open tmp failed")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer out.Close()
|
|
||||||
|
|
||||||
sc := bufio.NewScanner(in)
|
|
||||||
// allow long lines if receipts ever sneak in (shouldn't, but safe)
|
|
||||||
buf := make([]byte, 0, 64*1024)
|
|
||||||
sc.Buffer(buf, 2*1024*1024)
|
|
||||||
|
|
||||||
var (
|
|
||||||
okCount int
|
|
||||||
failCount int
|
|
||||||
)
|
|
||||||
|
|
||||||
for sc.Scan() {
|
|
||||||
line := sc.Bytes()
|
|
||||||
if len(line) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var rec preauthSpoolRecord
|
|
||||||
if err := json.Unmarshal(line, &rec); err != nil {
|
|
||||||
// malformed line: keep it so we don't lose evidence
|
|
||||||
_, _ = out.Write(append(line, '\n'))
|
|
||||||
failCount++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// attempt insert
|
|
||||||
if err := db.InsertPreauth(ctx, dbConn, rec.Fields, rec.CheckoutDate); err != nil {
|
|
||||||
// DB still flaky or data issue: keep it for later retry
|
|
||||||
_, _ = out.Write(append(line, '\n'))
|
|
||||||
failCount++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
okCount++
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := sc.Err(); err != nil {
|
|
||||||
log.WithError(err).Warn("drain spool: scanner error")
|
|
||||||
// best effort; do not replace spool
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = out.Sync()
|
|
||||||
|
|
||||||
// Replace original spool with temp (atomic on Windows is best-effort; still OK here)
|
|
||||||
_ = in.Close()
|
|
||||||
_ = out.Close()
|
|
||||||
|
|
||||||
if err := os.Rename(tmp, spool); err != nil {
|
|
||||||
log.WithError(err).Warn("drain spool: rename failed")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if okCount > 0 || failCount > 0 {
|
|
||||||
log.WithFields(log.Fields{
|
|
||||||
"inserted": okCount,
|
|
||||||
"remaining": failCount,
|
|
||||||
}).Info("preauth spool drained")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,313 +0,0 @@
|
|||||||
package handlers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/config"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/dispenser"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/lockserver"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
type dispenserCallResult struct {
|
|
||||||
status string
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
type fakeDoorCardDispenser struct {
|
|
||||||
prepareCurrent dispenserCallResult
|
|
||||||
deliverCurrent dispenserCallResult
|
|
||||||
beginNextErr error
|
|
||||||
beginNext func() error
|
|
||||||
prepareNext dispenserCallResult
|
|
||||||
calls []string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *fakeDoorCardDispenser) PrepareCurrentCard(context.Context) (string, error) {
|
|
||||||
d.calls = append(d.calls, "prepare current")
|
|
||||||
return d.prepareCurrent.status, d.prepareCurrent.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *fakeDoorCardDispenser) DeliverCurrentCard(context.Context) (string, error) {
|
|
||||||
d.calls = append(d.calls, "deliver current")
|
|
||||||
return d.deliverCurrent.status, d.deliverCurrent.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *fakeDoorCardDispenser) BeginPrepareNextCard(context.Context) error {
|
|
||||||
d.calls = append(d.calls, "begin prepare next")
|
|
||||||
if d.beginNext != nil {
|
|
||||||
return d.beginNext()
|
|
||||||
}
|
|
||||||
return d.beginNextErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *fakeDoorCardDispenser) PrepareNextCard(context.Context) (string, error) {
|
|
||||||
d.calls = append(d.calls, "prepare next")
|
|
||||||
return d.prepareNext.status, d.prepareNext.err
|
|
||||||
}
|
|
||||||
|
|
||||||
type fakeDoorCardLockServer struct {
|
|
||||||
sequenceErr error
|
|
||||||
buildCalls int
|
|
||||||
sequenceCalls int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *fakeDoorCardLockServer) BuildCommand(lockserver.DoorCardRequest, time.Time, time.Time) error {
|
|
||||||
l.buildCalls++
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *fakeDoorCardLockServer) LockSequence() error {
|
|
||||||
l.sequenceCalls++
|
|
||||||
return l.sequenceErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func performIssueDoorCardRequest(
|
|
||||||
t *testing.T,
|
|
||||||
dispenser *fakeDoorCardDispenser,
|
|
||||||
lock *fakeDoorCardLockServer,
|
|
||||||
) (*httptest.ResponseRecorder, cmstypes.StatusRec, *App) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
payload := lockserver.DoorCardRequest{
|
|
||||||
RoomField: "101",
|
|
||||||
CheckinTime: "2026-07-23 15:00:00 +0100",
|
|
||||||
CheckoutTime: "2026-07-24 11:00:00 +0100",
|
|
||||||
FollowStr: "0",
|
|
||||||
}
|
|
||||||
body, err := json.Marshal(payload)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
app := &App{
|
|
||||||
disp: dispenser,
|
|
||||||
lockserver: lock,
|
|
||||||
cfg: &config.ConfigRec{},
|
|
||||||
}
|
|
||||||
request := httptest.NewRequest(http.MethodPost, "/issuedoorcard", bytes.NewReader(body))
|
|
||||||
request.Header.Set("Content-Type", "application/json")
|
|
||||||
recorder := httptest.NewRecorder()
|
|
||||||
|
|
||||||
app.issueDoorCard(recorder, request)
|
|
||||||
|
|
||||||
var response cmstypes.StatusRec
|
|
||||||
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
|
||||||
t.Fatalf("decode response %q: %v", recorder.Body.String(), err)
|
|
||||||
}
|
|
||||||
return recorder, response, app
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIssueDoorCardPhysicalOutcomeContract(t *testing.T) {
|
|
||||||
encodingErr := errors.New("key encoding failed")
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
dispenser fakeDoorCardDispenser
|
|
||||||
lockErr error
|
|
||||||
wantHTTP int
|
|
||||||
wantMessage string
|
|
||||||
wantCalls []string
|
|
||||||
wantLockSequence int
|
|
||||||
wantCardWell string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "initial dispenser preparation failure is unavailable",
|
|
||||||
dispenser: fakeDoorCardDispenser{
|
|
||||||
prepareCurrent: dispenserCallResult{status: "Card jammed", err: errors.New("card jammed")},
|
|
||||||
},
|
|
||||||
wantHTTP: http.StatusServiceUnavailable,
|
|
||||||
wantMessage: "Dispense error: card jammed",
|
|
||||||
wantCalls: []string{"prepare current"},
|
|
||||||
wantCardWell: "Card jammed",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "initial empty card well has stable message",
|
|
||||||
dispenser: fakeDoorCardDispenser{
|
|
||||||
prepareCurrent: dispenserCallResult{status: "Card empty", err: dispenser.ErrCardWellEmpty},
|
|
||||||
},
|
|
||||||
wantHTTP: http.StatusServiceUnavailable,
|
|
||||||
wantMessage: dispenser.CardWellEmptyMessage,
|
|
||||||
wantCalls: []string{"prepare current"},
|
|
||||||
wantCardWell: "Card empty",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "encoding and accepted delivery command succeed",
|
|
||||||
wantHTTP: http.StatusOK,
|
|
||||||
wantMessage: "Card issued successfully",
|
|
||||||
wantCalls: []string{"prepare current", "deliver current", "begin prepare next"},
|
|
||||||
wantLockSequence: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "successful encoding with delivery command failure is unavailable",
|
|
||||||
dispenser: fakeDoorCardDispenser{
|
|
||||||
deliverCurrent: dispenserCallResult{status: "Card jammed", err: errors.New("delivery jammed")},
|
|
||||||
},
|
|
||||||
wantHTTP: http.StatusServiceUnavailable,
|
|
||||||
wantMessage: "Card delivery could not be confirmed",
|
|
||||||
wantCalls: []string{"prepare current", "deliver current"},
|
|
||||||
wantLockSequence: 1,
|
|
||||||
wantCardWell: "Card jammed",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "failed next-card dispatch does not undo delivered card",
|
|
||||||
dispenser: fakeDoorCardDispenser{
|
|
||||||
beginNextErr: errors.New("dispatch failed"),
|
|
||||||
},
|
|
||||||
wantHTTP: http.StatusOK,
|
|
||||||
wantMessage: "Card issued successfully",
|
|
||||||
wantCalls: []string{"prepare current", "deliver current", "begin prepare next"},
|
|
||||||
wantLockSequence: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "encoding failure with safe recovery remains retryable",
|
|
||||||
lockErr: encodingErr,
|
|
||||||
wantHTTP: http.StatusBadGateway,
|
|
||||||
wantMessage: encodingErr.Error(),
|
|
||||||
wantCalls: []string{"prepare current", "deliver current", "prepare next"},
|
|
||||||
wantLockSequence: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "encoding failure with failed-card delivery failure is unavailable",
|
|
||||||
dispenser: fakeDoorCardDispenser{
|
|
||||||
deliverCurrent: dispenserCallResult{status: "Card jammed", err: errors.New("delivery jammed")},
|
|
||||||
},
|
|
||||||
lockErr: encodingErr,
|
|
||||||
wantHTTP: http.StatusServiceUnavailable,
|
|
||||||
wantMessage: "Dispenser recovery failed; another encoding attempt is not safe",
|
|
||||||
wantCalls: []string{"prepare current", "deliver current"},
|
|
||||||
wantLockSequence: 1,
|
|
||||||
wantCardWell: "Card jammed",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "encoding failure with empty card well has stable message",
|
|
||||||
dispenser: fakeDoorCardDispenser{
|
|
||||||
prepareNext: dispenserCallResult{status: "Card empty", err: dispenser.ErrCardWellEmpty},
|
|
||||||
},
|
|
||||||
lockErr: encodingErr,
|
|
||||||
wantHTTP: http.StatusServiceUnavailable,
|
|
||||||
wantMessage: dispenser.CardWellEmptyMessage,
|
|
||||||
wantCalls: []string{"prepare current", "deliver current", "prepare next"},
|
|
||||||
wantLockSequence: 1,
|
|
||||||
wantCardWell: "Card empty",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
dispenser := test.dispenser
|
|
||||||
lock := &fakeDoorCardLockServer{sequenceErr: test.lockErr}
|
|
||||||
|
|
||||||
recorder, response, app := performIssueDoorCardRequest(t, &dispenser, lock)
|
|
||||||
|
|
||||||
if recorder.Code != test.wantHTTP {
|
|
||||||
t.Fatalf("HTTP status = %d, want %d", recorder.Code, test.wantHTTP)
|
|
||||||
}
|
|
||||||
if response.Code != test.wantHTTP {
|
|
||||||
t.Fatalf("response code = %d, want %d", response.Code, test.wantHTTP)
|
|
||||||
}
|
|
||||||
if response.Message != test.wantMessage {
|
|
||||||
t.Fatalf("response message = %q, want %q", response.Message, test.wantMessage)
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(dispenser.calls, test.wantCalls) {
|
|
||||||
t.Fatalf("dispenser calls = %#v, want %#v", dispenser.calls, test.wantCalls)
|
|
||||||
}
|
|
||||||
if lock.sequenceCalls != test.wantLockSequence {
|
|
||||||
t.Fatalf("lock sequence calls = %d, want %d", lock.sequenceCalls, test.wantLockSequence)
|
|
||||||
}
|
|
||||||
if test.wantLockSequence > 0 && lock.buildCalls != 1 {
|
|
||||||
t.Fatalf("build command calls = %d, want 1", lock.buildCalls)
|
|
||||||
}
|
|
||||||
if test.wantLockSequence == 0 && lock.buildCalls != 0 {
|
|
||||||
t.Fatalf("build command calls = %d, want 0", lock.buildCalls)
|
|
||||||
}
|
|
||||||
if got := app.CardWellStatus(); got != test.wantCardWell {
|
|
||||||
t.Fatalf("card-well status = %q, want %q", got, test.wantCardWell)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type responseTrackingRecorder struct {
|
|
||||||
*httptest.ResponseRecorder
|
|
||||||
wroteResponse bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *responseTrackingRecorder) WriteHeader(statusCode int) {
|
|
||||||
r.wroteResponse = true
|
|
||||||
r.ResponseRecorder.WriteHeader(statusCode)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *responseTrackingRecorder) Write(body []byte) (int, error) {
|
|
||||||
r.wroteResponse = true
|
|
||||||
return r.ResponseRecorder.Write(body)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIssueDoorCardDispatchesNextCardBeforeHTTP200(t *testing.T) {
|
|
||||||
var recorder *responseTrackingRecorder
|
|
||||||
dispatchCalled := false
|
|
||||||
dispenser := &fakeDoorCardDispenser{
|
|
||||||
beginNext: func() error {
|
|
||||||
dispatchCalled = true
|
|
||||||
if recorder.wroteResponse {
|
|
||||||
t.Error("HTTP response started before next-card preparation was dispatched")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
lock := &fakeDoorCardLockServer{}
|
|
||||||
payload := lockserver.DoorCardRequest{
|
|
||||||
RoomField: "101",
|
|
||||||
CheckinTime: "2026-07-23 15:00:00 +0100",
|
|
||||||
CheckoutTime: "2026-07-24 11:00:00 +0100",
|
|
||||||
}
|
|
||||||
body, err := json.Marshal(payload)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
app := &App{disp: dispenser, lockserver: lock, cfg: &config.ConfigRec{}}
|
|
||||||
request := httptest.NewRequest(http.MethodPost, "/issuedoorcard", bytes.NewReader(body))
|
|
||||||
request.Header.Set("Content-Type", "application/json")
|
|
||||||
recorder = &responseTrackingRecorder{ResponseRecorder: httptest.NewRecorder()}
|
|
||||||
|
|
||||||
app.issueDoorCard(recorder, request)
|
|
||||||
|
|
||||||
if !dispatchCalled {
|
|
||||||
t.Fatal("next-card preparation was not dispatched")
|
|
||||||
}
|
|
||||||
if recorder.Code != http.StatusOK {
|
|
||||||
t.Fatalf("HTTP status = %d, want %d", recorder.Code, http.StatusOK)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIssueDoorCardLogsNextCardDispatchFailureAndStillSucceeds(t *testing.T) {
|
|
||||||
var logOutput bytes.Buffer
|
|
||||||
standardLogger := log.StandardLogger()
|
|
||||||
previousOutput := standardLogger.Out
|
|
||||||
standardLogger.SetOutput(&logOutput)
|
|
||||||
t.Cleanup(func() {
|
|
||||||
standardLogger.SetOutput(previousOutput)
|
|
||||||
})
|
|
||||||
|
|
||||||
dispenser := &fakeDoorCardDispenser{beginNextErr: errors.New("dispatch failed")}
|
|
||||||
lock := &fakeDoorCardLockServer{}
|
|
||||||
recorder, _, _ := performIssueDoorCardRequest(t, dispenser, lock)
|
|
||||||
|
|
||||||
if recorder.Code != http.StatusOK {
|
|
||||||
t.Fatalf("HTTP status = %d, want %d", recorder.Code, http.StatusOK)
|
|
||||||
}
|
|
||||||
logged := logOutput.String()
|
|
||||||
if !strings.Contains(logged, "dispatch failed") ||
|
|
||||||
!strings.Contains(logged, "Next card preparation dispatch") {
|
|
||||||
t.Fatalf("dispatch failure log = %q", logged)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,534 +0,0 @@
|
|||||||
package handlers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"database/sql"
|
|
||||||
"encoding/json"
|
|
||||||
"encoding/xml"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/config"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/creditcall"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/dispenser"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/errorhandlers"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/lockserver"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/mail"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/paymentsvc"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/printer"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/logging"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
type doorCardDispenser interface {
|
|
||||||
PrepareCurrentCard(context.Context) (string, error)
|
|
||||||
DeliverCurrentCard(context.Context) (string, error)
|
|
||||||
BeginPrepareNextCard(context.Context) error
|
|
||||||
PrepareNextCard(context.Context) (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type App struct {
|
|
||||||
disp doorCardDispenser
|
|
||||||
lockserver lockserver.LockServer
|
|
||||||
paymentService *paymentsvc.Service
|
|
||||||
isPayment bool
|
|
||||||
db *sql.DB
|
|
||||||
cfg *config.ConfigRec
|
|
||||||
dbMu sync.Mutex
|
|
||||||
cardWellMu sync.RWMutex
|
|
||||||
cardWellStatus string
|
|
||||||
availabilityMu sync.Mutex
|
|
||||||
availabilityTimers map[string]*time.Timer
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewApp(disp *dispenser.Client, lockType, encoderAddress, cardWellStatus string, db *sql.DB, cfg *config.ConfigRec) *App {
|
|
||||||
app := &App{
|
|
||||||
isPayment: cfg.IsPayment,
|
|
||||||
disp: disp,
|
|
||||||
lockserver: lockserver.NewLockServer(lockType, encoderAddress, errorhandlers.FatalError),
|
|
||||||
db: db,
|
|
||||||
cfg: cfg,
|
|
||||||
availabilityTimers: make(map[string]*time.Timer),
|
|
||||||
}
|
|
||||||
app.SetCardWellStatus(cardWellStatus)
|
|
||||||
return app
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) SetPaymentService(service *paymentsvc.Service) {
|
|
||||||
app.paymentService = service
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) RegisterRoutes(mux *http.ServeMux) {
|
|
||||||
mux.HandleFunc("/issuedoorcard", app.issueDoorCard)
|
|
||||||
mux.HandleFunc("/printroomticket", app.printRoomTicket)
|
|
||||||
mux.HandleFunc("/takepreauth", app.takePreauthorization)
|
|
||||||
mux.HandleFunc("/takepayment", app.takePayment)
|
|
||||||
mux.HandleFunc("/dispenserstatus", app.reportDispenserStatus)
|
|
||||||
mux.HandleFunc("/testissuedoorcard", app.testIssueDoorCard)
|
|
||||||
mux.HandleFunc("/ping-pdq", app.fetchChipDNAStatus)
|
|
||||||
mux.HandleFunc("/logerror", app.onChipDNAError)
|
|
||||||
mux.HandleFunc("/api/payment/sale", app.salePayment)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) takePreauthorization(w http.ResponseWriter, r *http.Request) {
|
|
||||||
const op = logging.Op("takePreauthorization")
|
|
||||||
|
|
||||||
var (
|
|
||||||
theResponse cmstypes.ResponseRec
|
|
||||||
theRequest cmstypes.TransactionRec
|
|
||||||
trResult creditcall.TransactionResultXML
|
|
||||||
result creditcall.PaymentResult
|
|
||||||
save bool
|
|
||||||
)
|
|
||||||
|
|
||||||
theResponse.Status.Code = http.StatusInternalServerError
|
|
||||||
theResponse.Status.Message = "500 Internal server error"
|
|
||||||
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
if !app.isPayment {
|
|
||||||
if !app.cfg.TestMode {
|
|
||||||
mail.SendEmailOnError(app.cfg.Hotel, app.cfg.Kiosk, "Payment Error", "Attempted preauthorization while payment processing is disabled")
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(types.ResultError, "Payment processing is disabled")
|
|
||||||
writeTransactionResult(w, http.StatusServiceUnavailable, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Method == http.MethodOptions {
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("takePreauthorization called")
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(types.ResultError, "Method not allowed; use POST")
|
|
||||||
writeTransactionResult(w, http.StatusMethodNotAllowed, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Header.Get("Content-Type") != "text/xml" {
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(types.ResultError, "Content-Type must be text/xml")
|
|
||||||
writeTransactionResult(w, http.StatusUnsupportedMediaType, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Read body error", string(op), "", "", 0)
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(types.ResultError, "Failed to read request body")
|
|
||||||
writeTransactionResult(w, http.StatusBadRequest, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := xml.Unmarshal(body, &theRequest); err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "ReadXML", string(op), "", "", 0)
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(types.ResultError, "Invalid XML payload")
|
|
||||||
writeTransactionResult(w, http.StatusBadRequest, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf(
|
|
||||||
"Preauthorization payload: Amount=%s, Type=%s",
|
|
||||||
theRequest.AmountMinorUnits,
|
|
||||||
theRequest.TransactionType,
|
|
||||||
)
|
|
||||||
|
|
||||||
client := &http.Client{Timeout: 300 * time.Second}
|
|
||||||
|
|
||||||
// ---- START TRANSACTION ----
|
|
||||||
|
|
||||||
body, err = callChipDNA(client, types.LinkStartTransaction, body)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Preauth processing error", string(op), "", "", 0)
|
|
||||||
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(types.ResultError, "No response from payment processor")
|
|
||||||
writeTransactionResult(w, http.StatusBadGateway, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := trResult.ParseTransactionResult(body); err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Parse transaction result error", string(op), "", "", 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
result.FillFromTransactionResult(trResult)
|
|
||||||
|
|
||||||
// ---- PRINT RECEIPT ----
|
|
||||||
|
|
||||||
printer.PrintReceipt(result.CardholderReceipt)
|
|
||||||
|
|
||||||
// ---- REDIRECT ----
|
|
||||||
|
|
||||||
theResponse.Status = result.Status
|
|
||||||
theResponse.Data, save = creditcall.BuildPreauthRedirectURL(result.Fields)
|
|
||||||
|
|
||||||
if save {
|
|
||||||
go app.persistPreauth(context.Background(), result.Fields, theRequest.CheckoutDate)
|
|
||||||
}
|
|
||||||
|
|
||||||
writeTransactionResult(w, http.StatusOK, theResponse)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) takePayment(w http.ResponseWriter, r *http.Request) {
|
|
||||||
const op = logging.Op("takePayment")
|
|
||||||
|
|
||||||
var (
|
|
||||||
theResponse cmstypes.ResponseRec
|
|
||||||
theRequest cmstypes.TransactionRec
|
|
||||||
trResult creditcall.TransactionResultXML
|
|
||||||
result creditcall.PaymentResult
|
|
||||||
)
|
|
||||||
|
|
||||||
theResponse.Status.Code = http.StatusInternalServerError
|
|
||||||
theResponse.Status.Message = "500 Internal server error"
|
|
||||||
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
if !app.isPayment {
|
|
||||||
if !app.cfg.TestMode {
|
|
||||||
mail.SendEmailOnError(app.cfg.Hotel, app.cfg.Kiosk, "Payment Error", "Attempted payment while payment processing is disabled")
|
|
||||||
theResponse.Status.Code = http.StatusServiceUnavailable
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(types.ResultError, "Payment processing is disabled")
|
|
||||||
writeTransactionResult(w, http.StatusServiceUnavailable, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Method == http.MethodOptions {
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("takePayment called")
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(types.ResultError, "Method not allowed; use POST")
|
|
||||||
writeTransactionResult(w, http.StatusMethodNotAllowed, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Header.Get("Content-Type") != "text/xml" {
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(types.ResultError, "Content-Type must be text/xml")
|
|
||||||
writeTransactionResult(w, http.StatusUnsupportedMediaType, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Read body error", string(op), "", "", 0)
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(types.ResultError, "Failed to read request body")
|
|
||||||
writeTransactionResult(w, http.StatusBadRequest, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := xml.Unmarshal(body, &theRequest); err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "ReadXML", string(op), "", "", 0)
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(types.ResultError, "Invalid XML payload")
|
|
||||||
writeTransactionResult(w, http.StatusBadRequest, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Transaction payload: Amount=%s, Type=%s",
|
|
||||||
theRequest.AmountMinorUnits,
|
|
||||||
theRequest.TransactionType,
|
|
||||||
)
|
|
||||||
|
|
||||||
client := &http.Client{Timeout: 300 * time.Second}
|
|
||||||
|
|
||||||
// ---- START TRANSACTION ----
|
|
||||||
|
|
||||||
body, err = callChipDNA(client, types.LinkStartTransaction, body)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Start transaction error", string(op), "", "", 0)
|
|
||||||
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(types.ResultError, "No response from payment processor")
|
|
||||||
writeTransactionResult(w, http.StatusBadGateway, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := trResult.ParseTransactionResult(body); err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Parse transaction result error", string(op), "", "", 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
result.FillFromTransactionResult(trResult)
|
|
||||||
|
|
||||||
res := result.Fields[types.TransactionResult]
|
|
||||||
|
|
||||||
if !strings.EqualFold(res, types.ResultApproved) {
|
|
||||||
printer.PrintReceipt(result.CardholderReceipt)
|
|
||||||
desc := result.Fields[types.ErrorDescription]
|
|
||||||
if desc == "" {
|
|
||||||
desc = result.Fields[types.Errors]
|
|
||||||
}
|
|
||||||
logging.Error(types.ServiceName, "Preauthorization failed", "Result: "+res+" Description: "+desc, string(op), "", app.cfg.Hotel, app.cfg.Kiosk)
|
|
||||||
theResponse.Status = result.Status
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(res, result.Fields[types.Errors])
|
|
||||||
|
|
||||||
writeTransactionResult(w, http.StatusOK, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---- CONFIRM TRANSACTION ----
|
|
||||||
|
|
||||||
ref := result.Fields[types.Reference]
|
|
||||||
log.Printf("Preauth approved, reference: %s. Sending confirm...", ref)
|
|
||||||
confirmReq := creditcall.ConfirmTransactionRequest{
|
|
||||||
Amount: theRequest.AmountMinorUnits,
|
|
||||||
Reference: ref,
|
|
||||||
}
|
|
||||||
|
|
||||||
body, err = confirmWithRetry(client, confirmReq, 2)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Confirm transaction error", string(op), "", "", 0)
|
|
||||||
mail.SendEmailOnError(app.cfg.Hotel, app.cfg.Kiosk, "Payment confirmation failed", "Reference: "+ref+", Error: "+err.Error())
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(types.ResultError, "ConfirmTransactionError")
|
|
||||||
writeTransactionResult(w, http.StatusBadGateway, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := trResult.ParseTransactionResult(body); err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Parse confirm result error", string(op), "", "", 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
result.FillFromTransactionResult(trResult)
|
|
||||||
|
|
||||||
res = result.Fields[types.TransactionResult]
|
|
||||||
|
|
||||||
if !strings.EqualFold(res, types.ResultApproved) {
|
|
||||||
printer.PrintReceipt(result.CardholderReceipt)
|
|
||||||
desc := result.Fields[types.ErrorDescription]
|
|
||||||
if desc == "" {
|
|
||||||
desc = result.Fields[types.Errors]
|
|
||||||
}
|
|
||||||
logging.Error(types.ServiceName, "Transaction not approved after confirm", "Confirm result: "+res+" Description: "+desc, string(op), "", app.cfg.Hotel, app.cfg.Kiosk)
|
|
||||||
mail.SendEmailOnError(app.cfg.Hotel, app.cfg.Kiosk, "Payment confirmation failed", "Reference: "+ref+", Confirm result: "+res+" Description: "+desc)
|
|
||||||
theResponse.Status = result.Status
|
|
||||||
theResponse.Data = creditcall.BuildFailureURL(res, result.Fields[types.Errors])
|
|
||||||
|
|
||||||
writeTransactionResult(w, http.StatusOK, theResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---- SUCCESS ----
|
|
||||||
|
|
||||||
printer.PrintReceipt(result.CardholderReceipt)
|
|
||||||
log.Printf("Transaction approved and confirmed, reference: %s", ref)
|
|
||||||
theResponse.Status = result.Status
|
|
||||||
theResponse.Data = creditcall.BuildSuccessURL(result.Fields)
|
|
||||||
|
|
||||||
writeTransactionResult(w, http.StatusOK, theResponse)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) issueDoorCard(w http.ResponseWriter, r *http.Request) {
|
|
||||||
const op = logging.Op("issueDoorCard")
|
|
||||||
var (
|
|
||||||
doorReq lockserver.DoorCardRequest
|
|
||||||
theResponse cmstypes.StatusRec
|
|
||||||
)
|
|
||||||
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
if r.Method == http.MethodOptions {
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("issueDoorCard called")
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
errorhandlers.WriteError(w, http.StatusMethodNotAllowed, "Method not allowed; use POST")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
|
|
||||||
errorhandlers.WriteError(w, http.StatusUnsupportedMediaType, "Content-Type must be application/json")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&doorReq); err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "ReadJSON", string(op), "", "", 0)
|
|
||||||
errorhandlers.WriteError(w, http.StatusBadRequest, "Invalid JSON payload: "+err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse times
|
|
||||||
checkIn, err := time.Parse(types.CustomLayout, doorReq.CheckinTime)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Invalid checkinTime format", string(op), "", "", 0)
|
|
||||||
errorhandlers.WriteError(w, http.StatusBadRequest, "Invalid checkinTime format: "+err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
checkOut, err := time.Parse(types.CustomLayout, doorReq.CheckoutTime)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Invalid checkoutTime format", string(op), "", "", 0)
|
|
||||||
errorhandlers.WriteError(w, http.StatusBadRequest, "Invalid checkoutTime format: "+err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
status, err := app.disp.PrepareCurrentCard(r.Context())
|
|
||||||
app.SetCardWellStatus(status)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Dispense error", string(op), "", "", 0)
|
|
||||||
if errors.Is(err, dispenser.ErrCardWellEmpty) {
|
|
||||||
errorhandlers.WriteError(w, http.StatusServiceUnavailable, dispenser.CardWellEmptyMessage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
errorhandlers.WriteError(w, http.StatusServiceUnavailable, "Dispense error: "+err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// doorReq.RoomField = "104"
|
|
||||||
// build lock server command
|
|
||||||
app.lockserver.BuildCommand(doorReq, checkIn, checkOut)
|
|
||||||
|
|
||||||
// lock server sequence
|
|
||||||
encodingErr := app.lockserver.LockSequence()
|
|
||||||
if encodingErr != nil {
|
|
||||||
logging.Error(types.ServiceName, encodingErr.Error(), "Key encoding", string(op), "", "", 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Once encoding has started, finish the physical dispenser sequence even if
|
|
||||||
// the HTTP client disconnects. Finalization remains bounded by one shared timeout.
|
|
||||||
finalizeCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
status, deliveryErr := app.disp.DeliverCurrentCard(finalizeCtx)
|
|
||||||
app.SetCardWellStatus(status)
|
|
||||||
if deliveryErr != nil {
|
|
||||||
if encodingErr != nil {
|
|
||||||
recoveryErr := fmt.Errorf("key encoding failed: %v; dispenser recovery failed: %w", encodingErr, deliveryErr)
|
|
||||||
logging.Error(types.ServiceName, recoveryErr.Error(), "Dispenser recovery", string(op), "", "", 0)
|
|
||||||
errorhandlers.WriteError(w, http.StatusServiceUnavailable, "Dispenser recovery failed; another encoding attempt is not safe")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
logging.Error(types.ServiceName, deliveryErr.Error(), "Card delivery", string(op), "", "", 0)
|
|
||||||
errorhandlers.WriteError(w, http.StatusServiceUnavailable, "Card delivery could not be confirmed")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if encodingErr != nil {
|
|
||||||
status, preparationErr := app.disp.PrepareNextCard(finalizeCtx)
|
|
||||||
app.SetCardWellStatus(status)
|
|
||||||
if preparationErr != nil {
|
|
||||||
recoveryErr := fmt.Errorf("key encoding failed: %v; dispenser recovery failed: %w", encodingErr, preparationErr)
|
|
||||||
logging.Error(types.ServiceName, recoveryErr.Error(), "Dispenser recovery", string(op), "", "", 0)
|
|
||||||
if errors.Is(preparationErr, dispenser.ErrCardWellEmpty) {
|
|
||||||
errorhandlers.WriteError(w, http.StatusServiceUnavailable, dispenser.CardWellEmptyMessage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
errorhandlers.WriteError(w, http.StatusServiceUnavailable, "Dispenser recovery failed; another encoding attempt is not safe")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
errorhandlers.WriteError(w, http.StatusBadGateway, encodingErr.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if preparationErr := app.disp.BeginPrepareNextCard(finalizeCtx); preparationErr != nil {
|
|
||||||
logging.Error(types.ServiceName, preparationErr.Error(), "Next card preparation dispatch", string(op), "", "", 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
theResponse.Code = http.StatusOK
|
|
||||||
theResponse.Message = "Card issued successfully"
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_ = json.NewEncoder(w).Encode(theResponse)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) printRoomTicket(w http.ResponseWriter, r *http.Request) {
|
|
||||||
const op = logging.Op("printRoomTicket")
|
|
||||||
var roomDetails printer.RoomDetailsRec
|
|
||||||
// Allow CORS preflight if needed
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
||||||
|
|
||||||
if r.Method == http.MethodOptions {
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Println("printRoomTicket called")
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
errorhandlers.WriteError(w, http.StatusMethodNotAllowed, "Method not allowed; use POST")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if ct := r.Header.Get("Content-Type"); !strings.Contains(ct, "xml") {
|
|
||||||
errorhandlers.WriteError(w, http.StatusUnsupportedMediaType, "Content-Type must be application/xml")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer r.Body.Close()
|
|
||||||
if err := xml.NewDecoder(r.Body).Decode(&roomDetails); err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "ReadXML", string(op), "", "", 0)
|
|
||||||
errorhandlers.WriteError(w, http.StatusBadRequest, "Invalid XML payload: "+err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := printer.BuildRoomTicket(roomDetails)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "BuildRoomTicket", string(op), "", "", 0)
|
|
||||||
errorhandlers.WriteError(w, http.StatusInternalServerError, "BuildRoomTicket failed: "+err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send to the Windows Epson TM-T82II via the printer package
|
|
||||||
if err := printer.SendToPrinter(data); err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "printRoomTicket", "printRoomTicket", "", "", 0)
|
|
||||||
errorhandlers.WriteError(w, http.StatusInternalServerError, "Print failed: "+err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Success
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
json.NewEncoder(w).Encode(cmstypes.StatusRec{
|
|
||||||
Code: http.StatusOK,
|
|
||||||
Message: "Print job sent successfully",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) reportDispenserStatus(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_ = json.NewEncoder(w).Encode(cmstypes.StatusRec{
|
|
||||||
Code: http.StatusOK,
|
|
||||||
Message: app.CardWellStatus(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) SetCardWellStatus(s string) {
|
|
||||||
app.cardWellMu.Lock()
|
|
||||||
prev := app.cardWellStatus
|
|
||||||
app.cardWellStatus = s
|
|
||||||
app.cardWellMu.Unlock()
|
|
||||||
|
|
||||||
if s != "" && prev != s {
|
|
||||||
mail.SendEmailOnError(app.cfg.Hotel, app.cfg.Kiosk, "Dispenser Error Status", "Status: "+s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) CardWellStatus() string {
|
|
||||||
app.cardWellMu.RLock()
|
|
||||||
defer app.cardWellMu.RUnlock()
|
|
||||||
return app.cardWellStatus
|
|
||||||
}
|
|
||||||
@ -1,82 +0,0 @@
|
|||||||
package handlers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"encoding/xml"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/creditcall"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/logging"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
func writeTransactionResult(w http.ResponseWriter, status int, theResponse cmstypes.ResponseRec) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.WriteHeader(status)
|
|
||||||
if err := json.NewEncoder(w).Encode(theResponse); err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "JSON encode error", "startTransaction", "", "", 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func callChipDNA(client *http.Client, url string, payload []byte) ([]byte, error) {
|
|
||||||
|
|
||||||
resp, err := client.Post(url, "text/xml", bytes.NewBuffer(payload))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
return io.ReadAll(resp.Body)
|
|
||||||
}
|
|
||||||
|
|
||||||
func confirmWithRetry(client *http.Client, req creditcall.ConfirmTransactionRequest, attempts int) ([]byte, error) {
|
|
||||||
|
|
||||||
payload, err := xml.Marshal(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var lastErr error
|
|
||||||
|
|
||||||
for i := 1; i <= attempts; i++ {
|
|
||||||
|
|
||||||
resp, err := client.Post(types.LinkConfirmTransaction, "text/xml", bytes.NewBuffer(payload))
|
|
||||||
if err != nil {
|
|
||||||
lastErr = err
|
|
||||||
} else {
|
|
||||||
|
|
||||||
body, readErr := io.ReadAll(resp.Body)
|
|
||||||
resp.Body.Close()
|
|
||||||
|
|
||||||
if readErr != nil {
|
|
||||||
lastErr = readErr
|
|
||||||
} else {
|
|
||||||
return body, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Warnf("ConfirmTransaction attempt %d/%d failed: %v", i, attempts, lastErr)
|
|
||||||
|
|
||||||
if i < attempts {
|
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, lastErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeJSON(w http.ResponseWriter, status int, payload any) {
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
||||||
w.WriteHeader(status)
|
|
||||||
|
|
||||||
if payload == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = json.NewEncoder(w).Encode(payload)
|
|
||||||
}
|
|
||||||
@ -1,334 +0,0 @@
|
|||||||
package handlers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/mail"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/paymentsvc"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/printer"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/logging"
|
|
||||||
"github.com/google/uuid"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SalePaymentRequest struct {
|
|
||||||
Reference string `json:"reference,omitempty"`
|
|
||||||
ConfirmNo string `json:"confirmNo,omitempty"`
|
|
||||||
Amount int64 `json:"amount"`
|
|
||||||
Currency string `json:"currency,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type paymentStreamMessage struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Code string `json:"code,omitempty"`
|
|
||||||
Response *cmstypes.ResponseRec `json:"response,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) salePayment(w http.ResponseWriter, r *http.Request) {
|
|
||||||
const op = logging.Op("salePayment")
|
|
||||||
|
|
||||||
response := cmstypes.ResponseRec{
|
|
||||||
Status: cmstypes.StatusRec{
|
|
||||||
Code: http.StatusInternalServerError,
|
|
||||||
Message: http.StatusText(http.StatusInternalServerError),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
setPaymentCORS(w)
|
|
||||||
|
|
||||||
if r.Method == http.MethodOptions {
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
response.Data = buildPaymentFailureURL(types.ResultError, "Method not allowed; use POST")
|
|
||||||
writeTransactionResult(w, http.StatusMethodNotAllowed, response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if app.paymentService == nil {
|
|
||||||
mail.SendEmailOnError(
|
|
||||||
app.cfg.Hotel,
|
|
||||||
app.cfg.Kiosk,
|
|
||||||
"Payment Service Not Configured",
|
|
||||||
"Payment service is not configured; cannot process payment requests",
|
|
||||||
)
|
|
||||||
|
|
||||||
response.Data = buildPaymentFailureURL(types.ResultError, "Payment service is not configured")
|
|
||||||
writeTransactionResult(w, http.StatusInternalServerError, response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if ct := r.Header.Get("Content-Type"); ct != "" && !strings.Contains(ct, "application/json") {
|
|
||||||
response.Data = buildPaymentFailureURL(types.ResultError, "Content-Type must be application/json")
|
|
||||||
writeTransactionResult(w, http.StatusUnsupportedMediaType, response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
var req SalePaymentRequest
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
||||||
logging.Error(
|
|
||||||
types.ServiceName,
|
|
||||||
err.Error(),
|
|
||||||
"ReadJSON",
|
|
||||||
string(op),
|
|
||||||
"",
|
|
||||||
app.cfg.Hotel,
|
|
||||||
app.cfg.Kiosk,
|
|
||||||
)
|
|
||||||
|
|
||||||
response.Data = buildPaymentFailureURL(types.ResultError, "invalid JSON payload: "+err.Error())
|
|
||||||
writeTransactionResult(w, http.StatusBadRequest, response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.Amount <= 0 {
|
|
||||||
response.Data = buildPaymentFailureURL(types.ResultError, "Amount must be greater than zero")
|
|
||||||
writeTransactionResult(w, http.StatusBadRequest, response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.Currency == "" {
|
|
||||||
req.Currency = "GBP"
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.Reference == "" {
|
|
||||||
req.Reference = req.ConfirmNo
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.Reference == "" {
|
|
||||||
req.Reference = uuid.NewString()
|
|
||||||
}
|
|
||||||
|
|
||||||
requestID := buildPaymentRequestID(req.Reference)
|
|
||||||
|
|
||||||
timeoutSeconds := app.cfg.TimeoutSeconds
|
|
||||||
if timeoutSeconds <= 0 {
|
|
||||||
timeoutSeconds = 300
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(
|
|
||||||
r.Context(),
|
|
||||||
time.Duration(timeoutSeconds)*time.Second,
|
|
||||||
)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
flusher, ok := w.(http.Flusher)
|
|
||||||
if !ok {
|
|
||||||
response.Data = buildPaymentFailureURL(
|
|
||||||
types.ResultError,
|
|
||||||
"Streaming payment updates are not supported",
|
|
||||||
)
|
|
||||||
writeTransactionResult(w, http.StatusInternalServerError, response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/x-ndjson; charset=utf-8")
|
|
||||||
w.Header().Set("Cache-Control", "no-cache")
|
|
||||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
||||||
|
|
||||||
encoder := json.NewEncoder(w)
|
|
||||||
|
|
||||||
var streamMu sync.Mutex
|
|
||||||
streamStarted := false
|
|
||||||
streamFailed := false
|
|
||||||
|
|
||||||
sendStreamMessage := func(message paymentStreamMessage) {
|
|
||||||
streamMu.Lock()
|
|
||||||
defer streamMu.Unlock()
|
|
||||||
|
|
||||||
if streamFailed {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := encoder.Encode(message); err != nil {
|
|
||||||
streamFailed = true
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
streamStarted = true
|
|
||||||
flusher.Flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
onStatus := func(update paymentsvc.StatusUpdate) {
|
|
||||||
if update.Code == "" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sendStreamMessage(paymentStreamMessage{
|
|
||||||
Type: "status",
|
|
||||||
Code: update.Code,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := app.paymentService.Sale(
|
|
||||||
ctx,
|
|
||||||
paymentsvc.SaleRequest{
|
|
||||||
RequestID: requestID,
|
|
||||||
Reference: req.Reference,
|
|
||||||
Amount: req.Amount,
|
|
||||||
Currency: req.Currency,
|
|
||||||
},
|
|
||||||
onStatus,
|
|
||||||
)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
status := http.StatusBadGateway
|
|
||||||
if errors.Is(err, paymentsvc.ErrPaymentInProgress) {
|
|
||||||
status = http.StatusConflict
|
|
||||||
}
|
|
||||||
|
|
||||||
logging.Error(
|
|
||||||
types.ServiceName,
|
|
||||||
err.Error(),
|
|
||||||
"Payment provider error",
|
|
||||||
string(op),
|
|
||||||
req.Reference,
|
|
||||||
app.cfg.Hotel,
|
|
||||||
app.cfg.Kiosk,
|
|
||||||
)
|
|
||||||
|
|
||||||
response.Status.Code = status
|
|
||||||
response.Status.Message = http.StatusText(status)
|
|
||||||
response.Data = buildPaymentFailureURL(types.ResultError, err.Error())
|
|
||||||
|
|
||||||
if !streamStarted {
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
||||||
writeTransactionResult(w, status, response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sendStreamMessage(paymentStreamMessage{
|
|
||||||
Type: "result",
|
|
||||||
Response: &response,
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if result == nil {
|
|
||||||
response.Status.Code = http.StatusBadGateway
|
|
||||||
response.Status.Message = "Empty payment result"
|
|
||||||
response.Data = buildPaymentFailureURL(
|
|
||||||
types.ResultError,
|
|
||||||
"Payment provider returned an empty result",
|
|
||||||
)
|
|
||||||
|
|
||||||
if !streamStarted {
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
||||||
writeTransactionResult(w, http.StatusBadGateway, response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sendStreamMessage(paymentStreamMessage{
|
|
||||||
Type: "result",
|
|
||||||
Response: &response,
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
response.Status.Code = http.StatusOK
|
|
||||||
|
|
||||||
if result.Success && strings.EqualFold(result.Status, "APPROVED") {
|
|
||||||
printer.PrintSaleReceipt(result.CustomerReceipt)
|
|
||||||
|
|
||||||
response.Status.Message = result.Message
|
|
||||||
response.Data = buildPaymentSuccessURL(result)
|
|
||||||
|
|
||||||
sendStreamMessage(paymentStreamMessage{
|
|
||||||
Type: "result",
|
|
||||||
Response: &response,
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
description := result.ErrorMessage
|
|
||||||
if description == "" {
|
|
||||||
description = result.Message
|
|
||||||
}
|
|
||||||
if description == "" {
|
|
||||||
description = result.Status
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Transaction failed: %s", description)
|
|
||||||
printer.PrintSaleReceipt(result.CustomerReceipt)
|
|
||||||
|
|
||||||
response.Status.Message = "Payment unsuccessful"
|
|
||||||
response.Data = buildPaymentFailureURL(types.ResultError, description)
|
|
||||||
|
|
||||||
sendStreamMessage(paymentStreamMessage{
|
|
||||||
Type: "result",
|
|
||||||
Response: &response,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func buildPaymentRequestID(reference string) string {
|
|
||||||
const prefix = "REQ_"
|
|
||||||
const maxLength = 60
|
|
||||||
|
|
||||||
suffix := fmt.Sprintf("_%d", time.Now().UnixMilli())
|
|
||||||
maxReferenceLength := maxLength - len(prefix) - len(suffix)
|
|
||||||
|
|
||||||
runes := []rune(reference)
|
|
||||||
if len(runes) > maxReferenceLength {
|
|
||||||
runes = runes[:maxReferenceLength]
|
|
||||||
}
|
|
||||||
|
|
||||||
return prefix + string(runes) + suffix
|
|
||||||
}
|
|
||||||
|
|
||||||
func setPaymentCORS(w http.ResponseWriter) {
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
}
|
|
||||||
|
|
||||||
func buildPaymentSuccessURL(result *paymentsvc.Result) string {
|
|
||||||
// txnReference := result.ReferenceNumber
|
|
||||||
// if txnReference == "" {
|
|
||||||
// txnReference = result.TransactionID
|
|
||||||
// }
|
|
||||||
|
|
||||||
q := url.Values{}
|
|
||||||
q.Set("CardNumber", hex.EncodeToString([]byte(result.CardNumber)))
|
|
||||||
q.Set("CardType", hex.EncodeToString([]byte(result.CardType)))
|
|
||||||
q.Set("ExpiryDate", hex.EncodeToString([]byte(result.ExpiryDate)))
|
|
||||||
q.Set("TxnReference", result.RequestID)
|
|
||||||
q.Set("CardHash", hex.EncodeToString([]byte(result.CardHash)))
|
|
||||||
q.Set("CardReference", hex.EncodeToString([]byte(result.CardReference)))
|
|
||||||
|
|
||||||
return (&url.URL{
|
|
||||||
Path: types.CheckinSuccessfulEndpoint,
|
|
||||||
RawQuery: q.Encode(),
|
|
||||||
}).String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func buildPaymentFailureURL(msgType, description string) string {
|
|
||||||
log.WithFields(log.Fields{
|
|
||||||
types.LogFieldError: msgType,
|
|
||||||
types.LogFieldDescription: description,
|
|
||||||
}).Error("Transaction failed")
|
|
||||||
|
|
||||||
q := url.Values{}
|
|
||||||
q.Set("MsgType", msgType)
|
|
||||||
q.Set("Description", description)
|
|
||||||
|
|
||||||
return (&url.URL{
|
|
||||||
Path: types.CheckinUnsuccessfulEndpoint,
|
|
||||||
RawQuery: q.Encode(),
|
|
||||||
}).String()
|
|
||||||
}
|
|
||||||
@ -1,162 +0,0 @@
|
|||||||
package handlers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/config"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/paymentsvc"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/paymentstatus"
|
|
||||||
)
|
|
||||||
|
|
||||||
type paymentProviderFunc func(
|
|
||||||
context.Context,
|
|
||||||
paymentsvc.SaleRequest,
|
|
||||||
paymentsvc.StatusHandler,
|
|
||||||
) (*paymentsvc.Result, error)
|
|
||||||
|
|
||||||
func (f paymentProviderFunc) Sale(
|
|
||||||
ctx context.Context,
|
|
||||||
request paymentsvc.SaleRequest,
|
|
||||||
onStatus paymentsvc.StatusHandler,
|
|
||||||
) (*paymentsvc.Result, error) {
|
|
||||||
return f(ctx, request, onStatus)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSalePaymentStreamsTerminalUnavailableAsAuthoritativeUnsuccessfulResult(t *testing.T) {
|
|
||||||
provider := paymentProviderFunc(func(
|
|
||||||
_ context.Context,
|
|
||||||
request paymentsvc.SaleRequest,
|
|
||||||
onStatus paymentsvc.StatusHandler,
|
|
||||||
) (*paymentsvc.Result, error) {
|
|
||||||
onStatus(paymentsvc.StatusUpdate{Code: paymentstatus.Starting})
|
|
||||||
onStatus(paymentsvc.StatusUpdate{Code: paymentstatus.TerminalUnavailable})
|
|
||||||
return &paymentsvc.Result{
|
|
||||||
RequestID: request.RequestID,
|
|
||||||
Operation: "SALE",
|
|
||||||
Status: "TERMINAL_UNAVAILABLE",
|
|
||||||
ErrorMessage: "Payment terminal is unavailable",
|
|
||||||
Amount: request.Amount,
|
|
||||||
Currency: request.Currency,
|
|
||||||
DeviceUsed: "terminal-1",
|
|
||||||
DeviceType: "Dojo Terminal",
|
|
||||||
}, nil
|
|
||||||
})
|
|
||||||
|
|
||||||
recorder := performSalePaymentRequest(t, provider)
|
|
||||||
if recorder.Code != http.StatusOK {
|
|
||||||
t.Fatalf("outer status = %d, want %d", recorder.Code, http.StatusOK)
|
|
||||||
}
|
|
||||||
messages := decodePaymentStream(t, recorder.Body)
|
|
||||||
if len(messages) != 3 {
|
|
||||||
t.Fatalf("stream messages = %#v, want two statuses and one result", messages)
|
|
||||||
}
|
|
||||||
if messages[0].Type != "status" || messages[0].Code != paymentstatus.Starting ||
|
|
||||||
messages[1].Type != "status" || messages[1].Code != paymentstatus.TerminalUnavailable {
|
|
||||||
t.Fatalf("status frames = %#v", messages[:2])
|
|
||||||
}
|
|
||||||
final := messages[2]
|
|
||||||
if final.Type != "result" || final.Response == nil {
|
|
||||||
t.Fatalf("final frame = %#v", final)
|
|
||||||
}
|
|
||||||
if final.Response.Status.Code != http.StatusOK {
|
|
||||||
t.Fatalf("nested status = %d, want %d", final.Response.Status.Code, http.StatusOK)
|
|
||||||
}
|
|
||||||
resultURL, err := url.Parse(final.Response.Data)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if resultURL.Path != types.CheckinUnsuccessfulEndpoint {
|
|
||||||
t.Fatalf("result path = %q, want %q", resultURL.Path, types.CheckinUnsuccessfulEndpoint)
|
|
||||||
}
|
|
||||||
if got := resultURL.Query().Get("Description"); got != "Payment terminal is unavailable" {
|
|
||||||
t.Fatalf("Description = %q", got)
|
|
||||||
}
|
|
||||||
|
|
||||||
body := recorder.Body.String()
|
|
||||||
for _, forbidden := range []string{
|
|
||||||
"Dojo returned HTTP 409",
|
|
||||||
"offline or currently in use",
|
|
||||||
"traceId",
|
|
||||||
"docs.dojo.tech",
|
|
||||||
} {
|
|
||||||
if strings.Contains(body, forbidden) {
|
|
||||||
t.Fatalf("stream exposed %q: %s", forbidden, body)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSalePaymentRetainsNestedBadGatewayForProviderErrorsAfterStreamingStarts(t *testing.T) {
|
|
||||||
provider := paymentProviderFunc(func(
|
|
||||||
_ context.Context,
|
|
||||||
_ paymentsvc.SaleRequest,
|
|
||||||
onStatus paymentsvc.StatusHandler,
|
|
||||||
) (*paymentsvc.Result, error) {
|
|
||||||
onStatus(paymentsvc.StatusUpdate{Code: paymentstatus.Starting})
|
|
||||||
return nil, errors.New("provider failed")
|
|
||||||
})
|
|
||||||
|
|
||||||
recorder := performSalePaymentRequest(t, provider)
|
|
||||||
if recorder.Code != http.StatusOK {
|
|
||||||
t.Fatalf("outer status = %d, want streaming status %d", recorder.Code, http.StatusOK)
|
|
||||||
}
|
|
||||||
messages := decodePaymentStream(t, recorder.Body)
|
|
||||||
if len(messages) != 2 || messages[1].Type != "result" || messages[1].Response == nil {
|
|
||||||
t.Fatalf("stream messages = %#v", messages)
|
|
||||||
}
|
|
||||||
if messages[1].Response.Status.Code != http.StatusBadGateway {
|
|
||||||
t.Fatalf("nested status = %d, want %d", messages[1].Response.Status.Code, http.StatusBadGateway)
|
|
||||||
}
|
|
||||||
resultURL, err := url.Parse(messages[1].Response.Data)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if resultURL.Path != types.CheckinUnsuccessfulEndpoint {
|
|
||||||
t.Fatalf("result path = %q, want %q", resultURL.Path, types.CheckinUnsuccessfulEndpoint)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func performSalePaymentRequest(t *testing.T, provider paymentsvc.Provider) *httptest.ResponseRecorder {
|
|
||||||
t.Helper()
|
|
||||||
app := &App{
|
|
||||||
paymentService: paymentsvc.NewService(provider),
|
|
||||||
cfg: &config.ConfigRec{
|
|
||||||
Hotel: "HOTEL",
|
|
||||||
Kiosk: 7,
|
|
||||||
TimeoutSeconds: 5,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
request := httptest.NewRequest(
|
|
||||||
http.MethodPost,
|
|
||||||
"/api/payment/sale",
|
|
||||||
strings.NewReader(`{"reference":"BOOKING-123","amount":10852,"currency":"GBP"}`),
|
|
||||||
)
|
|
||||||
request.Header.Set("Content-Type", "application/json")
|
|
||||||
recorder := httptest.NewRecorder()
|
|
||||||
app.salePayment(recorder, request)
|
|
||||||
return recorder
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodePaymentStream(t *testing.T, body io.Reader) []paymentStreamMessage {
|
|
||||||
t.Helper()
|
|
||||||
decoder := json.NewDecoder(body)
|
|
||||||
var messages []paymentStreamMessage
|
|
||||||
for {
|
|
||||||
var message paymentStreamMessage
|
|
||||||
if err := decoder.Decode(&message); err != nil {
|
|
||||||
if errors.Is(err, io.EOF) {
|
|
||||||
return messages
|
|
||||||
}
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
messages = append(messages, message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,252 +0,0 @@
|
|||||||
package handlers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/creditcall"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/errorhandlers"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/lockserver"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/mail"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
|
||||||
"gitea.futuresens.co.uk/futuresens/logging"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (app *App) testIssueDoorCard(w http.ResponseWriter, r *http.Request) {
|
|
||||||
const op = logging.Op("issueDoorCard")
|
|
||||||
var (
|
|
||||||
doorReq lockserver.DoorCardRequest
|
|
||||||
theResponse cmstypes.StatusRec
|
|
||||||
)
|
|
||||||
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
if r.Method == http.MethodOptions {
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("issueDoorCard called")
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
errorhandlers.WriteError(w, http.StatusMethodNotAllowed, "Method not allowed; use POST")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
|
|
||||||
errorhandlers.WriteError(w, http.StatusUnsupportedMediaType, "Content-Type must be application/json")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&doorReq); err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "ReadJSON", string(op), "", "", 0)
|
|
||||||
errorhandlers.WriteError(w, http.StatusBadRequest, "Invalid JSON payload: "+err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
now := time.Now()
|
|
||||||
checkIn := time.Date(now.Year(), now.Month(), now.Day(), 23, 0, 0, 0, now.Location())
|
|
||||||
checkOut := checkIn.Add(2 * time.Hour)
|
|
||||||
|
|
||||||
// Ensure dispenser ready (card at encoder) BEFORE we attempt encoding.
|
|
||||||
// With queued dispenser ops, this will not clash with polling.
|
|
||||||
status, err := app.disp.PrepareCurrentCard(r.Context())
|
|
||||||
app.SetCardWellStatus(status)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Dispense error", string(op), "", "", 0)
|
|
||||||
errorhandlers.WriteError(w, http.StatusServiceUnavailable, "Dispense error: "+err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// build lock server command
|
|
||||||
app.lockserver.BuildCommand(doorReq, checkIn, checkOut)
|
|
||||||
|
|
||||||
// lock server sequence
|
|
||||||
if err := app.lockserver.LockSequence(); err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "Key encoding", string(op), "", "", 0)
|
|
||||||
errorhandlers.WriteError(w, http.StatusBadGateway, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
theResponse.Code = http.StatusOK
|
|
||||||
theResponse.Message = "Card issued successfully"
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_ = json.NewEncoder(w).Encode(theResponse)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) fetchChipDNAStatus(w http.ResponseWriter, r *http.Request) {
|
|
||||||
const op = logging.Op("fetchChipDNAStatus")
|
|
||||||
var theResponse cmstypes.StatusRec
|
|
||||||
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
status, err := creditcall.ReadPdqStatus(app.cfg.Hotel, app.cfg.Kiosk)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "fetchChipDNAStatus", string(op), "", app.cfg.Hotel, app.cfg.Kiosk)
|
|
||||||
errorhandlers.WriteError(w, http.StatusServiceUnavailable, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
b, err := json.MarshalIndent(status, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(types.ServiceName, err.Error(), "MarshalIndent", string(op), "", "", 0)
|
|
||||||
errorhandlers.WriteError(w, http.StatusInternalServerError, "Failed to marshal status data")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
theResponse.Code = http.StatusOK
|
|
||||||
theResponse.Message = string(b)
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_ = json.NewEncoder(w).Encode(theResponse)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) onChipDNAError(w http.ResponseWriter, r *http.Request) {
|
|
||||||
const op = logging.Op("onChipDNAError")
|
|
||||||
var tr creditcall.TransactionResultXML
|
|
||||||
title := "ChipDNA Error"
|
|
||||||
message := ""
|
|
||||||
|
|
||||||
log.Println("onChipDNAError called")
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
if r.Method == http.MethodOptions {
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
errorhandlers.WriteError(w, http.StatusMethodNotAllowed, "Method not allowed; use POST")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
message = "Failed to read request body: " + err.Error()
|
|
||||||
mail.SendEmailOnError(app.cfg.Hotel, app.cfg.Kiosk, title, message)
|
|
||||||
errorhandlers.WriteError(w, http.StatusBadRequest, "Unable to read request body")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(body) == 0 {
|
|
||||||
message = "Received empty request body"
|
|
||||||
mail.SendEmailOnError(app.cfg.Hotel, app.cfg.Kiosk, title, message)
|
|
||||||
errorhandlers.WriteError(w, http.StatusBadRequest, "Empty body")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := tr.ParseTransactionResult(body); err != nil {
|
|
||||||
logging.Error(
|
|
||||||
types.ServiceName,
|
|
||||||
err.Error(),
|
|
||||||
"Parse transaction result error",
|
|
||||||
string(op),
|
|
||||||
"",
|
|
||||||
app.cfg.Hotel,
|
|
||||||
app.cfg.Kiosk,
|
|
||||||
)
|
|
||||||
message = "Failed to parse transaction result: " + err.Error()
|
|
||||||
mail.SendEmailOnError(app.cfg.Hotel, app.cfg.Kiosk, title, message)
|
|
||||||
errorhandlers.WriteError(w, http.StatusBadRequest, "Invalid XML")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, e := range tr.Entries {
|
|
||||||
|
|
||||||
switch e.Key {
|
|
||||||
|
|
||||||
case creditcall.KeyErrors:
|
|
||||||
mail.SendEmailOnError(app.cfg.Hotel, app.cfg.Kiosk, title, e.Value)
|
|
||||||
|
|
||||||
case creditcall.KeyIsAvailable:
|
|
||||||
isAvailable := strings.EqualFold(e.Value, "true")
|
|
||||||
app.handleAvailabilityDebounced(isAvailable)
|
|
||||||
}
|
|
||||||
|
|
||||||
logging.Error(
|
|
||||||
types.ServiceName,
|
|
||||||
e.Value,
|
|
||||||
e.Key,
|
|
||||||
string(op),
|
|
||||||
"",
|
|
||||||
app.cfg.Hotel,
|
|
||||||
app.cfg.Kiosk,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte(`{"status":"received"}`))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) handleAvailabilityDebounced(isAvailable bool) {
|
|
||||||
const (
|
|
||||||
debounceDay = 30
|
|
||||||
debounceNight = 600
|
|
||||||
title = "ChipDNA Error"
|
|
||||||
)
|
|
||||||
|
|
||||||
key := app.availabilityKey()
|
|
||||||
|
|
||||||
app.availabilityMu.Lock()
|
|
||||||
defer app.availabilityMu.Unlock()
|
|
||||||
|
|
||||||
// If device becomes available -> cancel pending timer
|
|
||||||
if isAvailable {
|
|
||||||
if t, exists := app.availabilityTimers[key]; exists {
|
|
||||||
t.Stop()
|
|
||||||
delete(app.availabilityTimers, key)
|
|
||||||
log.Println("PDQ availability restored - debounce timer cancelled")
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Device became unavailable -> start 10s debounce if not already started
|
|
||||||
if _, exists := app.availabilityTimers[key]; exists {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
debounce := debounceDay
|
|
||||||
|
|
||||||
hour := time.Now().Hour()
|
|
||||||
if hour < 6 {
|
|
||||||
debounce = debounceNight
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("PDQ reported unavailable - starting %ds debounce timer", debounce)
|
|
||||||
|
|
||||||
timer := time.AfterFunc(time.Duration(debounce)*time.Second, func() {
|
|
||||||
mail.SendEmailOnError(
|
|
||||||
app.cfg.Hotel,
|
|
||||||
app.cfg.Kiosk,
|
|
||||||
title,
|
|
||||||
fmt.Sprintf("ChipDNA PDQ unavailable for more than %d seconds", debounce),
|
|
||||||
)
|
|
||||||
|
|
||||||
app.availabilityMu.Lock()
|
|
||||||
delete(app.availabilityTimers, key)
|
|
||||||
app.availabilityMu.Unlock()
|
|
||||||
})
|
|
||||||
|
|
||||||
app.availabilityTimers[key] = timer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) availabilityKey() string {
|
|
||||||
return fmt.Sprintf("hotel=%s|kiosk=%d|app=%p",
|
|
||||||
strings.TrimSpace(app.cfg.Hotel),
|
|
||||||
app.cfg.Kiosk,
|
|
||||||
app,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@ -1,291 +0,0 @@
|
|||||||
package lockserver
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
kabaSTX = 0x02
|
|
||||||
kabaETX = 0x03
|
|
||||||
kabaACK = 0x06
|
|
||||||
kabaNAK = 0x15
|
|
||||||
)
|
|
||||||
|
|
||||||
// BuildCommand builds a key encoding request command for the dormakaba/Kaba lock server.
|
|
||||||
// KR|KTD|WS192.168.135.20|KC2|RN41|KO000000|GA241213|TI16:56|GD241214|DT11:00|G#75|
|
|
||||||
func (lock *KabaLockServer) BuildCommand(doorReq DoorCardRequest, checkIn, checkOut time.Time) error {
|
|
||||||
const funcName = "DormakabaLockServer.BuildCommand"
|
|
||||||
|
|
||||||
room := strings.TrimSpace(doorReq.RoomField)
|
|
||||||
if room == "" {
|
|
||||||
return fmt.Errorf("[%s] roomField is required", funcName)
|
|
||||||
}
|
|
||||||
|
|
||||||
if checkIn.IsZero() {
|
|
||||||
return fmt.Errorf("[%s] checkin time is required", funcName)
|
|
||||||
}
|
|
||||||
|
|
||||||
if checkOut.IsZero() {
|
|
||||||
return fmt.Errorf("[%s] checkout time is required", funcName)
|
|
||||||
}
|
|
||||||
|
|
||||||
ws := dormakabaWorkstationID()
|
|
||||||
|
|
||||||
ga := checkIn.Format("060102") // yyMMdd, example: 241213
|
|
||||||
gd := checkOut.Format("060102") // yyMMdd, example: 241214
|
|
||||||
ti := checkIn.Format("15:04") // HH:mm, example: 16:56
|
|
||||||
dt := checkOut.Format("15:04") // HH:mm, example: 11:00
|
|
||||||
|
|
||||||
payload := fmt.Sprintf(
|
|
||||||
"KR|KTD|WS%s|KC%s|RN%s|KO000000|GA%s|TI%s|GD%s|DT%s|G#75|",
|
|
||||||
ws,
|
|
||||||
lock.encoderAddr,
|
|
||||||
room,
|
|
||||||
ga,
|
|
||||||
ti,
|
|
||||||
gd,
|
|
||||||
dt,
|
|
||||||
)
|
|
||||||
|
|
||||||
lock.command = wrapKabaFrame(payload)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// LockSequence starts the link and performs key encoding.
|
|
||||||
func (lock *KabaLockServer) LockSequence() error {
|
|
||||||
const funcName = "KabaLockServer.LockSequence"
|
|
||||||
|
|
||||||
conn, err := InitializeServerConnection(LockServerURL)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
reader := bufio.NewReader(conn)
|
|
||||||
|
|
||||||
regs, err := lock.linkStart(conn, reader)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("[%s] linkStart failed: %v", funcName, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, reg := range regs {
|
|
||||||
log.Printf("Received: %q", reg)
|
|
||||||
}
|
|
||||||
|
|
||||||
raw, err := lock.requestEncoding(conn, reader)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("[%s] request encoding failed: %v", funcName, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("Encoding response: %s", raw)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// linkStart sends the dormakaba/Kaba LS command.
|
|
||||||
// LS|DA241213|TI165607|WS192.168.135.20|PW1234|
|
|
||||||
func (lock *KabaLockServer) linkStart(conn net.Conn, reader *bufio.Reader) ([]string, error) {
|
|
||||||
ws := dormakabaWorkstationID()
|
|
||||||
pw := dormakabaPassword()
|
|
||||||
|
|
||||||
payload := fmt.Sprintf(
|
|
||||||
"LS|DA%s|TI%s|WS%s|PW%s|",
|
|
||||||
time.Now().Format("060102"), // yyMMdd
|
|
||||||
time.Now().Format("150405"), // HHmmss
|
|
||||||
ws,
|
|
||||||
pw,
|
|
||||||
)
|
|
||||||
|
|
||||||
command := wrapKabaFrame(payload)
|
|
||||||
|
|
||||||
log.Printf("Sending Link Start command: %q", command)
|
|
||||||
|
|
||||||
if _, err := conn.Write(command); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to send Link Start command: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var registers []string
|
|
||||||
timeout := 10 * time.Second
|
|
||||||
|
|
||||||
for {
|
|
||||||
conn.SetReadDeadline(time.Now().Add(timeout))
|
|
||||||
|
|
||||||
b, err := reader.ReadByte()
|
|
||||||
if err != nil {
|
|
||||||
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
|
||||||
if len(registers) > 0 {
|
|
||||||
return registers, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, fmt.Errorf("error reading Link Start response: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch b {
|
|
||||||
case kabaACK:
|
|
||||||
registers = append(registers, "ACK")
|
|
||||||
continue
|
|
||||||
|
|
||||||
case kabaNAK:
|
|
||||||
return registers, fmt.Errorf("received NAK after Link Start")
|
|
||||||
|
|
||||||
case kabaSTX:
|
|
||||||
frame, err := readKabaFrame(conn, reader, b, timeout)
|
|
||||||
if err != nil {
|
|
||||||
return registers, fmt.Errorf("failed to read Link Start frame: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
frameText := string(frame)
|
|
||||||
registers = append(registers, frameText)
|
|
||||||
|
|
||||||
clean := cleanKabaFrame(frameText)
|
|
||||||
if strings.HasPrefix(clean, "LA|") {
|
|
||||||
return registers, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
log.Warnf("Ignoring unexpected byte during Link Start: 0x%X", b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lock *KabaLockServer) requestEncoding(conn net.Conn, reader *bufio.Reader) (string, error) {
|
|
||||||
log.Printf("Sending Encoding command: %q", lock.command)
|
|
||||||
|
|
||||||
if _, err := conn.Write(lock.command); err != nil {
|
|
||||||
return "", fmt.Errorf("failed to send Encoding command: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
deadline := time.Now().Add(60 * time.Second)
|
|
||||||
|
|
||||||
for {
|
|
||||||
remaining := time.Until(deadline)
|
|
||||||
if remaining <= 0 {
|
|
||||||
return "", fmt.Errorf("timeout waiting for dormakaba encoding response")
|
|
||||||
}
|
|
||||||
|
|
||||||
conn.SetReadDeadline(time.Now().Add(remaining))
|
|
||||||
|
|
||||||
b, err := reader.ReadByte()
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("error reading encoding response: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch b {
|
|
||||||
case kabaACK:
|
|
||||||
log.Debug("Received ACK after Encoding command")
|
|
||||||
continue
|
|
||||||
|
|
||||||
case kabaNAK:
|
|
||||||
return "", fmt.Errorf("received NAK after Encoding command")
|
|
||||||
|
|
||||||
case kabaSTX:
|
|
||||||
frame, err := readKabaFrame(conn, reader, b, 60*time.Second)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to read encoding response frame: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
raw := string(frame)
|
|
||||||
clean := cleanKabaFrame(raw)
|
|
||||||
|
|
||||||
log.Printf("Received Encoding frame: %q", clean)
|
|
||||||
|
|
||||||
if strings.HasPrefix(clean, "KA|") {
|
|
||||||
return parseDormakabaEncodingResponse(clean)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Warnf("Ignoring non-KA frame while waiting for encoding result: %q", clean)
|
|
||||||
|
|
||||||
default:
|
|
||||||
log.Warnf("Ignoring unexpected byte while waiting for encoding response: 0x%X", b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseDormakabaEncodingResponse(clean string) (string, error) {
|
|
||||||
if strings.Contains(clean, "|ASOK|") {
|
|
||||||
return "Success: " + clean, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.Contains(clean, "|AS") {
|
|
||||||
return "", fmt.Errorf("negative dormakaba response: %s", clean)
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", fmt.Errorf("unexpected dormakaba response: %s", clean)
|
|
||||||
}
|
|
||||||
|
|
||||||
func readKabaFrame(conn net.Conn, reader *bufio.Reader, firstByte byte, timeout time.Duration) ([]byte, error) {
|
|
||||||
frame := []byte{firstByte}
|
|
||||||
|
|
||||||
for {
|
|
||||||
conn.SetReadDeadline(time.Now().Add(timeout))
|
|
||||||
|
|
||||||
b, err := reader.ReadByte()
|
|
||||||
if err != nil {
|
|
||||||
return frame, fmt.Errorf("error reading frame body: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
frame = append(frame, b)
|
|
||||||
|
|
||||||
if b == kabaETX {
|
|
||||||
return frame, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func wrapKabaFrame(payload string) []byte {
|
|
||||||
command := make([]byte, 0, len(payload)+2)
|
|
||||||
command = append(command, kabaSTX)
|
|
||||||
command = append(command, []byte(payload)...)
|
|
||||||
command = append(command, kabaETX)
|
|
||||||
|
|
||||||
return command
|
|
||||||
}
|
|
||||||
|
|
||||||
func cleanKabaFrame(raw string) string {
|
|
||||||
return strings.Trim(raw, string([]byte{kabaSTX, kabaETX}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func dormakabaPassword() string {
|
|
||||||
if strings.TrimSpace(Cert) != "" {
|
|
||||||
return strings.TrimSpace(Cert)
|
|
||||||
}
|
|
||||||
|
|
||||||
return "1234"
|
|
||||||
}
|
|
||||||
|
|
||||||
func dormakabaWorkstationID() string {
|
|
||||||
parsed, err := url.Parse(LockServerURL)
|
|
||||||
if err == nil && parsed.Host != "" {
|
|
||||||
host := parsed.Host
|
|
||||||
|
|
||||||
if h, _, splitErr := net.SplitHostPort(host); splitErr == nil {
|
|
||||||
return h
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Trim(host, "/")
|
|
||||||
}
|
|
||||||
|
|
||||||
raw := strings.TrimSpace(LockServerURL)
|
|
||||||
raw = strings.TrimPrefix(raw, "http://")
|
|
||||||
raw = strings.TrimPrefix(raw, "https://")
|
|
||||||
raw = strings.Trim(raw, "/")
|
|
||||||
|
|
||||||
if h, _, splitErr := net.SplitHostPort(raw); splitErr == nil {
|
|
||||||
return h
|
|
||||||
}
|
|
||||||
|
|
||||||
if idx := strings.Index(raw, ":"); idx >= 0 {
|
|
||||||
return raw[:idx]
|
|
||||||
}
|
|
||||||
|
|
||||||
return raw
|
|
||||||
}
|
|
||||||
@ -1,68 +0,0 @@
|
|||||||
package mail
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/logging"
|
|
||||||
mailjet "github.com/mailjet/mailjet-apiv3-go"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
apiKey = "60f358a27e98562641c08f51e5450c9e"
|
|
||||||
secretKey = "068b65c3b337a0e3c14389544ecd771f"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
moduleName = "mail"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// sendErrorEmail is the e-mail address to which to send an e-mail if there is an error during checkin or payment
|
|
||||||
SendErrorEmails []string
|
|
||||||
)
|
|
||||||
|
|
||||||
// SendMail will send reception an e-mail
|
|
||||||
func SendMail(recipient, title, message string) {
|
|
||||||
const funcName = "SendMail"
|
|
||||||
|
|
||||||
mailjetClient := mailjet.NewMailjetClient(apiKey, secretKey)
|
|
||||||
messagesInfo := []mailjet.InfoMessagesV31{
|
|
||||||
mailjet.InfoMessagesV31{
|
|
||||||
From: &mailjet.RecipientV31{
|
|
||||||
Email: "kiosk@cms.futuresens.co.uk",
|
|
||||||
Name: "Futuresens Kiosk",
|
|
||||||
},
|
|
||||||
To: &mailjet.RecipientsV31{
|
|
||||||
mailjet.RecipientV31{
|
|
||||||
Email: recipient,
|
|
||||||
Name: "",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Subject: title,
|
|
||||||
TextPart: message,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
messages := mailjet.MessagesV31{Info: messagesInfo}
|
|
||||||
_, err := mailjetClient.SendMailV31(&messages)
|
|
||||||
if err != nil {
|
|
||||||
theFields := log.Fields{}
|
|
||||||
theFields["mailerror"] = true
|
|
||||||
theFields["recipient"] = recipient
|
|
||||||
theFields[logging.LogFunction] = funcName
|
|
||||||
theFields[logging.LogModule] = moduleName
|
|
||||||
theFields[logging.LogError] = err.Error()
|
|
||||||
theFields["error"] = err.Error()
|
|
||||||
|
|
||||||
log.WithFields(theFields).Error("sendmail error")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func SendEmailOnError(hotel string, kiosk int, title, errMsg string) {
|
|
||||||
log.Println("sendEmailOnError called")
|
|
||||||
|
|
||||||
message := fmt.Sprintf("Hotel: %s, kiosk: %d.\n%s", hotel, kiosk, errMsg)
|
|
||||||
for _, recipient := range SendErrorEmails {
|
|
||||||
SendMail(recipient, title, message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
package mail
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Test_SendMail(t *testing.T) {
|
|
||||||
SendMail("zotacrtx5@gmail.com", "Test Subjectp", "Test Message")
|
|
||||||
}
|
|
||||||
@ -1,449 +0,0 @@
|
|||||||
package paybridge
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/url"
|
|
||||||
"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"
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Client struct {
|
|
||||||
URL string
|
|
||||||
APIKey string
|
|
||||||
TimeoutSeconds int
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewClient(url, apiKey string, timeoutSeconds int) *Client {
|
|
||||||
if timeoutSeconds <= 0 {
|
|
||||||
timeoutSeconds = 300
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Client{
|
|
||||||
URL: url,
|
|
||||||
APIKey: apiKey,
|
|
||||||
TimeoutSeconds: timeoutSeconds,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) Sale(ctx context.Context, req paymentsvc.SaleRequest, onStatus paymentsvc.StatusHandler) (*paymentsvc.Result, error) {
|
|
||||||
if req.Currency == "" {
|
|
||||||
req.Currency = "GBP"
|
|
||||||
}
|
|
||||||
|
|
||||||
payBridgeReq := PaymentRequest{
|
|
||||||
RequestID: req.RequestID,
|
|
||||||
Amount: req.Amount,
|
|
||||||
Currency: req.Currency,
|
|
||||||
Operation: "SALE",
|
|
||||||
TimeoutSeconds: c.TimeoutSeconds,
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.doPayment(ctx, payBridgeReq, onStatus)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
ws, _, err := websocket.DefaultDialer.DialContext(ctx, connectURL, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%w: %v", ErrConnectionFailed, err)
|
|
||||||
}
|
|
||||||
defer ws.Close()
|
|
||||||
|
|
||||||
done := make(chan struct{})
|
|
||||||
defer close(done)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
_ = ws.Close()
|
|
||||||
case <-done:
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
jwt, err := c.readAuthSuccess(ws)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Info("PayBridge authentication successful")
|
|
||||||
emitStatus(onStatus, paymentstatus.Started)
|
|
||||||
|
|
||||||
if err := ws.WriteJSON(Envelope{
|
|
||||||
Type: types.MesTypePaymentRequest,
|
|
||||||
JWT: jwt,
|
|
||||||
Data: req,
|
|
||||||
Timestamp: time.Now().UnixMilli(),
|
|
||||||
}); err != nil {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
// log.Println("PayBridge payment result raw:", string(raw))
|
|
||||||
if err := json.Unmarshal(raw, &result); err != nil {
|
|
||||||
return nil, fmt.Errorf("decode payment_result: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
Error struct {
|
|
||||||
Code string `json:"code"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
} `json:"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:
|
|
||||||
log.WithField(
|
|
||||||
"messageType",
|
|
||||||
head.Type,
|
|
||||||
).Warn("Unknown PayBridge intermediate message type")
|
|
||||||
|
|
||||||
emitStatus(onStatus, paymentstatus.Processing)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) connectURL() (string, error) {
|
|
||||||
u, err := url.Parse(c.URL)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf(
|
|
||||||
"parse PayBridge WebSocket URL: %w",
|
|
||||||
err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
q := u.Query()
|
|
||||||
q.Set("api_key", c.APIKey)
|
|
||||||
u.RawQuery = q.Encode()
|
|
||||||
|
|
||||||
return u.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
defer ws.SetReadDeadline(time.Time{})
|
|
||||||
|
|
||||||
_, raw, err := ws.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
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,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
package paybridge
|
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrConnectionFailed = errors.New("paybridge connection failed")
|
|
||||||
ErrAuthFailed = errors.New("paybridge authentication failed")
|
|
||||||
ErrUnexpectedMessage = errors.New("paybridge unexpected message")
|
|
||||||
)
|
|
||||||
@ -1,47 +0,0 @@
|
|||||||
package paybridge
|
|
||||||
|
|
||||||
import "gitea.futuresens.co.uk/futuresens/hardlink/internal/paymentsvc"
|
|
||||||
|
|
||||||
func mapPaymentResult(res PaymentResultEnvelope) *paymentsvc.Result {
|
|
||||||
var merchantReceipt string
|
|
||||||
if res.Data.ReceiptData.Merchant != nil {
|
|
||||||
merchantReceipt = *res.Data.ReceiptData.Merchant
|
|
||||||
}
|
|
||||||
|
|
||||||
return &paymentsvc.Result{
|
|
||||||
Success: res.Data.Success,
|
|
||||||
TransactionID: res.Data.TransactionID,
|
|
||||||
RequestID: res.Data.RequestID,
|
|
||||||
Operation: res.Data.Operation,
|
|
||||||
Status: res.Data.Status,
|
|
||||||
Message: res.Data.Message,
|
|
||||||
ErrorMessage: res.Data.ErrorMessage,
|
|
||||||
Amount: res.Data.Amount,
|
|
||||||
Currency: res.Data.Currency,
|
|
||||||
AuthCode: res.Data.AuthCode,
|
|
||||||
DeviceUsed: res.Data.DeviceUsed,
|
|
||||||
DeviceType: res.Data.DeviceType,
|
|
||||||
ReferenceNumber: res.Data.ReferenceNumber,
|
|
||||||
LastFourDigits: res.Data.LastFourDigits,
|
|
||||||
CardType: res.Data.CardType,
|
|
||||||
CardNumber: res.Data.CardNumber,
|
|
||||||
ExpiryDate: res.Data.ExpiryDate,
|
|
||||||
CardHash: res.Data.CardHash,
|
|
||||||
CardReference: res.Data.CardReference,
|
|
||||||
CustomerReceipt: res.Data.ReceiptData.Customer,
|
|
||||||
MerchantReceipt: merchantReceipt,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func mapPaymentError(req PaymentRequest, res PaymentErrorEnvelope) *paymentsvc.Result {
|
|
||||||
return &paymentsvc.Result{
|
|
||||||
Success: false,
|
|
||||||
TransactionID: res.Data.TransactionID,
|
|
||||||
RequestID: req.RequestID,
|
|
||||||
Operation: req.Operation,
|
|
||||||
Status: res.Data.Status,
|
|
||||||
ErrorMessage: res.Data.Error,
|
|
||||||
Amount: req.Amount,
|
|
||||||
Currency: req.Currency,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,41 +0,0 @@
|
|||||||
package paybridge
|
|
||||||
|
|
||||||
const (
|
|
||||||
payBridgeFinalStatusApproved = "APPROVED"
|
|
||||||
payBridgeFinalStatusDeclined = "DECLINED"
|
|
||||||
payBridgeFinalStatusCancelled = "CANCELLED"
|
|
||||||
payBridgeFinalStatusTimeout = "TIMEOUT"
|
|
||||||
payBridgeFinalStatusVoided = "VOIDED"
|
|
||||||
payBridgeFinalStatusVoidedDailyLimitExceeded = "VOIDED_DAILY_LIMIT_EXCEEDED"
|
|
||||||
payBridgeFinalStatusDailyLimitVoidFailed = "DAILY_LIMIT_EXCEEDED_VOID_FAILED"
|
|
||||||
payBridgeFinalStatusDailyLimitValidationError = "DAILY_LIMIT_VALIDATION_ERROR"
|
|
||||||
payBridgeFinalStatusError = "ERROR"
|
|
||||||
payBridgeFinalStatusFailed = "FAILED"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
payBridgeMessageInsertOrSwipeCard = "insert or swipe card"
|
|
||||||
payBridgeMessageInsertSwipeOrPresentCard = "insert, swipe or present card"
|
|
||||||
payBridgeMessageInsertCard = "insert card"
|
|
||||||
payBridgeMessagePleaseWait = "please wait"
|
|
||||||
payBridgeMessageDoNotRemoveCard = "please wait. do not remove card"
|
|
||||||
payBridgeMessageTryAnotherInterface = "please try another interface"
|
|
||||||
payBridgeMessagePIN = "pin"
|
|
||||||
payBridgeMessagePINAgain = "pin again"
|
|
||||||
payBridgeMessageTransactionCancelled = "transaction cancelled"
|
|
||||||
payBridgeMessageProcessing = "processing"
|
|
||||||
payBridgeMessageApproved = "approved"
|
|
||||||
payBridgeMessageDeclined = "declined"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
payBridgeCodeInsertCard = "101"
|
|
||||||
payBridgeCodeEnterPIN = "106"
|
|
||||||
payBridgeCodeCancelled = "124"
|
|
||||||
payBridgeCodePleaseWait = "1017"
|
|
||||||
payBridgeCodePresentCard = "1109"
|
|
||||||
payBridgeCodePresentCardAlternate = "1113"
|
|
||||||
payBridgeCodeEnterPINAgain = "1129"
|
|
||||||
payBridgeCodeTryAnotherInterface = "1274"
|
|
||||||
payBridgeCodeDoNotRemoveCard = "1312"
|
|
||||||
)
|
|
||||||
@ -1,125 +0,0 @@
|
|||||||
package paybridge
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/paymentstatus"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMapPayBridgeFinalStatus(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
status string
|
|
||||||
success bool
|
|
||||||
want string
|
|
||||||
}{
|
|
||||||
{"approved success", "APPROVED", true, paymentstatus.Approved},
|
|
||||||
{"approved without success", "APPROVED", false, paymentstatus.Error},
|
|
||||||
{"declined", "DECLINED", false, paymentstatus.Declined},
|
|
||||||
{"cancelled", "CANCELLED", false, paymentstatus.Cancelled},
|
|
||||||
{"timeout", "TIMEOUT", false, paymentstatus.Timeout},
|
|
||||||
{"voided", "VOIDED", false, paymentstatus.Voided},
|
|
||||||
{"voided daily limit exceeded", "VOIDED_DAILY_LIMIT_EXCEEDED", false, paymentstatus.DailyLimitExceeded},
|
|
||||||
{"daily limit void failed", "DAILY_LIMIT_EXCEEDED_VOID_FAILED", false, paymentstatus.VoidFailed},
|
|
||||||
{"daily limit validation error", "DAILY_LIMIT_VALIDATION_ERROR", false, paymentstatus.LimitValidationError},
|
|
||||||
{"error", "ERROR", false, paymentstatus.Error},
|
|
||||||
{"failed", "FAILED", false, paymentstatus.Error},
|
|
||||||
{"unknown", "UNKNOWN", false, paymentstatus.Error},
|
|
||||||
{"approved case insensitive", "approved", true, paymentstatus.Approved},
|
|
||||||
{"declined case insensitive", "declined", false, paymentstatus.Declined},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
got := mapPayBridgeFinalStatus(test.status, test.success)
|
|
||||||
if got != test.want {
|
|
||||||
t.Fatalf("got %q, want %q", got, test.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMapPayBridgeStatusMessages(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
status string
|
|
||||||
want string
|
|
||||||
}{
|
|
||||||
{"insert or swipe card", "insert or swipe card", paymentstatus.PresentCard},
|
|
||||||
{"insert swipe or present card", "insert, swipe or present card", paymentstatus.PresentCard},
|
|
||||||
{"insert card", "insert card", paymentstatus.InsertCard},
|
|
||||||
{"please wait", "please wait", paymentstatus.PleaseWait},
|
|
||||||
{"do not remove card", "please wait. do not remove card", paymentstatus.DoNotRemoveCard},
|
|
||||||
{"try another interface", "please try another interface", paymentstatus.TryAnotherInterface},
|
|
||||||
{"pin", "pin", paymentstatus.EnterPIN},
|
|
||||||
{"pin again", "pin again", paymentstatus.EnterPINAgain},
|
|
||||||
{"transaction cancelled", "transaction cancelled", paymentstatus.Cancelled},
|
|
||||||
{"processing", "processing", paymentstatus.Processing},
|
|
||||||
{"approved", "approved", paymentstatus.Authorized},
|
|
||||||
{"declined", "declined", paymentstatus.Declined},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
got, matched := mapPayBridgeStatus(test.status, "")
|
|
||||||
if !matched {
|
|
||||||
t.Fatal("expected status to match")
|
|
||||||
}
|
|
||||||
if got != test.want {
|
|
||||||
t.Fatalf("got %q, want %q", got, test.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMapPayBridgeStatusCodes(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
code string
|
|
||||||
want string
|
|
||||||
}{
|
|
||||||
{"insert card", "101", paymentstatus.InsertCard},
|
|
||||||
{"enter pin", "106", paymentstatus.EnterPIN},
|
|
||||||
{"cancelled", "124", paymentstatus.Cancelled},
|
|
||||||
{"please wait", "1017", paymentstatus.PleaseWait},
|
|
||||||
{"present card", "1109", paymentstatus.PresentCard},
|
|
||||||
{"present card alternate", "1113", paymentstatus.PresentCard},
|
|
||||||
{"enter pin again", "1129", paymentstatus.EnterPINAgain},
|
|
||||||
{"try another interface", "1274", paymentstatus.TryAnotherInterface},
|
|
||||||
{"do not remove card", "1312", paymentstatus.DoNotRemoveCard},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
got, matched := mapPayBridgeStatus("", test.code)
|
|
||||||
if !matched {
|
|
||||||
t.Fatal("expected code to match")
|
|
||||||
}
|
|
||||||
if got != test.want {
|
|
||||||
t.Fatalf("got %q, want %q", got, test.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMapPayBridgeStatusNormalization(t *testing.T) {
|
|
||||||
got, matched := mapPayBridgeStatus(" PiN AgAiN ", "")
|
|
||||||
if !matched || got != paymentstatus.EnterPINAgain {
|
|
||||||
t.Fatalf("normalized message got (%q, %t), want (%q, true)", got, matched, paymentstatus.EnterPINAgain)
|
|
||||||
}
|
|
||||||
|
|
||||||
got, matched = mapPayBridgeStatus("", " 101 ")
|
|
||||||
if !matched || got != paymentstatus.InsertCard {
|
|
||||||
t.Fatalf("trimmed code got (%q, %t), want (%q, true)", got, matched, paymentstatus.InsertCard)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMapPayBridgeStatusUnknown(t *testing.T) {
|
|
||||||
got, matched := mapPayBridgeStatus("unknown message", "999")
|
|
||||||
if matched {
|
|
||||||
t.Fatal("unknown status and code must not match")
|
|
||||||
}
|
|
||||||
if got != paymentstatus.Processing {
|
|
||||||
t.Fatalf("got %q, want %q", got, paymentstatus.Processing)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,65 +0,0 @@
|
|||||||
package paybridge
|
|
||||||
|
|
||||||
type Envelope struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Data any `json:"data,omitempty"`
|
|
||||||
Timestamp int64 `json:"timestamp,omitempty"`
|
|
||||||
JWT string `json:"jwt,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type PaymentRequest struct {
|
|
||||||
RequestID string `json:"requestId"`
|
|
||||||
Amount int64 `json:"amount"`
|
|
||||||
Currency string `json:"currency"`
|
|
||||||
Operation string `json:"operation"`
|
|
||||||
TimeoutSeconds int `json:"timeoutSeconds,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type PaymentResultEnvelope struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Data struct {
|
|
||||||
Success bool `json:"success"`
|
|
||||||
TransactionID string `json:"transactionId"`
|
|
||||||
RequestID string `json:"requestId"`
|
|
||||||
Operation string `json:"operation"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
ErrorMessage string `json:"errorMessage"`
|
|
||||||
Amount int64 `json:"amount"`
|
|
||||||
Currency string `json:"currency"`
|
|
||||||
AuthCode string `json:"authCode"`
|
|
||||||
DeviceUsed string `json:"deviceUsed"`
|
|
||||||
DeviceType string `json:"deviceType"`
|
|
||||||
ReferenceNumber string `json:"referenceNumber"`
|
|
||||||
LastFourDigits string `json:"lastFourDigits"`
|
|
||||||
CardType string `json:"cardType"`
|
|
||||||
CardNumber string `json:"cardNumber"`
|
|
||||||
ExpiryDate string `json:"expiryDate"`
|
|
||||||
CardHash string `json:"cardHash"`
|
|
||||||
CardReference string `json:"cardReference"`
|
|
||||||
|
|
||||||
ReceiptData struct {
|
|
||||||
Merchant *string `json:"merchant"`
|
|
||||||
Customer string `json:"customer"`
|
|
||||||
} `json:"receiptData"`
|
|
||||||
} `json:"data"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type PaymentErrorEnvelope struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Data struct {
|
|
||||||
TransactionID string `json:"transactionId"`
|
|
||||||
Error string `json:"error"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
} `json:"data"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type StatusUpdateEnvelope struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
|
|
||||||
Data struct {
|
|
||||||
Status string `json:"status"`
|
|
||||||
Code string `json:"code"`
|
|
||||||
Timestamp int64 `json:"timestamp"`
|
|
||||||
} `json:"data"`
|
|
||||||
}
|
|
||||||
@ -1,65 +0,0 @@
|
|||||||
package paymentsvc
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrPaymentInProgress = errors.New("payment is already in progress")
|
|
||||||
ErrProviderNotConfigured = errors.New("payment provider is not configured")
|
|
||||||
)
|
|
||||||
|
|
||||||
type StatusUpdate struct {
|
|
||||||
Code string `json:"code"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type StatusHandler func(StatusUpdate)
|
|
||||||
|
|
||||||
type Provider interface {
|
|
||||||
Sale(
|
|
||||||
ctx context.Context,
|
|
||||||
req SaleRequest,
|
|
||||||
onStatus StatusHandler,
|
|
||||||
) (*Result, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Service struct {
|
|
||||||
provider Provider
|
|
||||||
|
|
||||||
mu sync.Mutex
|
|
||||||
busy bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewService(provider Provider) *Service {
|
|
||||||
return &Service{
|
|
||||||
provider: provider,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Service) Sale(
|
|
||||||
ctx context.Context,
|
|
||||||
req SaleRequest,
|
|
||||||
onStatus StatusHandler,
|
|
||||||
) (*Result, error) {
|
|
||||||
if s == nil || s.provider == nil {
|
|
||||||
return nil, ErrProviderNotConfigured
|
|
||||||
}
|
|
||||||
|
|
||||||
s.mu.Lock()
|
|
||||||
if s.busy {
|
|
||||||
s.mu.Unlock()
|
|
||||||
return nil, ErrPaymentInProgress
|
|
||||||
}
|
|
||||||
s.busy = true
|
|
||||||
s.mu.Unlock()
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
s.mu.Lock()
|
|
||||||
s.busy = false
|
|
||||||
s.mu.Unlock()
|
|
||||||
}()
|
|
||||||
|
|
||||||
return s.provider.Sale(ctx, req, onStatus)
|
|
||||||
}
|
|
||||||
@ -1,37 +0,0 @@
|
|||||||
package paymentsvc
|
|
||||||
|
|
||||||
type SaleRequest struct {
|
|
||||||
RequestID string `json:"requestId"`
|
|
||||||
Reference string `json:"reference"`
|
|
||||||
Amount int64 `json:"amount"`
|
|
||||||
Currency string `json:"currency"`
|
|
||||||
}
|
|
||||||
|
|
||||||
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"`
|
|
||||||
|
|
||||||
TransactionID string `json:"transactionId,omitempty"`
|
|
||||||
ReferenceNumber string `json:"referenceNumber,omitempty"`
|
|
||||||
AuthCode string `json:"authCode,omitempty"`
|
|
||||||
|
|
||||||
Amount int64 `json:"amount"`
|
|
||||||
Currency string `json:"currency"`
|
|
||||||
|
|
||||||
DeviceUsed string `json:"deviceUsed,omitempty"`
|
|
||||||
DeviceType string `json:"deviceType,omitempty"`
|
|
||||||
|
|
||||||
CardNumber string `json:"cardNumber,omitempty"`
|
|
||||||
LastFourDigits string `json:"lastFourDigits,omitempty"`
|
|
||||||
CardType string `json:"cardType,omitempty"`
|
|
||||||
ExpiryDate string `json:"expiryDate,omitempty"`
|
|
||||||
CardHash string `json:"cardHash,omitempty"`
|
|
||||||
CardReference string `json:"cardReference,omitempty"`
|
|
||||||
|
|
||||||
CustomerReceipt string `json:"customerReceipt,omitempty"`
|
|
||||||
MerchantReceipt string `json:"merchantReceipt,omitempty"`
|
|
||||||
}
|
|
||||||
@ -1,16 +1,15 @@
|
|||||||
// Package lockserver provides functionality for interacting with Assa Abloy lock servers.
|
|
||||||
package lockserver
|
package lockserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
"strings"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BuildCommand builds a key encoding request command for the Assa Abloy lock server.
|
// Build key encoding request command for the Assa Abloy lock server.
|
||||||
func (lock *AssaLockServer) BuildCommand(doorReq DoorCardRequest, checkIn, checkOut time.Time) error {
|
func (lock *AssaLockServer) BuildCommand(doorReq DoorCardRequest, checkIn, checkOut time.Time) error {
|
||||||
ci := checkIn.Format("200601021504")
|
ci := checkIn.Format("200601021504")
|
||||||
co := checkOut.Format("200601021504")
|
co := checkOut.Format("200601021504")
|
||||||
@ -19,7 +18,7 @@ func (lock *AssaLockServer) BuildCommand(doorReq DoorCardRequest, checkIn, check
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LockSequence checks heartbeat of the Assa Abloy lock server and performs key encoding
|
// Checks heart beat of the Assa Abloy lock server and perform key encoding
|
||||||
func (lock *AssaLockServer) LockSequence() error {
|
func (lock *AssaLockServer) LockSequence() error {
|
||||||
const funcName = "AssaLockServer.LockSequence"
|
const funcName = "AssaLockServer.LockSequence"
|
||||||
|
|
||||||
@ -72,3 +71,4 @@ func parseAssaResponse(raw string) (string, error) {
|
|||||||
}
|
}
|
||||||
return "Success: " + clean, nil
|
return "Success: " + clean, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,11 +25,10 @@ const (
|
|||||||
Omnitec = "omnitec"
|
Omnitec = "omnitec"
|
||||||
Salto = "salto"
|
Salto = "salto"
|
||||||
TLJ = "tlj"
|
TLJ = "tlj"
|
||||||
Dormakaba = "kaba"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Cert string
|
Cert string
|
||||||
LockServerURL string
|
LockServerURL string
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -39,11 +38,6 @@ type (
|
|||||||
LockSequence() error
|
LockSequence() error
|
||||||
}
|
}
|
||||||
|
|
||||||
KabaLockServer struct {
|
|
||||||
encoderAddr string
|
|
||||||
command []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
AssaLockServer struct {
|
AssaLockServer struct {
|
||||||
encoderAddr string
|
encoderAddr string
|
||||||
command string
|
command string
|
||||||
@ -75,24 +69,22 @@ func NewLockServer(lockType, encoderAddr string, fatalError func(error)) LockSer
|
|||||||
return &SaltoLockServer{encoderAddr: encoderAddr}
|
return &SaltoLockServer{encoderAddr: encoderAddr}
|
||||||
case TLJ:
|
case TLJ:
|
||||||
return &TLJLockServer{encoderAddr: encoderAddr}
|
return &TLJLockServer{encoderAddr: encoderAddr}
|
||||||
case Dormakaba:
|
|
||||||
return &KabaLockServer{encoderAddr: encoderAddr}
|
|
||||||
default:
|
default:
|
||||||
fatalError(fmt.Errorf("unsupported LockType: %s; must be 'assaabloy' or 'omnitec'", lockType))
|
fatalError(fmt.Errorf("unsupported LockType: %s; must be 'assaabloy' or 'omnitec'", lockType))
|
||||||
return nil // This line will never be reached, but is needed to satisfy the compiler
|
return nil // This line will never be reached, but is needed to satisfy the compiler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitializeServerConnection(LockserverURL string) (net.Conn, error) {
|
func InitializeServerConnection(LockserverUrl string) (net.Conn, error) {
|
||||||
const funcName = "InitializeServerConnection"
|
const funcName = "InitializeServerConnection"
|
||||||
// Parse the URL to extract host and port
|
// Parse the URL to extract host and port
|
||||||
parsedURL, err := url.Parse(LockserverURL)
|
parsedUrl, err := url.Parse(LockserverUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("[%s] failed to parse LockserverURL: %v", funcName, err)
|
return nil, fmt.Errorf("[%s] failed to parse LockserverUrl: %v", funcName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove any leading/trailing slashes just in case
|
// Remove any leading/trailing slashes just in case
|
||||||
address := strings.Trim(parsedURL.Host, "/")
|
address := strings.Trim(parsedUrl.Host, "/")
|
||||||
|
|
||||||
// Establish a TCP connection to the Visionline server
|
// Establish a TCP connection to the Visionline server
|
||||||
conn, err := net.Dial("tcp", address)
|
conn, err := net.Dial("tcp", address)
|
||||||
@ -110,7 +102,7 @@ func sendAndReceive(conn net.Conn, command []byte) (string, error) {
|
|||||||
return "", fmt.Errorf("failed to send command: %v", err)
|
return "", fmt.Errorf("failed to send command: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.SetReadDeadline(time.Now().Add(20 * time.Second))
|
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
|
||||||
|
|
||||||
buf := make([]byte, 128)
|
buf := make([]byte, 128)
|
||||||
reader := bufio.NewReader(conn)
|
reader := bufio.NewReader(conn)
|
||||||
@ -7,12 +7,12 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BuildCommand builds key encoding request command for the Omnitec lock server.
|
// Build key encoding request command for the Omnitec lock server.
|
||||||
func (lock *OmniLockServer) BuildCommand(doorReq DoorCardRequest, checkIn, checkOut time.Time) error {
|
func (lock *OmniLockServer) BuildCommand(doorReq DoorCardRequest, checkIn, checkOut time.Time) error {
|
||||||
const funcName = "OmniLockServer.BuildCommand"
|
const funcName = "OmniLockServer.BuildCommand"
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
@ -25,7 +25,7 @@ func (lock *OmniLockServer) BuildCommand(doorReq DoorCardRequest, checkIn, check
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("[%s] failed to convert lockId to integer: %v", funcName, err)
|
return fmt.Errorf("[%s] failed to convert lockId to integer: %v", funcName, err)
|
||||||
}
|
}
|
||||||
formattedLockID := fmt.Sprintf("%04d", idInt)
|
formattedLockId := fmt.Sprintf("%04d", idInt)
|
||||||
|
|
||||||
// Format date/time parts
|
// Format date/time parts
|
||||||
dt := checkOut.Format("15:04") // DT = HH:mm
|
dt := checkOut.Format("15:04") // DT = HH:mm
|
||||||
@ -37,7 +37,7 @@ func (lock *OmniLockServer) BuildCommand(doorReq DoorCardRequest, checkIn, check
|
|||||||
payload := fmt.Sprintf(
|
payload := fmt.Sprintf(
|
||||||
"KR|KC%s|KTD|RN%s|%s|DT%s|G#75|GA%s|GD%s|KO0000|DA%s|TI%s|",
|
"KR|KC%s|KTD|RN%s|%s|DT%s|G#75|GA%s|GD%s|KO0000|DA%s|TI%s|",
|
||||||
lock.encoderAddr,
|
lock.encoderAddr,
|
||||||
formattedLockID,
|
formattedLockId,
|
||||||
hostname,
|
hostname,
|
||||||
dt,
|
dt,
|
||||||
ga,
|
ga,
|
||||||
@ -52,7 +52,7 @@ func (lock *OmniLockServer) BuildCommand(doorReq DoorCardRequest, checkIn, check
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LockSequence starts link to the Omnitec lock server and perform key encoding
|
// Starts link to the Omnitec lock server and perform key encoding
|
||||||
func (lock *OmniLockServer) LockSequence() error {
|
func (lock *OmniLockServer) LockSequence() error {
|
||||||
const funcName = "OmniLockServer.LockSequence"
|
const funcName = "OmniLockServer.LockSequence"
|
||||||
|
|
||||||
@ -136,4 +136,4 @@ func parseOmniResponse(raw string) (string, error) {
|
|||||||
return "", fmt.Errorf("negative response code: %s", clean)
|
return "", fmt.Errorf("negative response code: %s", clean)
|
||||||
}
|
}
|
||||||
return "Success: " + clean, nil
|
return "Success: " + clean, nil
|
||||||
}
|
}
|
||||||
@ -155,7 +155,7 @@ func (lock *SaltoLockServer) LockSequence() error {
|
|||||||
reader := bufio.NewReader(conn)
|
reader := bufio.NewReader(conn)
|
||||||
|
|
||||||
// 1. Send ENQ
|
// 1. Send ENQ
|
||||||
log.Infof("LockSequence: sending ENQ")
|
log.Infof("Sending ENQ")
|
||||||
if _, e := conn.Write([]byte{ENQ}); e != nil {
|
if _, e := conn.Write([]byte{ENQ}); e != nil {
|
||||||
return fmt.Errorf("failed to send ENQ: %w", e)
|
return fmt.Errorf("failed to send ENQ: %w", e)
|
||||||
}
|
}
|
||||||
@ -166,7 +166,7 @@ func (lock *SaltoLockServer) LockSequence() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 3. Send command frame
|
// 3. Send command frame
|
||||||
log.Infof("LockSequence: sending encoding command: %q", string(lock.command))
|
log.Infof("Sending encoding command: %q", string(lock.command))
|
||||||
if _, e := conn.Write(lock.command); e != nil {
|
if _, e := conn.Write(lock.command); e != nil {
|
||||||
return fmt.Errorf("failed to send command frame: %w", e)
|
return fmt.Errorf("failed to send command frame: %w", e)
|
||||||
}
|
}
|
||||||
192
main.go
Normal file
192
main.go
Normal file
@ -0,0 +1,192 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"os/signal"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/tarm/serial"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"gitea.futuresens.co.uk/futuresens/hardlink/bootstrap"
|
||||||
|
"gitea.futuresens.co.uk/futuresens/hardlink/config"
|
||||||
|
"gitea.futuresens.co.uk/futuresens/hardlink/dispenser"
|
||||||
|
"gitea.futuresens.co.uk/futuresens/hardlink/handlers"
|
||||||
|
"gitea.futuresens.co.uk/futuresens/hardlink/lockserver"
|
||||||
|
"gitea.futuresens.co.uk/futuresens/hardlink/logging"
|
||||||
|
"gitea.futuresens.co.uk/futuresens/hardlink/printer"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
buildVersion = "1.0.28"
|
||||||
|
serviceName = "hardlink"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Load config
|
||||||
|
config := config.ReadHardlinkConfig()
|
||||||
|
printer.Layout = readTicketLayout()
|
||||||
|
printer.PrinterName = config.PrinterName
|
||||||
|
lockserver.Cert = config.Cert
|
||||||
|
lockserver.LockServerURL = config.LockserverUrl
|
||||||
|
dispHandle := &serial.Port{}
|
||||||
|
|
||||||
|
// Setup logging and get file handle
|
||||||
|
logFile, err := logging.SetupLogging(config.LogDir, serviceName, buildVersion)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to set up logging: %v\n", err)
|
||||||
|
}
|
||||||
|
defer logFile.Close()
|
||||||
|
|
||||||
|
// Initialize dispenser
|
||||||
|
if !config.TestMode {
|
||||||
|
dispenser.SerialPort = config.DispenserPort
|
||||||
|
dispenser.Address = []byte(config.DispenserAdrr)
|
||||||
|
dispHandle, err = dispenser.InitializeDispenser()
|
||||||
|
if err != nil {
|
||||||
|
handlers.FatalError(err)
|
||||||
|
}
|
||||||
|
defer dispHandle.Close()
|
||||||
|
|
||||||
|
status, err := dispenser.CheckDispenserStatus(dispHandle)
|
||||||
|
if err != nil {
|
||||||
|
if len(status) == 0 {
|
||||||
|
err = fmt.Errorf("%s; wrong dispenser address: %s", err, config.DispenserAdrr)
|
||||||
|
handlers.FatalError(err)
|
||||||
|
} else {
|
||||||
|
fmt.Println(status)
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if status, err = dispenser.DispenserSequence(dispHandle); err != nil {
|
||||||
|
handlers.FatalError(err)
|
||||||
|
}
|
||||||
|
log.Infof("Dispenser initialized on port %s, %s", config.DispenserPort, status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test lock-server connection
|
||||||
|
switch strings.ToLower(config.LockType) {
|
||||||
|
case lockserver.TLJ:
|
||||||
|
|
||||||
|
default:
|
||||||
|
lockConn, err := lockserver.InitializeServerConnection(config.LockserverUrl)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
log.Errorf(err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Connected to the lock server successfuly at %s\n", config.LockserverUrl)
|
||||||
|
log.Infof("Connected to the lock server successfuly at %s", config.LockserverUrl)
|
||||||
|
lockConn.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
database, err := bootstrap.OpenDB(&config)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("DB init failed: %v", err)
|
||||||
|
}
|
||||||
|
defer database.Close()
|
||||||
|
|
||||||
|
if config.IsPayment {
|
||||||
|
fmt.Println("Payment processing is enabled")
|
||||||
|
log.Info("Payment processing is enabled")
|
||||||
|
startChipDnaClient()
|
||||||
|
} else {
|
||||||
|
fmt.Println("Payment processing is disabled")
|
||||||
|
log.Info("Payment processing is disabled")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create App and wire routes
|
||||||
|
app := handlers.NewApp(dispHandle, config.LockType, config.EncoderAddress, database, config.IsPayment)
|
||||||
|
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
app.RegisterRoutes(mux)
|
||||||
|
|
||||||
|
addr := fmt.Sprintf(":%d", config.Port)
|
||||||
|
log.Infof("Starting HTTP server on http://localhost%s", addr)
|
||||||
|
fmt.Printf("Starting HTTP server on http://localhost%s", addr)
|
||||||
|
if err := http.ListenAndServe(addr, mux); err != nil {
|
||||||
|
handlers.FatalError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func readTicketLayout() printer.LayoutOptions {
|
||||||
|
const layoutName = "TicketLayout.xml"
|
||||||
|
var layout printer.LayoutOptions
|
||||||
|
|
||||||
|
// 1) Read the file
|
||||||
|
data, err := os.ReadFile(layoutName)
|
||||||
|
if err != nil {
|
||||||
|
handlers.FatalError(fmt.Errorf("failed to read %s: %v", layoutName, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2) Unmarshal into your struct
|
||||||
|
if err := xml.Unmarshal(data, &layout); err != nil {
|
||||||
|
handlers.FatalError(fmt.Errorf("failed to parse %s: %v", layoutName, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return layout
|
||||||
|
}
|
||||||
|
|
||||||
|
func startChipDnaClient() {
|
||||||
|
startClient := func() (*exec.Cmd, error) {
|
||||||
|
cmd := exec.Command("./ChipDNAClient/ChipDnaClient.exe")
|
||||||
|
err := cmd.Start()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to start ChipDnaClient: %v", err)
|
||||||
|
}
|
||||||
|
log.Infof("ChipDnaClient started with PID %d", cmd.Process.Pid)
|
||||||
|
return cmd, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd, err := startClient()
|
||||||
|
if err != nil {
|
||||||
|
handlers.FatalError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restart loop
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
err := cmd.Wait()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("ChipDnaClient exited unexpectedly: %v", err)
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
cmd, err = startClient()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Restart failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Info("ChipDnaClient restarted successfully")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Handle shutdown signals
|
||||||
|
sigs := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
go func() {
|
||||||
|
<-sigs
|
||||||
|
log.Info("Shutting down...")
|
||||||
|
if cmd.Process != nil {
|
||||||
|
log.Info("Sending SIGTERM to ChipDnaClient...")
|
||||||
|
_ = cmd.Process.Signal(syscall.SIGTERM)
|
||||||
|
// wait up to 5s for graceful shutdown
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() { done <- cmd.Wait() }()
|
||||||
|
select {
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
log.Warn("ChipDnaClient did not exit in time, killing...")
|
||||||
|
_ = cmd.Process.Kill()
|
||||||
|
case err := <-done:
|
||||||
|
log.Infof("ChipDnaClient exited cleanly: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
}()
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package creditcall
|
package payment
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
@ -9,7 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
"gitea.futuresens.co.uk/futuresens/cmstypes"
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
"gitea.futuresens.co.uk/futuresens/hardlink/types"
|
||||||
_ "github.com/denisenkom/go-mssqldb"
|
_ "github.com/denisenkom/go-mssqldb"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@ -50,12 +50,6 @@ type (
|
|||||||
transactionRes string
|
transactionRes string
|
||||||
transactionState string
|
transactionState string
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfirmTransactionRequest struct {
|
|
||||||
XMLName xml.Name `xml:"ConfirmTransactionRequest"`
|
|
||||||
Amount string `xml:"Amount"`
|
|
||||||
Reference string `xml:"TransactionReference"`
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ParseTransactionResult parses the XML into entries.
|
// ParseTransactionResult parses the XML into entries.
|
||||||
@ -78,10 +72,13 @@ func (ti *TransactionInfo) FillFromTransactionResult(trResult TransactionResultX
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *PaymentResult) FillFromTransactionResult(trResult TransactionResultXML) {
|
func (r *PaymentResult) FillFromTransactionResult(trResult TransactionResultXML) {
|
||||||
r.Fields = make(map[string]string)
|
if r.Fields == nil {
|
||||||
|
r.Fields = make(map[string]string)
|
||||||
|
}
|
||||||
|
|
||||||
for _, e := range trResult.Entries {
|
for _, e := range trResult.Entries {
|
||||||
switch e.Key {
|
switch e.Key {
|
||||||
|
|
||||||
case types.ReceiptData, types.ReceiptDataMerchant:
|
case types.ReceiptData, types.ReceiptDataMerchant:
|
||||||
// intentionally ignored
|
// intentionally ignored
|
||||||
|
|
||||||
@ -99,7 +96,7 @@ func (r *PaymentResult) FillFromTransactionResult(trResult TransactionResultXML)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// BuildPaymentRedirectURL builds the redirect URL to send the guest to after payment.
|
// BuildRedirectURL builds the redirect URL to send the guest to after payment.
|
||||||
func BuildPaymentRedirectURL(result map[string]string) string {
|
func BuildPaymentRedirectURL(result map[string]string) string {
|
||||||
res := result[types.TransactionResult]
|
res := result[types.TransactionResult]
|
||||||
|
|
||||||
@ -110,7 +107,7 @@ func BuildPaymentRedirectURL(result map[string]string) string {
|
|||||||
log.WithField(types.LogResult, result[types.ConfirmResult]).
|
log.WithField(types.LogResult, result[types.ConfirmResult]).
|
||||||
Info("Transaction approved and confirmed")
|
Info("Transaction approved and confirmed")
|
||||||
|
|
||||||
return BuildSuccessURL(result)
|
return buildSuccessURL(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not confirmed
|
// Not confirmed
|
||||||
@ -136,7 +133,7 @@ func BuildPreauthRedirectURL(result map[string]string) (string, bool) {
|
|||||||
log.WithField(types.LogResult, result[types.TransactionResult]).
|
log.WithField(types.LogResult, result[types.TransactionResult]).
|
||||||
Info("Account verification approved")
|
Info("Account verification approved")
|
||||||
|
|
||||||
return BuildSuccessURL(result), false
|
return buildSuccessURL(result), false
|
||||||
|
|
||||||
// Transaction type Sale?
|
// Transaction type Sale?
|
||||||
case strings.EqualFold(tType, types.SaleTransactionType):
|
case strings.EqualFold(tType, types.SaleTransactionType):
|
||||||
@ -144,7 +141,7 @@ func BuildPreauthRedirectURL(result map[string]string) (string, bool) {
|
|||||||
log.WithField(types.LogResult, result[types.ConfirmResult]).
|
log.WithField(types.LogResult, result[types.ConfirmResult]).
|
||||||
Info("Amount preauthorized successfully")
|
Info("Amount preauthorized successfully")
|
||||||
|
|
||||||
return BuildSuccessURL(result), true
|
return buildSuccessURL(result), true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,11 +149,10 @@ func BuildPreauthRedirectURL(result map[string]string) (string, bool) {
|
|||||||
return BuildFailureURL(res, result[types.Errors]), false
|
return BuildFailureURL(res, result[types.Errors]), false
|
||||||
}
|
}
|
||||||
|
|
||||||
func BuildSuccessURL(result map[string]string) string {
|
func buildSuccessURL(result map[string]string) string {
|
||||||
q := url.Values{}
|
q := url.Values{}
|
||||||
q.Set("CardNumber", hex.EncodeToString([]byte(result[types.PanMasked])))
|
q.Set("CardNumber", hex.EncodeToString([]byte(result[types.PAN_MASKED])))
|
||||||
q.Set("CardType", hex.EncodeToString([]byte(result[types.CardType])))
|
q.Set("ExpiryDate", hex.EncodeToString([]byte(result[types.EXPIRY_DATE])))
|
||||||
q.Set("ExpiryDate", hex.EncodeToString([]byte(result[types.ExpiryDate])))
|
|
||||||
q.Set("TxnReference", result[types.Reference])
|
q.Set("TxnReference", result[types.Reference])
|
||||||
q.Set("CardHash", hex.EncodeToString([]byte(result[types.CardHash])))
|
q.Set("CardHash", hex.EncodeToString([]byte(result[types.CardHash])))
|
||||||
q.Set("CardReference", hex.EncodeToString([]byte(result[types.CardReference])))
|
q.Set("CardReference", hex.EncodeToString([]byte(result[types.CardReference])))
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package creditcall
|
package payment
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -11,8 +11,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/db"
|
"gitea.futuresens.co.uk/futuresens/hardlink/db"
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
"gitea.futuresens.co.uk/futuresens/hardlink/types"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
/* ==============================
|
/* ==============================
|
||||||
Public Entry Point
|
Public Entry Point (LEGACY)
|
||||||
============================== */
|
============================== */
|
||||||
|
|
||||||
func ReleasePreauthorizations(database *sql.DB) error {
|
func ReleasePreauthorizations(database *sql.DB) error {
|
||||||
@ -78,7 +78,6 @@ func handlePreauthRelease(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Infof("res=%s state=%s", info.transactionRes, info.transactionState)
|
|
||||||
|
|
||||||
// If already voided or declined → mark released
|
// If already voided or declined → mark released
|
||||||
if isAlreadyReleased(info) {
|
if isAlreadyReleased(info) {
|
||||||
@ -87,7 +86,8 @@ func handlePreauthRelease(
|
|||||||
|
|
||||||
// Only void approved + uncommitted
|
// Only void approved + uncommitted
|
||||||
if !isVoidable(info) {
|
if !isVoidable(info) {
|
||||||
log.Infof("Preauth %s not eligible for void (res=%s state=%s)", ref, info.transactionRes, info.transactionState)
|
log.Infof("Preauth %s not eligible for void (res=%s state=%s)",
|
||||||
|
ref, info.transactionRes, info.transactionState)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1,41 +0,0 @@
|
|||||||
// Package paymentstatus defines progress codes emitted by Hardlink's
|
|
||||||
// POST /api/payment/sale stream. These codes are not authoritative final
|
|
||||||
// transaction decisions: clients may continue check-in only after receiving a
|
|
||||||
// final result frame whose response.data begins with /successful.
|
|
||||||
package paymentstatus
|
|
||||||
|
|
||||||
const (
|
|
||||||
Starting = "PAYMENT_STARTING"
|
|
||||||
Started = "PAYMENT_STARTED"
|
|
||||||
RequestSent = "PAYMENT_REQUEST_SENT"
|
|
||||||
Accepted = "PAYMENT_ACCEPTED"
|
|
||||||
PresentCard = "PAYMENT_PRESENT_CARD"
|
|
||||||
InsertCard = "PAYMENT_INSERT_CARD"
|
|
||||||
SwipeCard = "PAYMENT_SWIPE_CARD"
|
|
||||||
EnterPIN = "PAYMENT_ENTER_PIN"
|
|
||||||
EnterPINAgain = "PAYMENT_ENTER_PIN_AGAIN"
|
|
||||||
PleaseWait = "PAYMENT_PLEASE_WAIT"
|
|
||||||
DoNotRemoveCard = "PAYMENT_DO_NOT_REMOVE_CARD"
|
|
||||||
RemoveCard = "PAYMENT_REMOVE_CARD"
|
|
||||||
TryAnotherInterface = "PAYMENT_TRY_ANOTHER_INTERFACE"
|
|
||||||
Processing = "PAYMENT_PROCESSING"
|
|
||||||
Authorized = "PAYMENT_AUTHORIZED"
|
|
||||||
Approved = "PAYMENT_APPROVED"
|
|
||||||
Cancelling = "PAYMENT_CANCELLING"
|
|
||||||
Cancelled = "PAYMENT_CANCELLED"
|
|
||||||
Declined = "PAYMENT_DECLINED"
|
|
||||||
Expired = "PAYMENT_EXPIRED"
|
|
||||||
Timeout = "PAYMENT_TIMEOUT"
|
|
||||||
SignatureRequired = "PAYMENT_SIGNATURE_REQUIRED"
|
|
||||||
SignatureRejecting = "PAYMENT_SIGNATURE_REJECTING"
|
|
||||||
SignatureRejected = "PAYMENT_SIGNATURE_REJECTED"
|
|
||||||
Voided = "PAYMENT_VOIDED"
|
|
||||||
DailyLimitExceeded = "PAYMENT_DAILY_LIMIT_EXCEEDED"
|
|
||||||
VoidFailed = "PAYMENT_VOID_FAILED"
|
|
||||||
LimitValidationError = "PAYMENT_LIMIT_VALIDATION_ERROR"
|
|
||||||
Error = "PAYMENT_ERROR"
|
|
||||||
TerminalUnavailable = "PAYMENT_TERMINAL_UNAVAILABLE"
|
|
||||||
|
|
||||||
DojoNotificationPrefix = "PAYMENT_DOJO_NOTIFICATION_"
|
|
||||||
DojoStatusPrefix = "PAYMENT_DOJO_STATUS_"
|
|
||||||
)
|
|
||||||
@ -1,84 +0,0 @@
|
|||||||
package paymentstatus_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"gitea.futuresens.co.uk/futuresens/hardlink/paymentstatus"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestFixedStatusValues(t *testing.T) {
|
|
||||||
statuses := []struct {
|
|
||||||
name string
|
|
||||||
value string
|
|
||||||
want string
|
|
||||||
}{
|
|
||||||
{"Starting", paymentstatus.Starting, "PAYMENT_STARTING"},
|
|
||||||
{"Started", paymentstatus.Started, "PAYMENT_STARTED"},
|
|
||||||
{"RequestSent", paymentstatus.RequestSent, "PAYMENT_REQUEST_SENT"},
|
|
||||||
{"Accepted", paymentstatus.Accepted, "PAYMENT_ACCEPTED"},
|
|
||||||
{"PresentCard", paymentstatus.PresentCard, "PAYMENT_PRESENT_CARD"},
|
|
||||||
{"InsertCard", paymentstatus.InsertCard, "PAYMENT_INSERT_CARD"},
|
|
||||||
{"SwipeCard", paymentstatus.SwipeCard, "PAYMENT_SWIPE_CARD"},
|
|
||||||
{"EnterPIN", paymentstatus.EnterPIN, "PAYMENT_ENTER_PIN"},
|
|
||||||
{"EnterPINAgain", paymentstatus.EnterPINAgain, "PAYMENT_ENTER_PIN_AGAIN"},
|
|
||||||
{"PleaseWait", paymentstatus.PleaseWait, "PAYMENT_PLEASE_WAIT"},
|
|
||||||
{"DoNotRemoveCard", paymentstatus.DoNotRemoveCard, "PAYMENT_DO_NOT_REMOVE_CARD"},
|
|
||||||
{"RemoveCard", paymentstatus.RemoveCard, "PAYMENT_REMOVE_CARD"},
|
|
||||||
{"TryAnotherInterface", paymentstatus.TryAnotherInterface, "PAYMENT_TRY_ANOTHER_INTERFACE"},
|
|
||||||
{"Processing", paymentstatus.Processing, "PAYMENT_PROCESSING"},
|
|
||||||
{"Authorized", paymentstatus.Authorized, "PAYMENT_AUTHORIZED"},
|
|
||||||
{"Approved", paymentstatus.Approved, "PAYMENT_APPROVED"},
|
|
||||||
{"Cancelling", paymentstatus.Cancelling, "PAYMENT_CANCELLING"},
|
|
||||||
{"Cancelled", paymentstatus.Cancelled, "PAYMENT_CANCELLED"},
|
|
||||||
{"Declined", paymentstatus.Declined, "PAYMENT_DECLINED"},
|
|
||||||
{"Expired", paymentstatus.Expired, "PAYMENT_EXPIRED"},
|
|
||||||
{"Timeout", paymentstatus.Timeout, "PAYMENT_TIMEOUT"},
|
|
||||||
{"SignatureRequired", paymentstatus.SignatureRequired, "PAYMENT_SIGNATURE_REQUIRED"},
|
|
||||||
{"SignatureRejecting", paymentstatus.SignatureRejecting, "PAYMENT_SIGNATURE_REJECTING"},
|
|
||||||
{"SignatureRejected", paymentstatus.SignatureRejected, "PAYMENT_SIGNATURE_REJECTED"},
|
|
||||||
{"Voided", paymentstatus.Voided, "PAYMENT_VOIDED"},
|
|
||||||
{"DailyLimitExceeded", paymentstatus.DailyLimitExceeded, "PAYMENT_DAILY_LIMIT_EXCEEDED"},
|
|
||||||
{"VoidFailed", paymentstatus.VoidFailed, "PAYMENT_VOID_FAILED"},
|
|
||||||
{"LimitValidationError", paymentstatus.LimitValidationError, "PAYMENT_LIMIT_VALIDATION_ERROR"},
|
|
||||||
{"Error", paymentstatus.Error, "PAYMENT_ERROR"},
|
|
||||||
{"TerminalUnavailable", paymentstatus.TerminalUnavailable, "PAYMENT_TERMINAL_UNAVAILABLE"},
|
|
||||||
}
|
|
||||||
|
|
||||||
seen := make(map[string]string, len(statuses))
|
|
||||||
for _, status := range statuses {
|
|
||||||
t.Run(status.name, func(t *testing.T) {
|
|
||||||
if status.value != status.want {
|
|
||||||
t.Fatalf("got %q, want %q", status.value, status.want)
|
|
||||||
}
|
|
||||||
if !strings.HasPrefix(status.value, "PAYMENT_") {
|
|
||||||
t.Fatalf("status %q does not have PAYMENT_ prefix", status.value)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if previous, exists := seen[status.value]; exists {
|
|
||||||
t.Errorf("%s and %s have duplicate value %q", previous, status.name, status.value)
|
|
||||||
}
|
|
||||||
seen[status.value] = status.name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDojoPrefixes(t *testing.T) {
|
|
||||||
if paymentstatus.DojoNotificationPrefix != "PAYMENT_DOJO_NOTIFICATION_" {
|
|
||||||
t.Fatalf(
|
|
||||||
"DojoNotificationPrefix = %q, want %q",
|
|
||||||
paymentstatus.DojoNotificationPrefix,
|
|
||||||
"PAYMENT_DOJO_NOTIFICATION_",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if paymentstatus.DojoStatusPrefix != "PAYMENT_DOJO_STATUS_" {
|
|
||||||
t.Fatalf(
|
|
||||||
"DojoStatusPrefix = %q, want %q",
|
|
||||||
paymentstatus.DojoStatusPrefix,
|
|
||||||
"PAYMENT_DOJO_STATUS_",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if paymentstatus.DojoNotificationPrefix == paymentstatus.DojoStatusPrefix {
|
|
||||||
t.Fatal("Dojo prefixes must differ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -39,7 +39,6 @@ type (
|
|||||||
Name string `xml:"customername"`
|
Name string `xml:"customername"`
|
||||||
Checkout string `xml:"checkoutdatetime"`
|
Checkout string `xml:"checkoutdatetime"`
|
||||||
RoomID string `xml:"roomno"`
|
RoomID string `xml:"roomno"`
|
||||||
Voucher string `xml:"voucher"`
|
|
||||||
Map string `xml:"roommap"`
|
Map string `xml:"roommap"`
|
||||||
Directions string `xml:"roomdirections"`
|
Directions string `xml:"roomdirections"`
|
||||||
}
|
}
|
||||||
@ -142,11 +141,6 @@ func BuildRoomTicket(details RoomDetailsRec) ([]byte, error) {
|
|||||||
write([]byte{ESC, 'a', CENTER})
|
write([]byte{ESC, 'a', CENTER})
|
||||||
writeStr(Layout.HotelSpecificDetails + "\n\n")
|
writeStr(Layout.HotelSpecificDetails + "\n\n")
|
||||||
|
|
||||||
if details.Voucher != "" {
|
|
||||||
s := strings.Repeat("*", 44)
|
|
||||||
writeStr(fmt.Sprintf("%s\n\n%s\n\n%s\n\n", s, details.Voucher, s))
|
|
||||||
}
|
|
||||||
|
|
||||||
// 8) Room map image
|
// 8) Room map image
|
||||||
mapPath := filepath.Join(Layout.RoomMapFolderPath, details.Map)
|
mapPath := filepath.Join(Layout.RoomMapFolderPath, details.Map)
|
||||||
mapBytes, err := printMap(mapPath)
|
mapBytes, err := printMap(mapPath)
|
||||||
@ -173,36 +167,7 @@ func BuildRoomTicket(details RoomDetailsRec) ([]byte, error) {
|
|||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrintReceipt(receipt string) {
|
func PrintCardholderReceipt(cardholderReceipt string) error {
|
||||||
|
|
||||||
if len(receipt) == 0 {
|
|
||||||
log.Warn("Empty cardholder receipt, skipping print")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := printCardholderReceipt(receipt); err != nil {
|
|
||||||
log.Errorf("PrintCardholderReceipt error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func PrintSaleReceipt(receipt string) {
|
|
||||||
if len(receipt) == 0 {
|
|
||||||
log.Warn("Empty sale receipt, skipping print")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
buf.WriteString(receipt)
|
|
||||||
buf.WriteByte('\n')
|
|
||||||
buf.Write([]byte{ESC, 'd', 7})
|
|
||||||
buf.Write([]byte{GS, 'V', 1})
|
|
||||||
|
|
||||||
|
|
||||||
if err := SendToPrinter(buf.Bytes()); err != nil {
|
|
||||||
log.Errorf("SendToPrinter error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func printCardholderReceipt(cardholderReceipt string) error {
|
|
||||||
receiptEntries, err := ParseCardholderReceipt([]byte(cardholderReceipt))
|
receiptEntries, err := ParseCardholderReceipt([]byte(cardholderReceipt))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("ParseCardholderReceipt: %w", err)
|
return fmt.Errorf("ParseCardholderReceipt: %w", err)
|
||||||
@ -2,89 +2,6 @@
|
|||||||
|
|
||||||
builtVersion is a const in main.go
|
builtVersion is a const in main.go
|
||||||
|
|
||||||
#### v1.3.5 - 23 July 2026
|
|
||||||
fixed dispenser delivery confirmation
|
|
||||||
|
|
||||||
#### v1.3.4 - 23 July 2026
|
|
||||||
added support for Dojo terminal-unavailable responses
|
|
||||||
|
|
||||||
#### v1.3.3 - 21 July 2026
|
|
||||||
added PDQ status streaming to the payment flow to allow the front end to display the status of the PDQ terminal
|
|
||||||
|
|
||||||
#### 1.3.2 - 20 July 2026
|
|
||||||
make dojo decline payment in case of signature required
|
|
||||||
|
|
||||||
#### 1.3.1 - 17 July 2026
|
|
||||||
added receipt printing functionality to the Dojo and Paybridge payment flow
|
|
||||||
|
|
||||||
#### 1.3.0 - 03 July 2026
|
|
||||||
added pluggable PayBridge and Dojo payment flow with CMS credentials
|
|
||||||
|
|
||||||
#### 1.2.11 - 26 June 2026
|
|
||||||
added PayBridge integration for payment processing functionality
|
|
||||||
|
|
||||||
#### 1.2.10 - 26 June 2026
|
|
||||||
added voucher field to the guest receipt
|
|
||||||
|
|
||||||
#### 1.2.9 - 02 June 2026
|
|
||||||
added Dormakaba lock server integration
|
|
||||||
|
|
||||||
#### 1.2.8 - 14 May 2026
|
|
||||||
Updated hardlink source layout to use cmd/hardlink for the main application entry point and internal/ for application packages. Runtime files and preauth-release layout remain unchanged. No functional changes.
|
|
||||||
|
|
||||||
#### 1.2.7 - 13 May 2026
|
|
||||||
retrieve CardType from the ChepDNA response
|
|
||||||
|
|
||||||
#### 1.2.6 - 20 April 2026
|
|
||||||
added the second attempt to send the card to the encoder after 6 seconds
|
|
||||||
|
|
||||||
#### 1.2.5 - 20 March 2026
|
|
||||||
removed early return on error when checking dispenser status in the start and final loops.
|
|
||||||
|
|
||||||
#### 1.2.4 - 18 March 2026
|
|
||||||
added check if keycard at the encoder position before trying to encode key
|
|
||||||
|
|
||||||
#### 1.2.3 - 17 March 2026
|
|
||||||
added check if keycard at the encoder position before trying to encode key
|
|
||||||
|
|
||||||
#### 1.2.2 - 11 February 2026
|
|
||||||
increased waiting time befor sending email on PDQ unavailability to 30 seconds day time and 10 minutes night time
|
|
||||||
to give it a chance to become available again
|
|
||||||
|
|
||||||
#### 1.2.1 - 09 February 2026
|
|
||||||
increased waiting time befor sending email on PDQ unavailability to 60 seconds
|
|
||||||
|
|
||||||
#### 1.2.0 - 09 February 2026
|
|
||||||
added testissuedoorcard endpoint for testing the full workflow of encoding a door card without moving the card out
|
|
||||||
added ping-pdq endpoint to check the status of the pdq terminal
|
|
||||||
added sending the email on the pdq disconnect event to notify support about the issue
|
|
||||||
added sending the email on the dispenser error status to notify support about the issue
|
|
||||||
|
|
||||||
#### 1.1.3 - 02 February 2026
|
|
||||||
increased timeout for reading response from the Assa abloy lock server to 20 seconds
|
|
||||||
|
|
||||||
#### 1.1.2 - 02 February 2026
|
|
||||||
added logging for unknown dispenser status positions
|
|
||||||
|
|
||||||
#### 1.1.1 - 02 February 2026
|
|
||||||
added contionuous polling of the dispenser status every 8 seconds to update the card well status
|
|
||||||
|
|
||||||
#### 1.1.0 - 26 January 2026
|
|
||||||
divided `/starttransaction` endpoint into two separate endpoints:
|
|
||||||
`/takepreauth` to request preauthorization payment
|
|
||||||
`/takepayment` to request taking payment
|
|
||||||
added preauth releaser functionality to release preauthorization payments after a defined time period
|
|
||||||
added db connection check before adding a transaction to the database
|
|
||||||
and reconnection functionality if the connection to the database is lost
|
|
||||||
added `/dispenserstatus` endpoint
|
|
||||||
key card always stays at encoder position
|
|
||||||
|
|
||||||
#### 1.0.30 - 09 January 2026
|
|
||||||
improved logging for preauth releaser
|
|
||||||
|
|
||||||
#### 1.0.29 - 08 January 2026
|
|
||||||
added count down before exiting the preauth releaser 20 seconds
|
|
||||||
|
|
||||||
#### 1.0.28 - 10 December 2025
|
#### 1.0.28 - 10 December 2025
|
||||||
added preauth releaser
|
added preauth releaser
|
||||||
|
|
||||||
|
|||||||
@ -7,13 +7,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ServiceName = "hardlink"
|
|
||||||
DateOnly = "2006-01-02"
|
DateOnly = "2006-01-02"
|
||||||
CustomLayout = "2006-01-02 15:04:05 -0700"
|
CustomLayout = "2006-01-02 15:04:05 -0700"
|
||||||
LinkStartTransaction = "http://127.0.0.1:18181/start-transaction/"
|
LinkTakePreauthorization = "http://127.0.0.1:18181/start-transaction/"
|
||||||
LinkConfirmTransaction = "http://127.0.0.1:18181/confirm-transaction/"
|
LinkTakePayment = "http://127.0.0.1:18181/start-and-confirm-transaction/"
|
||||||
LinkTransactionInformation = "http://127.0.0.1:18181/transaction-information/"
|
LinkTransactionInformation = "http://127.0.0.1:18181/transaction-information/"
|
||||||
LinkChipDNAStatus = "http://127.0.0.1:18181/chipdna-status/"
|
|
||||||
LinkVoidTransaction = "http://127.0.0.1:18181/void-transaction/"
|
LinkVoidTransaction = "http://127.0.0.1:18181/void-transaction/"
|
||||||
// Transaction types
|
// Transaction types
|
||||||
SaleTransactionType = "sale"
|
SaleTransactionType = "sale"
|
||||||
@ -23,7 +21,6 @@ const (
|
|||||||
ResultApproved = "approved"
|
ResultApproved = "approved"
|
||||||
ResultDeclined = "declined"
|
ResultDeclined = "declined"
|
||||||
ResultCancelled = "cancelled"
|
ResultCancelled = "cancelled"
|
||||||
ResultCanceled = "canceled"
|
|
||||||
ResultPending = "pending"
|
ResultPending = "pending"
|
||||||
ResultStateUncommitted = "uncommitted"
|
ResultStateUncommitted = "uncommitted"
|
||||||
ResultStateVoided = "voided"
|
ResultStateVoided = "voided"
|
||||||
@ -35,14 +32,12 @@ const (
|
|||||||
CardReference = "CARD_REFERENCE"
|
CardReference = "CARD_REFERENCE"
|
||||||
CardHash = "CARD_HASH"
|
CardHash = "CARD_HASH"
|
||||||
Errors = "ERRORS"
|
Errors = "ERRORS"
|
||||||
ErrorDescription = "ERROR_DESCRIPTION"
|
|
||||||
ReceiptData = "RECEIPT_DATA"
|
ReceiptData = "RECEIPT_DATA"
|
||||||
ReceiptDataMerchant = "RECEIPT_DATA_MERCHANT"
|
ReceiptDataMerchant = "RECEIPT_DATA_MERCHANT"
|
||||||
ReceiptDataCardholder = "RECEIPT_DATA_CARDHOLDER"
|
ReceiptDataCardholder = "RECEIPT_DATA_CARDHOLDER"
|
||||||
Reference = "REFERENCE"
|
Reference = "REFERENCE"
|
||||||
CardType = "CARD_SCHEME_ID"
|
PAN_MASKED = "PAN_MASKED"
|
||||||
PanMasked = "PAN_MASKED"
|
EXPIRY_DATE = "EXPIRY_DATE"
|
||||||
ExpiryDate = "EXPIRY_DATE"
|
|
||||||
TransactionResult = "TRANSACTION_RESULT"
|
TransactionResult = "TRANSACTION_RESULT"
|
||||||
TransactionType = "TRANSACTION_TYPE"
|
TransactionType = "TRANSACTION_TYPE"
|
||||||
TransactionState = "TRANSACTION_STATE"
|
TransactionState = "TRANSACTION_STATE"
|
||||||
@ -50,25 +45,6 @@ const (
|
|||||||
ConfirmErrors = "CONFIRM_ERRORS"
|
ConfirmErrors = "CONFIRM_ERRORS"
|
||||||
TotalAmount = "TOTAL_AMOUNT"
|
TotalAmount = "TOTAL_AMOUNT"
|
||||||
|
|
||||||
// Dojo terminal session statuses
|
|
||||||
ResultCaptured = "captured"
|
|
||||||
ResultSignatureAccepted = "signatureverificationaccepted"
|
|
||||||
ResultInitiateRequested = "initiaterequested"
|
|
||||||
ResultInitiated = "initiated"
|
|
||||||
ResultAuthorized = "authorized"
|
|
||||||
ResultCancelRequested = "cancelrequested"
|
|
||||||
ResultExpired = "expired"
|
|
||||||
ResultSignatureRejected = "signatureverificationrejected"
|
|
||||||
ResultSignatureRequired = "signatureverificationrequired"
|
|
||||||
|
|
||||||
//PayBridge message types
|
|
||||||
MesTypePaymentRequest = "payment_request"
|
|
||||||
MesTypePaymentResult = "payment_result"
|
|
||||||
MesTypePaymentError = "payment_error"
|
|
||||||
MesTypePaymentStatusUpdate = "payment_status_update"
|
|
||||||
MesTypePaymentAccepted = "payment_accepted"
|
|
||||||
MesTypeAuthSuccess = "auth_success"
|
|
||||||
|
|
||||||
// Log field keys
|
// Log field keys
|
||||||
LogFieldError = "error"
|
LogFieldError = "error"
|
||||||
LogFieldDescription = "description"
|
LogFieldDescription = "description"
|
||||||
Loading…
x
Reference in New Issue
Block a user