Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 11 Next »


Getting started with the Monnify Android SDK

First off, the Monnify Android SDK allows you to accept payments from customers in your Android application via:

  1. Card Payment
  2. Bank Transfer (this is what is special:)


1. Add the dependency for the Monnify SDK

To your root build.gradle file add:

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url 'http://dl.bintray.com/teamapt/MonnifyAndroidSdk'
        }
    }
}

To your app-level build.gradle file add:

dependencies {
    // ...
    implementation 'com.teamapt.monnify.sdk:monnify-android-sdk:1.0.18'
}

Remember to add Internet permission to application manifest:

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

2. Create an instance of the Monnify SDK

KOTLIN

class MainActivity : AppCompatActivity() {
    // ...

    var monnify = Monnify.instance

    override fun onCreate(savedInstanceState: Bundle?) {
        // ...
    }

}

JAVA

class MainActivity extends AppCompatActivity {
    // ...

    private Monnify monnify = Monnify.Companion.getInstance();

    @Override
	public void onCreate(Bundle savedInstanceState) {
        // ...
    }
}

3. Set your merchant API key, contract code and application mode

This is done in the onCreate() method of the Launch Activity. The merchant Api Key and contract code can be gotten from your Monnify account dashbard. The application mode should be either TEST or LIVE. The TEST mode works on a sandbox environment and payment can be simulated here. Remember to switch to ApplicationMode.LIVE when generating APKs for production

KOTLIN

monnify.setApiKey("MY_MERCHANT_API_KEY7")
monnify.setContractCode("111222333444555")

monnify.setApplicationMode(ApplicationMode.TEST)

JAVA

monnify.setApiKey("MY_MERCHANT_API_KEY7");
monnify.setContractCode("111222333444555");

monnify.setApplicationMode(ApplicationMode.TEST);

4. Set your merchant API key, contract code and application mode

This is done when the customer clicks perhaps on a 'Pay' button. The initializePayment() method requires the activity context, an object of the TransactionDetails class, request code, result key You are allowed to set the request code and request key yourself in order to have more control, and to prevent clashes with other args in the main app.

KOTLIN

val transaction = TransactionDetails.Builder()
            .amount(BigDecimal("2000"))
            .currencyCode("NGN")
            .customerName("Customer Name")
            .customerEmail("mail.cus@tome.er")
            .paymentReference("PAYMENT_REF")
            .paymentDescription("Description of payment")
            .incomeSplitConfig(arrayListOf<SubAccountDetails>())
            .build()
            
monnify.initializePayment(
            this@MainActivity,
            transaction,
            INITIATE_PAYMENT_REQUEST_CODE,
            KEY_RESULT)

JAVA

TransactionDetails transaction = new TransactionDetails.Builder()
            .amount(new BigDecimal("2000"))
            .currencyCode("NGN")
            .customerName("Customer Name")
            .customerEmail("mail.cus@tome.er")
            .paymentReference("PAYMENT_REF")
            .paymentDescription("Description of payment")
            .build();

monnify.initializePayment(
        MainActivity.this,
                transaction,
                INITIATE_PAYMENT_REQUEST_CODE,
                KEY_RESULT);


5. Get outcome of the payment attempt so that you can update application UI after payment gateway is closed.

This is done in the onActivityResult() method of your activity. Use the request code and data key passed in the initializePayment() method to get data returned by the SDK

KOTLIN

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        
        val monnifyTransactionResponse = data?.getParcelableExtra(KEY_RESULT) as MonnifyTransactionResponse

        var message = ""
        message = when(monnifyTransactionResponse.status) {
            Status.PENDING -> "Transaction not paid"
            Status.PAID -> "Customer paid exact amount"
            Status.OVERPAID -> "Customer paid more than expected amount."
            Status.PARTIALLY_PAID -> "Customer paid less than expected amount."
            Status.FAILED -> "Transaction completed unsuccessfully. This means no payment came in for Account Transfer method or attempt to charge card failed."
            Status.PAYMENT_GATEWAY_ERROR -> "Payment gateway error"
        }

        Toast.makeText(this@MainActivity, message, Toast.LENGTH_LONG).show()

    }

JAVA

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    
    MonnifyTransactionResponse monnifyTransactionResponse = (MonnifyTransactionResponse) data.getParcelableExtra(KEY_RESULT);
    
    if (monnifyTransactionResponse == null)
        return;

    String message = "";
    switch (monnifyTransactionResponse.getStatus()) {
        case PENDING: { message = "Transaction not paid for."; break; }
        case PAID: { message = "Customer paid exact amount"; break; }
        case OVERPAID: { message = "Customer paid more than expected amount."; break; }
        case PARTIALLY_PAID: { message = "Customer paid less than expected amount."; break; }
        case FAILED: { message = "Transaction completed unsuccessfully. This means no payment came in for Account Transfer method or attempt to charge card failed."; break; }
        case PAYMENT_GATEWAY_ERROR: { message = "Payment gateway error"; break; }
    }

    Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}

TADA!

Possible return types from the SDK and meanings below:

TypeMeaning
PENDINGTransaction not paid for.
PAIDThe customer paid exact amount
OVERPAIDThe customer paid more than the expected amount.
PARTIALLY_PAIDThe customer paid less than the expected amount.
FAILEDTransaction completed unsuccessfully. This means no payment came in for Account Transfer method or attempt to charge card failed.
PAYMENT_GATEWAY_ERRORPayment tried but an error occurred on Monnify gateway


SubAccounts

You can add sub-accounts to receive settlements for a particular transaction when initializing the transaction as shown below:


Kotlin

val subAccount1 = SubAccountDetails("MFY_SUB_319452883968", 10.5, BigDecimal("500"), true)

val subAccount2 = SubAccountDetails("MFY_SUB_259811283666", 10.5, BigDecimal("1000"), false)
                
transaction = TransactionDetails.Builder()
            //...
            .incomeSplitConfig(arrayListOf<SubAccountDetails>(Arrays.asList(subAccount1, subAccount2)))
            .build()


JAVA

final SubAccountDetails subAccount1 =
                new SubAccountDetails("MFY_SUB_319452883968", 10.5, new BigDecimal("500"), true);

final SubAccountDetails subAccount2 =
                new SubAccountDetails("MFY_SUB_259811283666", 10.5, new BigDecimal("1000"), false);
        
TransactionDetails transaction = new TransactionDetails.Builder()
            //...
            .incomeSplitConfig(new ArrayList<SubAccountDetails>(Arrays.asList(subAccount1, subAccount2)))
            .build();


  • No labels