feat: improve CreditCall receipt layout
This commit is contained in:
+157
-78
@@ -245,95 +245,174 @@ func printCardholderReceipt(cardholderReceipt string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// receiptColumns follows the existing 44-column normal-size text convention.
|
||||
const receiptColumns = 44
|
||||
|
||||
func BuildCardholderReceipt(entries []ReceiptEntryXML) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
rendered := make([]bool, len(entries))
|
||||
take := func(id string) string {
|
||||
for i, entry := range entries {
|
||||
if !rendered[i] && entry.ReceiptEntryId == id && entry.Value != "" {
|
||||
rendered[i] = true
|
||||
return receiptEntryText(entry)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
write := func(b []byte) { buf.Write(b) }
|
||||
// writeStr := func(s string) { buf.WriteString(s) }
|
||||
writeln := func(s string) {
|
||||
buf.WriteString(s)
|
||||
// Set and restore only text state; leave printer setup and transport alone.
|
||||
normal := func() { buf.Write([]byte{ESC, 'a', 0, ESC, 'E', BOLD_OFF, GS, '!', NORMAL_FONT}) }
|
||||
normal()
|
||||
hasRows, newSection := false, false
|
||||
beginLine := func(center bool) {
|
||||
if hasRows && newSection {
|
||||
buf.WriteString(strings.Repeat("-", receiptColumns) + "\n")
|
||||
}
|
||||
hasRows, newSection = true, false
|
||||
if center {
|
||||
buf.Write([]byte{ESC, 'a', CENTER})
|
||||
}
|
||||
}
|
||||
text := func(value string, bold bool) {
|
||||
if bold {
|
||||
buf.Write([]byte{ESC, 'E', BOLD_ON})
|
||||
}
|
||||
buf.WriteString(value)
|
||||
if bold {
|
||||
buf.Write([]byte{ESC, 'E', BOLD_OFF})
|
||||
}
|
||||
}
|
||||
line := func(value string, bold, center bool) {
|
||||
if value == "" {
|
||||
return
|
||||
}
|
||||
beginLine(center)
|
||||
text(value, bold)
|
||||
buf.WriteByte('\n')
|
||||
normal()
|
||||
}
|
||||
pair := func(left, right string, boldRight bool) {
|
||||
if gap, fits := receiptPairGap(left, right); fits {
|
||||
beginLine(false)
|
||||
text(left+gap, false)
|
||||
text(right, boldRight)
|
||||
buf.WriteByte('\n')
|
||||
normal()
|
||||
return
|
||||
}
|
||||
line(left, false, false)
|
||||
line(right, boldRight, false)
|
||||
}
|
||||
|
||||
// Build a lookup map by ReceiptEntryId
|
||||
m := make(map[string]ReceiptEntryXML, len(entries))
|
||||
for _, e := range entries {
|
||||
m[e.ReceiptEntryId] = e
|
||||
line(take("Recipient"), true, true)
|
||||
line(take("MerchantName"), false, true)
|
||||
newSection = true
|
||||
line(take("CardScheme"), false, false)
|
||||
line(take("PanMasked"), false, false)
|
||||
pair(take("Aid"), take("PanSequence"), false)
|
||||
pair(take("TransactionSource"), take("TransactionType"), false)
|
||||
newSection = true
|
||||
// Align the provider amount without parsing or modifying its value.
|
||||
for i, entry := range entries {
|
||||
if entry.ReceiptEntryId == "TotalAmount" && entry.Value != "" {
|
||||
rendered[i] = true
|
||||
label := entry.Label
|
||||
if label == "" {
|
||||
label = "TOTAL"
|
||||
}
|
||||
label = strings.TrimSuffix(label, ":") + ":"
|
||||
if gap, fits := receiptPairGap(label, entry.Value); fits {
|
||||
line(label+gap+entry.Value, true, false)
|
||||
} else {
|
||||
line(receiptEntryText(entry), true, false)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 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:]
|
||||
pair(take("CardholderVerification"), take("TransactionResult"), true)
|
||||
newSection = true
|
||||
line(take("AuthCode"), false, false)
|
||||
line(take("AuthReference"), false, false)
|
||||
line(take("TransactionSeqNumber"), false, false)
|
||||
pair(take("MerchantIdMasked"), take("TerminalIdMasked"), false)
|
||||
line(take("AuthDateTime"), false, false)
|
||||
footer := take("RetentionMessage")
|
||||
// Preserve every remaining occurrence, including unknown IDs, in provider order.
|
||||
for i, entry := range entries {
|
||||
if !rendered[i] && entry.Value != "" {
|
||||
line(receiptEntryText(entry), false, false)
|
||||
}
|
||||
}
|
||||
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})
|
||||
|
||||
newSection = true
|
||||
line(footer, false, true)
|
||||
normal()
|
||||
buf.Write([]byte{ESC, 'd', 7})
|
||||
buf.Write([]byte{GS, 'V', 1})
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func receiptEntryText(entry ReceiptEntryXML) string {
|
||||
label := entry.Label
|
||||
if label == "" {
|
||||
switch entry.ReceiptEntryId {
|
||||
case "Recipient", "MerchantName", "CardScheme", "TransactionSource", "TransactionType",
|
||||
"CardholderVerification", "TransactionResult", "AuthDateTime", "RetentionMessage":
|
||||
return entry.Value
|
||||
case "PanMasked":
|
||||
label = "Card"
|
||||
case "Aid":
|
||||
label = "AID"
|
||||
case "PanSequence":
|
||||
label = "PAN Seq No"
|
||||
case "TotalAmount":
|
||||
label = "TOTAL"
|
||||
case "AuthCode":
|
||||
label = "Auth Code"
|
||||
case "AuthReference":
|
||||
label = "Ref"
|
||||
case "TransactionSeqNumber":
|
||||
label = "Transaction Seq. No"
|
||||
case "MerchantIdMasked":
|
||||
label = "MID"
|
||||
case "TerminalIdMasked":
|
||||
label = "TID"
|
||||
default:
|
||||
var readable strings.Builder
|
||||
runes := []rune(entry.ReceiptEntryId)
|
||||
for i, r := range runes {
|
||||
if i > 0 && r >= 'A' && r <= 'Z' &&
|
||||
((runes[i-1] >= 'a' && runes[i-1] <= 'z') ||
|
||||
(i+1 < len(runes) && runes[i+1] >= 'a' && runes[i+1] <= 'z')) {
|
||||
readable.WriteByte(' ')
|
||||
}
|
||||
if r == '_' || r == '-' {
|
||||
r = ' '
|
||||
}
|
||||
readable.WriteRune(r)
|
||||
}
|
||||
label = strings.TrimSpace(readable.String())
|
||||
}
|
||||
}
|
||||
if label == "" {
|
||||
return entry.Value
|
||||
}
|
||||
return strings.TrimSuffix(label, ":") + ": " + entry.Value
|
||||
}
|
||||
|
||||
func receiptPairGap(left, right string) (string, bool) {
|
||||
if left == "" || right == "" || len(left)+2+len(right) > receiptColumns {
|
||||
return "", false
|
||||
}
|
||||
// Only printable ASCII has a predictable single-column width here.
|
||||
for _, r := range left + right {
|
||||
if r < ' ' || r > '~' {
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
return strings.Repeat(" ", receiptColumns-len(left)-len(right)), true
|
||||
}
|
||||
|
||||
func printLogo(path string) ([]byte, error) {
|
||||
const maxLogoWidth = 384
|
||||
f, err := os.Open(path)
|
||||
|
||||
Reference in New Issue
Block a user