Example #1
0
 public WeixinpayHelper(Context context)
 {
     _context = context;
     _payRequest = new PayReq();
     _msgApi = WXAPIFactory.CreateWXAPI(_context, null);
     _sb = new System.Text.StringBuilder();
     _msgApi.RegisterApp(Constants.ApiKey);
 }
 protected override void OnCreate(Bundle bundle)
 {
     base.OnCreate(bundle);
     SetContentView(Resource.Layout.payresult);
     api = WXAPIFactory.CreateWXAPI(this, Constants.APP_ID);
     api.HandleIntent(Intent, this);
     Toast.MakeText(this, PackageName, ToastLength.Long).Show();
 }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            Api = WXAPIFactory.CreateWXAPI(this, Constants.APP_ID, false);
            SetContentView(Resource.Layout.Main);
            //   Api.RegisterApp(Constants.APP_ID);
            button = FindViewById<Button>(Resource.Id.MyButton);
            button.Click += OnClick;

        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            show = (TextView)FindViewById(Resource.Id.editText_prepay_id);
            req = new PayReq();
            sb = new System.Text.StringBuilder();
            msgApi = WXAPIFactory.CreateWXAPI(this, null);
            msgApi.RegisterApp(Constants.APP_ID);
            //生成prepay_id
            Button payBtn = FindViewById<Button>(Resource.Id.unifiedorder_btn);

            Dictionary<string, string> xml = new Dictionary<string, string>();

            payBtn.Click += (o, e) => {
                ProgressDialog dialog = ProgressDialog.Show(this, "abc".ToString(), "def");

                IRunnable payRunnable = new Runnable(() => {
                    string url = string.Format("https://api.mch.weixin.qq.com/pay/unifiedorder");
                    string entity = genProductArgs();

                    byte[] buf = Util.httpPost(url, entity);

                    string content = Encoding.Default.GetString(buf);

                    xml = decodeXml(content);
                });

                AsyncTask.Execute(payRunnable);

                if (dialog != null)
                {
                    dialog.Dismiss();
                }
                sb.Append("prepay_id\n" + resultunifiedorder["prepay_id"] + "\n\n");
                show.Text = sb.ToString();
            };

    		Button appayBtn = FindViewById<Button>(Resource.Id.appay_btn);
            appayBtn.Click += (o, e) => {
                sendPayReq();
            };
            //生成签名参数
            Button appay_pre_btn = FindViewById<Button>(Resource.Id.appay_pre_btn);
            appay_pre_btn.Click += (o, e) => {
                genPayReq();
            };
        }
		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			wxApi = WXAPIFactory.CreateWXAPI(this,  APP_ID, true);
			wxApi.RegisterApp(APP_ID);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// Get our button from the layout resource,
			// and attach an event to it

			Button button = FindViewById<Button>(Resource.Id.myButton);
			button.Click += delegate { 

				if(wxApi.IsWXAppInstalled && wxApi.IsWXAppSupportAPI){

					SendMessageToWX.Req req;
					WXWebpageObject textObj = new WXWebpageObject();
					textObj.WebpageUrl = "http://lucazulian.it";
					WXMediaMessage msg = new WXMediaMessage();
					msg.Title = "Luca Zulian website";
					msg.Description = "Hello xamarin from wechat";

					msg.mediaObject = textObj;
					req = new SendMessageToWX.Req();
					req.Transaction = BuildTransaction("webpage");
					req.Message = msg;

					req.Scene = SendMessageToWX.Req.WXSceneSession;
					bool res = MainActivity.wxApi.SendReq(req);

					if(res){
						Console.WriteLine("successfully sent message");
					}

				}
				else{
					Toast.MakeText(this, "wxApi does not supported!", ToastLength.Long).Show();
				}

			};

		}
Example #6
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            //官方demo的app_id,如使用请用本项目中的debug.keystore!!!可简单的覆盖Xamarin.Android的原有debug.keystore
            //强烈建议使用自建的微信app_id和keystore,注意包名等细节,确保可正常调起,如何创建请百度
            wxApi = WXAPIFactory.CreateWXAPI(this,  APP_ID, true);
            wxApi.RegisterApp(APP_ID);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            button.Click += delegate { wxapi.WXUtil.sendWxReq(this, "http://www.qmyd360.com", "Xamarin.Android Wechat Binding","Hello Xmarin", null, 1); };

            Button button1 = FindViewById<Button>(Resource.Id.MyButton1);
            button1.Click += delegate { wxapi.WXUtil.sendWxReq(this, "http://www.qmyd360.com", "Xamarin.Android Wechat Binding", "Hello Xmarin", null, 0); };

            Button button2 = FindViewById<Button>(Resource.Id.MyButton2);
            button2.Click += delegate { wxApi.OpenWXApp(); };
        }
