Integrating Paytm PGSDK_V2.0 for Android
This article provides a step-by-step guide to integrating Paytm PGSDK_V2.0 into your Android application for seamless payment processing.
Prerequisites
- Android Studio
- Paytm Merchant Account
- Paytm PGSDK_V2.0 Library
Step 1: Set up the Paytm Merchant Account
- Create a Paytm Merchant Account (if you haven’t already) at https://paytm.com/merchant-onboarding
- Obtain your Merchant ID, Merchant Key, and Website (for Mobile Checkout).
Step 2: Integrate the Paytm PGSDK_V2.0 Library
2.1 Add the dependency in your app’s `build.gradle` file:
dependencies {
implementation 'com.paytm.pgsdk:pgsdk:1.x.x'
}
2.2 Sync your project with Gradle files.
// Sync your project with the Gradle files to ensure the dependency is added successfully.
Step 3: Implement the Payment Flow
3.1 Create a Payment Request
// Create a HashMap to store payment parameters
HashMap paramMap = new HashMap<>();
paramMap.put("MID", "YOUR_MERCHANT_ID");
paramMap.put("ORDER_ID", "ORDER_ID");
paramMap.put("CUST_ID", "CUSTOMER_ID");
paramMap.put("CHANNEL_ID", "WAP");
paramMap.put("TXN_AMOUNT", "AMOUNT");
paramMap.put("WEBSITE", "YOUR_WEBSITE");
paramMap.put("CALLBACK_URL", "YOUR_CALLBACK_URL");
paramMap.put("INDUSTRY_TYPE_ID", "INDUSTRY_TYPE");
3.2 Initiate Payment
// Create a PaytmChecksum object to generate checksum
PaytmChecksum.generateSignature(paramMap, "YOUR_MERCHANT_KEY").thenAccept(checksum -> {
// Add checksum to the parameter map
paramMap.put("CHECKSUMHASH", checksum);
// Initiate the payment using Paytm PGSDK
PaytmPGService.getPGService().launchPayment(paramMap, new PaytmPGService.PaytmPaymentParam(YOUR_ACTIVITY), new PaytmPGService.PaytmPaymentListener() {
@Override
public void onTransactionResponse(Bundle bundle) {
// Handle the transaction response
// Check for success, failure, or pending status
}
@Override
public void networkNotAvailable() {
// Handle network errors
}
@Override
public void clientAuthenticationFailed(String s) {
// Handle client authentication failures
}
@Override
public void someUIErrorOccurred(String s) {
// Handle UI errors
}
@Override
public void onErrorLoadingWebPage(int i, String s, String s1) {
// Handle web page loading errors
}
@Override
public void onBackPressedCancelTransaction() {
// Handle user cancelling the transaction
}
});
}).exceptionally(throwable -> {
// Handle any exceptions during checksum generation
return null;
});
Step 4: Handle the Transaction Response
4.1 Success
// If the transaction is successful, the response bundle will contain the transaction details:
String transactionId = bundle.getString("TXNID");
String paymentMode = bundle.getString("PAYMENTMODE");
// Access other transaction details as needed.
4.2 Failure
// If the transaction fails, the response bundle will contain error details:
String errorCode = bundle.getString("RESPMSG");
String errorDescription = bundle.getString("RESPMSG");
// Access other error details as needed.
4.3 Pending
// If the transaction is pending, the response bundle will contain the status:
String status = bundle.getString("STATUS");
// Handle pending status appropriately.
Step 5: Secure Integration
- Use the production environment only after rigorous testing.
- Do not store sensitive information (like Merchant Key) in your code directly.
- Implement proper error handling and security measures.
Code Example: Implementing Paytm Integration
package com.example.paytm_integration;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;
import com.paytm.pgsdk.PaytmPGService;
import com.paytm.pgsdk.PaytmPaymentParam;
import com.paytm.pgsdk.PaytmPaymentListener;
import com.paytm.pgsdk.PaytmChecksum;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Replace these values with your actual merchant credentials
String mid = "YOUR_MERCHANT_ID";
String merchantKey = "YOUR_MERCHANT_KEY";
String website = "YOUR_WEBSITE";
// Create payment parameters
HashMap paramMap = new HashMap<>();
paramMap.put("MID", mid);
paramMap.put("ORDER_ID", "ORDER_ID");
paramMap.put("CUST_ID", "CUSTOMER_ID");
paramMap.put("CHANNEL_ID", "WAP");
paramMap.put("TXN_AMOUNT", "AMOUNT");
paramMap.put("WEBSITE", website);
paramMap.put("CALLBACK_URL", "YOUR_CALLBACK_URL");
paramMap.put("INDUSTRY_TYPE_ID", "INDUSTRY_TYPE");
// Generate checksum
CompletableFuture checksumFuture = PaytmChecksum.generateSignature(paramMap, merchantKey);
checksumFuture.thenAccept(checksum -> {
// Add checksum to the parameter map
paramMap.put("CHECKSUMHASH", checksum);
// Initialize Paytm PGSDK
PaytmPGService.getPGService().launchPayment(paramMap, new PaytmPaymentParam(MainActivity.this), new PaytmPaymentListener() {
@Override
public void onTransactionResponse(Bundle bundle) {
// Handle the transaction response
String response = bundle.getString("STATUS");
if (response.equals("TXN_SUCCESS")) {
Toast.makeText(MainActivity.this, "Transaction Successful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Transaction Failed", Toast.LENGTH_SHORT).show();
}
}
@Override
public void networkNotAvailable() {
Toast.makeText(MainActivity.this, "Network Not Available", Toast.LENGTH_SHORT).show();
}
// ... (Implement other methods)
});
}).exceptionally(throwable -> {
// Handle any exceptions during checksum generation
Toast.makeText(MainActivity.this, "Error generating checksum", Toast.LENGTH_SHORT).show();
return null;
});
}
}
Conclusion
By following these steps, you can successfully integrate Paytm PGSDK_V2.0 into your Android application. Remember to test thoroughly and ensure your implementation is secure.