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.
- using Android.Webkit;
- using Java.Lang;
- using Android.Util;
- using Java.Security;
- using System.Text;
- using Android.Net.Http;
- using Java.Interop;
Step 7
Next step is to create variables and then declare the variables in OnCreate().
C# Code
- private static string txnid;
- private static string TAG = "MainActivity";
- private static WebView webviewPayment;
- private WebViewClient webViewClient = new MyWebViewClient();
- private static Context context;
- private static string SUCCESS_URL = "https://www.payumoney.com/mobileapp/payumoney/success.php";
- private static string FAILED_URL = "https://www.payumoney.com/mobileapp/payumoney/failure.php";
- private static string firstname = "Anbu";
- private static string email = "[email protected]";
- private static string productInfo = "test";
- private static string mobile = "8220155182";
- public static string totalAmount = "100";
OnCreate Method
C# Code
- webviewPayment = (WebView) FindViewById(Resource.Id.webView1);
- webviewPayment.Settings.JavaScriptEnabled = true;
- webviewPayment.Settings.SetSupportZoom(true);
- webviewPayment.Settings.DomStorageEnabled = true;
- webviewPayment.Settings.LoadWithOverviewMode = true;
- webviewPayment.Settings.UseWideViewPort = true;
- webviewPayment.Settings.CacheMode = CacheModes.NoCache;
- webviewPayment.Settings.SetSupportMultipleWindows(true);
- webviewPayment.Settings.JavaScriptCanOpenWindowsAutomatically = true;
- webviewPayment.AddJavascriptInterface(new PayUJavaScriptInterface(this), "PayUMoney");
- Java.Lang.StringBuilder url_s = new Java.Lang.StringBuilder();
- url_s.Append("https://test.payu.in/_payment"); //PauMoney Test URL
- Log.Info(TAG, "call url " + url_s);
- webviewPayment.PostUrl(url_s.ToString(), Encoding.UTF8.GetBytes(getPostString()));
- webviewPayment.SetWebViewClient(webViewClient);
Step 8
Implement WebViewClient() by declaring MyWebViewClient Refer on WebViewClient.
C# Code
-
- private class MyWebViewClient: WebViewClient {
- public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon) {
- base.OnPageStarted(view, url, favicon);
- }
- public override void OnPageFinished(WebView view, string url) {
- base.OnPageFinished(view, url);
- }
- public override void OnReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
- Log.Info("Error", "Exception caught!");
- handler.Cancel();
- }
- }
Step 9
Followed Web Client declare Java Interface. Using after complete payment process its redirect to Success of Failure.
C# Code
-
- private class PayUJavaScriptInterface: Java.Lang.Object {
- Context mContext;
- public PayUJavaScriptInterface(Context c) {
- mContext = c;
- }
-
- [Export]
- [JavascriptInterface]
- public void success(long id, string paymentId) {
- Intent intent = new Intent(mContext, typeof(SuccessActivity));
- mContext.StartActivity(intent);
- }
- [Export]
- [JavascriptInterface]
- public void failure(long id, string paymentId) {
- Intent intent = new Intent(mContext, typeof(FailureActivity));
- mContext.StartActivity(intent);
- }
- }
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,
Step 11
Press F5 or Build and Run the application.
Finally, we have successfully implemented WebView of PayUMoney Gateway in Xamarin Android Payment App.