【问题标题】:Working on PayU money sdk, But not able to connect success or failure activity after proceed to payment in xamarin android在PayU money sdk上工作,但在xamarin android中继续付款后无法连接成功或失败活动
【发布时间】:2019-02-20 14:17:21
【问题描述】:

我正在处理 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 = "anbukm91@gmail.com";
        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();

            }


        }
    }

}

这是 .NetStandart 项目中的消费控制。

<?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_URL 和 FAILED_URL 吗?它们是什么?向我们展示您的代码,以便我们为您提供帮助。
  • 是的,我配置了 SUCCESS_URL 和 FAILED_URL。我会更新我的问题。请检查。
  • 您的问题解决了吗?

标签: c# xamarin.forms xamarin.android


【解决方案1】:

原因:

successfailure 函数未触发,因为您的 FAILED_URLSUCCESS_URL 没有从 HTML document 调用 C# successfailure 方法。

你可以尝试设置你的

FAILED_URL = "https://www.payumoney.com/mobileapp/payumoney/failure.php"

然后再测试,你会发现failure方法在失败后点击continue to website会触发(重定向到失败的url)。

解决方案:

设置正确的FAILED_URLSUCCESS_URL

请您的 Web 开发人员在 success/failure url 上添加一个 html 按钮,并在 C# 中使用 same 方法名称。

看看这个thread,你会发现你缺少什么。

本站会告诉你如何从JavaScript调用c#方法: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 = "anbukm91@gmail.com";
            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:

【讨论】:

  • 华,我会试试你的解决方案
  • 我昨天试过了,付款成功或失败后我都没有得到(HTML视图)视图。但是我给出的网址是成功还是失败。你能简要解释一下如何使用 xamarin 表单执行此操作。
  • @praveenaHM 我更新了我的code 并添加了gif 以使其清楚。失败后我没有付款并跳转到failure acvtivity。我没有测试成功,因为我没有帐户。我认为成功与失败是一样的。这是你想要的吗?
  • 我会检查并通知您。
猜你喜欢
  • 2016-08-18
  • 2014-09-13
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
  • 2018-07-07
  • 1970-01-01
  • 2021-07-04
相关资源
最近更新 更多