updated lock server
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user