今天為大家?guī)肀容^簡單的支付后臺處理。
首先下載官方的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以獲取數(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調起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 =
|