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(); }; }
/// <summary> /// 3 调起微信支付,(前提:微信已安装,且没有被应用回收,即正常人工可以使用) /// 新建一个包wxapi,建一个类名为WXPayEntryActivity作为接受微信的支付结果(回调名必须是: /// 你的包名+.wxapi.WXPayEntryActivity),添加如下代码到Mainfast里面就行了 exported = true /// activity android:name=".wxapi.WXPayEntryActivity" android:exported="true" 实现onResp函数 /// 不过,最终结果以服务器的返回为准notify_url,App等待并轮询结果,接下来就是要为了做安全性设计, /// 把一些重要的东西放到服务器生生成,这样是为防止客户端被反编译,建议GenPackageSign/genProductArgs方法在服务端生成 /// </summary> public bool SendPayReq(PayReq pReq) { bool isOk = false; try { if (!msgApi.IsWXAppInstalled) { Toast.MakeText(this.Context, "您还没有安装微信,暂不支持此功能!", ToastLength.Short).Show(); } else if (!msgApi.IsWXAppSupportAPI) { Toast.MakeText(this.Context, "你安装的微信版本不支持当前开放API!", ToastLength.Short).Show(); } else { msgApi.RegisterApp(Constants.APPID); msgApi.OpenWXApp(); isOk = msgApi.SendReq(pReq); } } catch (Exception ex) { Toast.MakeText(this.Context, ex.GetAllMsg(), ToastLength.Short).Show(); //throw; } return(isOk); }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.vipcompany); am = (MyApplication)this.Application; orderid = DateTime.Now.ToString("yyyyMMddHHmmss") + am.Appuser.Uid; var toolbar = inittoolbar("会员企业"); //注册微信 satetact = this; wxApi = WXAPIFactory.CreateWXAPI(this, appid, true); wxApi.RegisterApp(appid); btn_tovip = FindViewById <Button>(Resource.Id.btn_vip); vipstate = FindViewById <TextView>(Resource.Id.vipstate); btn_tovip.Click += (s, e) => { if (wxApi.IsWXAppInstalled) { sendpayorder(); } else { Toast.MakeText(satetact, "该手机上未安装微信", ToastLength.Short).Show(); } }; }
void initWechatShare(Context context, string yourWeChatAppId) { if (mWXApi == null) { mWXApi = WXAPIFactory.CreateWXAPI(context, yourWeChatAppId, true); } mWXApi.RegisterApp(yourWeChatAppId); }
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); // 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(); } }; }
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(); } }; }
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(); }; }
void butRegisterApp_Click(object sender, EventArgs e) { // 将该app注册到微信 api.RegisterApp(Configuration.APP_ID); }
private bool RegToWx() { wxApi = WXAPIFactory.CreateWXAPI(this, appID, true); return(wxApi.RegisterApp(appID)); }
//发起预支付请求 public void sendpayorder() { string TimeStamp = getTimestamp(); //随机字符串 string NonceStr = getNoncestr(); //创建支付应答对象 var packageReqHandler = new RequestHandler(); //初始化 packageReqHandler.init(); packageReqHandler.setParameter("body", "会员公司费用"); //商品信息 127字符 packageReqHandler.setParameter("appid", appid); packageReqHandler.setParameter("mch_id", mchid); packageReqHandler.setParameter("nonce_str", NonceStr.ToLower()); packageReqHandler.setParameter("notify_url", "http://XXX/pay.aspx"); packageReqHandler.setParameter("out_trade_no", orderid); //商家订单号 packageReqHandler.setParameter("spbill_create_ip", "192.168.1.1"); //用户的公网ip,不是商户服务器IP // packageReqHandler.setParameter("total_fee", (am.Aconfiginfo.Vipmoney*100).ToString()); //商品金额,以分为单位(money * 100).ToString() packageReqHandler.setParameter("total_fee", "1"); packageReqHandler.setParameter("trade_type", "APP"); packageReqHandler.setParameter("attach", "");//自定义参数 127字符 string Sign = packageReqHandler.CreateMd5Sign("key", AppKey); packageReqHandler.setParameter("sign", Sign); string data = packageReqHandler.parseXML(); string prepayXml = HttpUtil.Send(data, "https://api.mch.weixin.qq.com/pay/unifiedorder"); var xdoc = new XmlDocument(); xdoc.LoadXml(prepayXml); XmlNode xn = xdoc.SelectSingleNode("xml"); XmlNodeList xnl = xn.ChildNodes; if (xnl.Count > 7) { string PrepayId = xnl[7].InnerText; TimeStamp = getTimestamp(); //随机字符串 NonceStr = getNoncestr(); PayReq payRequest = new PayReq(); payRequest.AppId = appid; payRequest.PrepayId = PrepayId; payRequest.PartnerId = mchid; payRequest.PackageValue = "Sign=WXPay"; payRequest.NonceStr = NonceStr; payRequest.TimeStamp = TimeStamp; var paySignReqHandler = new RequestHandler(); paySignReqHandler.setParameter("appid", payRequest.AppId); paySignReqHandler.setParameter("noncestr", payRequest.NonceStr); paySignReqHandler.setParameter("package", "Sign=WXPay"); paySignReqHandler.setParameter("partnerid", payRequest.PartnerId); paySignReqHandler.setParameter("prepayid", payRequest.PrepayId); paySignReqHandler.setParameter("timestamp", payRequest.TimeStamp); // paySignReqHandler.setParameter("signType", "MD5"); string PaySign = paySignReqHandler.CreateMd5Sign("key", AppKey); payRequest.Sign = PaySign; Toast.MakeText(this, "正在调起支付", ToastLength.Short).Show(); wxApi.RegisterApp(appid); //这个返回 一直都是true 哪怕你参数都是错的 我也是醉了 我被这个状态迷惑了好久在找其他问题 bool issceuss = wxApi.SendReq(payRequest); // Toast.MakeText(this, "状态"+ issceuss, ToastLength.Short).Show(); } }
// 将该app注册到微信 private void BtnRegister_Click(object sender, EventArgs e) { var result = api.RegisterApp(APP_ID); Toast.MakeText(this, "注册结果:" + result, ToastLength.Short).Show(); }
public override void OnReceive(Context context, Intent intent) { var result = wxApi.RegisterApp(appId); }