Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday 19 May 2016

PayTm payment gateway integration with android

Hello


A payment gateway is an e-commerce application service provider service that authorizes credit card payments for e-businesses, online retailers, bricks and clicks, 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.

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')     // this lib copy and paste in tour project lib folder
}



actvity_main.xml

<?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>

string.xml

<resources>
    <string name="app_name">Paytm gateway sample</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">samsetdev.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>

ActvityMain.java

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 MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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) {

}

@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
}

});
}
}


Thank you

I hope this blog very helps you

Full Source code Paytmpaymentgateway


Live Sample



No comments:

Post a Comment