我正在研究xamarin表格。在我的申请中整合payu钱。它工作正常,并遵循this文章来实现。
我希望在付款后显示成功和失败消息。现在我无法在PayUJavaScriptInterface中读取成功或失败。为什么方法不在PayUJavaScriptInterface中触发?
这是xamarin android中的CustomRenderer类。
using System; using System.Security.Cryptography; using System.Text; using Android.Content; using Android.Net.Http; using Android.Util; using Android.Webkit; using Java.Interop; using Java.Lang; using Java.Security; using PayuTest.Droid; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; using RCBazaar.CustomRenderer; using RCBazaar.Droid;
[assembly: ExportRenderer(typeof(ExtendedWebView), typeof(ExtendedWebViewRenderer))] namespace PayuTest.Droid {
public class ExtendedWebViewRenderer : WebViewRenderer
{
private WebViewClient webViewClient = new MyWebViewClient();
private static Context context;
private static string SUCCESS_URL = "http://www.rcbazaar.com/PayuReturn.aspx#";
private static string FAILED_URL = "https://www.google.co.in/";
private static string firstname = "Anbu";
private static string lastname = "AV";
public string url_s;
private static string email = "[email protected]";
private static string productInfo = "93314";
private static string mobile = "8220155182";
public static string totalAmount = "10.00";
public ExtendedWebViewRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
try
{
base.OnElementChanged(e);
if (this.Control != null)
{
var webView = new global::Android.Webkit.WebView(this.Context);
var view = (ExtendedWebView)Element;
Control.Settings.JavaScriptEnabled = true;
Control.Settings.SetSupportZoom(false);
Control.Settings.DomStorageEnabled = true;
Control.Settings.LoadWithOverviewMode = true;
Control.Settings.UseWideViewPort = true;
Control.Settings.CacheMode = CacheModes.NoCache;
Control.Settings.SetSupportMultipleWindows(true);
Control.Settings.JavaScriptCanOpenWindowsAutomatically = true;
Control.AddJavascriptInterface(new PayUJavaScriptInterface(this.Context), "PayUMoney"); //JavaInterface
url_s = "https://test.payu.in/_payment";
Control.PostUrl(url_s, Encoding.UTF8.GetBytes(getPostString()));
Control.SetWebViewClient(webViewClient);
}
}
catch (System.Exception ex)
{
}
}
//PostString is Append All parameters
public string getPostString()
{
string TxtStr = Generate();
string strHash = Generatehash512(TxtStr + DateTime.Now);
string txnid = strHash.ToString().Substring(0, 20);
string key = "W5bPaX"; //Key gtKFFx
string salt = "H7b5NDWN"; //salt eCwWELxi
Java.Lang.StringBuilder post = new Java.Lang.StringBuilder();
Java.Lang.StringBuilder checkSumStr = new Java.Lang.StringBuilder();
try
{
checkSumStr.Append(key);
checkSumStr.Append("|");
checkSumStr.Append(txnid);
checkSumStr.Append("|");
checkSumStr.Append(1);
checkSumStr.Append("|");
checkSumStr.Append(productInfo);
checkSumStr.Append("|");
checkSumStr.Append(firstname);
checkSumStr.Append("|");
checkSumStr.Append(email);
checkSumStr.Append("|||||||||||");
checkSumStr.Append(salt);
var ss = Generatehash512(checkSumStr.ToString());
post.Append("key=");
post.Append(key);
post.Append("&");
post.Append("txnid=");
post.Append(txnid);
post.Append("&");
post.Append("amount=");
post.Append(1);
post.Append("&");
post.Append("productinfo=");
post.Append(productInfo);
post.Append("&");
post.Append("firstname=");
post.Append(firstname);
post.Append("&");
post.Append("email=");
post.Append(email);
post.Append("&");
post.Append("phone=");
post.Append(mobile);
post.Append("&");
post.Append("surl=");
post.Append(SUCCESS_URL);
post.Append("&");
post.Append("furl=");
post.Append(FAILED_URL);
post.Append("&");
post.Append("hash=");
post.Append(ss);
}
catch (NoSuchAlgorithmException e1)
{
// TODO Auto-generated catch block
e1.PrintStackTrace();
}
return post.ToString();
}
//Generate random transaction id
public string Generate()
{
long ticks = System.DateTime.Now.Ticks;
System.Threading.Thread.Sleep(200);
Java.Util.Random rnd = new Java.Util.Random();
string rndm = Integer.ToString(rnd.NextInt()) + (System.DateTime.Now.Ticks - ticks / 1000);
return rndm;
}
//Generating Hash(Checksum) string
public string Generatehash512(string text)
{
byte[] message = Encoding.UTF8.GetBytes(text);
UnicodeEncoding UE = new UnicodeEncoding();
byte[] hashValue;
SHA512Managed hashString = new SHA512Managed();
string hex = "";
hashValue = hashString.ComputeHash(message);
foreach (byte x in hashValue)
{
hex += System.String.Format("{0:x2}", x);
}
return hex;
}
//Java Interface After Payment its Return Success/failure
private class PayUJavaScriptInterface : Java.Lang.Object
{
Context mContext;
public PayUJavaScriptInterface(Context c)
{
mContext = c;
}
// public void Success
[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);
}
}
//WebView Client Run Time
private class MyWebViewClient : WebViewClient
{
public override void OnPageStarted(Android.Webkit.WebView view, string url, Android.Graphics.Bitmap favicon)
{
base.OnPageStarted(view, url, favicon);
}
public override void OnPageFinished(Android.Webkit.WebView view, string url)
{
base.OnPageFinished(view, url);
}
public override void OnReceivedSslError(Android.Webkit.WebView view, SslErrorHandler handler, SslError error)
{
Log.Info("Error", "Exception caught!");
handler.Proceed();
}
}
}
}
这是.Net Standard项目中的Consuming Control。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:payutest="clr-namespace:RCBazaar.CustomRenderer"
x:Class="RCBazaar.Views.PaymentGateways.PayUView">
<ContentPage.Content>
<StackLayout>
<payutest:ExtendedWebView
VerticalOptions="FillAndExpand"
HorizontalOptions="StartAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
原因:
success
和failure
函数没有触发,因为你的FAILED_URL
和SUCCESS_URL
没有从C#
称success
failure
或HTML document
方法。
你可以尝试设置你的
FAILED_URL = "https://www.payumoney.com/mobileapp/payumoney/failure.php"
然后再次测试,你会发现当你在失败后点击failure
时会触发continue to website
方法(重定向到失败的URL)。
解:
设置正确的FAILED_URL
和SUCCESS_URL
。
请您的Web开发人员在success/failure url
上添加一个html按钮,其中包含C#中相同的方法名称。
看看这个thread,你会发现你所缺少的。
这个网站将告诉你如何从c#
调用JavaScript
方法:call_csharp_from_javascript
更新:
在xamarin android的CustomRenderer
类中的代码,我改变了你的FAILED_URL
:
public class ExtendedWebViewRenderer : WebViewRenderer
{
private WebViewClient webViewClient = new MyWebViewClient();
private static Context context;
private static string SUCCESS_URL = "http://www.rcbazaar.com/PayuReturn.aspx#";
private static string FAILED_URL = "https://www.payumoney.com/mobileapp/payumoney/failure.php";
private static string firstname = "Anbu";
private static string lastname = "AV";
public string url_s;
private static string email = "[email protected]";
private static string productInfo = "93314";
private static string mobile = "8220155182";
public static string totalAmount = "10.00";
public ExtendedWebViewRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
try
{
base.OnElementChanged(e);
if (this.Control != null)
{
var webView = new global::Android.Webkit.WebView(this.Context);
var view = (ExtendedWebView)Element;
Control.Settings.JavaScriptEnabled = true;
Control.Settings.SetSupportZoom(false);
Control.Settings.DomStorageEnabled = true;
Control.Settings.LoadWithOverviewMode = true;
Control.Settings.UseWideViewPort = true;
Control.Settings.CacheMode = CacheModes.NoCache;
Control.Settings.SetSupportMultipleWindows(true);
Control.Settings.JavaScriptCanOpenWindowsAutomatically = true;
Control.AddJavascriptInterface(new PayUJavaScriptInterface(this.Context), "PayUMoney"); //JavaInterface
url_s = "https://test.payu.in/_payment";
Control.PostUrl(url_s, Encoding.UTF8.GetBytes(getPostString()));
Control.SetWebViewClient(webViewClient);
}
}
catch (System.Exception ex)
{
}
}
//PostString is Append All parameters
public string getPostString()
{
string TxtStr = Generate();
string strHash = Generatehash512(TxtStr + DateTime.Now);
string txnid = strHash.ToString().Substring(0, 20);
string key = "W5bPaX"; //Key gtKFFx
string salt = "H7b5NDWN"; //salt eCwWELxi
Java.Lang.StringBuilder post = new Java.Lang.StringBuilder();
Java.Lang.StringBuilder checkSumStr = new Java.Lang.StringBuilder();
try
{
checkSumStr.Append(key);
checkSumStr.Append("|");
checkSumStr.Append(txnid);
checkSumStr.Append("|");
checkSumStr.Append(1);
checkSumStr.Append("|");
checkSumStr.Append(productInfo);
checkSumStr.Append("|");
checkSumStr.Append(firstname);
checkSumStr.Append("|");
checkSumStr.Append(email);
checkSumStr.Append("|||||||||||");
checkSumStr.Append(salt);
var ss = Generatehash512(checkSumStr.ToString());
post.Append("key=");
post.Append(key);
post.Append("&");
post.Append("txnid=");
post.Append(txnid);
post.Append("&");
post.Append("amount=");
post.Append(1);
post.Append("&");
post.Append("productinfo=");
post.Append(productInfo);
post.Append("&");
post.Append("firstname=");
post.Append(firstname);
post.Append("&");
post.Append("email=");
post.Append(email);
post.Append("&");
post.Append("phone=");
post.Append(mobile);
post.Append("&");
post.Append("surl=");
post.Append(SUCCESS_URL);
post.Append("&");
post.Append("furl=");
post.Append(FAILED_URL);
post.Append("&");
post.Append("hash=");
post.Append(ss);
}
catch (NoSuchAlgorithmException e1)
{
// TODO Auto-generated catch block
e1.PrintStackTrace();
}
return post.ToString();
}
//Generate random transaction id
public string Generate()
{
long ticks = System.DateTime.Now.Ticks;
System.Threading.Thread.Sleep(200);
Java.Util.Random rnd = new Java.Util.Random();
string rndm = Integer.ToString(rnd.NextInt()) + (System.DateTime.Now.Ticks - ticks / 1000);
return rndm;
}
//Generating Hash(Checksum) string
public string Generatehash512(string text)
{
byte[] message = Encoding.UTF8.GetBytes(text);
UnicodeEncoding UE = new UnicodeEncoding();
byte[] hashValue;
SHA512Managed hashString = new SHA512Managed();
string hex = "";
hashValue = hashString.ComputeHash(message);
foreach (byte x in hashValue)
{
hex += System.String.Format("{0:x2}", x);
}
return hex;
}
//Java Interface After Payment its Return Success/failure
private class PayUJavaScriptInterface : Java.Lang.Object
{
Context mContext;
public PayUJavaScriptInterface(Context c)
{
mContext = c;
}
//public void Success
[Export]
[JavascriptInterface]
public void success(long id, string paymentId)
{
Intent intent = new Intent(mContext, typeof(success));
mContext.StartActivity(intent);
}
[Export]
[JavascriptInterface]
public void failure(long id, string paymentId)
{
Intent intent = new Intent(mContext, typeof(failure));
mContext.StartActivity(intent);
}
}
//WebView Client Run Time
private class MyWebViewClient : WebViewClient
{
public override void OnPageStarted(Android.Webkit.WebView view, string url, Android.Graphics.Bitmap favicon)
{
base.OnPageStarted(view, url, favicon);
}
public override void OnPageFinished(Android.Webkit.WebView view, string url)
{
base.OnPageFinished(view, url);
}
public override void OnReceivedSslError(Android.Webkit.WebView view, SslErrorHandler handler, SslError error)
{
Log.Info("Error", "Exception caught!");
handler.Proceed();
}
}
}
这是一个gif: