Build on Android
Prerequisites and assumptions
To build with the mPOS SDK, the following must be observed:
- Your device has been setup on the Global Accelerex TMS platform with all the necessary parameters (Terminal ID, Merchant ID, etc) as obtained from your bank.
- Your application minSdkVersion is API 21 (Lollipop 5.0) or later
- Your application targetSdkVersion is API 31 (R, 12.0) or later
- Your application uses AndroidX
- Both Kotlin and Java languages are supported
Step 1: Configure Gradle for the mPOS SDK
- Setup maven repository with the url shared with you In you project level build.gradle or otherwise.
allprojects {
repositories {
google()
mavenCentral()
maven {
url "https://<url-shared-with-you>"
}
}
}
The following should be configured in the app level build.gradle
file
- Configure build dependencies
depedencies {
implementation "com.globalaccelerex.mpos:payment:<latest-version>"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
- Add kotlin options to target 1.8 jvm version
kotlinOptions {
jvmTarget = '1.8'
}
- Remove some offending packaging options included in some of the libraries that were used to build the sdk. Add the following in your android block:
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LGPL2.1'
exclude 'AndroidManifest.xml'
}
Step 3: Create a CheckoutFragment or activity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.globalaccelerex.mpos.payment.MposPayment
import com.globalaccelerex.mpos.payment.PaymentRequest
import com.globalaccelerex.mpos.payment.RequestType
import com.globalaccelerex.mpos.payment.TransactionStatus
class CheckoutFragment : Fragment(R.layout.fragment_checkout) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentCheckoutBinding.bind(view)
binding.buttonCheckout.setOnClickListener {
startCheckout()
}
}
private fun startCheckout() {
}
}
import android.os.Bundle;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class CheckoutFragment extends Fragment {
public CheckoutFragment() {
super(R.layout.fragment_checkout);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FragmentCheckoutBinding binding = FragmentCheckoutBinding.bind(view);
binding.buttonCheckout.setOnClickListener(v -> startCheckout());
}
private void startCheckout() {
}
}
Step 4: Add code to start the payment flow and handle result
- Register for activity result. In this callback, you will receive the result for the payment/transaction processing.
- Implement the startCheckout method by adding code to build the payment request.
class CheckoutFragment : Fragment(R.layout.fragment_checkout) {
// Register the the payment result using activity results API
private val cardPayment =
registerForActivityResult(MposPayment.CardTransactionContract()) { result ->
when (result.status) {
TransactionStatus.APPROVED -> {
showCheckoutResult(result.responseData!!)
}
TransactionStatus.DECLINED -> {
val response = result.responseData!!
Toast.makeText(requireContext(), response.message, Toast.LENGTH_SHORT).show()
}
TransactionStatus.FAILED,
TransactionStatus.CANCELLED,
TransactionStatus.TIMEOUT -> {
// Transaction failed Check the response message
Toast.makeText(requireContext(), result.responseMessage, Toast.LENGTH_SHORT)
.show()
}
}
}
private fun showCheckoutResult(responseData: CardTransactionResponse) {
// Show result
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentCheckoutBinding.bind(view)
binding.buttonCheckout.setOnClickListener {
startCheckout()
}
}
private fun startCheckout() {
// Create a checkout request
val checkoutAmount = 2.0
val request = PaymentRequest {
requestType = RequestType.PURCHASE
amount = checkoutAmount
printReceipt = false
}
// Start the payment process
cardPayment.launch(request)
}
}
public class CheckoutFragment extends Fragment {
public CheckoutFragment() {
super(R.layout.fragment_checkout);
}
private final ActivityResultLauncher<PaymentRequest> cardPayment = registerForActivityResult(new MposPayment.CardTransactionContract(), this::onActivityResult);
private void onActivityResult(TransactionResult result) {
switch (result.getStatus()) {
case APPROVED:
CardTransactionResponse successResponse = result.getResponseData();
assert successResponse != null;
showCheckoutResult(successResponse);
break;
case DECLINED:
CardTransactionResponse response = result.getResponseData();
assert response != null;
Toast.makeText(getContext(), response.getMessage(), Toast.LENGTH_SHORT).show();
break;
case FAILED:
case CANCELLED:
case TIMEOUT:
Toast.makeText(getContext(), result.getResponseMessage(), Toast.LENGTH_SHORT).show();
break;
}
}
private void showCheckoutResult(CardTransactionResponse successResponse) {
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
FragmentCheckoutBinding binding = FragmentCheckoutBinding.bind(view);
binding.buttonCheckout.setOnClickListener(v -> startCheckout());
}
private void startCheckout() {
double checkoutAmount = 2.0;
PaymentRequest request = new PaymentRequest.Builder()
.setAmount(checkoutAmount)
.setRequestType(RequestType.PURCHASE)
.build();
cardPayment.launch(request);
}
}