Create Android Payment Gateway Using WebView PayUMoney

Introduction

In this article, we will learn how to implement PayUMoney payment gateway in an Android Xamarin application. Using payment gateway, we can use credit card, debit card, or Netbanking facility for paying online.

Let’s start,

Step 1

Open Visual Studio->New Project->Templates->Visual C#->Android->Blank App

Select Blank App. Then give Project Name and Project Location.

 

Step 2

Next, create New Layout for Payment Success and Failure Page.

Go to Solution Explorer-> Project Name->Resources->layout, then right click to Add->New Item. This opens a new Dialog box where you need to select Android Layout and give names as Success.axml and Failure.axml.

Step 3

After this, create Activity Page for both design pages.

Step 4

Next,open Solution Explorer-> Project Name->Resources->Layout->Main.axml and click to add WebView.

 

Step 5

Add Reference Namespace for Mono.Android.Export. Solution Explorer-> Project Name-> References-> Mono.Android.Export.

 

Step 6

After that, open Solution Explorer-> Project Name->MainActivity.cs then Add below Namespaces.

  1. using Android.Webkit;  
  2. using Java.Lang;  
  3. using Android.Util;  
  4. using Java.Security;  
  5. using System.Text;  
  6. using Android.Net.Http;  
  7. using Java.Interop;  

Step 7

Next step is to create variables and then declare the variables in OnCreate(). 

C# Code

  1. private static string txnid;  
  2. private static string TAG = "MainActivity";  
  3. private static WebView webviewPayment;  
  4. private WebViewClient webViewClient = new MyWebViewClient();  
  5. private static Context context;  
  6. private static string SUCCESS_URL = "https://www.payumoney.com/mobileapp/payumoney/success.php";  
  7. private static string FAILED_URL = "https://www.payumoney.com/mobileapp/payumoney/failure.php";  
  8. private static string firstname = "Anbu";  
  9. private static string email = "anbukm91@gmail.com";  
  10. private static string productInfo = "test";  
  11. private static string mobile = "8220155182";  
  12. public static string totalAmount = "100";  

OnCreate Method 

C# Code

  1. webviewPayment = (WebView) FindViewById(Resource.Id.webView1);  
  2. webviewPayment.Settings.JavaScriptEnabled = true;  
  3. webviewPayment.Settings.SetSupportZoom(true);  
  4. webviewPayment.Settings.DomStorageEnabled = true;  
  5. webviewPayment.Settings.LoadWithOverviewMode = true;  
  6. webviewPayment.Settings.UseWideViewPort = true;  
  7. webviewPayment.Settings.CacheMode = CacheModes.NoCache;  
  8. webviewPayment.Settings.SetSupportMultipleWindows(true);  
  9. webviewPayment.Settings.JavaScriptCanOpenWindowsAutomatically = true;  
  10. webviewPayment.AddJavascriptInterface(new PayUJavaScriptInterface(this), "PayUMoney"); //JavaInterface  
  11. Java.Lang.StringBuilder url_s = new Java.Lang.StringBuilder();  
  12. url_s.Append("https://test.payu.in/_payment"); //PauMoney Test URL  
  13. Log.Info(TAG, "call url " + url_s);  
  14. webviewPayment.PostUrl(url_s.ToString(), Encoding.UTF8.GetBytes(getPostString()));  
  15. webviewPayment.SetWebViewClient(webViewClient);  

Step 8

Implement WebViewClient() by declaring MyWebViewClient Refer on WebViewClient.

C# Code

  1. //WebView Client Run Time  
  2. private class MyWebViewClient: WebViewClient {  
  3.     public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon) {  
  4.         base.OnPageStarted(view, url, favicon);  
  5.     }  
  6.     public override void OnPageFinished(WebView view, string url) {  
  7.         base.OnPageFinished(view, url);  
  8.     }  
  9.     public override void OnReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {  
  10.         Log.Info("Error""Exception caught!");  
  11.         handler.Cancel();  
  12.     }  
  13. }  

Step 9

Followed Web Client declare Java Interface. Using after complete payment process its redirect to Success of Failure. 

C# Code

  1. //Java Interface After Payment its Return Success/failure  
  2. private class PayUJavaScriptInterface: Java.Lang.Object {  
  3.     Context mContext;  
  4.     public PayUJavaScriptInterface(Context c) {  
  5.         mContext = c;  
  6.     }  
  7.     // public void Success  
  8.     [Export]  
  9.     [JavascriptInterface]  
  10.     public void success(long id, string paymentId) {  
  11.             Intent intent = new Intent(mContext, typeof(SuccessActivity));  
  12.             mContext.StartActivity(intent);  
  13.         }  
  14.         [Export]  
  15.         [JavascriptInterface]  
  16.     public void failure(long id, string paymentId) {  
  17.         Intent intent = new Intent(mContext, typeof(FailureActivity));  
  18.         mContext.StartActivity(intent);  
  19.     }  
  20. }  

Step 10