Example #7
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.PayLayout);

            _msgApi = WXAPIFactory.CreateWXAPI(this, null);
            _sb = new System.Text.StringBuilder();

            #region 获取控件

            _btnAlipay = FindViewById<Button>(Resource.Id.btn_Alipay);
            _btnTecentPay = FindViewById<Button>(Resource.Id.btn_TecentPay);
            _btnCancel = FindViewById<Button>(Resource.Id.btn_Cancel);
            _layout = FindViewById<LinearLayout>(Resource.Id.pop_layout);

            #endregion 获取控件

            // Create your application here

            #region 事件绑定

            _layout.Click += (sender, args) =>
            {
                Toast.MakeText(ApplicationContext,
                    "提示:点击窗口外部关闭窗口!",
                    ToastLength.Long);
            };
            _btnAlipay.Click += AlipayPay;

            _btnTecentPay.Click += TecentPayPay;

            _btnCancel.Click += (sender, args) =>
            {
                Finish();
            };

            #endregion 事件绑定

            #region Handler  回调处理

            _handler = new Handler((msg) =>
            {
                var msgWhat = (MsgWhat)msg.What;

                switch (msgWhat)
                {
                    #region 支付宝支付结果回调

                    case MsgWhat.AlipayPayFlag:
                        {
                            PayResult payResult = new PayResult((string)msg.Obj);

                            // 支付宝返回此次支付结果及加签,建议对支付宝签名信息拿签约时支付宝提供的公钥做验签
                            string resultInfo = payResult.Result;

                            string resultStatus = payResult.ResultStatus;

                            // 判断resultStatus 为“9000”则代表支付成功,具体状态码代表含义可参考接口文档
                            switch (resultStatus)
                            {
                                case "9000":
                                    {
                                        Toast.MakeText(this, "支付成功",
                                   ToastLength.Short).Show();
                                        break;
                                    }
                                // 判断resultStatus 为非“9000”则代表可能支付失败
                                // “8000”代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认,最终交易是否成功以服务端异步通知为准(小概率状态)
                                case "8000":
                                    {
                                        Toast.MakeText(this, "支付结果确认中",
                                        ToastLength.Short).Show(); break;
                                    }
                                case "4000":
                                    {
                                        Toast.MakeText(this, "未安装支付宝",
                                   ToastLength.Short).Show();
                                        break;
                                    }
                                default:
                                    {
                                        // 其他值就可以判断为支付失败,包括用户主动取消支付,或者系统返回的错误
                                        Toast.MakeText(this, "支付失败",
                                            ToastLength.Short).Show();
                                        break;
                                    }
                            }
                            break;
                        }

                    #endregion 支付宝支付结果回调
                    #region 微信获取认证结果回调

                    case MsgWhat.TencentValidateFlag:
                        {

                            Dictionary<string, string> xml = null;
                            try
                            {
                                xml = DecodeXml(msg.Obj.ToString());
                            }
                            catch (System.Exception)
                            {

                                Toast.MakeText(this, "服务器请求异常",
                                        ToastLength.Long).Show();
                                break;
                            }

                            if (xml.ContainsKey("return_code"))
                            {
                                var returnCode = xml["return_code"];

                                if (returnCode == "FAIL")
                                {
                                    var errMsg = xml["return_msg"];
                                    Toast.MakeText(this, "请求微信异常:" + errMsg,
                                        ToastLength.Long).Show();
                                }
                                else
                                {
                                    _sb.Append("prepay_id\n" + xml["prepay_id"] + "\n\n");
                                    SendPayReq();
                                }
                            }
                            //Toast.MakeText(this, "验证结果:" + msg.Obj,
                            //ToastLength.Short).Show();
                            break;
                        }

                    #endregion 微信获取认证结果回调
                    #region 微信支付结果回调

                    case MsgWhat.TencentPayFlag:
                        {
                            Toast.MakeText(this, "检查结果为:" + msg.Obj,
                                ToastLength.Short).Show();
                            break;
                        }
                        #endregion 微信支付结果回调
                }
            });

            #endregion Handler  回调处理

        }
Example #8
0
        static WeChat() {
            var streamInfo = Application.GetResourceStream(new Uri("config.xml", UriKind.Relative));

            var sr = new StreamReader(streamInfo.Stream);
            var document = XDocument.Parse(sr.ReadToEnd());

            appId = (from results in document.Descendants()
                         where results.Name.LocalName == "preference" && ((string)results.Attribute("name") == WECHAT_APPID_KEY)
                         select (string)results.Attribute("value")).First();

            api = WXAPIFactory.CreateWXAPI(appId);
        }