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
11 Comments
Jan 5, 2020 at 10:48 PM /
Thanks for the material. But please can you show me where to use pay method. ie pay(View view)
Jan 6, 2020 at 1:18 PM /
Please use it when a user clicks a button
Jan 9, 2020 at 8:34 AM /
Thanks for the code, but I’m getting transaction reference not found from the back end which I used the same php verification code u put here. Android SDK will process it fine and send the reference to my back end but it will still return it as reference not found. What could be the cause sir?
Jan 9, 2020 at 10:02 AM /
I integrated ur code on my android but when I run it it will return transaction reference not found from my back end. What could be the cause?. I’m talking about android paystack integration
Mar 5, 2020 at 11:30 AM /
How do retrieve transaction details or history and display it to the user?
Mar 6, 2020 at 3:45 AM /
Great post. Rather than getting the card info. Does paystack have its own card form ?? Something similar to the web app ?
Mar 6, 2020 at 10:16 AM /
Yes it does
Mar 23, 2020 at 8:42 PM /
What if i want to add OTP verification to a transaction. How does one go about it?
Dec 21, 2020 at 2:16 PM /
Please am trying to use this but if i place PaystackSdk.initialize(getApplicationContext()); in onCreate in fragment or onCreateView in fragment, it marks PaystackSdk red and i followed the steps until this line where i need to add the Paystacksdk.initialize(getApplicationContext).
Please what do i do? thank you
Feb 13, 2021 at 11:30 PM /
Please how can I implement the mobile money payment option into my app instead of card?
Feb 14, 2021 at 10:02 AM /
You will need to discusss with the mobile money agent to see if they have an API you can connect to. Thanks for this intelligent question