失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 微信小程序支付之c#后台实现方法实例详解

微信小程序支付之c#后台实现方法实例详解

时间:2021-08-27 00:18:54

相关推荐

微信小程序支付之c#后台实现方法实例详解

web前端|js教程

csharp,.net,小程序

web前端-js教程

本文主要介绍了微信小程序支付之c#后台实现方法的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下,希望能帮助到大家。

vpn拨号源码,Ubuntu双系统出错,手机短视频爬虫,php 保存php文件,seo网络实战lzw

微信小程序支付c#后台实现

java用户管理系统源码,vscode显示代码结构树,ubuntu下root,tomcat部署及应用,爬虫测试模型,php 微信天气预报接口,云南省短视频seo,iis 发布静态网站,模板消息 颜色lzw

今天为大家带来比较简单的支付后台处理

qq登录页面源码,vscode每运行一个新代码,ubuntu 重启网关,tomcat按用户分配线程,apache 爬虫框架,php168系统下载,荔湾区seo优化哪家专业lzw

首先下载官方的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./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)//获取openidthat.getOpenId(res.code) } }); }, getOpenId: function (code) { //获取openIDvar 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) {// successwx.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 } }) },

微信小程序支付及退款流程实例分享

php如何实现微信小程序支付及退款

详解微信小程序支付功能开发错误总结

如果觉得《微信小程序支付之c#后台实现方法实例详解》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。