How to Implement Secure Payment on Android using Paystack
Nowadays, most businesses require their customers to make payments to purchase a good or service. Customers on the other hand are looking for a flexible, easy, convenient and secure way to make these payments.
In this article, I will show you how to accept payments on Android using Paystack’s sdk.
Paystack helps businesses in Africa get paid by anyone, anywhere in the world.
This is how our app will look like
On click of that pay button — Depending on the type of transaction, and card, a pin pad view (provided by Paystack
) will popup, requesting the user to input his/her pin. If the details provided was successful, the card will be charged. If not, appropriate error messages will be displayed.
Here is a video showing full implementation on an app I worked on:
App was written entirely in:
- Kotlin
STAGES
- Set up Paystack
- Set up Android Studio
SETUP PAYSTACK
The first step is to create a new Paystack
account
- Visit https://paystack.com/
- Click on Create Free Account
- Fill the required form, confirm your email, provide necessary documents
- Login to dashboard
Your dashboard should look like this
- Switch the toggle at the top right conner from ‘Live’ to ‘Test’. (Because you are currently testing, once you’ve finished testing and ready to ship to production, you can switch back to Live
)
- Click on the profile icon
at the top right corner, click on Profile
- Navigate to Api Keys And WebHooks
In there, lies all your configurations for both ‘Test Mode’ and ‘Production Mode’. There are 2 keys
1. Secret Key and
2. Public Key
Secret key
as the name implies is confidential and should never be disclosed or seen as part of the code in your app.
Public key
will be used to initialise the paystack’s sdk
in our app
Copy the Test Public Key
and keep it somewhere, we will use this shortly in our app
SET UP ANDROID STUDIO
- Open Android Studio,
Create A New Project
— Name it whatever you want - Navigate to your Build.gradle (app) file and include the following:
— DataBinding for Interacting with views
buildFeatures {
viewBinding true
}
— Paystack Dependency
implementation 'co.paystack.android:paystack:3.1.2'
- Make sure you Sync Your Project after adding the dependency
- Under your project directory, right click and create a new
class
with the name App.kt — This is a global class which extendsApplication()
class and called before the application starts.
- Now Open Manifest.xml and add the following
— Internet Permission (Added before the opening application tag)
<!-- Internet permission -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
— In the Application tag, add android:name
attribute to point to the App
Class you just created
android:name=".MyApp"
— Before the closing Application
Tag, add Paystsack
meta tag which includes your Test Public Key which was gotten from your Paystacks
dashboard
<meta-data
android:name="co.paystack.android.PublicKey"
android:value="paste the test public key here" />
DESIGN LAYOUT
Open activity_main.xml
paste the following code. We are using a constraint layout nested in a scrollview.
GET CARD DETAILS AND VALIDATE
It’s time to get the card details (Card number, Expiry date, CVV) and do proper validations. I added a text watcher on all fields, This is the explanation of what the code does:
- Check the length of the string entered
- Add appropriate characters (space, slash) as a user types card number and cvv respectively
- On click of “
Pay
” button, set your pay stack public key, initialize the charge object (Get the card number, expiry and cvv) — Perform validation - Charge the card, receive call backs — On successful charge and failed charge
- On each case, perform a verification on your backend and send the transaction reference to your back end.
(You should also set up webhooks
in the backend of your application, so that for each successful transaction, a charge.success
webhook event is sent to the URL
as a POST
request)
See — https://paystack.com/docs/payments/webhooks/
Our full MainActivity code looks like this
With this, you have successfully implemented a secure payment system on android.
Please check the full code on Github:
NOTE: Github repo does not contain any test public key, running the app will fail with an error —
Invalid public key. To create a token, you must use a valid public key. Ensure that you have set a public key.
So, make sure you provide a valid test key from your paystack
dashboard.
Inspired by Emmanuel Yanum — : FullStack Dev.
Cheers!! 😃
Reach Me
https://linktr.ee/ibrajix