Pre-Authorization with myPOS Smart SDK
The Pre-Authorization flow allows you to place a hold on a customer’s card for a specific amount without immediately capturing the funds. You can later choose to either complete or cancel the transaction using the preauthorization code.
1. Perform Pre-Authorization Request
Use MyPOSPreauthorization to initiate a hold on the customer's funds:
// Build the preauth request
MyPOSPreauthorization preauth = MyPOSPreauthorization.builder()
// Mandatoy parameters
.productAmount(1.23)
.currency(Currency.EUR)
.foreignTransactionId(UUID.randomUUID().toString())
// Optional parameters
// Reference number. Maximum length: 20 alpha numeric characters
.reference("asd123asd", ReferenceType.REFERENCE_NUMBER)
// Set print receipt mode
.printMerchantReceipt(MyPOSUtil.RECEIPT_ON)
.printCustomerReceipt(MyPOSUtil.RECEIPT_ON)
.build();
// If you want to initiate a moto transaction:
payment.setMotoTransaction(true)
// Start the transaction
MyPOSAPI.createPreauthorization(MainActivity.this, preauth, PREAUTH_REQUEST_CODE);
2. Perform Pre-Authorization Completion
To capture the held funds, use the MyPOSPreauthorizationCompletion builder:
// Build the preauth completion
MyPOSPreauthorizationCompletion preauthCompletion = MyPOSPreauthorizationCompletion.builder()
// Mandatory parameters
.productAmount(1.23)
.currency(Currency.EUR)
.preauthorizationCode("1111")
.foreignTransactionId(UUID.randomUUID().toString())
// Optional parameters
// Reference number. Maximum length: 20 alpha numeric characters
.reference("asd123asd", ReferenceType.REFERENCE_NUMBER)
// Set print receipt mode
.printMerchantReceipt(MyPOSUtil.RECEIPT_ON)
.printCustomerReceipt(MyPOSUtil.RECEIPT_ON)
.build();
// Start the transaction
MyPOSAPI.completePreauthorization(MainActivity.this, preauthCompletion, PREAUTH_COMPLETION_REQUEST_CODE);
3. Perform Pre-Authorization Cancellation
To release the held funds, use the MyPOSPreauthorizationCancellation builder:
// Build the preauth cancellation
MyPOSPreauthorizationCancellation preauthCancellation = MyPOSPreauthorizationCancellation.builder()
// Mandatoy parameters
.preauthorizationCode("1111")
.foreignTransactionId(UUID.randomUUID().toString())
// Optional parameters
// Reference number. Maximum length: 20 alpha numeric characters
.reference("asd123asd", ReferenceType.REFERENCE_NUMBER)
// Set print receipt mode
.printMerchantReceipt(MyPOSUtil.RECEIPT_ON)
.printCustomerReceipt(MyPOSUtil.RECEIPT_ON)
.build();
// Start the transaction
MyPOSAPI.cancelPreauthorization(MainActivity.this, preauthCancellation, PREAUTH_CANCELLATION_REQUEST_CODE);
4. Handle the Result
Use onActivityResult() in your Activity to capture the result of the pre-authorization, completion, or cancellation.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// The same request code as when calling MyPOSAPI.createPreauthorization
if (requestCode == PREAUTH_REQUEST_CODE) {
// The transaction was processed, handle the response
if (resultCode == RESULT_OK) {
// Something went wrong in the Payment core app and the result couldn't be returned properly
if (data == null) {
Toast.makeText(this, "Transaction cancelled", Toast.LENGTH_SHORT).show();
return;
}
int transactionResult = data.getIntExtra("status", TransactionProcessingResult.TRANSACTION_FAILED);
Toast.makeText(this, "Pre-Auth transaction has completed. Result: " + transactionResult, Toast.LENGTH_SHORT).show();
// TODO: handle each transaction response accordingly
if (transactionResult == TransactionProcessingResult.TRANSACTION_SUCCESS) {
String preauthCode = data.getStringExtra("preauth_code");
// Transaction is successful
}
} else {
// The user cancelled the transaction
Toast.makeText(this, "Transaction cancelled", Toast.LENGTH_SHORT).show();
}
}
}
5. Retrieve Pre-Authorization Code
Use the following code to extract the preauthorization code from the transaction result:
String preauthCode = data.getStringExtra("preauth_code");
Tip: Store the preauthCode securely if you intend to complete or cancel the transaction later. Make sure the same foreignTransactionId is not reused across multiple requests.