Android Paytm Integration
Day By Paytm Usage Increasing Fastly and Many Shopping cart and sites start accepting paytm payment accept even irctc start accepting paytm payment.
Today lets we Start integrating paytm gateway in Our Android App.
Let's Start!.
First Signup a Business Account They Need some Docs and Infromation of PAN ,GST , etc. But Dont Worry if You Don't Have this Paytm Also Provide sandbox signup without any of these documents.
Step :1
Visit Here and Signup : - Paytm Business Signup
Click On Start Application Then This Popup Will Open.
Step 2 :
Click Continue.
Signup for Business Account Or Sanbox Account Option on Top Available.
Step 3 : After Signup Copy Our Credentials .
Step : 4 (Server Works Now!)
Download Paytm Checksum utlities from here : Link : Download Paytm PHP Utlity
Upload The file on Local Sever or Live Server (if you have live server).
Currently i am Using Xampp Server. and Address is (http://192.168.43.110)
Here is all Utlity file after extracted.
Step 7 :
Copying the php directory to /opt/lampp/htdocs/ (my local server).
Step 8 :
Editing generateChecksum.php with our Credential of paytm .
I Just Edit Credential and Change print_r function to json_encode to get Result in Json format and parse in android.
<?php header("Pragma: no-cache"); header("Cache-Control: no-cache"); header("Expires: 0"); // following files need to be included require_once("./lib/config_paytm.php"); require_once("./lib/encdec_paytm.php"); $checkSum = ""; // below code snippet is mandatory, so that no one can use your checksumgeneration url for other purpose . $paramList = array(); $paramList["MID"] = 'xxxxxxxxxxxxx'; //Provided by Paytm $paramList["ORDER_ID"] = 'ORDER0000001'; //unique OrderId for every request $paramList["CUST_ID"] = 'CUST00001'; // unique customer identifier $paramList["INDUSTRY_TYPE_ID"] = 'xxxxxxxxxxx'; //Provided by Paytm $paramList["CHANNEL_ID"] = 'WAP'; //Provided by Paytm $paramList["TXN_AMOUNT"] = '1.00'; // transaction amount $paramList["WEBSITE"] = 'xxxxxxxx';//Provided by Paytm $paramList["CALLBACK_URL"] = 'https://pguat.paytm.com/paytmchecksum/paytmCallback.jsp';//Provided by Paytm $paramList["EMAIL"] = 'abc@gmail.com'; // customer email id $paramList["MOBILE_NO"] = '9999999999'; // customer 10 digit mobile no. $checkSum = getChecksumFromArray($paramList,PAYTM_MERCHANT_KEY); $paramList["CHECKSUMHASH"] = $checkSum; echo json_encode($paramList); ?>
Step 9: (Simple Android App with Paytm integration).
Start Android Studio and Create a new Project.
Step 10 : Add two Library of android ( 1 .paytm sdk, 2 .volley ) in build.gradle
Also Add the Internet Permission first in AndroidManifest.xml
in AndroidManifest.xml
in build.gradle
in AndroidManifest.xml
<uses¬permission android:name="android.permission.INTERNET"/> <uses¬permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
in build.gradle
compile 'com.android.volley:volley:1.1.0' compile 'com.paytm:pgplussdk:1.1.3'
Then Sync now.
Step : 11 Create Layout
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:padding="10dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:src="@drawable/books" android:layout_width="100dp" android:layout_height="100dp" /> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:padding="10dp" android:text="Book Sample" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:textColor="#000" android:padding="10dp" android:text="Rs 25.00" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <Button android:layout_gravity="end" android:layout_margin="5dp" android:padding="10dp" android:textColor="#fff" android:background="@color/colorPrimary" android:text="Buy" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> </android.support.constraint.ConstraintLayout>
server code for creating checksum
public void ServerResponse(){ RequestQueue queue = Volley.newRequestQueue(this); StringRequest sr = new StringRequest(com.android.volley.Request.Method.POST, "http://192.168.43.110/Php/generateChecksum.php", new com.android.volley.Response.Listener<String>() { @Override public void onResponse(String response) { //progressDialog.dismiss(); try { Map<String,String> map=new HashMap<>(); JSONObject jsonObj = new JSONObject(response); for(int i=0;i<jsonObj.length();i++){ map.put("MID",jsonObj.getString("MID")); map.put("ORDER_ID",jsonObj.getString("ORDER_ID")); map.put("CUST_ID",jsonObj.getString("CUST_ID")); map.put("INDUSTRY_TYPE_ID",jsonObj.getString("INDUSTRY_TYPE_ID")); map.put("CHANNEL_ID",jsonObj.getString("CHANNEL_ID")); map.put("TXN_AMOUNT",jsonObj.getString("TXN_AMOUNT")); map.put("WEBSITE",jsonObj.getString("WEBSITE")); map.put("CALLBACK_URL",jsonObj.getString("CALLBACK_URL")); map.put("EMAIL",jsonObj.getString("EMAIL")); map.put("MOBILE_NO",jsonObj.getString("MOBILE_NO")); map.put("CHECKSUMHASH",jsonObj.getString("CHECKSUMHASH")); PaytmPay(map); } } catch (JSONException e) { Toast.makeText(MainActivity.this, "Failed To Parse Response", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } Log.d("Response", response); } }, new com.android.volley.Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("Errors", String.valueOf(error)); } }); queue.add(sr); sr.setRetryPolicy(new DefaultRetryPolicy( 20000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); }
Payment Method Foy Paytm
public void PaytmPay(Map<String,String> paramMap) { PaytmPGService Service = null; Service = PaytmPGService.getStagingService(); PaytmOrder Order = new PaytmOrder((HashMap<String, String>) paramMap); Service.initialize(Order, null); Service.startPaymentTransaction(this, true, true, new PaytmPaymentTransactionCallback() { @Override public void someUIErrorOccurred(String inErrorMessage) { Log.d("LOG", "UI Error Occur."); Toast.makeText(getApplicationContext(), " UI Error Occur. ", Toast.LENGTH_LONG).show(); } @Override public void onTransactionResponse(Bundle inResponse) { Log.d("LOG", "Payment Transaction : " + inResponse); Toast.makeText(getApplicationContext(), "Payment Transaction response "+inResponse.toString(), Toast.LENGTH_LONG).show(); } @Override public void networkNotAvailable() { Log.d("LOG", "UI Error Occur."); Toast.makeText(getApplicationContext(), " UI Error Occur. ", Toast.LENGTH_LONG).show(); } @Override public void clientAuthenticationFailed(String inErrorMessage) { Log.d("LOG", "UI Error Occur."); Toast.makeText(getApplicationContext(), " Severside Error "+ inErrorMessage, Toast.LENGTH_LONG).show(); } @Override public void onErrorLoadingWebPage(int iniErrorCode, String inErrorMessage, String inFailingUrl) { Log.d("LOG",inErrorMessage); } @Override public void onBackPressedCancelTransaction() { Log.d("LOG","Back"); // TODO Auto-generated method stub } @Override public void onTransactionCancel(String inErrorMessage, Bundle inResponse) { Log.d("LOG", "Payment Transaction Failed " + inErrorMessage); Toast.makeText(getBaseContext(), "Payment Transaction Failed ", Toast.LENGTH_LONG).show(); } }); }
Complete MainActivity.java Code :
package paytmdemo.supercoders.in.paytmpaymentgateway; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.android.volley.DefaultRetryPolicy; import com.android.volley.RequestQueue; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import com.paytm.pgsdk.PaytmClientCertificate; import com.paytm.pgsdk.PaytmOrder; import com.paytm.pgsdk.PaytmPGService; import com.paytm.pgsdk.PaytmPaymentTransactionCallback; import org.json.JSONException; import org.json.JSONObject; import java.util.HashMap; import java.util.Map; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=(Button)findViewById(R.id.pay); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ServerResponse(); } }); } public void ServerResponse(){ RequestQueue queue = Volley.newRequestQueue(this); StringRequest sr = new StringRequest(com.android.volley.Request.Method.POST, "http://192.168.43.110/Php/generateChecksum.php", new com.android.volley.Response.Listener<String>() { @Override public void onResponse(String response) { //pro gressDialog.dismiss(); try { Map<String,String> map=new HashMap<>(); JSONObject jsonObj = new JSONObject(response); for(int i=0;i<jsonObj.length();i++){ map.put("MID",jsonObj.getString("MID")); map.put("ORDER_ID",jsonObj.getString("ORDER_ID")); map.put("CUST_ID",jsonObj.getString("CUST_ID")); map.put("INDUSTRY_TYPE_ID",jsonObj.getString("INDUSTRY_TYPE_ID")); map.put("CHANNEL_ID",jsonObj.getString("CHANNEL_ID")); map.put("TXN_AMOUNT",jsonObj.getString("TXN_AMOUNT")); map.put("WEBSITE",jsonObj.getString("WEBSITE")); map.put("CALLBACK_URL",jsonObj.getString("CALLBACK_URL")); map.put("EMAIL",jsonObj.getString("EMAIL")); map.put("MOBILE_NO",jsonObj.getString("MOBILE_NO")); map.put("CHECKSUMHASH",jsonObj.getString("CHECKSUMHASH")); PaytmPay(map); } } catch (JSONException e) { Toast.makeText(MainActivity.this, "Failed To Parse Response", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } Log.d("Response", response); } }, new com.android.volley.Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("Errors", String.valueOf(error)); } }); queue.add(sr); sr.setRetryPolicy(new DefaultRetryPolicy( 20000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); } public void PaytmPay(Map<String,String> paramMap) { PaytmPGService Service = null; Service = PaytmPGService.getStagingService(); PaytmOrder Order = new PaytmOrder((HashMap<String, String>) paramMap); Service.initialize(Order, null); Service.startPaymentTransaction(this, true, true, new PaytmPaymentTransactionCallback() { @Override public void someUIErrorOccurred(String inErrorMessage) { Log.d("LOG", "UI Error Occur."); Toast.makeText(getApplicationContext(), " UI Error Occur. ", Toast.LENGTH_LONG).show(); } @Override public void onTransactionResponse(Bundle inResponse) { Log.d("LOG", "Payment Transaction : " + inResponse); Toast.makeText(getApplicationContext(), "Payment Transaction response "+inResponse.toString(), Toast.LENGTH_LONG).show(); } @Override public void networkNotAvailable() { Log.d("LOG", "UI Error Occur."); Toast.makeText(getApplicationContext(), " UI Error Occur. ", Toast.LENGTH_LONG).show(); } @Override public void clientAuthenticationFailed(String inErrorMessage) { Log.d("LOG", "UI Error Occur."); Toast.makeText(getApplicationContext(), " Severside Error "+ inErrorMessage, Toast.LENGTH_LONG).show(); } @Override public void onErrorLoadingWebPage(int iniErrorCode, String inErrorMessage, String inFailingUrl) { Log.d("LOG",inErrorMessage); } @Override public void onBackPressedCancelTransaction() { Log.d("LOG","Back"); // TODO Auto-generated method stub } @Override public void onTransactionCancel(String inErrorMessage, Bundle inResponse) { Log.d("LOG", "Payment Transaction Failed " + inErrorMessage); Toast.makeText(getBaseContext(), "Payment Transaction Failed ", Toast.LENGTH_LONG).show(); } }); } }
Output :
No comments:
Post a Comment