微信小程序支付c#后台实现
2017-10-16 16:33
711 查看
今天为大家带来比较简单的支付后台处理
首先下载官方的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中加入代码如下:
微信小程序的代码如下:
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
}
})
},
首先下载官方的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
}
})
},
相关文章推荐
- 微信小程序支付c#后台实现
- 微信小程序支付之c#后台实现方法
- 微信小程序 c#后台支付结果回调
- 微信小程序 c#后台支付结果回调
- 微信小程序 支付后台java实现实例
- [转]微信小程序 c#后台支付结果回调
- C#之使用NotifyIcon实现任务栏托盘菜单,图标闪烁效果及气泡提示 很多程序是只需要后台运行的,甚至不需要自己的应用界面。NotifyIcon提供了程序在任务栏的显示功能 程序下载链接如下
- 微信小程序开发之webview组件内网页实现微信原生支付
- 微信小程序支付源码 Demo 后台服务端代码
- 4部分代码轻松实现微信小程序支付功能
- Android能否实现一键清理后台程序后程序自启动(类似QQ微信不被清理软件杀死)
- 微信扫码支付asp.net(C#)实现步骤
- C#开发微信门户及应用(47) - 整合Web API、微信后台管理及前端微信小程序的应用方案
- 微信APP支付-Java后台实现
- 微信小程序支付功能 php后台对接完整代码分享
- 微信小程序支付JAVA后台逻辑
- 微信APP支付-Java后台实现
- C#+Hybrid App(Android)实现微信APP支付
- 微信APP支付-Java后台实现
- Android实现微信、QQ的程序前后台切换