43 lines
974 B
Go
43 lines
974 B
Go
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
|
|
}
|