Legacy Conversion - AEVI-AppFlow/pos-android-sdk GitHub Wiki
The POS Flow API is a completely new API provided by AEVI. It can be used on a single device at the same time as the original AEVI Public SDK if the applications installed on the device support it. However, there are times when you may be required to convert between calls to the new POS Flow API and the original v2 API. Usually this involves converting transaction references from one API to the other. The two scenarios to and from the v2 API are described here.
Conversion of references from AEVI Public SDK v2 requests
For completion requests or other two stage financial transactions the transaction references of the previous request (pre-authorisation) that initiated the payment must be returned along with the request. For conversion between a previous v2 TransactionResult
references and the new API you can make use of the following example.
public class Converter {
public static AdditionalData getAdditionalDataFromResultReferences(TransactionReferences transactionReferences) {
AdditionalData additionalData = new AdditionalData();
for (TransactionReferenceCode code : transactionReferences.getTransactionCodes()) {
additionalData.addData(code.getName(), code.getValue());
}
return additionalData;
}
}
POS Flow preAuthCompletions
also require the amount to be specifically set. If you would like to obtain the amount directly from a previousTransactionResult
the following code can be used to obtain it.
static Amounts getCompletionAmounts(TransactionResult result) throws LegacyConversionException {
TransactionReferences references = result.getTransactionReferences();
String currency = null;
if (references.getTransactionReferenceCode(TransactionReferences.CURRENCY) != null) {
currency = references.getTransactionReferenceCode(TransactionReferences.CURRENCY).getValue();
}
if (currency == null) {
Log.e(TAG, "No currency reference available for completion request - erroring out");
throw new LegacyConversionException(CURRENCY_NOT_SUPPORTED);
}
BigDecimal amount = null;
if (references.getTransactionReferenceCode(TransactionReferences.AMOUNT) != null) {
String amountStr = references.getTransactionReferenceCode(TransactionReferences.AMOUNT).getValue();
if (amountStr.matches("[0-9]+")) {
amount = longToBigDecimal(Long.parseLong(amountStr));
} else if (amountStr.contains(".")) {
amount = new BigDecimal(amountStr);
}
}
if (amount == null) {
Log.e(TAG, "No amount available for completion request - erroring out");
throw new LegacyConversionException(MALFORMED_REQUEST);
}
return new Amounts(convertBigDecimalToLong(amount), currency);
}
private static BigDecimal longToBigDecimal(long l) {
return BigDecimal.valueOf(l).movePointLeft(2);
}
private static long convertBigDecimalToLong(BigDecimal amount) {
return Math.round(amount.doubleValue() * 100);
}
Conversion of references from POS Flow requests
Conversely for example to VOID/REVERSE a completion transaction performed via the POS Flow API you must include the references into the v2 ReversalRequest
.
A TransactionReferences
object can be obtained as below.
public static TransactionReferences getReferencesFromAdditionalData(AdditionalData additionalData) {
LegacyTransactionResult legacyTransactionResult = new LegacyTransactionResult();
for (String key : additionalData.getKeys()) {
legacyTransactionResult.addTransactionReference(key, additionalData.getValue(key, String.class));
}
return legacyTransactionResult.getTransactionReferences();
}
static class LegacyTransactionResult extends TransactionResult {
private final Bundle transactionReferences = new Bundle();
LegacyTransactionResult() {
super(new Bundle());
}
void addTransactionReference(String name, String value) {
transactionReferences.putString(name, value);
getBundle().putBundle(ResultOption.TRANSACTION_REFERENCES.toString(), transactionReferences);
}
}
Additional data
The AdditionalData
parameter passed to the method above should be obtained from a valid TransactionResponse
object returned via this API. The API is designed to support multiple transactions and multiple responses per transaction. In most instances however there will only ever be one transaction and one response. Therefore, the previous transaction references can be obtained using.
TransactionResponse transactionResponse = response.getTransactions().get(0).getTransactionResponses().get(0);
AdditionalData previousTransactionRefs = transactionResponse.getReferences();
Where response
above is the PaymentResponse
object obtained in the initiatePayment
example above. The above is for example only and obviously production code should ideally check the Transaction
list and TransactionResponse
list sizes before attempting to extract the data.
Error messages
Error messages are associated with each TransactionResponse
that they were generated for. Therefore, in order to identify an error message the list of TransactionResponse
objects should be iterated. Again in most situations there will be only a single TransactionResponse
, therefore, as above you can obtain this simply by using the following.
TransactionResponse transactionResponse = response.getTransactions().get(0).getTransactionResponses().get(0);
String failureMessage = transactionResponse.getOutcomeMessage();