How to integrate a paytm gateway in android
A payment gateway is an e-commerce application service provider service that authorizes credit card payments for e-businesses, online retailers or traditional brick and mortar.
Lets start how to integrate PayTm gateway.Before integrating gateway we are learn and know about PayTm sdk and libraries.
Step 1. build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.samset.paytmpaymentgatwaysample"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
compile files('libs/PGSDK_v1.0.jar')
}
Step 2. PGSDK_v1.0.jar file download it by click here.
After download the jar file put it in a libs folder
Step 3. String.xml file
<resources>
<string name="app_name">SampleMerchantApp</string>
<string name="order_id">Order Id : </string>
<string name="customer_id">Customer Id : </string>
<string name="transaction_amount">Transaction Amount : </string>
<string name="channel_id">Channel Id : </string>
<string name="industry_type_id">Industry Type Id : </string>
<string name="website">Website : </string>
<string name="theme">Theme : </string>
<string name="cust_email_id">Email Id : </string>
<string name="cust_mobile_no">Mobile Number : </string>
<string name="merchant_id">Merchant Id : </string>
<string name="checksum_generation_url">Checksum Generation URL : </string>
<string name="checksum_verification_url">Checksum Verification URL : </string>
<string name="start_transaction">Start Transaction</string>
<string name="sample_order_id">ORDER</string>
<string name="sample_customer_id">CUST23657</string>
<string name="sample_transaction_amount">1</string>
<string name="sample_channel_id">WAP</string>
<string name="sample_industry_type_id">Retail</string>
<string name="sample_website">simplifiedandroidcoding.blogspot.com</string>
<string name="sample_theme">merchant</string>
<string name="sample_cust_email_id">abc@gmail.com</string>
<string name="sample_cust_mobile_no">123</string>
<string name="sample_merchant_id_staging">WorldP64425807474247</string>
<string name="sample_checksum_generation_url">https://pguat.paytm.com/merchant-chksum/ChecksumGenerator</string>
<string name="sample_checksum_verification_url">https://pguat.paytm.com/merchant-chksum/ValidateChksum</string>
</resources>
Step 4. activity_main.xml File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.samset.paytmpaymentgatwaysample.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
Step 5. merchantapp.xml File
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#a3cdc9"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order Details"
android:textStyle="bold"
android:textSize="18dip"
android:textColor="#fff"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dip"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:weightSum="1">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/order_id"
android:textStyle="bold"
android:textSize="16dip"
android:gravity="right"/>
<EditText
android:id="@+id/order_id"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/sample_order_id"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:weightSum="1">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/customer_id"
android:textStyle="bold"
android:textSize="16dip"
android:gravity="right"/>
<EditText
android:id="@+id/customer_id"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/sample_customer_id"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:weightSum="1">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/transaction_amount"
android:textStyle="bold"
android:textSize="16dip"
android:gravity="right"/>
<EditText
android:id="@+id/transaction_amount"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/sample_transaction_amount"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:weightSum="1">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/cust_email_id"
android:textStyle="bold"
android:textSize="16dip"
android:gravity="right"/>
<EditText
android:id="@+id/cust_email_id"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/sample_cust_email_id"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:weightSum="1">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/cust_mobile_no"
android:textStyle="bold"
android:textSize="16dip"
android:gravity="right"/>
<EditText
android:id="@+id/cust_mobile_no"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/sample_cust_mobile_no"
android:singleLine="true" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Merchant Properties"
android:textStyle="bold"
android:textSize="18dip"
android:textColor="#fff"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:weightSum="1">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/merchant_id"
android:textStyle="bold"
android:textSize="16dip"
android:gravity="right"/>
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:id="@+id/merchant_id"
android:text="@string/sample_merchant_id_staging"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:weightSum="1">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/channel_id"
android:textStyle="bold"
android:textSize="16dip"
android:gravity="right"/>
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:id="@+id/channel_id"
android:text="@string/sample_channel_id"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:weightSum="1">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/industry_type_id"
android:textStyle="bold"
android:textSize="16dip"
android:gravity="right"/>
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:id="@+id/industry_type_id"
android:text="@string/sample_industry_type_id"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:weightSum="1">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/website"
android:textStyle="bold"
android:textSize="16dip"
android:gravity="right"/>
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:id="@+id/website"
android:text="@string/sample_website"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:weightSum="1">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/theme"
android:textStyle="bold"
android:textSize="16dip"
android:gravity="right"/>
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:id="@+id/theme"
android:text="@string/sample_theme"
android:singleLine="true"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:weightSum="1"
android:visibility="gone"
android:id="@+id/checksum_generation">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/checksum_generation_url"
android:textStyle="bold"
android:textSize="16dip"
android:gravity="right"/>
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:id="@+id/checksum_generation_url"
android:text="@string/sample_checksum_generation_url"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:weightSum="1"
android:visibility="gone"
android:id="@+id/checksum_verification">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="@string/checksum_verification_url"
android:textStyle="bold"
android:textSize="16dip"
android:gravity="right"/>
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:id="@+id/checksum_verification_url"
android:text="@string/sample_checksum_verification_url"
android:singleLine="true"/>
</LinearLayout>
<Button
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/start_transaction"
android:text="@string/start_transaction"
android:onClick="onStartTransaction"
android:background="#34211d"
android:textColor="#fff"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="16dip"
android:layout_marginTop="10dip"/>
</LinearLayout>
</ScrollView>
Step 6. Mainactivity.java
package com.simplifiedandroidcoding.paytmpaymentgatwaysample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Step 7. MerchantActivity.java
package com.simplifiedandroidcoding.paytmpaymentgatwaysample;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;
import com.paytm.pgsdk.PaytmClientCertificate;
import com.paytm.pgsdk.PaytmMerchant;
import com.paytm.pgsdk.PaytmOrder;
import com.paytm.pgsdk.PaytmPGService;
import com.paytm.pgsdk.PaytmPaymentTransactionCallback;
/**
* This is the sample app which will make use of the PG SDK. This activity will
* show the usage of Paytm PG SDK API's.
**/
public class MerchantActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.merchantapp);
initOrderId();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
//This is to refresh the order id: Only for the Sample App's purpose.
@Override
protected void onStart(){
super.onStart();
initOrderId();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
private void initOrderId() {
Random r = new Random(System.currentTimeMillis());
String orderId = "ORDER" + (1 + r.nextInt(2)) * 10000 + r.nextInt(10000);
EditText orderIdEditText = (EditText) findViewById(R.id.order_id);
orderIdEditText.setText(orderId);
}
public void onStartTransaction(View view) {
PaytmPGService Service = PaytmPGService.getStagingService();
Map<String, String> paramMap = new HashMap<String, String>();
// these are mandatory parameters
paramMap.put("ORDER_ID", ((EditText) findViewById(R.id.order_id)).getText().toString());
paramMap.put("MID", ((EditText) findViewById(R.id.merchant_id)).getText().toString());
paramMap.put("CUST_ID", ((EditText) findViewById(R.id.customer_id)).getText().toString());
paramMap.put("CHANNEL_ID", ((EditText) findViewById(R.id.channel_id)).getText().toString());
paramMap.put("INDUSTRY_TYPE_ID", ((EditText) findViewById(R.id.industry_type_id)).getText().toString());
paramMap.put("WEBSITE", ((EditText) findViewById(R.id.website)).getText().toString());
paramMap.put("TXN_AMOUNT", ((EditText) findViewById(R.id.transaction_amount)).getText().toString());
paramMap.put("THEME", ((EditText) findViewById(R.id.theme)).getText().toString());
paramMap.put("EMAIL", ((EditText) findViewById(R.id.cust_email_id)).getText().toString());
paramMap.put("MOBILE_NO", ((EditText) findViewById(R.id.cust_mobile_no)).getText().toString());
PaytmOrder Order = new PaytmOrder(paramMap);
PaytmMerchant Merchant = new PaytmMerchant(
"https://pguat.paytm.com/paytmchecksum/paytmCheckSumGenerator.jsp",
"https://pguat.paytm.com/paytmchecksum/paytmCheckSumVerify.jsp");
Service.initialize(Order, Merchant, null);
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.
Log.d("LOG", "Payment Transaction is successful " + inResponse);
Toast.makeText(getApplicationContext(), "Payment Transaction is successful ", Toast.LENGTH_LONG).show();
}
@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.
Log.d("LOG", "Payment Transaction Failed " + inErrorMessage);
Toast.makeText(getBaseContext(), "Payment Transaction Failed ", Toast.LENGTH_LONG).show();
}
@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) {
}
// had to be added: NOTE
@Override
public void onBackPressedCancelTransaction() {
// TODO Auto-generated method stub
}
});
}
}
Step 8. Screenshot
Reuired solution for below error
ReplyDeletecom.samset.paytmpaymentgatwaysample E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
ReplyDeleteSolution was to use WebChromeClient (import android.webkit.WebChromeClient;) rather than WebViewClient
payment failed due to any of these reason error is coming
ReplyDeleteGood post....thanks for sharing.. very useful for me i will bookmark this . Thanks.
ReplyDeleteBest Deals at BuyUrBest
Attempt to invoke virtual method 'java.lang.String com.example.hp.paytm_integrate.Checksum.getChecksumHash()' on a null object reference
ReplyDeleteat com.example.hp.paytm_integrate.MainActivity$2.onResponse(MainActivity.java:93)
It shows payment failed due to any of these reason.
ReplyDelete