微信小程序支付之c#后台实现方法
程序员文章站
2022-07-06 20:30:36
微信小程序支付c#后台实现
今天为大家带来比较简单的支付后台处理
首先下载官方的c#模板(wxpayapi),将模板(wxpayapi)添加到服务器上,然后在wxp...
微信小程序支付c#后台实现
今天为大家带来比较简单的支付后台处理
首先下载官方的c#模板(wxpayapi),将模板(wxpayapi)添加到服务器上,然后在wxpayapi项目目录中添加两个“一般处理程序” (改名为getopenid.ashx、pay.ashx)
之后打开business目录下的jsapipay.cs,在jsapipay.cs中修改如下两处
然后在getopenid.ashx中加入代码如下:
public class getopenid : ihttphandler { public string openid { get; set; } public void processrequest(httpcontext context) { string code = httpcontext.current.request.querystring["code"]; wxpaydata data = new wxpaydata(); data.setvalue("appid", wxpayconfig.appid); data.setvalue("secret", wxpayconfig.appsecret); data.setvalue("code", code); data.setvalue("grant_type", "authorization_code"); string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.tourl(); //请求url以获取数据 string result = httpservice.get(url); log.debug(this.gettype().tostring(), "getopenidandaccesstokenfromcode response : " + result); //保存access_token,用于收货地址获取 jsondata jd = jsonmapper.toobject(result); //access_token = (string)jd["access_token"]; //获取用户openid openid = (string)jd["openid"]; context.response.write(openid);//获取h5调起js api参数 }
在pay.ashx中加入代码如下:
public class pay : ihttphandler { public void processrequest(httpcontext context) { context.response.contenttype = "text/plain"; string openid = httpcontext.current.request.querystring["openid"]; string total_fee = httpcontext.current.request.querystring["total_fee"]; jsapipay jsapipay = new jsapipay(context); jsapipay.openid = openid; jsapipay.total_fee = int.parse(total_fee); wxpaydata unifiedorderresult = jsapipay.getunifiedorderresult(); context.response.write(jsapipay.getjsapiparameters());//获取h5调起js api参数 }
然后发布就可以了(记得将相关的信息appid等填好)
微信小程序的代码如下:
wxpay: function () { var that = this //登陆获取code wx.login({ success: function (res) { console.log(res.code) //获取openid that.getopenid(res.code) } }); }, getopenid: function (code) { //获取openid var that = this; wx.request({ url: 'http://*******/wxpayapi/getopenid.ashx?code='+ code , //改为自己的域名 data: {}, // method: 'get', success: function (res) { var a12=res.data that.generateorder(a12) //console.log(a12) }, fail: function () { // fail }, complete: function () { // complete } }) }, /**生成商户订单 */ generateorder: function (openid) { var that = this; //console.log(openid) //统一支付 wx.request({ url: 'http://*******/wxpayapi/pay.ashx', //改为自己的域名 //method: 'get', data: { total_fee: 1,//1分 openid: openid, }, header: { 'content-type': 'application/json' }, success: function (res) { var pay = res.data //发起支付 var timestamp = pay.timestamp; var packages = pay.package; var paysign = pay.paysign; var noncestr = pay.noncestr; var param = { "timestamp": timestamp, "package": packages, "paysign": paysign, "signtype": "md5", "noncestr": noncestr }; that.pay(param) }, }) }, /* 支付 */ pay: function (param) { wx.requestpayment({ timestamp: param.timestamp, noncestr: param.noncestr, package: param.package, signtype: param.signtype, paysign: param.paysign, success: function (res) { // success wx.navigateback({ delta: 1, // 回退前 delta(默认为1) 页面 success: function (res1) { wx.showtoast({ title: '支付成功', icon: 'success', duration: 2000 }); }, fail: function () { // fail }, complete: function () { } }) }, fail: function (res) { // fail }, complete: function () { // complete } }) },
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!