在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、获取微信支付 MCHID,KEY,APPID,APPSecret 四个支付关键值. 微信公众号: https://mp.weixin.qq.com/ 二、把WxPayAPI添加到制作项目中,在Config.cs文件里修改获取到的MCHID,KEY,APPID,APPSecret四个关键值以及NOTIFY_URL值(NOTIFY_URL是支付授权目录),并在MVC项目里建一个WeChatWeb控制器,里面加上逻辑代码.并传递微信jsapi支付所需的参数.代码示例如下: 后台Action代码 /// <summary> /// 获取微信支付相关信息 /// </summary> /// <returns></returns> [HttpGet] public virtual ActionResult Index() { JsApiPay jsApiPay = new JsApiPay(); OStudent model = null; try { //调用【网页授权获取用户信息】接口获取用户的openid和access_token jsApiPay.GetOpenidAndAccessToken(); //获取微信支付者公共id jsApiPay.openid = jsApiPay.openid; string ID = Request["ID"]; //如果要获取页面传递过来的值,需修改GetOpenidAndAccessToken()方法里面的代码,加上Request.Url.Query获取参数 model = OStudentSiteService.GetByKey(id).AppendData as OStudent; if (model != null) { jsApiPay.total_fee = 1;//测试 订单金额(1表示分,正式金额要*100) jsApiPay.Order_ID = model.order_ID; //订单号(自己定义订单号) } //JSAPI支付预处理 //调用统一下单,获得下单结果 WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(); //从统一下单成功返回的数据中获取微信浏览器调起jsapi支付所需的参数 var wxJsApiParam = jsApiPay.GetJsApiParameters(); //获取到的是json格式字符串 ViewBag.wxJsApiParam = wxJsApiParam; //前台页面js调用 Log.Debug(this.GetType().ToString(), "wxJsApiParam : " + wxJsApiParam); } catch (Exception ex) { Response.Write(ex.Message + "," + ex.Source); Response.End(); } return View(model); } //修改支付方式 [HttpPost] public virtual JsonResult PayMethod() { AjaxJsonResult ajax = new AjaxJsonResult() { err = true, msg = string.Empty, timeout = 3 }; string id = Request.Form["id"]; string payMethod = Request.Form["payMethod"]; var model = (Project.Core.Models.Model.OStudent)OStudentSiteService.GetByKey(id).AppendData; model.payMethod = payMethod; //支付方式 OperationResult result = OStudentSiteService.Modify(model); if (result.ResultType == OperationResultType.Success) { ajax.err = false; ajax.msg = "操作成功"; } return Json(ajax); } /// <summary> /// 修改支付状态 /// </summary> /// <param name="userID"></param> /// <returns></returns> [HttpPost] public virtual string EditPayStatus(Guid userID) { string msg = "error"; var model = OStudentSiteService.GetByKey(userID).AppendData as OStudent; model.Status = (int)X.Project.Site.Models.Enum.PayStatus.Success; //付款成功 OperationResult result = OStudentSiteService.Modify(model); if (result.ResultType == OperationResultType.Success) { msg = "ok"; } return msg; } 前台Index.chtml视图页面JS代码 <script type="text/javascript"> var _wxJsApiParam = eval('(@Html.Raw(ViewBag.wxJsApiParam))'); function callpay() { //选择支付方式 var payMethod = $("input[name='PayMethod']:checked").val(); if (payMethod == "" || payMethod == null) { layer.msg("请选择支付方式", function () { }) return false; } $.ajax({ type: "POST", dataType: "JSON", url: '@Url.Action("PayMethod", "WeChatWeb")', data: { payMethod: payMethod, id: '@Model.ID' }, success: function (data) { //表示修改支付方式成功 if (!data.err) { //1表示微信支付,则调用微信支付 if (payMethod == "1") { if (typeof WeixinJSBridge == "undefined") { if (document.addEventListener) { document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); } else if (document.attachEvent) { document.attachEvent('WeixinJSBridgeReady', jsApiCall); document.attachEvent('onWeixinJSBridgeReady', jsApiCall); } } else { jsApiCall(); } } else if (payMethod == "2") { layer.alert('恭喜您,操作成功!', function () { window.location.href = "@Url.Action(MVC.Default.Index())" ; //操作成功后的跳转页面 }); } } else { layer.msg("操作失败", function () { }) return false; } } }); } //调用微信JS api 支付 function jsApiCall() { WeixinJSBridge.invoke('getBrandWCPayRequest', _wxJsApiParam, function (res) { if (res.err_msg == "get_brand_wcpay_request:cancel") { layer.msg("已取消支付", function () { }); return false; } else if (res.err_msg == "get_brand_wcpay_request:ok") { //支付成功 //ajax $.ajax({ type: "POST", dataType: "text", url: '@Url.Action("EditPayStatus", "WeChatWeb")', data: { userID: '@Model.ID' }, error: function (request) { layer.msg("服务器错误!", function () { }); return false; }, success: function (data) { window.location.href = "@Url.Action(MVC.Default.Index())"; //支付成功后跳转的页面 } }); //ajax end } }); } </script> 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持极客世界。 |
请发表评论