6. Display transaction context
Last updated
Was this helpful?
After each transaction, your digital wallet application can display transaction details.
NFC Wallet SDK provides a TransactionContext in these callbacks:
ContactlessPaymentServiceListener.onTransactionCompleted()
ContactlessPaymentServiceListener.onError()
Treat TransactionContext as sensitive data.
Call TransactionContext.wipe() after you finish using it.
Read the values you need from the context, then format them for display.
The following utility converts BCD-encoded values to strings.
Adjust the formatting to match your UI and your payment network requirements.
Last updated
Was this helpful?
Was this helpful?
@Override
public void onTransactionCompleted(TransactionContext ctx) {
try {
TransactionData data = TransactionData.from(ctx);
// Display data in your UI.
} finally {
ctx.wipe();
}
}
@Override
public void onError(
TransactionContext ctx,
PaymentServiceErrorCode errorCode,
String message) {
if (ctx == null) {
// Handle the error when no transaction context is available.
return;
}
try {
TransactionData data = TransactionData.from(ctx);
// Display data and error details in your UI.
} finally {
ctx.wipe();
}
}
/**
* Convenience data class for transaction display.
*/
public class TransactionData {
private final String currencyCode;
private final double amount;
private final String transactionDate;
private final String transactionType;
private final String transactionId;
private TransactionData(
String currencyCode,
double amount,
String transactionDate,
String transactionType,
String transactionId) {
this.currencyCode = currencyCode;
this.amount = amount;
this.transactionDate = transactionDate;
this.transactionType = transactionType;
this.transactionId = transactionId;
}
public static TransactionData from(TransactionContext ctx) {
String currencyCode = TransactionDisplayUtils.bcdToString(ctx.getCurrencyCode());
String transactionDate = TransactionDisplayUtils.bcdToString(ctx.getTrxDate());
String transactionType = TransactionDisplayUtils.getTransactionType(ctx.getTrxType());
return new TransactionData(
currencyCode,
ctx.getAmount(),
transactionDate,
transactionType,
ctx.getTrxId());
}
public String getCurrencyCode() {
return currencyCode;
}
public double getAmount() {
return amount;
}
public String getTransactionDate() {
return transactionDate;
}
public String getTransactionType() {
return transactionType;
}
public String getTransactionId() {
return transactionId;
}
}public final class TransactionDisplayUtils {
private TransactionDisplayUtils() {
// Utility class.
}
public static String bcdToString(byte[] bcd) {
if (bcd == null) {
return "";
}
StringBuilder sb = new StringBuilder();
for (byte b : bcd) {
sb.append(bcdToString(b));
}
return sb.toString();
}
public static String bcdToString(byte bcd) {
int high = (bcd & 0xF0) >>> 4;
int low = (bcd & 0x0F);
return String.valueOf(high) + low;
}
public static String getTransactionType(byte trxType) {
switch (trxType) {
case 0:
return "PAY";
case 32:
return "REFUND";
default:
return "TRANSACTION";
}
}
}