Finally, add all parameter values to append StringBuilder then send to PayUMoney URL, here two values are important key and Salt.Its Reference by PayUMoney, if the correct gateway is not working. After PayUJavaScriptInterface Method to following below codes,

  1. //PostString is Append All parameters  
  2. private string getPostString() {  
  3.     string TxtStr = Generate();  
  4.     string txnid = hashCal("SHA-256", TxtStr).Substring(0, 20);  
  5.     txnid = "TXN" + txnid;  
  6.     string key = "rjQUPktU"//Key  
  7.     string salt = "e5iIg1jwi8"//salt  
  8.     Java.Lang.StringBuilder post = new Java.Lang.StringBuilder();  
  9.     post.Append("key=");  
  10.     post.Append(key);  
  11.     post.Append("&");  
  12.     post.Append("txnid=");  
  13.     post.Append(txnid);  
  14.     post.Append("&");  
  15.     post.Append("amount=");  
  16.     post.Append(totalAmount);  
  17.     post.Append("&");  
  18.     post.Append("productinfo=");  
  19.     post.Append(productInfo);  
  20.     post.Append("&");  
  21.     post.Append("firstname=");  
  22.     post.Append(firstname);  
  23.     post.Append("&");  
  24.     post.Append("email=");  
  25.     post.Append(email);  
  26.     post.Append("&");  
  27.     post.Append("phone=");  
  28.     post.Append(mobile);  
  29.     post.Append("&");  
  30.     post.Append("surl=");  
  31.     post.Append(SUCCESS_URL);  
  32.     post.Append("&");  
  33.     post.Append("furl=");  
  34.     post.Append(FAILED_URL);  
  35.     post.Append("&");  
  36.     Java.Lang.StringBuilder checkSumStr = new Java.Lang.StringBuilder();  
  37.     MessageDigest digest = null;  
  38.     string hash;  
  39.     try {  
  40.         digest = MessageDigest.GetInstance("SHA-512"); // MessageDigest.getInstance("SHA-256");  
  41.         checkSumStr.Append(key);  
  42.         checkSumStr.Append("|");  
  43.         checkSumStr.Append(txnid);  
  44.         checkSumStr.Append("|");  
  45.         checkSumStr.Append(totalAmount);  
  46.         checkSumStr.Append("|");  
  47.         checkSumStr.Append(productInfo);  
  48.         checkSumStr.Append("|");  
  49.         checkSumStr.Append(firstname);  
  50.         checkSumStr.Append("|");  
  51.         checkSumStr.Append(email);  
  52.         checkSumStr.Append("|||||||||||");  
  53.         checkSumStr.Append(salt);  
  54.         digest.Update(Encoding.ASCII.GetBytes(checkSumStr.ToString()));  
  55.         hash = bytesToHexString(digest.Digest());  
  56.         post.Append("hash=");  
  57.         post.Append(hash);  
  58.         post.Append("&");  
  59.         Log.Info(TAG, "SHA result is " + hash);  
  60.     } catch (NoSuchAlgorithmException e1) {  
  61.         // TODO Auto-generated catch block  
  62.         e1.PrintStackTrace();  
  63.     }  
  64.     post.Append("service_provider=");  
  65.     post.Append("payu_paisa");  
  66.     return post.ToString();  
  67. }  
  68. Convert Byte to Hash key Values: -  
  69.     //string Byte to Hash key Conversation  
  70.     private static string bytesToHexString(byte[] bytes) {  
  71.         StringBuffer sb = new StringBuffer();  
  72.         for (int i = 0; i < bytes.Length; i++) {  
  73.             string hex = Integer.ToHexString(0xFF & bytes[i]);  
  74.             if (hex.Length == 1) {  
  75.                 sb.Append('0');  
  76.             }  
  77.             sb.Append(hex);  
  78.         }  
  79.         return sb.ToString();  
  80.     }  
  81. public string hashCal(string type, string str) {  
  82.     StringBuffer hexString = new StringBuffer();  
  83.     try {  
  84.         MessageDigest digestTxID = null;  
  85.         digestTxID = MessageDigest.GetInstance(type);  
  86.         digestTxID.Reset();  
  87.         digestTxID.Update(Encoding.ASCII.GetBytes(str.ToString()));  
  88.         byte[] messageDigest = digestTxID.Digest();  
  89.         for (int i = 0; i < messageDigest.Length; i++) {  
  90.             string hex = Integer.ToHexString(0xFF & messageDigest[i]);  
  91.             if (hex.Length == 1) hexString.Append("0");  
  92.             hexString.Append(hex);  
  93.         }  
  94.     } catch (NoSuchAlgorithmException nsae) {}  
  95.     return hexString.ToString();  
  96. }  
  97. Generate TxnID Random Number: -  
  98.     //Txnid Generate  
  99.     public string Generate() {  
  100.         long ticks = System.DateTime.Now.Ticks;  
  101.         System.Threading.Thread.Sleep(200);  
  102.         Java.Util.Random rnd = new Java.Util.Random();  
  103.         string rndm = Integer.ToString(rnd.NextInt()) + (System.DateTime.Now.Ticks - ticks / 1000);  
  104.         // int myRandomNo = rnd.Next(10000, 99999);  
  105.         string txnid = hashCal("SHA-256", rndm).Substring(0, 20);  
  106.         return txnid;  
  107.     }  
  108. }  

Step 11

Press F5 or Build and Run the application.

 

Finally, we have successfully implemented WebView of PayUMoney Gateway in Xamarin Android Payment App.