updated lock server

This commit is contained in:
2025-06-18 11:54:29 +01:00
parent d9871e6a1c
commit b002c80b01
6 changed files with 104 additions and 23 deletions
+42
View File
@@ -0,0 +1,42 @@
package payment
import (
"fmt"
"net"
"time"
)
// StartTransaction sends a start transaction XML to ChipDNA Server
func (pc *PaymentClient) StartTransaction(amountMinorUnits int, reference string) (string, error) {
conn, err := net.Dial("tcp", pc.Addr)
if err != nil {
return "", fmt.Errorf("failed to connect: %w", err)
}
defer conn.Close()
// Set timeout
conn.SetDeadline(time.Now().Add(15 * time.Second))
// Format XML request
request := fmt.Sprintf(`
<StartTransaction>
<Amount>%d</Amount>
<AmountType>Actual</AmountType>
<Reference>%s</Reference>
<TransactionType>Sale</TransactionType>
</StartTransaction>`, amountMinorUnits, reference)
_, err = conn.Write([]byte(request))
if err != nil {
return "", fmt.Errorf("failed to write to server: %w", err)
}
// Read response
buff := make([]byte, 4096)
n, err := conn.Read(buff)
if err != nil {
return "", fmt.Errorf("failed to read from server: %w", err)
}
return string(buff[:n]), nil
}
+32
View File
@@ -0,0 +1,32 @@
package payment
import (
"net"
"net/url"
"fmt"
"strings"
)
// PaymentClient holds connection data
type PaymentClient struct {
Addr string // e.g., "127.0.0.1:1869"
}
func InitializeConnection(paymentrUrl string) (net.Conn, error) {
const funcName = "InitializeServerConnection"
// Parse the URL to extract host and port
parsedUrl, err := url.Parse(paymentrUrl)
if err != nil {
return nil, fmt.Errorf("[%s] failed to parse LockserverUrl: %v", funcName, err)
}
// Remove any leading/trailing slashes just in case
address := strings.Trim(parsedUrl.Host, "/")
// Establish a TCP connection to the Visionline server
conn, err := net.Dial("tcp", address)
if err != nil {
return nil, fmt.Errorf("failed to connect to payment server: %v", err)
}
return conn, nil
}