MedaPay Android SDK

Download

MedaPay SDK can be used in your App to simplify payment for your customers. Your customers can choose to pay from Amole/Dashen Bank, CBE Birr/CBE, HelloCash/LionBank and mBirr/Multiple Microfinances.

Installation

Add this to your project's gradle file

allprojects {
repositories {
...
maven {
url 'https://360ground.bintray.com/meda-pay/'
}
}
}

Add this in your app's build.gradle file

implementation 'com.a360ground.medapay:sdk:1.0.2'

Add the following permission if not already

<uses-permission android:name="android.permission.INTERNET" />

Configuration

We recommend to initialize MedaPay configurations at app startup unless you have dynamic configurations.

Kotlin

MedaPay.configure(
"apiKey",
MedaPayEnvironment.SANDBOX,
MerchantInfo(
cbeBirrMerchantId = "cbeMerchantId",
amoleMerchantId = "amoleMerchatId",
helloCashMerchantId = "helloCashMerchantId",
mBirrMerchantId = "mBirrMerchantId"
),
)

or since MerchantInfo is optional you can configure without MerchantInfo

MedaPay.configure(
"apiKey",
MedaPayEnvironment.SANDBOX
)

Java

MedaPay.configure(
"apiKey",
new MerchantInfo(
"cbeMerchantId",
"amoleMerchatId",
"helloCashMerchantId",
"mBirrMerchantId"
),
MedaPayEnvironment.SANDBOX
);

or

MedaPay.configure(
"apiKey",
MedaPayEnvironment.SANDBOX
)

The api key can be found in MedaPay developer portal.

Creating Payment Object

Pay object have Builder structure to initialize payment parameters

Kotlin

val pay = MedaPayRequest.builder {
amount(10.0)
from("0911121314")
withPaymentLabel("Test Payment")
}.build()

Java

MedaPayActivity.Pay pay= new MedaPayRequest.Builder()
.amount(10)
.from("0921928038")
.withPaymentLabel("Test Payment")
.build();

It is possible to add other payment properties in the builder like paymentOptions and callbackUrl

Start Payment

Payment can be started from Activity or Fragment, You need the Pay object as parameter which we see in the above example

Kotlin

MedaPayRequest.start(pay, this, object : MedaPayRequest.ReferenceNumberListener {
override fun onReferenceNumberGenerated(referenceNumber: String) {
// Reference number generated
}
})

Java

MedaPayRequest.start(pay, this, new MedaPayRequest.ReferenceNumberListener() {
@Override
public void onReferenceNumberGenerated(@NotNull String s) {
// Reference number generated
}
});

this refers and Activity or Fragment

java onReferenceNumberGenerated(referenceNumber: String) method will be invoked when a reference number is generated. You have to implement your own way to store the reference number so you can ask MedaPay the status of payment if the payment doesn't end in the screen. (Remember there are many ways to complete payment so you might not get the result from the screen)

Getting Result

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == MedaPayRequest.MEDA_PAY_REQUEST_CODE) {
val result = data?.extras?.getString("result")
val response = data?.extras?.getString("response")
// Parse the response here
}
}

onActivityResult should be implemented in the calling activity or fragment to listen the results. There are three different resultCodes RESULT_SUCCESS, RESULT_ERROR and RESULT_CLOSED

  • RESULT_SCUCCESS will be returned if the payment is successful on payment screen
  • RESULT_ERROR will be returned if there is an error while opening the payment screen
  • RESULT_CLOSED will be returned if the user closed the payment screen. If this result is returned after reference number generated means the user might finish the payment someway so you have to track this payment by the reference number

Mostly there are be two values returned in bundle. result and response. The result value is one of SUCCESS, CLOSED or ERROR and the response is JSON string which will differ based on the result.

Getting Payment Status

Here are scenarios of getting payment status

  • Some payments options request pin on payment screen so the result can be fetched automatically from payment screen in this case you will get notified by onActivityResult
  • User close the payment screen and pay the bill another time or from another place using USSD you should track bill status your own

MedaPay provides a way to check payment status

Kotlin

MedaPay.getBillStatus("referenceNumber", object : BillStatusResult{
override fun onBillStatusLoaded(billStatus: BillStatus?) {
}
override fun onBillStatusNotFound(billStatus: BillStatus?) {
}
override fun onBillStatusLoadingFailed(t: Throwable) {
}
})

Java

MedaPay.getBillStatus("referenceNumber", new BillStatusResult() {
@Override
public void onBillStatusLoadingFailed(@NotNull Throwable t) {
}
@Override
public void onBillStatusNotFound(@Nullable BillStatus billStatus) {
}
@Override
public void onBillStatusLoaded(@Nullable BillStatus billStatus) {
}
});