5. Display the transaction context
Last updated
Was this helpful?
Was this helpful?
func displayTransactionContext(_ context: TransactionContext) {
// Set this to your locale.
let locale = Locale(identifier: "en_US")
// Map ISO-4217 numeric codes to alphabetic codes.
// Expand this mapping based on your supported currencies.
let iso4217NumToCode: [Int: String] = [
978: "EUR",
840: "USD"
]
let currencyFormatter = NumberFormatter()
currencyFormatter.locale = locale
currencyFormatter.numberStyle = .currency
if let currencyCode = iso4217NumToCode[context.currencyCode.bcdToInt()] {
currencyFormatter.currencyCode = currencyCode
}
let rawDateFormatter = DateFormatter()
rawDateFormatter.dateFormat = "yyMMdd"
guard let transactionDate = rawDateFormatter.date(from: context.transactionDate.bcdToString()) else {
// Fallback: skip date rendering if parsing fails.
return
}
let displayDateFormatter = DateFormatter()
displayDateFormatter.locale = locale
displayDateFormatter.dateStyle = .medium
let amountStr = currencyFormatter.string(from: context.amount as NSNumber) ?? "-"
let dateStr = displayDateFormatter.string(from: transactionDate)
let transactionTypeStr = context.transactionType.bcdToString()
switch transactionTypeStr {
case "00":
// Purchase
break
case "20":
// Refund / correction
break
default:
// Unknown transaction type
break
}
// Update your UI with amountStr, dateStr, scheme, isTransit, and transactionID.
}