Android SDKBeta

The Android SDK provides UI components and methods that allow you acceptpayment in your Android app.

Beta Release

The Android SDK is currently a beta release. If you encounter any issues or have suggestions while using it, don't hesitate to reach out. We’d love to hear from you!

Introduction

The Android SDK is collection of methods and interfaces that allow developers to build a secure, and convenient payment flow for their Android applications. Integration is a two-step process:

  1. Initiate the transaction on the server
  2. Complete it on the SDK

Project Requirements

Paystack Android SDK is designed to support Android 6.0 (API level 23) and above. We do not support OS versions below Android 6.0. Before using the SDK, you need to ensure your app meets the following requirements:

  • Android 6.0 (API level 23) and above
  • Android Gradle Plugin 7.2 and above
  • Gradle 7.1.3 and above
  • AndroidX

Installation

To use the SDK, add the paystack-ui in the dependencies block of your app-level build file:

Latest dependency version

You should check Maven Central to get the latest version before installation.

1dependencies {
2 implementation 'com.paystack.android:paystack-ui:0.0.9'
3}

Ensure you sync the project to download the SDK into your project. On successful installation, you’d have access to the UI components and methods to accept payment in your Android app.

Paystack Builder

The Paystack class uses a builder pattern to handle housekeeping and foundational tasks before the UI components can be used. It is part of the core library:

1import com.paystack.android.core.Paystack

The table below shows the methods available in the Paystack class:

MethodsDescription
setPublicKey("public-key")This method is used to set the public key. Without a public key, you can’t complete a transaction.
setLoggingEnabled(boolean)This method determines if you get debug logs or not. By default, it’s set to false which means you won’t get any debug log.
1Paystack.builder()
2 .setPublicKey("pk_domain_xxxxxxxx")
3 .setLoggingEnabled(true)
4 .build()

Initialize Transaction

The SDK requires an access_code to display the UI component that accepts payment. To get the access_code, make a POST request to the Initialize TransactionAPI endpoint from your server:

Show Response
1curl https://api.paystack.co/transaction/initialize
2-H "Authorization: Bearer YOUR_SECRET_KEY"
3-H "Content-Type: application/json"
4-d '{ "email": "[email protected]",
5 "amount": "500000"
6 }'
7-X POST
1{
2 "status": true,
3 "message": "Authorization URL created",
4 "data": {
5 "authorization_url": "https://checkout.paystack.com/nkdks46nymizns7",
6 "access_code": "nkdks46nymizns7",
7 "reference": "nms6uvr1pl"
8 }
9}

On a successful initialization of the transaction, you get a response that contains an access_code. You need to return this access_code back to your mobile app.

Secret key safeguarding

Do not make an API request to the Initialize Transaction endpoint directly on your mobile app because it requires your secret key. Your secret key should only be used on your server where stronger security measures can be put in place.

Payment Sheet

The PaymentSheet is the UI component that contains the payment form and the available payment channels. It is part of the UI library that can be imported as shown below:

1import com.paystack.android.ui.paymentsheet.PaymentSheet

The PaymentSheet class is initialized in the onCreate method of your Activity or Fragment with two arguments:

ArgumentsExampleDescription
ActivitythisThis is the reference to your Activity or Fragment
CallbackpaymentResultThis is the method that handles all post-payment processes. It’s argument has the type of PaystackSheetResult
1private lateinit var paymentSheet: PaymentSheet
2
3override fun onCreate(savedInstanceState: Bundle?) {
4 super.onCreate(savedInstanceState)
5 setContentView(R.layout.activity_main)
6
7 // library initialization code snippets and others go here
8
9 paymentSheet = PaymentSheet(this, ::paymentComplete)
10
11 // more snippet
12}

The PaymentSheet comes with a launch method that allows you trigger the display of the component. The launch method takes the access_code from a previously initialized transaction.

1fun makePayment() {
2 // Pass access_code from transaction initialize call on the server
3 paymentSheet.launch("br6cgmvflhn3qtd")
4}

Payment Sheet Result

The PaymentSheetResult exposes the different states of a transaction. It is the argument type for the PaymentSheet callback:

1import com.paystack.android.ui.paymentsheet.PaymentSheetResult
2
3private fun paymentComplete(paymentSheetResult: PaymentSheetResult) {
4
5}

The different state that the PaymentSheetResult exposes are:

StateDescription
CompletedThe customer completed the payment process. We return a paymentCompletionDetails that contains the transaction reference. You should use the transaction reference to verify the transaction status and amount on the server before providing value.
CancelledThe customer cancelled the payment.
FailedAn error occurred during the payment process. This result contains an error. It may also contain a reference for the transaction.
1private fun paymentComplete(paymentSheetResult: PaymentSheetResult) {
2 val message = when (paymentSheetResult) {
3 PaymentSheetResult.Cancelled -> "Cancelled"
4 is PaymentSheetResult.Failed -> {
5 Log.e("Something went wrong", paymentSheetResult.error.message.orEmpty(), paymentSheetResult.error)
6 paymentSheetResult.error.message ?: "Failed"
7 }
8
9 is PaymentSheetResult.Completed -> {
10 // Returns the transaction reference PaymentCompletionDetails(reference={TransactionRef})
11 Log.d("Payment successful", paymentSheetResult.paymentCompletionDetails.toString())
12 "Successful"
13 }
14 }
15
16 Toast.makeText(this, "Payment $message", Toast.LENGTH_SHORT).show()
17}