微信公众号支付(MVC版本)
程序员文章站
2023-11-17 13:42:10
一、获取微信支付 mchid,key,appid,appsecret 四个支付关键值.
微信支付商户平台 https://pay.weixin.qq.com/index....
一、获取微信支付 mchid,key,appid,appsecret 四个支付关键值.
微信支付商户平台 https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2f
1.登录微信支付商户平台获取到商户号(mchid),
2.在"账号中心"栏目下"api安全"栏目里设置api密钥(key)
微信公众号:
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代码
/// <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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。