This article details Paystack integration with Android. Paystack is one of the Top Payment Gateways we have in Nigeria

The Paystack Android SDK allows businesses using Paystack to collect payments within their Android app.

The SDK shoulders the burden of PCI compliance, allowing your app to send card information directly to Paystack servers where they can process them. All your server needs to do is call Paystack to verify the status of the transaction. The SDK also validates card details client-side and provides an interface for collecting additional verification like card PIN and OTP.

Paystack supports Android back to version 16, which is the first SDK version that includes TLSv1.2 which is required by their servers. Native app support for user devices older than API 16 will not be available.

Paystack integration with Android

Installations

To install the library on Android Studio, add the following lines to your project’s build.gradle file.

Enable Permissions

To prepare for use, you must ensure that your app has internet permissions by making sure the uses-permission line below is present in the AndroidManifest.xml.

Set Your Public Key

Before you can charge a card with the PaystackSdk class, you need to set your public key. Your public key is available on the Paystack Dashboard.

Set your public key in your project’s AndroidManifest.xml by adding the following lines of code to the tag of your AndroidManifest.xml

Initialize SDK

To use the Paystack Android SDK, you need to first initialize it using the PaystackSdk class.

Make sure to call this method in the onCreate method of your Fragment or Activity or Application

Collecting card information

Create a form that collects card information. You need to collect your customers’ card numbers, expiration dates and CVC. PaystackSDK has a Card class that allows you to collect and verify these Card information, here is a sample Android XML code that helps you collect card information

Validate Card Values and Charge User

Using the functions: setCurrency, setPlan, setSubaccount, setTransactionCharge, setAmount, setEmail, setReference, setBearer, putMetadata, putCustomField, you can set up a fresh transaction direct from the SDK.

The following codes retrieve values from the form and proceed to call the charge function that will be shown later

When an error occurs or transaction or concludes successfully, we will call the methods available in the callback you provided.

OnSuccess will be called once the charge succeeds here we call a function to verify the transaction on the server and perform the desired action you want on the server

beforeValidate is called every time the SDK needs to request user input. This function currently only allows the app know that the SDK is requesting further user input.OnError

onError is called if an error occurred during processing. Some Exception types that you should watch include
ExpiredAccessCodeException: This would be thrown if the access code has already been used to attempt a charge.
ChargeException: This would be thrown if the charge failed. It would hold the message from the server.

Send the reference to your backend and verify by calling our REST API. An authorization will be returned which will let you know if its code is reusable. You can learn more about our verify call here

Below is a sample code on how you can achieve that using php

Do you have any questions on Paystack integration with Android or need help, place a comment below

error0