Integrating Paytm Wallet SDK in an Android Application

08 / Feb / 2016 by Ashish Mishra 14 comments

How Paytm SDK Work

The Paytm Payments SDK for iOS and Android enables merchants to integrate payments into their mobile app. The following diagram describes how a typical payment transaction works. The components involved in completing a payment transaction are as follows:

  • User Placing the Order: The end user who wants to initiate a payment transaction.
  • Merchant Application: The Application of the merchant where the Paytm Wallet needs to be integrated. This may be an Android or iOS application.
  • Paytm SDK: The SDK provided by Paytm to make all the API calls to Payment Gateway and Merchant’s Web server (which is explained in the next point).
  • Merchant’s Web server: The merchant needs to setup this web server at their end. The logic to generate and verify CheckSum needs to be setup on this server. Checksum helps ensure the integrity of transactions i.e. it helps ensure that the transaction requests have not been intercepted and tampered with. Checksum utilities are provided by Paytm for different development platforms.
  • Payment Gateway: The Payment Gateway of Paytm. It will communicate with the Paytm SDK and Merchant’s Web server. In case, the payment is made via credit cards/debit cards/net banking, the Payment Gateway will interact with the Banks as well.

SDK Flow

Steps for Android Integration

Step are given below.

  • download PGSDK library from http://paywithpaytm.com/developer/#web_plugin or https://github.com/Paytm-Payments/Paytm_Android_App_Kit
  • Client side certificate (Optional :provide by PAYTM Web).
    Password for the Client side certificate. (Optional: provided by PAYTM Web).
    Add Client side certificate file inside raw folder. If raw folder is not there then create raw folder within “res” folder.
  • Add the INTERNET and ACCESS_NETWORK_STATE permissions to your AndroidManifest.xml. These two permissions are required for PGSDK Service to run. AndroidManifest.xml <uses-permission android:name=”android.permission.INTERNET”/>
    <uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”/>
  • Add PaytmPGActivity in your AndroidManifest.xml and this activity already present in your PGSDK.This Activity perform all transaction related information and provide you callback.

AndroidManifest.xml

<activity
android:name=”com.paytm.pgsdk.PaytmPGActivity”
android:configChanges=”keyboardHidden|orientation|keyboard”
/>

How to use API

  1. Obtain the PaytmPGService instance. Either obtain Staging service or obtain Production service depending on your need. PaytmPGService.getStagingService() will return the Service Object pointing to Staging Environment.PaytmPGService.getProductionService() will return the Service Object pointing to Production Environment.
  2. Create PaytmOrder Object holding the Order Information. Syntax for creating PaytmOrder Object is as follows:

PaytmOrder Order = new PaytmOrder(Map<String, String> paramMap);

Where,
paramMap is the map containing request parameter names and their respective values. Please refer Example given in the next sub-section.

  1. Create PaytmMerchant Object holding the Merchant Configuration. Syntax for creating PaytmMerchant Object is as follows:

PaytmMerchant Merchant = new PaytmMerchant(String inChecksumGenerationURL, String inChecksumVerificationURL);

where,
inChecksumGenerationURL is the Merchant Checksum Generation URL and
inChecksumVerificationURL Merchant Checksum Verification URL

  1. Create PaytmClientCertificate Object holding the Certificate information. This step is conditional. Only if the client certificate is requested and used by merchant (as given in Prerequisites section above), then create the PaytmClientCertificate object. Otherwise pass the PaytmClientCertificate as null. Syntax for creating PaytmClientCertificate Object is as follows:

PaytmClientCertificate Certificate = new PaytmClientCertificate(String inPassword, String inFileName);

where,
inPassword is the Client side certificate password
inFileName is the Client side certificate file name. This file must be present inside “raw” folder.

  1. Call the initialize() method to set PaytmOrder, PaytmMerchant and PaytmClientCertificate Objects. Method Signature is as follows:

void com.paytm.pgsdk.PaytmPGService.initialize(PaytmOrder inOrder, PaytmMerchant inMerchant, PaytmClientCertificate inCertificate);

