added creditcall payment method
This commit is contained in:
+143
-12
@@ -5,13 +5,13 @@ import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"image"
|
||||
|
||||
"image/color"
|
||||
"image/draw"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/alexbrainman/printer"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -19,6 +19,21 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
CardholderReceipt struct {
|
||||
XMLName xml.Name `xml:"ReceiptData"`
|
||||
ReceiptEntries struct {
|
||||
ReceiptEntry []ReceiptEntryXML `xml:"ReceiptEntry"`
|
||||
} `xml:"ReceiptEntries"`
|
||||
}
|
||||
|
||||
ReceiptEntryXML struct {
|
||||
ReceiptEntryId string `xml:"ReceiptEntryId"`
|
||||
Value string `xml:"Value"`
|
||||
Label string `xml:"Label"`
|
||||
ReceiptItemType string `xml:"ReceiptItemType"`
|
||||
Priority string `xml:"Priority"`
|
||||
}
|
||||
|
||||
RoomDetailsRec struct {
|
||||
XMLName xml.Name `xml:"roomdetails"`
|
||||
Name string `xml:"customername"`
|
||||
@@ -54,11 +69,30 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
ESC = 0x1B
|
||||
GS = 0x1D
|
||||
CENTER = 0x01
|
||||
BOLD_ON = 0x01
|
||||
BOLD_OFF = 0x00
|
||||
LARGE_FONT = 0x11
|
||||
WIDER_FONT = 0x10
|
||||
NORMAL_FONT = 0x00
|
||||
)
|
||||
|
||||
var (
|
||||
Layout LayoutOptions
|
||||
PrinterName string
|
||||
)
|
||||
|
||||
func ParseCardholderReceipt(data []byte) ([]ReceiptEntryXML, error) {
|
||||
var tr CardholderReceipt
|
||||
if err := xml.Unmarshal(data, &tr); err != nil {
|
||||
return nil, fmt.Errorf("XML unmarshal: %w", err)
|
||||
}
|
||||
return tr.ReceiptEntries.ReceiptEntry, nil
|
||||
}
|
||||
|
||||
func BuildRoomTicket(details RoomDetailsRec) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
@@ -66,17 +100,6 @@ func BuildRoomTicket(details RoomDetailsRec) ([]byte, error) {
|
||||
write := func(b []byte) { buf.Write(b) }
|
||||
writeStr := func(s string) { buf.WriteString(s) }
|
||||
|
||||
const (
|
||||
ESC = 0x1B
|
||||
GS = 0x1D
|
||||
CENTER = 0x01
|
||||
BOLD_ON = 0x01
|
||||
BOLD_OFF = 0x00
|
||||
LARGE_FONT = 0x11
|
||||
WIDER_FONT = 0x10
|
||||
NORMAL_FONT = 0x00
|
||||
)
|
||||
|
||||
// 0) Hotel logo at top
|
||||
logoBytes, err := printLogo(Layout.LogoPath)
|
||||
if err != nil {
|
||||
@@ -144,6 +167,114 @@ func BuildRoomTicket(details RoomDetailsRec) ([]byte, error) {
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func PrintCardholderReceipt(cardholderReceipt string) error {
|
||||
receiptEntries, err := ParseCardholderReceipt([]byte(cardholderReceipt))
|
||||
if err != nil {
|
||||
return fmt.Errorf("ParseCardholderReceipt: %w", err)
|
||||
}
|
||||
|
||||
data, err := BuildCardholderReceipt(receiptEntries)
|
||||
if err != nil {
|
||||
return fmt.Errorf("BuildCardholderReceipt: %w", err)
|
||||
}
|
||||
|
||||
// Send to the Windows Epson TM-T82II via the printer package
|
||||
if err := SendToPrinter(data); err != nil {
|
||||
return fmt.Errorf("SendToPrinter: %w", err)
|
||||
}
|
||||
log.Info("Cardholder receipt printed successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func BuildCardholderReceipt(entries []ReceiptEntryXML) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
write := func(b []byte) { buf.Write(b) }
|
||||
// writeStr := func(s string) { buf.WriteString(s) }
|
||||
writeln := func(s string) {
|
||||
buf.WriteString(s)
|
||||
buf.WriteByte('\n')
|
||||
}
|
||||
|
||||
// Build a lookup map by ReceiptEntryId
|
||||
m := make(map[string]ReceiptEntryXML, len(entries))
|
||||
for _, e := range entries {
|
||||
m[e.ReceiptEntryId] = e
|
||||
}
|
||||
|
||||
// Utility to center text
|
||||
// center := func(s string) {
|
||||
// write([]byte{ESC, 'a', CENTER})
|
||||
// writeln(s)
|
||||
// write([]byte{ESC, 'a', 0})
|
||||
// }
|
||||
|
||||
// Utility for bold lines
|
||||
// boldOn := func() { write([]byte{GS, '!', WIDER_FONT}) }
|
||||
// boldOff := func() { write([]byte{GS, '!', NORMAL_FONT}) }
|
||||
|
||||
// boldOn()
|
||||
|
||||
// 1) Header: CARDHOLDER COPY
|
||||
writeln(m["Recipient"].Value)
|
||||
// writeStr("\n\n")
|
||||
// 2) Merchant name
|
||||
writeln(m["MerchantName"].Value)
|
||||
// writeStr("\n\n")
|
||||
// 3) AID
|
||||
writeln("AID: " + m["Aid"].Value)
|
||||
|
||||
// 4) Card scheme & pan
|
||||
writeln(fmt.Sprintf("%s Card: %s", m["CardScheme"].Value, m["PanMasked"].Value))
|
||||
|
||||
// 5) PAN Seq No
|
||||
writeln("PAN Seq No: " + m["PanSequence"].Value)
|
||||
|
||||
// 6) Transaction source
|
||||
writeln(m["TransactionSource"].Value)
|
||||
|
||||
// 7) Transaction type (Sale/Account Verification)
|
||||
writeln(m["TransactionType"].Value)
|
||||
|
||||
// 8) Total
|
||||
// assuming value like "GBP1.50" — insert space after currency
|
||||
total := m["TotalAmount"].Value
|
||||
if !strings.HasPrefix(total, "GBP") && len(total) > 3 {
|
||||
total = total[:3] + " " + total[3:]
|
||||
}
|
||||
writeln("TOTAL: " + total)
|
||||
|
||||
// 9) Cardholder verification
|
||||
writeln(m["CardholderVerification"].Value)
|
||||
|
||||
// 10) Approved/Declined
|
||||
writeln(m["TransactionResult"].Value)
|
||||
|
||||
// 11) Auth code
|
||||
writeln("Auth Code: " + m["AuthCode"].Value)
|
||||
|
||||
// 12) Reference
|
||||
writeln("Ref: " + m["AuthReference"].Value)
|
||||
|
||||
// 13) Merchant & terminal IDs
|
||||
writeln("MID: " + m["MerchantIdMasked"].Value)
|
||||
writeln("TID: " + m["TerminalIdMasked"].Value)
|
||||
|
||||
// 14) Date/time
|
||||
writeln(m["AuthDateTime"].Value)
|
||||
|
||||
// 15) Retention message
|
||||
writeln(m["RetentionMessage"].Value)
|
||||
|
||||
// boldOff()
|
||||
|
||||
// finally feed & cut
|
||||
write([]byte{ESC, 'd', 7}) // Feed 5 lines
|
||||
write([]byte{GS, 'V', 1})
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func printLogo(path string) ([]byte, error) {
|
||||
const maxLogoWidth = 384
|
||||
f, err := os.Open(path)
|
||||
|
||||
Reference in New Issue
Block a user