您现在的位置是:网站首页> 编程资料编程资料
微信公众号支付(MVC版本)_实用技巧_
2023-05-24
450人已围观
简介 微信公众号支付(MVC版本)_实用技巧_
一、获取微信支付 MCHID,KEY,APPID,APPSecret 四个支付关键值.
微信支付商户平台 https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2F
1.登录微信支付商户平台获取到商户号(MCHID),
2.在"账号中心"栏目下"API安全"栏目里设置API密钥(KEY)
微信公众号: https://mp.weixin.qq.com/
1.登录微信公众在"基本配置"栏获取应用ID(APPID)和应用密钥(APPSecret)
2.在"接口权限"栏目下"网页账号"绑定正式支付的域名 (如:××××.net,不要http:和"/"斜杠)
3.在"微信支付"栏目下"开发配置"里面设置公众支付的支付授权目录(如:××××.net/WeChatWeb/)
二、把WxPayAPI添加到制作项目中,在Config.cs文件里修改获取到的MCHID,KEY,APPID,APPSecret四个关键值以及NOTIFY_URL值(NOTIFY_URL是支付授权目录),并在MVC项目里建一个WeChatWeb控制器,里面加上逻辑代码.并传递微信jsapi支付所需的参数.代码示例如下:
后台Action代码
////// 获取微信支付相关信息 /// ///[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); } /// /// 修改支付状态 /// /// ///[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代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相关内容
- 拥有网页版小U盘 ASP.NET实现文件上传与下载功能_实用技巧_
- ASP.NET中GridView的文件输出流方式_实用技巧_
- ASP.NET MVC4中使用Html.DropDownListFor的方法示例_实用技巧_
- ASP.NET Razor模板引擎中输出Html的两种方式_实用技巧_
- ASP.NET MVC中使用JavaScriptResult的用法示例_实用技巧_
- ASP.NET在MVC控制器中获取Form表单值的方法_实用技巧_
- asp.net基础学习之前端页面布局_实用技巧_
- asp.net实现中英文多域名检测的方法_实用技巧_
- asp.net基础学习之控件的使用方法_实用技巧_
- asp.net+xml+flash实现的图片展示效果示例_实用技巧_
