微信小程序支付c#后臺實(shí)現(xiàn)
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設(shè)、昌平網(wǎng)站維護(hù)、網(wǎng)站推廣。
今天為大家?guī)肀容^簡單的支付后臺處理
首先下載官方的c#模板(WxPayAPI),將模板(WxPayAPI)添加到服務(wù)器上,然后在WxPayAPI項(xiàng)目目錄中添加兩個“一般處理程序” (改名為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/oauth3/access_token?" + data.ToUrl(); //請求url以獲取數(shù)據(jù) 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調(diào)起JS API參數(shù) }
在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調(diào)起JS API參數(shù) }
然后發(fā)布就可以了(記得將相關(guān)的信息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) //統(tǒng)一支付 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 //發(fā)起支付 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(默認(rèn)為1) 頁面 success: function (res1) { wx.showToast({ title: '支付成功', icon: 'success', duration: 2000 }); }, fail: function () { // fail }, complete: function () { } }) }, fail: function (res) { // fail }, complete: function () { // complete } }) },
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
分享題目:微信小程序支付之c#后臺實(shí)現(xiàn)方法
網(wǎng)站路徑:http://bm7419.com/article46/jjdphg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、全網(wǎng)營銷推廣、微信公眾號、移動網(wǎng)站建設(shè)、網(wǎng)站改版、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)