Integrating PayTM API for Mobile
Not as simple as it seems.
One of the most amazing recent experiences I had with a popular third-party SDK was with the PayTM payment gateway Android SDK. The task was to integrate the SDK into a project to facilitate the acceptance of in-app payments through PayTM.
“What comes easy won’t last long, and what lasts long won’t come easy.” ― Pep Talk Radio
Importing the SDK within the android studio project was smooth as butter. All it needed was the addition of one line in the app level build.gradle file.
implementation 'com.paytm:pgplussdk:1.4.4'
The project was synced and all the classes related to the development kit were downloaded.
The next thing to make was a helper class. Since processing a payment requires two major things: the creation of a checksum, and creation of an order, so its code had to be written separately from the calling code to reduce clutter and increase readability. Now came the real challenge.
As mentioned above, the processing of payment requires the generation of a checksum. But the trick here is that the checksum has to be generated on a server, which is not very clearly mentioned in the documentation.

As can be observed above, there is no mention of the word ‘server’ anywhere on the main guide page. Although upon clicking the generate checksum link, it is mentioned that the code has to be run on a server, or the languages in which the sample code is stated also hint that the action is to be carried out on a server environment, an explicit declaration of the same on this page itself would do no harm. I feel it is due to this reason, that many of the forums online are filled with questions regarding why the sample java code stated here isn’t running on android.
Even though not realizing the requirements of the checksum generation wasted a lot of my time, but something more troublesome was coming. I chose Google Cloud Functions to serve as a platform to generate and verify the order checksum, and so went over to the Node.js sample code. What I saw next was the most unexpectable thing from a vendor as large as PayTM.
The switch shown here is an actual part of the official Node.js sample code hosted by PayTM themselves, and it quite evident that the case /generateChecksum is the case responsible for handling creation of checksum, but it is filled with garbage default values. I can not imagine a greater horror for a beginner developer than this.
What followed was a few hours of fiddling around with the raw methods present in the repository until I could finally manage to come up with a working set of methods that could be deployed to Cloud Functions. (You can find the code here). This easily took up a handful of hours and made me lose track of where I originally was in my project. I don’t really feel this puts a third-party library or a sample repository in a good light.
The next thing needed was a network client that could relay my requests over to the PayTM servers. Volley seemed to be just right for the job. Integrating it was quite simple, all the app level build.gradle needed was:
implementation 'com.android.volley:volley:1.1.1'
And to send the request,
StringRequest stringRequest = new StringRequest(Request.Method.POST, checksumServerURL, new Response.Listener<String>() {
@Override
public void onResponse(final String response) {
//Created the order with the checksum here
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handled network errors here
}
}) {
@Override
protected Map<String, String> getParams() {
return orderParamsMap;
}
};
Volley.newRequestQueue(context).add(stringRequest);
To be honest, I went into this incident with an intention to integrate a third-party payment method, but I came out with the integration as well as a contribution to the betterment of the documentation available for the integration. Throughout my endeavour, I came across a lot of messages, posts & questions regarding the same issue that I was facing, which clearly means that finding a solution to this meant helping a lot of other people. So all in all, it turned out to be a bitter-sweet experience for me. But I will suggest anyone who’s on the same path, do give my account a read, you will really save yourself from a lot of trouble!
Here’s the working sample code for future references.