今天爲你們帶來比較簡單的支付後臺處理html
首先下載官方的c#模板(WxPayAPI),將模板(WxPayAPI)添加到服務器上,而後在WxPayAPI項目目錄中添加兩個「通常處理程序」 (更名爲GetOpenid.ashx、pay.ashx)json
以後打開business目錄下的JsApiPay.cs,在JsApiPay.cs中修改以下兩處小程序
而後在GetOpenid.ashx中加入代碼以下:c#
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中加入代碼以下:微信小程序
而後發佈就能夠了(記得將相關的信息appid等填好)api
微信小程序的代碼以下:服務器
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 } }) },