inOrder  is the Object which is holding Order Information.
inMerchant  is the Object which is holding Merchant Configuration.
inCertificate is the Object which is holding Certificate Information. Pass this as null if no client certificate is used by the merchant (as given above in the Prerequisites section

Example-

//Getting the Service Instance. PaytmPGService.getStagingService()  will return the Service pointing to Staging Environment and PaytmPGService.getProductionService() will return the Service pointing to Production Environment.
PaytmPGService Service = null;
Service = PaytmPGService.getStagingService();
//or
Service = PaytmPGService.getProductionService();
//Create new order Object having all order information.
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("REQUEST_TYPE", "DEFAULT");
paramMap.put("ORDER_ID", ‘ORDER12345’)
paramMap.put("MID", "klbGlV59135347348753");
paramMap.put("CUST_ID”,’CUST110’)
paramMap.put("CHANNEL_ID", "WAP");
paramMap.put("INDUSTRY_TYPE_ID", "Retail");
paramMap.put("WEBSITE", "paytm");
paramMap.put("TXN_AMOUNT", "1.0");
paramMap.put("THEME ", "merchant");

PaytmOrder Order = new PaytmOrder(paramMap);
//Create new Merchant Object having all merchant configuration.
PaytmMerchant Merchant = new PaytmMerchant( "http://hostname/<checksum-gerneration-URL>", " http://hostname/<checksum-verification-URL>");

//Set PaytmOrder and PaytmMerchant objects. Call this method and set both objects before starting transaction.
Service.initialize(Order, Merchant, null);

//Start the Payment Transaction. Before starting the transaction ensure that initialize method is called.
Service.startPaymentTransaction(this, true, true, new PaytmPaymentTransactionCallback()
{
  @Override
  public void someUIErrorOccurred(String inErrorMessage)
  {
  // Some UI Error Occurred in Payment Gateway Activity.
  // This may be due to initialization of views in Payment Gateway Activity or may be due to initialization of webview.
  // Error Message details the error occurred.
  }
  
  @Override
  public void onTransactionSuccess(Bundle  inResponse)
  {
    // After successful transaction this method gets called.
    // Response bundle contains the merchant response parameters.
  }
  
  @Override
  public void onTransactionFailure(String inErrorMessage, Bundle  inResponse)
  {
    // This method gets called if transaction failed.
    // Here in this case transaction is completed, but with a failure.
    // Error Message describes the reason for failure.
    // Response bundle contains the merchant response parameters.
  }
  
  @Override
  public void networkNotAvailable()
  {
  // If network is not available, then this method gets called.
  }
  
  @Override
  public void clientAuthenticationFailed(String  inErrorMessage)
  {
  // This method gets called if client authentication failed.
  // Failure may be due to following reasons
  //      1. Server error or downtime.
  //      2. Server unable to generate checksum or checksum response is 
  //         not in proper format.
  //      3. Server failed to authenticate that client. That is value of
  //         payt_STATUS is 2.
  // Error Message describes the reason for failure.
  }
  
  @Override
  public void onErrorLoadingWebPage(int iniErrorCode, String inErrorMessage, String inFailingURL)
  {
  // This page gets called if some error occurred while loading some URL in Webview.
  // Error Code and Error Message describes the error.
  // Failing URL is the URL that failed to load.
  }
  });

Test your  transactions:

The following Staging Wallet can be used for testing transactions:
Mobile number: 7777777777
Password: Paytm12345
OTP – 489871

Response Codes

Interpretation of the most common response codes can be found below:

  • 101-  Transaction Successful
  • 141-  Transaction cancelled by customer after landing on Payment Gateway Page.
  • 227-  Payment Failed due to a technical error. Please try after some time.
  • 810-  Page closed by customer after landing on Payment Gateway Page.
  • 8102-   Transaction cancelled by customer post login. Customer had sufficient Wallet balance for completing transaction.
  • 8103-  Transaction cancelled by customer post login. Customer had in-sufficient Wallet balance for completing transaction.
FOUND THIS USEFUL? SHARE IT

comments (14)

  1. Sagar

    Hi I am using paytm sdk in android app . and i have checksum and payment gateway response url. when I test the checksum url at development and staging using postman/rest client (using urlencode parameters) . Its working but when i test the same with android . I get some error. I am using django at backend . I have compare both the objects sending from app and postman . I have found no diffrence in it . What could be the problem

    Reply
    1. Ashis Mishra

      Please check your request param while sending data from mobile app.
      Paytm provide complete doc how to send data when you are requesting from mobile app.

      Reply
    1. Ashis Mishra

      Please contact PAYTM technical support team they will provide you url inChecksumGenerationURL and inChecksumVerificationURL both.

      Reply
    1. Ashish Mishra Post author

      Hi Rajashree,

      Please let me know if you facing any issue while integrating with IOS sdk.
      You can directly communicate with PAYTM technical team and they will help out.

      Reply
    1. Ashish Mishra Post author

      Through checksum utility you can generate nChecksumGenerationURL and nChecksumVerificationURL .You can check in sdk provided by paytm team for web application.

      Backend developer provide you inChecksumGenerationURL and inChecksumVerificationURL from checksum utility.

      Reply
  2. Ravindra Varpe

    Hello
    I am using your way to integrate in android app in my code getStagingService() working correctly but when we got to production environment then server application show session expired or clear cookies

    plase give some solution

    Reply

Leave a comment -