Android Payment Gateway Integration Guide: PAYTM

Himanshu Nautiyal
The Zalonin
Published in
4 min readJul 8, 2019

Install SDK

Install Paytm Android SDK using Android Studio and IntelliJ. To add the SDK to your app, add the following dependency in your build.gradle:

dependencies { compile(‘com.paytm:pgplussdk:1.3.3’) { transitive = true; } }

SMS Permission

To allow SDK to auto read the OTP sent by the bank during account verification, you need static and at runtime permissions

Add the following code to your AndroidManifest.xml to get static permission

<uses-permission android:name=”android.permission.INTERNET”/> 
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”/>
<uses-permission android:name=”android.permission.READ_SMS”/>
<uses-permission android:name=”android.permission.RECEIVE_SMS”/>
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_SMS}, 101); }

Proguard Rules

If you’re using proguard for your build, you need to add the following lines to your proguard file (proguard-rules.pro):

-keepclassmembers class com.paytm.pgsdk.paytmWebView$PaytmJavaScriptInterface { public *; }

These are the basic steps which one need to follow to set up the environment.

First of all, let’s understand the basic flow that we need to follow

Steps
1.
Make a request to the server and generate a checksum(a hashed key containing information of different parameters). The server code is provided by paytm we just need to modify it a little bit.
2. Get the response from the server inside your android app. The response contains a JSON with different keys value pair mainly CHECKSUM
3. Now start the transaction, i.e. after this, a screen will be prompted requesting you for the mobile number and transaction method
4. Once the transaction is successful and you get the TRAX_SUCCESS as a response reverify the transaction by making a request to URL discussed below and if the response contains status as successful, “Yeah we did it”

Now let’s Follow the code below to convert our imagination into reality.

STEP 1. Generating Checksum

Firstly make a post request to the server from the Android app (you can use any method but I will be using OkHTTP for the request)

Android Payment gateway integration: PAYTM

Server Code

Paytm provide the source code for the generation of checksum which can be downloaded from here.

The “route.js” file present in the above repo contains a route “/generate_checksum” which can be modified as

Don't forget to change credential present at ./paytm/paytm_config/

Here we observe that we have used staging services, “Staging ” is nothing but refers to testing. Before giving live credentials of the merchant, paytm make sure that the integration is fully completed so that users using that app may not find any problem in the transaction.

Once the server is set up properly we will get a response inside our Android app containing all required credentials like MID, ORDERID, Website, etc.

Make sure the credential that you use for generating the checksum must be same as the data you send to the app, else you will get an “Invalid checksum Error”.

Use these credential inside the app to launch the payment screen

Inside the OnResponse(…) function we make Paytm services and start the transaction.
Create a new instance of Paytm Services

//for staging
PaytmPGService Service = PaytmPGService.getStagingService();
//for production
PaytmPGService Service = PaytmPGService.getProductionService();

As we are done with creating a PaytmOrder, Now its time to start the transaction. This is done using startTransaction method. This method is responsible for shifting the Apps UI thread to Paytm Payment Page. This paytm payment page is not fired by any magic but the paytm SDK which we installed in our starting steps

Once you start the activity you will be prompted with a screen asking for your mobile number, which will finally lead you to the Payment page.

If you are using the stagging services than the UI of the payment page will be different.

Once you successfully make the payment you will get payment successful as the response with some other parameters like orderId, Gateway, Payment methods, etc.

Now we are only left with re-verification of the paytm response thus obtained.
For this, we again need to make a request to our server which will further make a request to the Paytm server to get the response. Once you get the response you can modify the UI Accordingly.

SERVER CODE :

If you send a request on the URL with path “/gettxnstatus” the server create a checksum and hit the paytm server for response.

Make sure that the request must be a post request with body as {ORDERID:”123433”} and the most important part, ORDERID must be that on which we have already made a transaction.

Inside the OnTransactionResponse(..) we get the response and thus we will get ORDERID for which transaction was successful. Now we will hit the above URL with the same ORDERID to get the re-verification response.

ANDROID CODE:

And if we get the transaction a successful in the response then we can easily state that the transaction is completely successful. Thus we can modify the URL.

How to get credential from Paytm?

You have to just make a merchant account on the Paytm business section and then just send a mail to the Paytm or you can call to their helpline number.
Paytm is very fast in this process and thus the provide these credential very fastly and efficiently.

Happy Coding. Subscribe now if you wanna read more.

--

--